Adding Options to your Database Seeder (NestJS)

Prateek Kathal
The Crowdlinker Chronicle
3 min readJan 30, 2020

--

A little while ago, I published an article on “Seeding Databases in NestJS.” Now, I have created this article basically as a small “icing on the cake” to add some magic to the plain old database seeder. It is mainly focused on passing options or arguments to the seeder from the command line.

Occasionally, you need to create a seeder based on inputs/arguments passed in the command itself. For eg: —-users=10. To achieve this, you can use the Node package command-line-args.

Installation

Just use your package manager to update your package.json .

npm install command-line-args
npm install @types/command-line-args --save-dev
// or yarn add command-line-args
// and yarn add -D @types/command-line-args

Passing Arguments

In this example, I am going to use users as an example. it is going to basically tell the seeder to seed a minimum set of users unless an argument is passed.

Please note that I am going to use the same directory structure & file names as mentioned in my previous article (link in the first paragraph of the article).

seed.ts

import * as commandLineArgs from 'command-line-args';

--

--

Prateek Kathal
The Crowdlinker Chronicle

Laravel 📦, NestJS 🦁, Docker 🐳 | Sr. Full Stack Developer @Crowdlinker