Nodejs: Importing Mock data with script
Tags: javascript, webdev, beginners, tutorial
Published: 2021-10-11 13:29:32

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 <mark>Seeding</mark> 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


![big](https://media1.giphy.com/media/UV9y6wbZfgCvTu73QT/giphy.gif?cid=82a1493b8pl0j5u8dxtis7as3xjr7da56lys9x52ttjvknq8&rid=giphy.gif&ct=g)

## 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:
 ```Javascript
npm i express, mongoose, dotenv, morgan
```
Install dev dependency 
 ```Javascript
npm i nodemon --save-dev
```

🥦 Create an entry point `index.js`

![pik1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hbcdi92jqhda1qjuzo99.PNG)

## Configure your `package.json` file.
![pik2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ifuyqbx950wb8jb4zh7.PNG)
 

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.

```Javascript
import express from 'express';
import mongoose from 'mongoose';



const app = express();
```

🎯 Because the front end typically runs on <mark>PORT:3000</mark>, 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
```Javascript
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. 

Result: 
![pik4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xy8duvrhzxeq5opt1zh0.PNG)
 
## Next, we'll design our express route.
Create a route folder and a `users.js` file within it. 
![pik6](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fntier1ymtavd79446ot.PNG)

## 
🎯 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. 

![pik 7](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pqwiy9uel6bilbxv3ofb.PNG)

```Javascript
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. 
```Javascript

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`
![mongod1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u89ce7eha655rghk7bou.PNG)

2) Open another cmd without closing the first and type `mongo --host localhost:27017`
![mongo3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/praehtpu1w49p415ruac.PNG)
 
3) Create a db folder and a db.js file in it.
![db new](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yrc88qyzyig8ymd7y87p.PNG)
 

4) Import the db and mount the route handler into the entry file `index.js`
![connect to db](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b703w7lscu3rxsr5wtv2.PNG)
 ![user routes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cp1b1cdu2wc0q0ij0a7f.PNG) 

5) open mongodb Compass and connect
![mongo connect](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ocka0h53dtzro0eiy6pm.PNG)
![compass1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bo86uuhqlxaae2sf7rri.PNG)
  

6) You should get on restart:
![established](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j0yhscts1m5hlj19nzox.PNG)

##  Moment of truth 🦚
Open postman or insomnia and let's create a new user
![isoma](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8mlg2xc6xlwrijv0h0wu.PNG)
 
  

Check if data was produced by refreshing your MongoDB compass.
 ![seed users](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/imf0h63vgcpxrod45qix.PNG) 

---

## 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. 

```Javascript
import * as fs from 'fs'; // to read our data from the json file

```

Also we need access to the user model
```Javascript
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.
```Javascript

[{
        "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) 
```Javascript
const users = JSON.parse(fs.readFileSync(`${__dirname}/data.json`, 'utf-8'));
```
---


The data is then imported into our database. 
```Javascript
//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. 
```Javascript
//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. 
```Javascript
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.
![delete](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8vjl4xrk70j91p3x33s7.PNG)

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. 
![no collection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zzjxulgou475xfuxvf4j.PNG)


🥦 Now let's import our mock data by using ` node data/dev-data.js --import`
 
![seeddd](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zpsn9d1r281tnqu0b093.PNG)


🥦 Let's double-check with our compass... 
![yay](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ze5qz1ctkylaype4xv0p.PNG)

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. 

## Reference  
 [Importing development data by Jonas IO ](https://www.youtube.com/watch?v=PWeDNjbUcUU&t=6s)