Skip to main content

Seeders

Introduction

Seeders can be used to pre-populate your DB tables with fake data. We harness the seeder utility provided by KnexJS.

We have written few commands to ease the seeders process for you though, so that you focus on doing wonderful things only.

caution

Seeders are irreversible. Once seeder is run, there is no option to rollback or revert the state of the DB.

Usage

Following are the list of operations that you can perform using the commands that we provide.

Create a new seeder

To create a new seeder file, simply run:

npm run seed:make add_mock_data_to_users_table

This command will create a seeder file add_mock_data_to_users_table.ts inside src/_db/seeds.

caution

All seeders are stored in src/_db/seeds folder.

src/_db/seeds/add_mock_data_to_users_table.ts
import * as Knex from "knex";
import { v4 as uuidv4 } from 'uuid';

export async function seed(knex: Knex): Promise<void> {
// Deletes ALL existing entries
await knex("users").truncate();
// Inserts seed entries
await knex("users").insert([
{ uuid: uuidv4(), firstName: 'Vinayak', lastName: 'Sarawagi', username: 'codingdogg' }
]);
};

Run seeder

To seed the DB Table with your data, simply run:

npm run seed:run

To run a specific seeder file only, do:

npm run seed:run --specific=add_mock_data_to_users_table.ts