307 lines
6 KiB
JavaScript
307 lines
6 KiB
JavaScript
#! /usr/bin/env node
|
|
|
|
console.log(
|
|
'This script populates some test books, authors, genres and bookinstances to your database. Specified database as argument - e.g.: node populatedb "mongodb+srv://cooluser:coolpassword@cluster0.lz91hw2.mongodb.net/local_library?retryWrites=true&w=majority"'
|
|
);
|
|
|
|
// Get arguments passed on command line
|
|
const userArgs = process.argv.slice(2);
|
|
|
|
const Item = require("./models/item");
|
|
const Category = require("./models/category");
|
|
|
|
const items = [];
|
|
const categories = [];
|
|
|
|
const mongoose = require("mongoose");
|
|
|
|
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 categoryCreate(index, name, simpleName, description) {
|
|
const category = new Category({
|
|
name: name,
|
|
simpleName: simpleName,
|
|
description: description,
|
|
});
|
|
await category.save();
|
|
categories[index] = category;
|
|
console.log(`Added category: ${name}`);
|
|
}
|
|
|
|
async function itemCreate(index, name, category, description, price, quantity) {
|
|
const item = new Item({
|
|
name: name,
|
|
category: category,
|
|
description: description,
|
|
price: price,
|
|
quantity: quantity,
|
|
});
|
|
await item.save();
|
|
items[index] = item;
|
|
console.log(`Added item: ${name}`);
|
|
}
|
|
|
|
async function createCategories() {
|
|
console.log("Adding categories");
|
|
await Promise.all([
|
|
categoryCreate(
|
|
0,
|
|
"Feed and Seed",
|
|
"feedandseed",
|
|
"Well well well, look at the city slicker pulling up in his fancy German car!"
|
|
),
|
|
categoryCreate(
|
|
1,
|
|
"Food",
|
|
"food",
|
|
"Frozen or fresh, we've got what you need!"
|
|
),
|
|
categoryCreate(
|
|
2,
|
|
"Household Goods",
|
|
"householdgoods",
|
|
"Basics and essentials for your home"
|
|
),
|
|
categoryCreate(
|
|
3,
|
|
"Personal Care",
|
|
"personalcare",
|
|
"Cleanliness is next to Godliness"
|
|
),
|
|
]);
|
|
}
|
|
|
|
async function createItems() {
|
|
console.log("Adding items");
|
|
await Promise.all([
|
|
itemCreate(
|
|
0,
|
|
"Corn Seed",
|
|
categories[0]._id,
|
|
"25lb. bag of corn seed",
|
|
19.99,
|
|
20
|
|
),
|
|
itemCreate(
|
|
1,
|
|
"Wheat Seed",
|
|
categories[0]._id,
|
|
"25lb. bag of wheat seed",
|
|
19.99,
|
|
20
|
|
),
|
|
itemCreate(
|
|
2,
|
|
"Seed Potatoes",
|
|
categories[0]._id,
|
|
"25lb. bag of seed potatoes",
|
|
14.99,
|
|
20
|
|
),
|
|
itemCreate(
|
|
3,
|
|
"Duck Feed",
|
|
categories[0]._id,
|
|
"25lb. bag of duck feed",
|
|
19.99,
|
|
20
|
|
),
|
|
itemCreate(
|
|
4,
|
|
"Chicken Feed",
|
|
categories[0]._id,
|
|
"25lb. bag of chicken feed",
|
|
19.99,
|
|
20
|
|
),
|
|
itemCreate(
|
|
5,
|
|
"Barn Pellets",
|
|
categories[0]._id,
|
|
"25lb. bag of dehydrated pine pellets",
|
|
19.99,
|
|
20
|
|
),
|
|
itemCreate(
|
|
6,
|
|
"Tuna Salad Sandwich",
|
|
categories[1]._id,
|
|
"Freshly-prepared tuna salad sandwich",
|
|
4.99,
|
|
15
|
|
),
|
|
itemCreate(
|
|
7,
|
|
"Egg Salad Sandwich",
|
|
categories[1]._id,
|
|
"Freshly-prepared egg salad sandwich",
|
|
4.99,
|
|
15
|
|
),
|
|
itemCreate(
|
|
8,
|
|
"Cola (2L.)",
|
|
categories[1]._id,
|
|
"2 liter bottle of cola",
|
|
1.99,
|
|
25
|
|
),
|
|
itemCreate(
|
|
9,
|
|
"Cola (12-pack)",
|
|
categories[1]._id,
|
|
"12 pack of cola",
|
|
3.99,
|
|
25
|
|
),
|
|
itemCreate(
|
|
10,
|
|
"Espresso Coffee Grounds",
|
|
categories[1]._id,
|
|
"Medium-strength espresso coffee grounds (10 oz.)",
|
|
3.99,
|
|
20
|
|
),
|
|
itemCreate(
|
|
11,
|
|
"Sliced American Cheese",
|
|
categories[1]._id,
|
|
"1 pack of sliced American cheese (24 slices per pack)",
|
|
3.99,
|
|
50
|
|
),
|
|
itemCreate(
|
|
12,
|
|
"Lasagna",
|
|
categories[1]._id,
|
|
"Frozen lasagna (1 lb.)",
|
|
9.99,
|
|
25
|
|
),
|
|
itemCreate(
|
|
13,
|
|
"Burritos",
|
|
categories[1]._id,
|
|
"Frozen burritos (12x)",
|
|
9.99,
|
|
25
|
|
),
|
|
itemCreate(
|
|
14,
|
|
"Sliders",
|
|
categories[1]._id,
|
|
"Frozen sliders (12x)",
|
|
9.99,
|
|
25
|
|
),
|
|
itemCreate(15, "Tuna", categories[1]._id, "One can of tuna", 0.99, 25),
|
|
itemCreate(
|
|
16,
|
|
"Crinkle-Cut Fries",
|
|
categories[1]._id,
|
|
"Frozen crinkle-cut fries (80 oz.)",
|
|
7.99,
|
|
25
|
|
),
|
|
itemCreate(
|
|
17,
|
|
"Supreme(R) Pizza",
|
|
categories[1]._id,
|
|
`18" frozen pizza with rising crust, officially Supreme(R) branded`,
|
|
7.99,
|
|
25
|
|
),
|
|
itemCreate(
|
|
18,
|
|
"Soap Bar",
|
|
categories[3]._id,
|
|
`1 soap bar (unsecented)`,
|
|
0.99,
|
|
25
|
|
),
|
|
itemCreate(
|
|
19,
|
|
"Body Wash",
|
|
categories[3]._id,
|
|
`Liquid body wash (18 fl. oz.)`,
|
|
9.99,
|
|
15
|
|
),
|
|
itemCreate(
|
|
20,
|
|
"Sponge",
|
|
categories[2]._id,
|
|
"Sponge with scrub strip",
|
|
0.99,
|
|
100
|
|
),
|
|
itemCreate(
|
|
21,
|
|
"Hair Conditioner",
|
|
categories[3]._id,
|
|
`Revitalizing and moisturizing hair conditioner (18 fl. oz.)`,
|
|
9.99,
|
|
15
|
|
),
|
|
itemCreate(
|
|
22,
|
|
"Shampoo",
|
|
categories[3]._id,
|
|
`Anti-dandruff shampoo (18 fl. oz.)`,
|
|
9.99,
|
|
15
|
|
),
|
|
itemCreate(
|
|
23,
|
|
"Toothpaste",
|
|
categories[3]._id,
|
|
`Anticavity fluoride toothpaste (60g)`,
|
|
2.99,
|
|
50
|
|
),
|
|
itemCreate(
|
|
24,
|
|
"Deodorant Stick",
|
|
categories[3]._id,
|
|
`Deodorant/antiperspirant stick (3oz)`,
|
|
3.99,
|
|
20
|
|
),
|
|
itemCreate(
|
|
25,
|
|
"Hand Soap",
|
|
categories[2]._id,
|
|
"Liquid hand soap (12 oz.)",
|
|
2.99,
|
|
25
|
|
),
|
|
itemCreate(
|
|
26,
|
|
"Toilet Paper",
|
|
categories[2]._id,
|
|
"Triple-ply toilet paper roll (6x)",
|
|
5.99,
|
|
25
|
|
),
|
|
itemCreate(
|
|
27,
|
|
"Paper towel",
|
|
categories[2]._id,
|
|
"Triple-ply paper towel roll (6x)",
|
|
5.99,
|
|
25
|
|
),
|
|
]);
|
|
}
|