Skip to content

Commit

Permalink
Instead of promisifying all methods of mongoose, plugin bluebird into… (
Browse files Browse the repository at this point in the history
#39)

* Instead of promisifying all methods of mongoose, plugin bluebird into mongoose

* Update node version in travis config
  • Loading branch information
kunalkapadia committed Sep 23, 2016
1 parent 4917e6b commit e272926
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: node_js
node_js:
- "4.4"
- "6.3"
- "4.5"
- "6.6"
services:
- mongodb
cache:
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import mongoose from 'mongoose';
import config from './config/env';
import app from './config/express';

// promisify mongoose
Promise.promisifyAll(mongoose);
// plugin bluebird promise in mongoose
mongoose.Promise = Promise;

// connect to mongo db
mongoose.connect(config.db, { server: { socketOptions: { keepAlive: 1 } } });
Expand Down
27 changes: 15 additions & 12 deletions server/controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import User from '../models/user';
* Load user and append to req.
*/
function load(req, res, next, id) {
User.get(id).then((user) => {
req.user = user; // eslint-disable-line no-param-reassign
return next();
}).error((e) => next(e));
User.get(id)
.then((user) => {
req.user = user; // eslint-disable-line no-param-reassign
return next();
})
.catch((e) => next(e));
}

/**
Expand All @@ -30,9 +32,9 @@ function create(req, res, next) {
mobileNumber: req.body.mobileNumber
});

user.saveAsync()
user.save()
.then((savedUser) => res.json(savedUser))
.error((e) => next(e));
.catch((e) => next(e));
}

/**
Expand All @@ -46,9 +48,9 @@ function update(req, res, next) {
user.username = req.body.username;
user.mobileNumber = req.body.mobileNumber;

user.saveAsync()
user.save()
.then((savedUser) => res.json(savedUser))
.error((e) => next(e));
.catch((e) => next(e));
}

/**
Expand All @@ -59,8 +61,9 @@ function update(req, res, next) {
*/
function list(req, res, next) {
const { limit = 50, skip = 0 } = req.query;
User.list({ limit, skip }).then((users) => res.json(users))
.error((e) => next(e));
User.list({ limit, skip })
.then((users) => res.json(users))
.catch((e) => next(e));
}

/**
Expand All @@ -69,9 +72,9 @@ function list(req, res, next) {
*/
function remove(req, res, next) {
const user = req.user;
user.removeAsync()
user.remove()
.then((deletedUser) => res.json(deletedUser))
.error((e) => next(e));
.catch((e) => next(e));
}

export default { load, get, create, update, list, remove };
5 changes: 3 additions & 2 deletions server/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ UserSchema.statics = {
*/
get(id) {
return this.findById(id)
.execAsync().then((user) => {
.exec()
.then((user) => {
if (user) {
return user;
}
Expand All @@ -66,7 +67,7 @@ UserSchema.statics = {
.sort({ createdAt: -1 })
.skip(skip)
.limit(limit)
.execAsync();
.exec();
}
};

Expand Down

0 comments on commit e272926

Please sign in to comment.