Knovour
3/2/2015 - 4:48 PM

sails.js call mongo model use waterline

sails.js call mongo model use waterline

// Example for call User Model
// Place under sails project folder
// need `npm i waterline --save` first if module not exist
// node hot-fix.js

var _ = require('lodash');
var Waterline = require('waterline');
var orm = new Waterline();
var config = {
  adapters: {
    mongo: require('sails-mongo')
  },
  connections: {
    mongo: _.extend(require('./config/connections').connections.someMongodbServer, {adapter: 'mongo'})
  }
};

var UserModel = require('./api/models/User');
UserModel.identity = 'user';
UserModel.connection = 'mongo';

var User = Waterline.Collection.extend(UserModel);

orm.loadCollection(User);
orm.initialize(config, function(err, models) {
  if(err) throw err;

  models.collections.user.find({}, function(err, users) {
    console.log(users);
  });
});