-
Notifications
You must be signed in to change notification settings - Fork 0
/
populate.js
77 lines (67 loc) · 1.88 KB
/
populate.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const userArgs = process.argv.slice(2);
const Category = require("./models/category");
const Item = require("./models/item");
const categories = [];
const items = [];
const mongoose = require("mongoose");
mongoose.set("strictQuery", false); // Prepare for Mongoose 7
const mongoDB = userArgs[0];
main().catch((err) => console.log(err));
async function main() {
console.log("Debug: About to connect");
await mongoose.connect(mongoDB);
console.log("Debug: Should be connected?");
await createCategories();
await createItems();
console.log("Debug: Closing mongoose");
mongoose.connection.close();
}
async function createCategories() {
console.log("Adding categories");
await Promise.all([
categoryCreate("Fantasy", "Books of fantasy genre"),
categoryCreate("Science Fiction", "Books of science fiction genre"),
categoryCreate("French Poetry", "Books of French poetry genre"),
]);
}
async function createItems() {
console.log("Adding items");
await Promise.all([
itemCreate(
"The Name of the Wind",
"A thrilling fantasy novel.",
categories[0],
19.99,
10,
"https://example.com/item1"
),
itemCreate(
"Dune",
"A science fiction masterpiece.",
categories[1],
24.99,
5,
"https://example.com/item2"
),
// Add more items as needed
]);
}
async function categoryCreate(name, description) {
const category = new Category({ name: name, description: description });
await category.save();
categories.push(category);
console.log(`Added category: ${name}`);
}
async function itemCreate(name, description, category, price, numberInStock, url) {
const item = new Item({
name: name,
description: description,
category: category,
price: price,
numberInStock: numberInStock,
url: url,
});
await item.save();
items.push(item);
console.log(`Added item: ${name}`);
}