From 40cd1f5a5eb5ac4d4fdf49a6871058dd8389e1f5 Mon Sep 17 00:00:00 2001 From: DorofeevMark Date: Tue, 11 Dec 2018 20:56:19 +0300 Subject: [PATCH] Added aliases collection --- src/models/page.js | 17 +++++++++-------- src/utils/database/aliases.js | 6 ++++++ src/utils/database/index.js | 4 +++- 3 files changed, 18 insertions(+), 9 deletions(-) create mode 100644 src/utils/database/aliases.js diff --git a/src/models/page.js b/src/models/page.js index 9425b3a..a9f3dfb 100644 --- a/src/models/page.js +++ b/src/models/page.js @@ -1,4 +1,5 @@ -const {pages: db} = require('../utils/database/index'); +const {pages: pagesDb} = require('../utils/database/index'); +const {aliases: aliasesDb} = require('../utils/database/index'); /** * @typedef {Object} PageData @@ -27,7 +28,7 @@ class Page { * @returns {Promise} */ static async get(_id) { - const data = await db.findOne({_id}); + const data = await pagesDb.findOne({_id}); return new Page(data); } @@ -39,7 +40,7 @@ class Page { * @returns {Promise} */ static async getAll(query = {}) { - const docs = await db.find(query); + const docs = await pagesDb.find(query); return Promise.all(docs.map(doc => new Page(doc))); } @@ -115,7 +116,7 @@ class Page { * @returns {Promise} */ get parent() { - return db.findOne({_id: this._parent}) + return pagesDb.findOne({_id: this._parent}) .then(data => new Page(data)); } @@ -125,7 +126,7 @@ class Page { * @returns {Promise} */ get children() { - return db.find({parent: this._id}) + return pagesDb.find({parent: this._id}) .then(data => data.map(page => new Page(page))); } @@ -136,11 +137,11 @@ class Page { */ async save() { if (!this._id) { - const insertedRow = await db.insert(this.data); + const insertedRow = await pagesDb.insert(this.data); this._id = insertedRow._id; } else { - await db.update({_id: this._id}, this.data); + await pagesDb.update({_id: this._id}, this.data); } return this; @@ -152,7 +153,7 @@ class Page { * @returns {Promise} */ async destroy() { - await db.remove({_id: this._id}); + await pagesDb.remove({_id: this._id}); delete this._id; diff --git a/src/utils/database/aliases.js b/src/utils/database/aliases.js new file mode 100644 index 0000000..895bb3f --- /dev/null +++ b/src/utils/database/aliases.js @@ -0,0 +1,6 @@ +const Datastore = require('nedb'); +const config = require('../../../config'); + +const db = new Datastore({filename: `./${config.database}/aliases.db`, autoload: true}); + +module.exports = db; diff --git a/src/utils/database/index.js b/src/utils/database/index.js index 0066c58..783acc0 100644 --- a/src/utils/database/index.js +++ b/src/utils/database/index.js @@ -1,4 +1,5 @@ const pages = require('./pages'); +const aliases = require('./aliases'); /** * @class Database @@ -142,5 +143,6 @@ class Database { module.exports = { class: Database, - pages: new Database(pages) + pages: new Database(pages), + aliases: new Database(aliases) };