Last active
May 30, 2017 14:28
-
-
Save robophil/a53ca5683fd71a36a0a96e6ef92155c4 to your computer and use it in GitHub Desktop.
using waterline with express
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A simple example of how to use Waterline v0.10 with Express | |
*/ | |
var _ = require('lodash'); | |
var 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'); | |
var 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', | |
user: 'root', | |
password: '' | |
} | |
}, | |
defaults: { | |
migrate: 'alter' | |
} | |
}; | |
////////////////////////////////////////////////////////////////// | |
// WATERLINE MODELS | |
////////////////////////////////////////////////////////////////// | |
var User = Waterline.Collection.extend({ | |
identity: 'user', | |
connection: 'myLocalMySql', | |
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); | |
////////////////////////////////////////////////////////////////// | |
// START WATERLINE | |
////////////////////////////////////////////////////////////////// | |
// Start Waterline passing adapters in | |
orm.initialize(config, function(err, models) { | |
if(err) throw err; | |
console.log('OK'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment