In this article, we are creating an Express and Mongoose seeder file that can import the JSON file data into the MongoDB database, and on one command it can delete the whole imported data from the database.
Firstly, let me show you the file that we will import into the MongoDB database with the help of Express and Mongoose seeder files.
[
{
"CourseName":"Angular Course",
"CourseDesc":"A Angular Development Full Course"
},
{
"CourseName":"React Course",
"CourseDesc":"A React JS Development Full Course"
},
{
"CourseName":"Node Course",
"CourseDesc":"A Node JS Development Full Course"
}
]
This above JSON file we will import into a MongoDB database using Express and Mongoose seeder files.
Now, let’s create our Express and Mongoose Seeder files.
Express and Mongoose Seeder File
Make this file on the root of your project.
const fs = require("fs");
const mongoose = require("mongoose");
// Add course models file that have a schema in it
const Course = require("./models/Course");
//Connect to Database
mongoose.connect("mongodb://localhost:27017",{
useNewUrlParser:true,
useUnifiedTopology:true
});
//Add JSON file path to get data of all courses
const courseData = JSON.parse(fs.readFileSync(`${__dirname}/data/course.json`,'utf-8'));
//import data into Database
const importData = async ()=>{
try {
await Course.create(courseData);
console.log("All Data Imported...");
process.exit();
} catch (error) {
console.log(error);
}
}
//Delete Data from Database
const deleteData = async ()=>{
try {
await Course.deleteMany();
console.log("All Data Deleted...");
process.exit();
} catch (error) {
console.log(error);
}
}
// flags for node seeder file
if(process.argv[2]==="-i"){
importData();
}else if(process.argv[2]==="-d"){
deleteData();
}
In this above seeder file, we are importing a file system module to handle files like in our case its Course data JSON file.
We are also importing Mongoose, which helps us to run queries on our database, In the above file we are connecting with the database with Mongoose. connect function, for this article, I am using mongoDBCompass to create a local database.
After connecting you will be able to run your file properly, without connecting with MongoDBCompass your queries will not execute.
readFileSync is a function that reads the file data, inside this we can define the path of the file we want to read, its course.json in our case.
__dirname, this is the current root path address.
Now inside the importData and deleteData, we are simply running our Mongoose create and deleteMany commands to dynamically create all the course.json file data inside the MongoDB database, and with deleteMany, we are deleting all the data.
Now to execute these methods we need to run two commands, one for importing data and another one for deleting data.
process.argv[2] is basically the second argument in the command that will run in the terminal.
Inside the terminal to import all the data we will run the below command
node seeder -i
To delete all the data we will run
node seeder -d
An index value of argv[0] is a node, an index value of argv[1] is seeder the name of the file we have created, and argv[2] is the -i, which is the reason in our file at the end we check for these two arguments, -i and -d.
if(process.argv[2]==="-i"){
importData();
}else if(process.argv[2]==="-d"){
deleteData();
}
These two are basically running these functions based on the argument we passed for importing and deleting i.e. -i and -d
I hope this article is useful, feel free to comment, Happy Coding 🙂
Check out our article on the Express and Mongoose CRUD Project.