-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataAdder.js
32 lines (28 loc) · 900 Bytes
/
dataAdder.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
const { MongoClient } = require('mongodb');
const { articleData, users, categories } = require('./data/data');
require('dotenv').config();
const data = articleData;
console.log('execution started');
//? DATABASE CONNECTION ////////////////////////////////////
const client = new MongoClient(process.env.DATABASE_CONNECTION_STRING);
// const jsonData = json.parse(data);
// console.log(jsonData);
const addData = async (col, data) => {
try {
await client.connect();
console.log('Connected successfully to the database server');
const database = client.db(process.env.DATABASE_NAME);
const collection = database.collection(col);
//const customData = {};
collection.insertMany(data, (err, res) => {
if (err) throw err;
console.log(res);
});
await client.close();
} catch (err) {
console.log(err.stack);
} finally {
await client.close();
}
};
addData('articles', data);