Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace obsoleting mongoose calls #187

Merged
merged 3 commits into from
Aug 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions app/controllers/index.js
Original file line number Diff line number Diff line change
@@ -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');

/*
Expand Down Expand Up @@ -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);
}
}

Expand Down
3 changes: 3 additions & 0 deletions app/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down
10 changes: 6 additions & 4 deletions app/models/group.js
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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
Expand Down
23 changes: 12 additions & 11 deletions app/models/plugins/isempty.js
Original file line number Diff line number Diff line change
@@ -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};
12 changes: 5 additions & 7 deletions app/models/user.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand Down
18 changes: 8 additions & 10 deletions app/routes/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 5 additions & 7 deletions app/routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 0 additions & 1 deletion test/tests_api/results.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down