newz2000 newz2000 - 1 month ago 7x
Node.js Question

Loading data (fixtures) the first time a Sails.js app is lifted

I'm using Sails.js 0.10 RC7. When a developer checks out a project and runs it, I'd like to pre-populate some data into the database. This should be done only the first time the app is run (or maybe we should have this happen whenever new data is added to the fixture).

I could put the data in

but as I understand it, that is run each time
sails lift
is run. I'm OK doing this and writing some logic, but if there is already a convention for taking care of this, it'd be great to use it.

Just to be clear, I'm not specifically referring to test fixtures, but some data that is assumed to be available in any running environment.

Anyone already solve this problem?


We face this issue with nearly every Sails project we do at Balderdash. We almost always solve it by building up data in the bootstrap and wrapping it in logic to detect whether the data already exists, just as you described. Something like:

User.find({admin: true}).exec(function(err, adminUser) {
    // If an admin user exists, skip the bootstrap data
    if (adminUser) { return cb(); }
    // Otherwise, create data below...

It's not as robust as true "up/down" database migrations, but since we use the very convenient (and schemaless) disk adapter for development, it's simple to just wipe the database by erasing the data file, and lift Sails again to get a freshly bootstrapped copy. We rarely miss the complications the db migrations entail, but we do realize that there are situations where they become necessary. The Sails community has responded by coming up with some solutions (here's one example), which is what we like to see!