Skip to content

Instantly share code, notes, and snippets.

@juanpasolano
Created September 9, 2014 21:50
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save juanpasolano/5c7596d8629eeeb8debd to your computer and use it in GitHub Desktop.
Save juanpasolano/5c7596d8629eeeb8debd to your computer and use it in GitHub Desktop.
seed database on sails js
/**
* Locations.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs :: http://sailsjs.org/#!documentation/models
*/
module.exports = {
seedData:[
{
country: 'Australia',
states: ['Brisbane', 'Perth', 'Sydney']
}
]
};
/**
* 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) {
async.series([
Locations.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)
};
/**
* 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: 'someMongodbServer',
/***************************************************************************
* *
* 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: 'alter',
/**
* 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.createEach(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.create(self.seedData).exec(function (err, results) {
if (err) {
sails.log.debug(err);
callback();
} else {
sails.log.debug(modelName + ' seed planted');
callback();
}
});
}
};
@RJTM
Copy link

RJTM commented Sep 19, 2014

Cheers!

@lommaj
Copy link

lommaj commented Apr 11, 2015

this is great, im pretty new to the sails framework, but this is going to help a bunch. thanks!!

@lommaj
Copy link

lommaj commented Apr 11, 2015

I was going to write a small app to do this and call it in bootstrap, this seems much better because the seed data is defined right in the model.

@fernandolguevara
Copy link

This is how I execute the sails seed task

@mp3por
Copy link

mp3por commented Oct 4, 2017

Hey,

in sails v1 self.adapter is undefined.

@nagarjun
Copy link

nagarjun commented Jan 8, 2018

How would associations work? For example, if I had another model called "Continents" and wanted to associate the Australia with a continent called Australia from a model called api/models/continents.js?

@Kaeper
Copy link

Kaeper commented Dec 28, 2018

Anyone could run this seed on sails 1.0 or 1.1 ?

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