Thursday, 11 May 2017

Knex migrate latest programmatically

I am trying to following stuff while starting a node app. I need to export my db object to other modules, so I need to export db.

The problem is migration code may give error, but because of asynchronous nature of nodejs, db config had already been exported. I am looking to synchronize following code, so that export happens only if migration completes.

/*
  1. Db init
  2. Migration stuff
*/
const db = knex(config);
(async () => {
try {
    const migrate = async () => db.migrate.latest();
    await migrate();
} catch(ex) {
    console.log('Error migrating: ', ex);
}
})();

module.exports = db;



via Nitin

No comments:

Post a Comment