Firstly What is database seeding?
Seeding a database is a process in which an initial set of data is provided to a database when it is being installed. It is especially useful when we want to populate the database with data we want to develop in future.
Now that we know what database seeding is let write some code,I believe you are familiar with Nodejs and MongoDB, so set up your Nodejs server. then create a product.model.js file and write the following:
Create product.controller.js file and write the following to create a product:
Now create a product.route.js file and write the following:
Now let create our seed.js file and write the following :
In your package.json add a seed script:
npm run seed
to seed data to MongoDB
Enjoy!!!
Top comments (3)
Great Article. Just a suggestion instead of giving pictures of code snippets, it becomes helpful if you add the code using markdown.
Alright man, thanks for the feed back
I think providing _id manually is not a good idea.