We want to test our apps with development data as developers, but recreating the wheel every time can be overkill. So we'll need to find out a way to import Mock data into our database using a single script that loads data from a 'json' file. Yes, a single script without the need to write the 'create function.' This procedure is known as Seeding and is absolutely unrelated to our current research. and it is completely independent of our existing project
Prerequisite
- Nodejs installed
- Database management (basics)
TOC
🔗 Setup node project
🔗 Connect MongoDB locally
🔗 Setup Express App
🔗 Import Data
🔗 Delete Data
Let's get right to it.
🥦 Make a new directory called seeding-tut with the command mkdir seed
Change directory cd seed
🥦 'npm init -y' creates a new node project.
Install packages:
npm i express, mongoose, dotenv, morgan
Install dev dependency
npm i nodemon --save-dev
🥦 Create an entry point index.js
Configure your package.json
file.
Note : the "type":"module" enables ES Modules in Node.js
🥦 Then, in the index.js file
, we import the packages and create an express app.
import express from 'express';
import mongoose from 'mongoose';
const app = express();
🎯 Because the front end typically runs on PORT:3000, we'll set our app to run on port 5353 to avoid any conflicts, and then we'll listen to our server.
Create your app
import express from 'express';
import mongoose from 'mongoose';
import morgan from 'morgan';
const app = express();
const PORT = 5353;
app.use(express.json()); //method inbuilt in express to recognize the incoming Request Object as a JSON Object.
app.get('/', (req, res) => {
return res.status(200).json('homepage')
})
app.listen(PORT, () => console.log(`server is running on http://locahost:${PORT}`));
🥦 Let's get the server up and running.
'npm run dev' is a command that you can use to test your code.
Next, we'll design our express route.
Create a route folder and a users.js
file within it.
🎯 To emphasize that seeding is independent to the project.
Let's start by building a user model. Make a folder for your schema and begin writing it.
import mongoose from 'mongoose';
const { Schema } = mongoose;
const userSchema = new mongoose.Schema({
fullName: {
type: String,
required: [true, 'A name is required'],
unique: true
},
email: {
type: String,
required: [true, 'Please enter a valid email to proceed']
},
phoneNumber: {
type: String,
required: [true, 'Please enter a valid phone number']
}
}, {
timestamps: true
})
// Exports schemas
export default mongoose.model('User', userSchema);
🥦
Create your route handler, add logic, and import the user model you made before.
import express from 'express';
const router = express.Router();
import User from '../models/user.js';
// Create a user
router.post('/users', async(req, res) => {
try {
const newUser = await User.create(req.body);
newUser.save();
return res.status(200).json({
message: 'user created successfully',
data: newUser
})
} catch (error) {
return res.status(500).json({
status: 'fail'
})
}
})
//Get all users
router.get('/users', async(req, res) => {
try {
const getAllUser = await User.find();
return res.status(200).json({
message: 'user data gotten successfully',
data: getAllUser
})
} catch (error) {
return res.status(500).json({
status: 'fail'
})
}
})
export default router;
👨💻 Let's connect to our local DB.
1) Open your cmd and type mongod
2) Open another cmd without closing the first and type mongo --host localhost:27017
3) Create a db folder and a db.js file in it.
4) Import the db and mount the route handler into the entry file index.js
5) open mongodb Compass and connect
Moment of truth 🦚
Open postman or insomnia and let's create a new user
Check if data was produced by refreshing your MongoDB compass.
You have made it this far... now let's seed dummy data into our DB.
Let's add some additional development data to our database now.
Make a folder called data and a file called dev-data.js in it.
To read the JSON file, we first require access to the file system module.
import * as fs from 'fs'; // to read our data from the json file
Also we need access to the user model
import * as fs from 'fs';
import mongoose from 'mongoose';
import dotenv from 'dotenv';
dotenv.config();
import User from '../models/user.js';
Create a data.json
file inside the data folder.
[{
"fullName": "John Doe",
"email": "john@yopmail.com",
"phoneNumber": "546 69 200898",
},
{
"fullName": "mary Doe",
"email": "mary@yopmail.com",
"phoneNumber": "777 69 200898",
}
]
Now we'll read our json file, but first we'll need to convert data to a Javasript object using (JSON.parse)
const users = JSON.parse(fs.readFileSync(`${__dirname}/data.json`, 'utf-8'));
The data is then imported into our database.
//seed or import Data into DB
const importData = async() => {
try {
await User.create(users);
console.log('Data seeded successfully....');
} catch (error) {
console.log(error)
process.exit();
}
}
We may also clean all databases using a single script.
//delete Data in DB
const deleteData = async() => {
try {
await Techie.deleteMany();
console.log('Data successfully deleted');
} catch (error) {
console.log(error)
}
process.exit();
}
🥦 Finally, to start our script, we construct a conditional statement.
if (process.argv[2] === '--import') {
importData();
} else if (process.argv[2] === '--delete') {
deleteData()
}
console.log(process.argv);
🥦 Explanation:
If the third index output of process.argv equals —-import, the importData() function will be called.
🥦 So, let's try it in our terminal.
Delete data: node data/dev-data.js --delete
Import data: node data/dev-data.js --import
Let me destroy all data in our DB first and then import the development data.
As explained earlier, because the third index was "delete" we fired the delete function and our DB got deleted.
Let's see if it was removed from our database.
When the MongoDB compass is refreshed.
🥦 Now let's import our mock data by using node data/dev-data.js --import
🥦 Let's double-check with our compass...
Yay!...and it worked.
Conclusion
With a single script, we were able to import and delete data.
It takes some practice to get the hang of this method.
It is, nonetheless, worthwhile.
Top comments (0)