-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducts.js
31 lines (24 loc) · 862 Bytes
/
products.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
const mongoose = require("mongoose");
require("dotenv").config();
const Category = require("./models/category");
const Product = require("./models/products");
const data = require("./data.json");
async function restoreProducts() {
await mongoose.connect(process.env.DATABASE, {});
await Category.deleteMany({});
await Product.deleteMany({});
for (let category of data) {
const { _id: categoryId } = await new Category({
name: category.name,
image: category.image,
}).save();
const products = category.products.map((product) => ({
...product,
category: categoryId,
}));
await Product.insertMany(products);
}
mongoose.disconnect();
console.info("Database Filled/Restored Successfully!!");
}
restoreProducts();