NodeJS Inverse Seed Generator is a really handy tool to have in a perfect development environment.
Sometimes you may wanna write the existing data of the database tables, to seed files for later usage. For example you might have some default basic data in a table and you might wanna keep them in the project to inject them by default to the database when the project is deployed on a separate environment. (Eg: Staging server to Live server).
In a scenario like these, what you normally would do is just export the current database dump to the new environment and connect the project to the database. But there can be more smarter ways of doing it right ?
That’s where Inverse Seeds comes to the picture.
Basically what Inverse Seeds or iSeeds means is, it inverting the typical Seeding functionality. So, instead of Running seeders and populating tables, you can create seeds with existing table data.
What you basically want to do is, first install the package on your NodeJS project.
$ npm i sequeliseed
Then you can generate as many as seeders based on the existing table data. Sequeliseed has its own CLI. So, it’s super easy to run commands and generate the seeds.
Generate a seeder file from a table.
$ npx sequeliseed generate table_name
Generate multiple seeders from multiple tables at once.
You can add as many as tables separated by a space.
$ npx sequeliseed generate table1_name table2_name table3_name
You can specify the environment that you’re running this seeds generator. This is important because, the environment is specified in Sequelize
config.json file. So, to connect to the correct database,make sure you specify the correct database.
In some cases, the default files and folders structure of Sequelize ORM is altered by developers for their convenience. In those cases, Sequeliseed might not be able to find the correct Sequelize config.json file which listed all the database configurations. So in those cases, you can can generate seeds by adding configurations manually.
$ npx sequeliseed generate table_name --config
This will prompt for some questions like what’s the Database type, Host, Username, Password and Database name.
You can simply put correct values and it will generate seeds for you.
That’s for stopping by. Leave a comment if you find this article useful! Thanks!