-
Notifications
You must be signed in to change notification settings - Fork 0
/
InsertRecords.js
45 lines (41 loc) · 1.28 KB
/
InsertRecords.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
39
40
41
42
43
44
45
require("dotenv").config();
const mongoose = require("mongoose");
const db = require("./models");
const handleNoArgs = require("./utils/handleNoArgs");
const csvToJSON = require("csvtojson");
const MONGODB_URI = process.env.MONGODB_URI || "mongodb://localhost/datanexus";
mongoose
.connect(MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log("\nConnected to DB.")
process.argv[2] && process.argv[3] && process.argv[4]
? addData(process.argv[2], process.argv[3], process.argv[4])
: handleNoArgs();
})
.catch((err) => {
console.log(err);
process.exit(1);
});
const addData = async (collection, directory, file) => {
const newData =
directory === "manifests"
? await csvToJSON().fromFile(`./data/${directory}/${file}`)
: require(`./data/${directory}/${file}`);
db[collection]
.insertMany(newData)
.then(() => {
console.log(`${directory} records succesfully updated`);
process.exit(0);
})
.catch((err) => {
console.log(err);
process.exit(1);
});
};
// arg2: collection to update, arg3: directory, arg4: file
// process.argv[2] && process.argv[3] && process.argv[4]
// ? addData(process.argv[2], process.argv[3], process.argv[4])
// : handleNoArgs();