Skip to content

Instantly share code, notes, and snippets.

@crobinson42
Last active March 6, 2016 19:50
Show Gist options
  • Save crobinson42/6abd0dba2a577f316308 to your computer and use it in GitHub Desktop.
Save crobinson42/6abd0dba2a577f316308 to your computer and use it in GitHub Desktop.
Seeding Sails.js Models
/**
* ActivityType.js
*
*
*
* ****** NOTE - Seed Data / Default DB Data *******
* On server/app lift, there are default activity types loaded into the database.
* 1. model's are assigned a key `seedData` of the data to be inserted to dB
* and this data is available in the global `Model`.seedData
* 2. config/models.js has custom methods that are attached to all models on server
* lift, `Model`.seed(callback) which look for `Model`.seedData
* 3. Finally, config/bootstrap.js will invoke `Model`.seed(callback)
*
*
* * This model, ActivityType *
* Default data uses `name` as the index on the frontend since we cannot
* predict the id that would be assigned on different DB migration actions
* in the future. Organization specific activity types are searched by
* attribute `organization` and indexed by organization & name
*
*/
var defaultActivityTypes = [{name : 'running'}, {name : 'hiking'}];
module.exports = {
// this is the default activityTypes that get added to the DB in config/bootstrap.js
seedData: defaultActivityTypes,
attributes: {
}
};
/**
* 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) {
/**
* Seed ActivityType defaults into DB
*
* We invoke this last in bootstrap.js file because it's async and we hand it
* the callback to be invoked when complete.
*
*/
// async is global
async.series([
ActivityType.seed
],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)
// cb();
};
/**
* Default model configuration
* (sails.config.models)
*
* Unless you override them, the following properties will be included
* in each of your models.
*
* For more info on Sails models, see:
* http://sailsjs.org/#!/documentation/concepts/ORM
*/
module.exports.models = {
/***************************************************************************
* *
* Your app's default connection. i.e. the name of one of your app's *
* connections (see `config/connections.js`) *
* *
***************************************************************************/
connection: 'mongoServer',
/***************************************************************************
* *
* How and whether Sails will attempt to automatically rebuild the *
* tables/collections/etc. in your schema. *
* *
* See http://sailsjs.org/#!/documentation/concepts/ORM/model-settings.html *
* *
***************************************************************************/
migrate: 'safe',
/**
* This method adds records to the database
*
* To use add a variable 'seedData' in your model and call the
* method in the bootstrap.js file
*/
seed: function (callback) {
var self = this;
var modelName = self.adapter.identity.charAt(0).toUpperCase() + self.adapter.identity.slice(1);
if (!self.seedData) {
sails.log.debug('No data avaliable to seed ' + modelName);
callback();
return;
}
self.count().exec(function (err, count) {
if (!err && count === 0) {
sails.log.debug('Seeding ' + modelName + '...');
if (self.seedData instanceof Array) {
self.seedArray(callback);
}else{
self.seedObject(callback);
}
} else {
sails.log.debug(modelName + ' had models, so no seed needed');
callback();
}
});
},
seedArray: function (callback) {
var self = this;
var modelName = self.adapter.identity.charAt(0).toUpperCase() + self.adapter.identity.slice(1);
self.findOrCreate(self.seedData).exec(function (err, results) {
if (err) {
sails.log.debug(err);
callback();
} else {
sails.log.debug(modelName + ' seed planted');
callback();
}
});
},
seedObject: function (callback) {
var self = this;
var modelName = self.adapter.identity.charAt(0).toUpperCase() + self.adapter.identity.slice(1);
self.findOrCreate(self.seedData).exec(function (err, results) {
if (err) {
sails.log.debug(err);
callback();
} else {
sails.log.debug(modelName + ' seed planted');
callback();
}
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment