Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 config/bootstrap.js 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?

like image 338
newz2000 Avatar asked Jun 06 '14 20:06

newz2000


People also ask

What does Sails lift do?

Run the Sails app in the current dir (if node_modules/sails exists, it will be used instead of the globally installed Sails). By default, Sails lifts your app in development mode. In the development environment, Sails uses Grunt to keep an eye on your files in /assets .

How do I run the sails app?

Create your app Empty · An empty Sails app, yours to configure (type "?" for help, or <CTRL+> to cancel) ? Type 1 (or press enter) to start with our "Web App" template: an opinionated starter project that includes essential features like login, password recovery, emails, and billing.

Is sails JS framework?

js (or Sails) is a model–view–controller (MVC) web application framework developed atop the Node. js environment, released as free and open-source software under the MIT License. It is designed to make it easy to build custom, enterprise-grade Node. js web applications and APIs.


1 Answers

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!

like image 88
sgress454 Avatar answered Oct 14 '22 04:10

sgress454