Skip to content

Instantly share code, notes, and snippets.

@particlebanana
Last active October 31, 2019 15:53
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save particlebanana/8441398 to your computer and use it in GitHub Desktop.
Save particlebanana/8441398 to your computer and use it in GitHub Desktop.
How to load the beta of Waterline v0.10 when using Express.js Make sure to use the `associations` branch of any adapters you plan on loading.
/**
* A simple example of how to use Waterline v0.10 with Express
*/
var express = require('express'),
_ = require('lodash'),
app = express(),
Waterline = require('waterline');
// Instantiate a new instance of the ORM
var orm = new Waterline();
//////////////////////////////////////////////////////////////////
// WATERLINE CONFIG
//////////////////////////////////////////////////////////////////
// Require any waterline compatible adapters here
var diskAdapter = require('sails-disk'),
mysqlAdapter = require('sails-mysql');
// Build A Config Object
var config = {
// Setup Adapters
// Creates named adapters that have have been required
adapters: {
'default': diskAdapter,
disk: diskAdapter,
mysql: mysqlAdapter
},
// Build Connections Config
// Setup connections using the named adapter configs
connections: {
myLocalDisk: {
adapter: 'disk'
},
myLocalMySql: {
adapter: 'mysql',
host: 'localhost',
database: 'foobar'
}
}
};
//////////////////////////////////////////////////////////////////
// WATERLINE MODELS
//////////////////////////////////////////////////////////////////
var User = Waterline.Collection.extend({
identity: 'user',
connection: 'myLocalDisk',
attributes: {
first_name: 'string',
last_name: 'string'
}
});
var Pet = Waterline.Collection.extend({
identity: 'pet',
connection: 'myLocalMySql',
attributes: {
name: 'string',
breed: 'string'
}
});
// Load the Models into the ORM
orm.loadCollection(User);
orm.loadCollection(Pet);
//////////////////////////////////////////////////////////////////
// EXPRESS SETUP
//////////////////////////////////////////////////////////////////
// Setup Express Application
app.use(express.bodyParser());
app.use(express.methodOverride());
// Build Express Routes (CRUD routes for /users)
app.get('/users', function(req, res) {
app.models.user.find().done(function(err, models) {
if(err) return res.json({ err: err }, 500);
res.json(models);
});
});
app.post('/users', function(req, res) {
app.models.user.create(req.body, function(err, model) {
if(err) return res.json({ err: err }, 500);
res.json(model);
});
});
app.get('/users/:id', function(req, res) {
app.models.user.findOne({ id: req.params.id }, function(err, model) {
if(err) return res.json({ err: err }, 500);
res.json(model);
});
});
app.del('/users/:id', function(req, res) {
app.models.user.destroy({ id: req.params.id }, function(err) {
if(err) return res.json({ err: err }, 500);
res.json({ status: 'ok' });
});
});
app.put('/users/:id', function(req, res) {
// Don't pass ID to update
delete req.body.id;
app.models.user.update({ id: req.params.id }, req.body, function(err, model) {
if(err) return res.json({ err: err }, 500);
res.json(model);
});
});
//////////////////////////////////////////////////////////////////
// START WATERLINE
//////////////////////////////////////////////////////////////////
// Start Waterline passing adapters in
orm.initialize(config, function(err, models) {
if(err) throw err;
app.models = models.collections;
app.connections = models.connections;
// Start Server
app.listen(3000);
});
@ubaltaci
Copy link

Can collections be accessible from other files without exposing models into something global ?

Is it a good practise writing sth like that;

var User = require('waterline')._collections.user

@thomasdashney
Copy link

^ I am wondering the same

@pablovilas
Copy link

I asked about this in the Sails IRC channel and this is one of the possible solutions: http://hastebin.com/megumaboku.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment