Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically run knex seed:run

To seed a database, knex says to run the following script:

$ knex seed:run

I was looking for a way to do this programmatically (ie in my javascript code). I have the following that compiles without any problems but it only returns a promise and doesn't actually do the seeding:

const knexInstance = require('knex')(config);
knexInstance.seed.run()

Any help appreciated.


1 Answers

Knex is using bluebird under the hood, so to run seed, you need to wait for promise to be finished.

Waiting for promise can be done by standard JavaScript await.

await knexInstance.seed.run();

Knex seed source code: GitHub

like image 51
Fka Avatar answered Aug 29 '26 03:08

Fka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!