Skip to content

Instantly share code, notes, and snippets.

@tlimpanont
Last active August 29, 2015 14:18
Show Gist options
  • Save tlimpanont/d87616ef38a0661f3b44 to your computer and use it in GitHub Desktop.
Save tlimpanont/d87616ef38a0661f3b44 to your computer and use it in GitHub Desktop.
if you want to work with mongoose lib in Sails. All you have to do is open a connection and invoke a done callback from the boostrap.js file.
/**
* Bootstrap
* (sails.config.bootstrap)
*
* An asynchronous bootstrap function that runs before your Sails app gets lifted.
* This gives you an opportunity to set up your data model, run jobs, or perform some special logic.
*
* For more information on bootstrapping your app, check out:
* http://sailsjs.org/#/documentation/reference/sails.config/sails.config.bootstrap.html
*/
module.exports.bootstrap = function(cb) {
// It's very important to trigger this callback method when you are finished
// with the bootstrap! (otherwise your server will never lift, since it's waiting on the bootstrap)
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var uri = this.connections[this.models.connection].uri;
mongoose.connect(uri);
// console.log(JSON.stringify(this, null, 4))
var db = mongoose.connection;
db.once('open', cb);
db.once('error', cb);
};
require("sails-test-helper");
describe("Mongoose Services", function() {
before(function(done) {
Mongoose.dropDatabase(done);
});
after(function(done) {
done();
});
describe("GET sample route", function() {
it("router path should not be found", function(done) {
request.get("/sample").expect(404).end(done);
});
});
describe('working with Mongoose models', function() {
it('Mongoose service models', function(done) {
expect(Mongoose.models).to.exist;
done();
});
it('should insert multiple users', function(done) {
var users = [{
firstname: "Theuy",
lastname: "Limpanont"
}, {
firstname: "Will",
lastname: "Smith"
}]
Mongoose.insert(users, Mongoose.models.User)
.then(function(users) {
expect(users).to.be.instanceof(Array);
expect(_.first(users).firstname).to.equal("Theuy");
expect(_.first(users).password).to.equal("123456");
expect(_.last(users).firstname).to.equal("Will");
done();
})
.catch(function(err) {
expect(err).to.be.null;
})
});
it('should handle errors when insert multiple incorrect users data', function(done) {
Mongoose.insert([{}, {}], Mongoose.models.User)
.then(function(users) {
})
.catch(function(err) {
expect(err).not.to.be.null;
expect(err.errors).to.instanceof(Object);
expect(err.name).to.equal("ValidationError");
done();
});
});
it('construct with correct params insert method', function(done) {
expect(function() {
Mongoose.insert({}, Mongoose.models.User);
}, "should be an intanceof Array").to.throw(Error);
expect(function() {
Mongoose.insert([{}, {}]);
}, "should have mongoose.model() ").to.throw(Error);
done();
});
it('find users with mongoose interface', function(done) {
Mongoose.models.User.find().exec(function(err, users) {
expect(err).to.be.null;
expect(users).to.be.instanceof(Array);
done();
});
});
it('find users with promisify interface', function(done) {
Mongoose.models.User.findAsync().then(function(users) {
expect(users).to.be.instanceof(Array);
done();
})
.catch(function(err) {
expect(err).to.be.null;
});
});
});
});
// make a service file as Mongoose.js
// [sails root] --> api --> services --> bootstrap.js
var Promise = require("bluebird");
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var deepPopulate = require('mongoose-deep-populate');
var UserSchema = new Schema({
firstname: {
type: String,
trim: true,
required: true
},
lastname: {
type: String,
trim: true,
required: true
},
password: {
type: String,
trim: true,
default: "123456"
},
})
var User = mongoose.model("User", UserSchema, "user");
exports.models = {
User: User
};
exports.dropDatabase = function(done) {
mongoose.connection.db.dropDatabase(function(err) {
done();
});
}
exports.insert = function(data, model) {
if (!(data instanceof Array)) {
throw new Error("data should be an instanceof Array ");
}
if (model === undefined) {
throw new Error("this insert method require a mongoose.model() ");
}
var promises = [];
data.forEach(function(item) {
promises.push(
new Promise(function(resolve, reject) {
new model(item).save(function(err, _item) {
if (err) {
reject(err);
}
resolve(_item);
})
})
)
});
return Promise.all(promises);
}
require('mongoomise').promisifyAll(mongoose, require('bluebird'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment