forked from TuanAnhNguyen2807/Restfull-API-CoffeeShop-CNPM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimportdb.js
33 lines (31 loc) · 1.13 KB
/
importdb.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 raw = require('./data.json')
const mongoose = require('mongoose')
const data = raw.data
const categoryModel = require('./models/category')
const productModel = require('./models/product')
mongoose
.connect("mongodb+srv://user:[email protected]/coffeeShopDB?retryWrites=true")
.then(() => {
console.log("Database Connection is ready...");
data.forEach(category => {
categoryModel
.create({name: category.name})
.then(resp=>{
category.dishes.forEach(dish=>{
productModel.create({
name: dish.name,
description: dish.description,
images: dish.images,
price: dish.price,
category: resp._id
})
.then(res=>console.log("Created " + res.name))
.catch(e=>console.error(e))
})
})
.catch(e=>console.error(e))
})
})
.catch((err) => {
console.log(err);
});