4
votes

I'm quite new to NodeJs but I was used to work with ORMs with PHP. Each time I deployed the site I did a sync to the database. Ex doctrine:schema:update --force when in Symfony.

However, in the documentation of Sequelize, they give examples where the sync is being done before each of the queries. Is this a good practice?

I just made a test and it's possible to make queries without the sync so I was wondering if it's a good idea make the sync only once when the application is launched. How do NodeJs developers are used to work?

In this example, I can do both:

User.sync().then(function () {
    return User.create({
        firstName: 'John',
        lastName: 'Hancock',
        locale: 'en'
    });
});

User.create({
    firstName: 'John',
    lastName: 'Hancock',
    locale: 'en'
});

The difference is that in the second case I'm not in a promise chain so I can't do a task that depends on the user creation without creating a promise on my own.

Thanks ;)

1

1 Answers

1
votes

The .sync() method creates the missing tables in the database.

See: http://docs.sequelizejs.com/en/v3/docs/schema/#syncing

I suppose that it is included in the examples to make sure that they work even when you don't have those tables in the database yet, and so you didn't have to run create table manually.

I wouldn't do that for general code, especially in production, because you usually don't want your code to silently change the database schema, you usually want to control the process yourself.

But for development it may be useful to not have to run any db migration scripts when you change your models, though I wouldn't expect it to work every time.