sails.js - SailsJS best practice to seed database with data before other Models are initialized -
there model other models assume existence. should initialized before api function called.
the way (it doesn't work):
1) define model in api/models, let's call location.js
2) add following bootstrap.js
var locations = require('../api/models/locations.js'); module.exports.bootstrap = function (cb) { // seed database 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 right way initial database seeding?
i error:
locations.create(locationsobj, function locationsobj(err, locations) { ^ typeerror: object #<bject> has no method 'create'
question 2 how cb function of bootstrap work? if there error, do?
the sails models globally available; don't need require @ bootstrap.js.
this use seed database. (see links enclose go gists)
include seed function @ config/models.js. methods declare in file extended models. link: seed method gist
define de data seed consume in model link: model's seed data
call seed method in config/bootstrap.js using async. link: calling method
have @ threat also: best way migrate table changes production sailsjs tables
Comments
Post a Comment