-
Notifications
You must be signed in to change notification settings - Fork 0
/
populateDB.js
164 lines (149 loc) · 6.54 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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// console.log(
// 'This script populates some test phones and companies to your database. Specified database as argument - e.g.: node populatedb "mongodb+srv://cooluser:coolpassword@cluster0.lz91hw2.mongodb.net/myDatabaseName"'
// );
//
// // Get arguments passed on command line
// const userArgs = process.argv.slice(2);
//
// if (!userArgs[0].startsWith('mongodb')) {
// console.log('ERROR: You need to specify a valid mongodb URL as the first argument');
// return;
// }
//
// const Phone = require('./models/phone');
// const Company = require('./models/company');
// const PhoneInstance = require('./models/phoneinstance');
//
// const companies = [];
// const phones = [];
// const phoneInstances = [];
//
// const mongoose = require("mongoose");
// mongoose.set("strictQuery", false);
//
// const mongoDB = userArgs[0];
// main().catch(err => console.log(err));
//
// async function main() {
// console.log("Debug: About to connect");
// await mongoose.connect(mongoDB, { useNewUrlParser: true, useUnifiedTopology: true });
// console.log("Debug: Should be connected?");
// await createCompanies();
// await createPhones();
// await createPhoneInstances();
// console.log("Debug: Closing mongoose");
// mongoose.connection.close();
// }
//
// async function createCompanies() {
// console.log("Adding companies");
// companies.push(await companyCreate("Apple Inc.", "American technology company", "https://en.wikipedia.org/wiki/Apple_Inc."));
// companies.push(await companyCreate("Samsung Electronics", "Multinational electronics company", "https://en.wikipedia.org/wiki/Samsung_Electronics"));
// // Add more companies as needed
// }
//
// async function companyCreate(name, description, wikiUrl) {
// const company = new Company({ name, description, wikiUrl });
// await company.save();
// console.log(`Added company: ${name}`);
// return company;
// }
//
// async function createPhones() {
// console.log("Adding phones");
// phones.push(await phoneCreate("iPhone 13", companies[0]._id, 799, "Latest model of Apple iPhone"));
// phones.push(await phoneCreate("Galaxy S21", companies[1]._id, 999, "Latest model of Samsung Galaxy Series"));
// // Add more phones as needed
// }
//
// async function phoneCreate(name, company, price, description) {
// const phone = new Phone({ name, company, price, description });
// await phone.save();
// console.log(`Added phone: ${name}`);
// return phone;
// }
//
// async function createPhoneInstances() {
// console.log("Adding phone instances");
// phoneInstances.push(await phoneInstanceCreate(phones[0]._id, "Available", phones[0].price));
// phoneInstances.push(await phoneInstanceCreate(phones[1]._id, "Currently Unavailable", phones[1].price));
// // Add more phone instances as needed
// }
//
// async function phoneInstanceCreate(phone, status, price) {
// const phoneInstance = new PhoneInstance({ phone, status, price });
// await phoneInstance.save();
// console.log(`Added phone instance for: ${phone}`);
// return phoneInstance;
// }
console.log(
'This script populates some test phones and companies to your database. Specified database as argument - e.g.: node populatedb "mongodb+srv://cooluser:coolpassword@cluster0.lz91hw2.mongodb.net/myDatabaseName"'
);
// Get arguments passed on command line
const userArgs = process.argv.slice(2);
if (!userArgs[0].startsWith('mongodb')) {
console.log('ERROR: You need to specify a valid mongodb URL as the first argument');
return;
}
const Phone = require('./models/phone');
const Company = require('./models/company');
const PhoneInstance = require('./models/phoneinstance');
const companies = [];
const phones = [];
const phoneInstances = [];
const mongoose = require("mongoose");
mongoose.set("strictQuery", false);
const mongoDB = userArgs[0];
main().catch(err => console.log(err));
async function main() {
console.log("Debug: About to connect");
await mongoose.connect(mongoDB, { useNewUrlParser: true, useUnifiedTopology: true });
console.log("Debug: Should be connected?");
await createCompanies();
await createPhones();
await createPhoneInstances();
console.log("Debug: Closing mongoose");
mongoose.connection.close();
}
async function createCompanies() {
console.log("Adding companies");
// New companies
companies.push(await companyCreate("Google", "American technology company", "https://en.wikipedia.org/wiki/Google"));
companies.push(await companyCreate("Huawei", "Chinese multinational technology company", "https://en.wikipedia.org/wiki/Huawei"));
companies.push(await companyCreate("Sony", "Japanese multinational conglomerate", "https://en.wikipedia.org/wiki/Sony"));
companies.push(await companyCreate("OnePlus", "Chinese smartphone manufacturer", "https://en.wikipedia.org/wiki/OnePlus"));
}
async function companyCreate(name, description, wikiUrl) {
const company = new Company({ name, description, wikiUrl });
await company.save();
console.log(`Added company: ${name}`);
return company;
}
async function createPhones() {
console.log("Adding phones");
// New phones
phones.push(await phoneCreate("Pixel 5", companies[2]._id, 699, "High-quality camera phone from Google"));
phones.push(await phoneCreate("Huawei P40", companies[3]._id, 789, "New generation smartphone from Huawei"));
phones.push(await phoneCreate("Sony Xperia 1 II", companies[4]._id, 950, "Flagship smartphone with a 4K display from Sony"));
phones.push(await phoneCreate("OnePlus 8T", companies[5]._id, 749, "Smooth display and fast charging OnePlus phone"));
}
async function phoneCreate(name, company, price, description) {
const phone = new Phone({ name, company, price, description });
await phone.save();
console.log(`Added phone: ${name}`);
return phone;
}
async function createPhoneInstances() {
console.log("Adding phone instances");
// New phone instances
phoneInstances.push(await phoneInstanceCreate(phones[2]._id, "Available", phones[2].price));
phoneInstances.push(await phoneInstanceCreate(phones[3]._id, "Currently Unavailable", phones[3].price));
phoneInstances.push(await phoneInstanceCreate(phones[4]._id, "Available", phones[4].price));
phoneInstances.push(await phoneInstanceCreate(phones[5]._id, "Available", phones[5].price));
}
async function phoneInstanceCreate(phone, status, price) {
const phoneInstance = new PhoneInstance({ phone, status, price });
await phoneInstance.save();
console.log(`Added phone instance for: ${phone}`);
return phoneInstance;
}