forked from kg-kartik/Ambulance-on-demand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seeder.js
38 lines (33 loc) · 842 Bytes
/
seeder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const fs = require("fs");
const mongoose = require("mongoose");
const dotenv = require("dotenv");
//Load env vars
dotenv.config({
path : ".env"
})
//Load models
const Ambulance = require("./model/ambulance");
//Connecting to db
mongoose.connect(process.env.MONGO_URI,{
useNewUrlParser : true,
useCreateIndex : true,
useUnifiedTopology : true
}).then(() => console.log('Database connected'))
.catch((err) => console.log(err,"error"));
//Read json files
const ambulance = JSON.parse(
fs.readFileSync(`${__dirname}/_data/ambulance.json`,'utf-8')
)
//Seeding data into the db
const importData = async () => {
try {
await Ambulance.create(ambulance)
console.log('Data Imported');
process.exit();
} catch (err) {
console.error(err);
}
};
if(process.argv[2] === '-i'){
importData();
}