Database Seeding in NodeJS with MongoDB
Seeders in NodeJS
Database seeding is a process in which we can put predefined data or dummy data in database as per need. such as the data of countries, cities, etc. is fixed data for everyone for these kind of data we can create database seeding files and we can put all the data into database with the help of 1 command only.
Step 1: We should have model firstly for which we want to create seeding so here is an example model Product.
productModel.js |
Step 2: As we want to run 'database seeders' separately in one command so have can create one file for seeders and in this file we also need to add database connection so that we can communicate with database because here app.js/index.js database connection doesn't work as we want to run this separately so example seeder.js file is:
Seeders.js |
Step 3: Now we can run command node seeders.js to create seeding in database
Thanks for reading this article I hope it will help you for creating seeders in node js with mongo database, don't forgot to share your feedback in comments sections.
Comments
Post a Comment