diff --git a/app/controllers/index.js b/app/controllers/index.js index 2d33f2fb..d3d7d007 100644 --- a/app/controllers/index.js +++ b/app/controllers/index.js @@ -1,11 +1,12 @@ // native modules -const EventEmitter = require('events').EventEmitter; +const {EventEmitter} = require('events'); // 3rd party modules const mongoose = require('mongoose'); const _ = require('lodash'); const invariant = require('invariant'); // application modules +const {isEmpty} = require('../models/plugins/isempty'); const logger = require('../tools/logger'); /* @@ -165,11 +166,7 @@ class DefaultController extends EventEmitter { // extra functions isEmpty(next) { - this._model.count({}, (error, count) => { - if (error) next(error); - else if (count === 0) next(true); - else next(false); - }); + return isEmpty(this._model, next); } } diff --git a/app/db.js b/app/db.js index 23deef7a..25d977c6 100644 --- a/app/db.js +++ b/app/db.js @@ -12,6 +12,9 @@ let tearingDown = false; const connect = function () { const options = { logger: logger.info.bind(logger), + appname: 'opentmi', + validateOptions: true, + promiseLibrary: Promise, loggerLevel: 'info' // @todo fetch from config file }; diff --git a/app/models/group.js b/app/models/group.js index 9a4b0abb..a09fb87f 100644 --- a/app/models/group.js +++ b/app/models/group.js @@ -1,14 +1,15 @@ - -/*! +/** * Module dependencies */ const mongoose = require('mongoose'); const QueryPlugin = require('mongoose-query'); +// application modules const logger = require('../tools/logger'); +const {IsEmpty} = require('./plugins/isempty'); -const Schema = mongoose.Schema; /* Implementation */ +const Schema = mongoose.Schema; const Types = Schema.Types; const ObjectId = Types.ObjectId; @@ -25,9 +26,10 @@ const GroupSchema = new Schema({ description: {type: String} }); /** - * Group plugin + * plugins */ GroupSchema.plugin(QueryPlugin); // install QueryPlugin +GroupSchema.plugin(IsEmpty); // install IsEmpty /** * Add your diff --git a/app/models/plugins/isempty.js b/app/models/plugins/isempty.js index 65705e8f..50f16c75 100644 --- a/app/models/plugins/isempty.js +++ b/app/models/plugins/isempty.js @@ -1,21 +1,22 @@ -const logger = require('../../tools/logger'); +function isEmpty(model, next) { + const pending = model.estimatedDocumentCount({}) + .then(count => count === 0); + return next ? pending + .then(empty => next(undefined, empty)).catch(next) + : pending; +} /** - * @method isEmpty + * Extend model with isEmpty -function which resolves boolean if collection is empty + * @method IsEmpty * @param {mongoose.Schema} schema * @param {Object} options - * @param {Function} [options.fn=Math.random] - * @param {String} [options.path='random'] */ function IsEmpty(schema, options) { // eslint-disable-line no-unused-vars const editedSchema = schema; - editedSchema.statics.isEmpty = function isEmpty(next) { - this.count({}, (error, count) => { - next(error, count === 0); - }); + editedSchema.statics.isEmpty = function empty(next) { + return isEmpty(this, next); }; - - logger.info('isEmpty registered'); } -module.exports = IsEmpty; +module.exports = {IsEmpty, isEmpty}; diff --git a/app/models/user.js b/app/models/user.js index 0ee4b621..f6fc7c59 100644 --- a/app/models/user.js +++ b/app/models/user.js @@ -1,12 +1,13 @@ - -/*! +/** * Module dependencies */ const mongoose = require('mongoose'); const QueryPlugin = require('mongoose-query'); const bcrypt = require('bcryptjs'); const _ = require('lodash'); +// application modules const logger = require('../tools/logger'); +const {IsEmpty} = require('./plugins/isempty'); /* Implementation */ const Schema = mongoose.Schema; @@ -42,15 +43,12 @@ const UserSchema = new Schema({ settings: {type: Types.Mixed} }); -/** - * User plugin - */ -// UserSchema.plugin(userPlugin, {}); /** - * Query Plugin + * Plugin */ UserSchema.plugin(QueryPlugin); // install QueryPlugin +UserSchema.plugin(IsEmpty); // install isEmpty /** * Add your diff --git a/app/routes/groups.js b/app/routes/groups.js index 4ba11c7f..102d9399 100644 --- a/app/routes/groups.js +++ b/app/routes/groups.js @@ -30,16 +30,14 @@ function Route(app) { app.use(router); - Group.count({}, (error, count) => { - if (error) { - logger.error(error); - return; - } - if (count === 0) { - (new Group({name: 'admins', users: []})).save(); - (new Group({name: 'users', users: []})).save(); - } - }); + Group.isEmpty() + .then((empty) => { + if (empty) { + (new Group({name: 'admins', users: []})).save(); + (new Group({name: 'users', users: []})).save(); + } + }) + .catch(error => logger.error(error)); Group.getUsers('admins', (error, users) => { if (error) { diff --git a/app/routes/users.js b/app/routes/users.js index 415bec34..ae658083 100644 --- a/app/routes/users.js +++ b/app/routes/users.js @@ -42,13 +42,11 @@ function createDefaultAdmin() { */ function Route(app) { // Create a default admin if there is no users in the database - User.count({}, (err, count) => { - if (err) { - logger.warn(err); - } else if (count === 0) { - createDefaultAdmin(); - } - }); + User.isEmpty() + .then((empty) => { + if (empty) { createDefaultAdmin(); } + }) + .catch(error => logger.warn(error)); // Create user routes const userRouter = express.Router(); diff --git a/test/tests_api/results.js b/test/tests_api/results.js index 02b1c19f..b2ea7c1c 100644 --- a/test/tests_api/results.js +++ b/test/tests_api/results.js @@ -5,7 +5,6 @@ const path = require('path'); const fs = require('fs'); // Third party components -const _ = require('lodash'); const superagent = require('superagent'); const {expect} = require('chai'); const logger = require('winston');