-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopulatedb.js
40 lines (34 loc) · 1.17 KB
/
populatedb.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
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:[email protected]/local_library?retryWrites=true&w=majority"'
);
const userArgs = process.argv.slice(2);
const Url = require("./models/url");
// const urls = [];
const mongoose = require("mongoose");
mongoose.set("strictQuery", false);
const mongoDB = userArgs[0];
console.log(userArgs)
console.log(mongoDB)
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 createUrls();
console.log("Debug: Closing mongoose");
mongoose.connection.close();
}
async function urlCreate(number, urlName) {
const url = new Url({ number: number, urlName: urlName });
await url.save();
console.log(url)
// urls.push(url);
console.log(`Added url: ${urlName} as ${number}`);
}
async function createUrls() {
console.log("Adding urls");
await Promise.all([
urlCreate(0, "https://www.google.com"),
urlCreate(1, "https://www.bing.com")
]);
}