Knex seeds

Yesterday I had created my migrations and everythings is great so far, but I will need some data in my table. So I have two options - well in fact I have many options but whatever - either writing a sql file with some default data and run it every time I need or using the seeds knex gives me.

Knex Seeds

Knex has a command that can generate data for your tables it is kinda similar to migrations.

1
npx knex seed:make type-tickets

It will create a seeds folder and create the file type-tickets inside. After a little modifications this is the result of my first seed.

1
2
3
4
5
6
7
8
9
10
11
exports.seed = function(knex, Promise) {
return knex('ft_ticket_types')
.del()
.then(() => {
return knex('ft_ticket_types').insert([
{ id: 1, name: 'Customer', code: 'CUS' },
{ id: 2, name: 'VIP', code: 'VIP' },
{ id: 3, name: 'Regular', code: 'REG' },
]);
});
};

Seeds functions must return a Promise, always! First I am connecting knex to the table ft_ticket_types. Then I delete all the data using the del() function. Since del returns a Promise I can chain the next action using then. Inside of the then I will call the knex function again but now I will call the insert method. The only parameter I pass is an array of objects where every object represent the table I had created yesterday.

Running seeds

Great, my seed is ready to go now I can run my seed using seed:run

1
npx knex seed:run

Looks like everything is working because the output message was

1
Ran 1 seed files

If I go to adminer - Yeah I love adminer! ❤️ - I can see my data there.

knex seeds

I can go to sleep now 😴