Last active
August 29, 2015 13:57
-
-
Save particlebanana/9348414 to your computer and use it in GitHub Desktop.
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
var _ = require('lodash'), | |
async = require('async'), | |
Waterline = require('waterline'); | |
// Instantiate a new instance of the ORM | |
var orm = new Waterline(); | |
var mysqlAdapter = require('sails-mysql'); | |
var config = { | |
// Setup Adapters | |
// Creates named adapters that have have been required | |
adapters: { | |
'default': mysqlAdapter, | |
mysql: mysqlAdapter | |
}, | |
// Build Connections Config | |
// Setup connections using the named adapter configs | |
connections: { | |
myLocalMySql: { | |
adapter: 'mysql', | |
host: 'localhost', | |
user: 'root', | |
database: 'foobar' | |
} | |
} | |
}; | |
////////////////////////////////////////////////////////////////// | |
// WATERLINE MODEL | |
////////////////////////////////////////////////////////////////// | |
var User = Waterline.Collection.extend({ | |
identity: 'user', | |
connection: 'myLocalMySql', | |
attributes: { | |
name: 'string' | |
} | |
}); | |
// Load the Models into the ORM | |
orm.loadCollection(User); | |
////////////////////////////////////////////////////////////////// | |
// START WATERLINE | |
////////////////////////////////////////////////////////////////// | |
// Start Waterline passing adapters in | |
orm.initialize(config, function(err, ontology) { | |
if(err) throw err; | |
var UserModel = ontology.collections.user; | |
// Keep track of the inserted users so we can use the id to update them. | |
// Data won't have an id attribute until it has been inserted. | |
var users = []; | |
function createUser(attributes, next) { | |
UserModel.create(attributes).exec(function(err, record) { | |
if(err) return next(err); | |
users.push(record); | |
next(); | |
}); | |
} | |
function updateUser(attributes, next) { | |
var attr = _.cloneDeep(attributes); | |
delete attr.id; | |
attr.name = attributes.name + ' up'; | |
UserModel.update({ id: attributes.id }, attr).exec(function(err, record) { | |
if(err) return next(err); | |
console.log(record); | |
next(); | |
}); | |
} | |
var letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', | |
'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']; | |
var data = letters.map(function(letter) { return { name: letter }; }); | |
async.each(data, createUser, function(err) { | |
if(err) console.log(err); | |
console.log('INSERTED ALL THE USERS'); | |
async.each(users, updateUser, function(err) { | |
if(err) console.log(err); | |
console.log('UPDATED ALL THE USERS'); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment