-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadd_product_mongo.js
29 lines (27 loc) · 976 Bytes
/
add_product_mongo.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
require('dotenv').config();
const mg_product = require('./models/mongodb/models/function_product_mongodb.js');
const {connectMongoDB} = require('./models/connection/mongodbConnect')
const product_json_fn = './mongodb_data/product_mongo_list.json';
const fs = require('fs');
const importProduct = async () => {
fs.readFile(product_json_fn, 'utf-8', async (readErr, data) => {
try {
await mg_product.dropAll();
if (readErr) {
console.log("Error reading file: " + readErr);
return;
}
const json_data = JSON.parse(data);
for (product of json_data) {
await mg_product.saveProduct(product.mysql_id, product.category, product.attribute)
}
console.log('Product Import Success')
process.exit()
} catch (error) {
console.error('Error with product import', error)
process.exit(1)
}
})
}
connectMongoDB();
importProduct();