4

There is a model that all other models assume its existence. It should be initialized before any API function is called.

The way I do this (it doesn't work):

1) Define model in api/models, let's call it Location.js

2) Add the following to bootstrap.js

    var Locations = require('../api/models/Locations.js');

    module.exports.bootstrap = function (cb) {

      // seed the database with Locations
        var locationsObj = {
            country: 'Australia',
            states: ['Brisbane', 'Perth', 'Sydney']
        };
        Location.create(locationsObj, function locationsObj(err, locations) {
            if (err) {
                cb(err);
            }
            console.log('locations created: ', locations);
        });
  }

Question 1 Is it the right way to do initial database seeding?

I get this error:

Locations.create(locationsObj, function locationsObj(err, locations) {
          ^
TypeError: Object #<bject> has no method 'create'

Question 2 How does the cb function of bootstrap work? what if there as an error, what to do?

user2867106
  • 1,139
  • 1
  • 13
  • 31
  • 1
    1) See answer from @Nizar. 2) If there's an error, you can pass it as an argument to `cb`, which will stop Sails from lifting. 3) Please don't ask two questions in one SO post! – sgress454 Apr 08 '14 at 16:26

4 Answers4

10

The sails models are globally available; so you don't need to require at bootstrap.js.

This is what I use to seed my database. (See the links I enclose to go to the gists)

  1. Include seed function at config/models.js. The methods you declare in this file will be extended to all your models. Link: Seed method gist

  2. Define de data the seed will consume in your model Link: Model's seed data

  3. Call the seed method in config/bootstrap.js using async. Link: Calling method

UPDATE

Have a look at this threat also: Best way to migrate table changes to production sailsjs tables

Community
  • 1
  • 1
Juan Solano
  • 1,188
  • 1
  • 17
  • 32
  • with sails v1.0.x in callback, this does not contain the adapter - breaking while extracting modelName - possible to update the code there ? – Danish Jul 24 '18 at 06:15
  • 1
    I have not used sails for a long time. I don't really know how has it change. I would check for possible plugins that do migrations better than this answer. – Juan Solano Jul 25 '18 at 04:31
2

From Cannot unit test my model in sailsjs:

"Once Sails app is lifted, you will have your models available automatically...

And in your case, your first line overrides the User model which would be otherwise constructed by Sails.js, that's why even though you have an object it's not a Waterline model."

sgress454
  • 24,870
  • 4
  • 74
  • 92
Nizar Blond
  • 1,826
  • 5
  • 20
  • 42
1

I know this is old but, for completeness:

You set

var Locations = ...

But but you call

Location.create()

(no 's') so you just have a typo.

Gadi
  • 1,152
  • 9
  • 6
0

in config/bootstrap.js you can write your seeds directly. Take a look at the example below.

await sails.models.role.createEach([
    {
      name: 'Admin',
    },
    {
      name: 'Normal-user',
    },
  ]);

here 'role' is name of the table created and not the model name.

sharad shetty
  • 342
  • 2
  • 5