node.js - How to deal with calling sequelize.sync() first? -
i'm bit new developing in nodejs, simple problem. i'm building typical webapp based on express + sequelize. i'm using sqlite in-memory since i'm prototyping @ moment. understand if use persistent sqlite file, may not problem, that's not goal @ moment. consider following:
var user = sequelize.define("user", { "username": datatypes.string, // etc, etc, etc }); sequelize.sync(); user.build({ "username": "mykospark" });
at first, got error on user.build() users table not existing yet. realized sequelize.sync() being called async, , insert happening before table created. re-arranged code user.build() call inside of sequelize.sync().complete() fixes problem, i'm not sure how apply rest of project.
my project uses models in bunch of different places. understanding want call sequelize.sync() once after models defined, can used freely. find way block entire nodejs app until sequelize.sync() finishes, doesn't seem form. suppose wrap every single model operation sequelize.sync().complete() call, doesn't seem right either.
so how people deal this?
your .sync() call should called once within app.js file. however, might have additional calls if manage multiple databases in 1 server. typically .sync()
call in server file , var user = sequelize.define("modelname"...
in models/modelname.js file. sequelize suggests type of guidance "create maintainable application database logic collected in models folder". development grows. later in answer, i'll provide easy step follow initializing file structure.
so case, have app.js, models/index.js , models/users.js. app.js server running .sync()
method. in models folder have required index.js folder configure connection database , collect model definitions. have user.js files add model class , instance methods. below example of models/user.js file might find helpful.
user.js
module.exports = function(sequelize, datatypes) { return sequelize.define('user', { username: datatypes.string, },{ classmethods: { dosomething: function(successcb, errcb, request) {} }, instancemethods: { somethingelse: function(successcb, errcb, request) {} } }); };
models/index.js --> see here
edit 03/14/17
now best option setup node app sequelize use sequelize-cli. sequelize migrations , has useful functionality in development , production environments. scope of question , revision answer, best approach following:
npm install sequelize-cli
use npm install sequelize-cli -g
if want installed globally.
then initialize sequelize migrations:
sequelize init
it should install following folders , files structure in folder initiated command:
config: -config.json models: -index.js seeders: migrations:
if want create model can run following command , auto generate file structure you. here example
sequelize model:create --name user --attributes "user:string email:string"
next should able see new model page
in models/page.js.
config: -config.json models: -index.js -user.js -page.js seeders: migrations:
you'll need go models/index.js , define new model database access correct path model. here example:
models/index.js
var sq = new sequelize(dbname, user, password, config); db = { sequelize: sequelize, sequelize: sq, page: sq.import(__dirname + '/page.js'), user: sq.import(__dirname + '/user.js') } module.exports = db;
if need make changes model can go migrations folder , add methods. follow sequelize migration docs here. now, app.js server. before run server need initialize databases. use following script initialize database before running server setup postgres db:
postgresinit.sh
[...] `sudo -u postgres createdb -u postgres -o $pg_user $pg_db. `
if prefer javascript solution, there solution here
app.js
[...] console.log('this sync table database') console.log('and console should read out executing (default): create table if not exists "table name"....') db.sequelize.sync(function(err){});
Comments
Post a Comment