-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpopulatedb.js
More file actions
325 lines (304 loc) · 9.22 KB
/
populatedb.js
File metadata and controls
325 lines (304 loc) · 9.22 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#! /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.: populatedb mongodb+srv://cooluser:coolpassword@cluster0-mbdj7.mongodb.net/local_library?retryWrites=true',
);
// Get arguments passed on command line
var 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 async = require('async');
const Smartphone = require('./models/smartphone');
const Keyboard = require('./models/keyboard');
const Mouse = require('./models/mouse');
const Television = require('./models/television');
const Category = require('./models/category');
var mongoose = require('mongoose');
var mongoDB = userArgs[0];
mongoose.connect(mongoDB, { useNewUrlParser: true, useUnifiedTopology: true });
mongoose.Promise = global.Promise;
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
const smartphones = [];
const keyboards = [];
const mice = [];
const televisions = [];
const categories = [];
function categoryCreate(name, description, cb) {
const category = new Category({
name,
description,
});
category.save(function (err) {
if (err) {
cb(err, null);
return;
}
console.log('New Category: ' + category);
categories.push(category);
cb(null, category);
});
}
function smartphoneCreate(name, description, category, price, stock, file_url, cb) {
const smartphone = new Smartphone({
name,
description: {
OS: description.OS,
RAM: description.RAM,
ROM: description.ROM,
battery: description.battery,
cameras: {
camera1: description.cameras.camera1,
camera2: description.cameras.camera2,
camera3: description.cameras.camera3,
},
},
category,
price,
stock,
file_url,
});
smartphone.save(function (err) {
if (err) {
cb(err, null);
return;
}
console.log('New Smartphone: ' + smartphone);
smartphones.push(smartphone);
cb(null, smartphone);
});
}
function keyboardCreate(name, description, category, price, stock, file_url, cb) {
const keyboard = new Keyboard({
name,
description,
category,
price,
stock,
file_url,
});
keyboard.save(function (err) {
if (err) {
cb(err, null);
return;
}
console.log('New Keyboard: ' + keyboard);
keyboards.push(keyboard);
cb(null, keyboard);
});
}
function mouseCreate(name, description, category, price, stock, file_url, cb) {
const mouse = new Mouse({
name,
description,
category,
price,
stock,
file_url,
});
mouse.save(function (err) {
if (err) {
cb(err, null);
return;
}
console.log('New Mouse: ' + mouse);
mice.push(mouse);
cb(null, mouse);
});
}
function televisionCreate(name, description, category, price, stock, file_url, cb) {
const television = new Television({
name,
description,
category,
price,
stock,
file_url,
});
television.save(function (err) {
if (err) {
cb(err, null);
return;
}
console.log('New Television: ' + television);
televisions.push(television);
cb(null, television);
});
}
function createCategories(cb) {
async.series(
[
function (callback) {
categoryCreate(
'Computer Accessories',
'In this category you will find all the accessories that you might ever need for your computer with no accessories',
callback,
);
},
function (callback) {
categoryCreate(
'Electronic Devices',
'Here you will be shocked by the powerful TVs, Laptops and Routers that we have to offer',
callback,
);
},
function (callback) {
categoryCreate(
'Phones',
'A collection of incredibly incredible smartphones and landline phones (if you remember those)',
callback,
);
},
],
// optional callback
cb,
);
}
//CREATE SMARTHPONES
function createSmartphones(cb) {
async.series(
[
function (callback) {
const cameras = {
camera1: '64 MP main camera',
camera2: '12 MP ultrawide camera',
camera3: '4 MP macro camera',
};
smartphoneCreate(
'Ultra One Stellar 5G',
{ OS: 'Android', RAM: 8, ROM: 128, battery: 5000, cameras },
categories[2],
2500,
240,
'/images/mildly-useful-N_xjrdKZPkw-unsplash.jpg',
callback,
);
},
function (callback) {
const cameras = {
camera1: '84 MP main camera',
camera2: '24 MP ultrawide camera',
camera3: '16 MP telephoto',
};
smartphoneCreate(
'Increndible Smartphone',
{ OS: 'Android', RAM: 16, ROM: 256, battery: 7000, cameras },
categories[2],
3000,
2,
'/images/rahul-chakraborty-xsGxhtAsfSA-unsplash.jpg',
callback,
);
},
function (callback) {
const cameras = {
camera1: '13 MP camera',
};
smartphoneCreate(
'Extra Purple 2',
{ OS: 'Android', RAM: 2, ROM: 16, battery: 3000, cameras },
categories[2],
700,
24,
'/images/sara-kurfess-6lcT2kRPvnI-unsplash.jpg',
callback,
);
},
],
// optional callback
cb,
);
}
function createKeyboards(cb) {
async.series(
[
function (callback) {
keyboardCreate(
'Rectangular',
'A perfectly rectangular keyboard that will enhance your typing because of its rectangularity',
categories[0],
39,
0,
'/images/wesley-tingey-FNhyekndnSM-unsplash.jpg',
callback,
);
},
function (callback) {
keyboardCreate(
'Circular',
'Some people like to be different, and you can definitely be differet with this keyboard',
categories[0],
12,
34,
'/images/nhu-nguyen-IL1qSqEMNBo-unsplash.jpg',
callback,
);
},
],
// optional callback
cb,
);
}
function createMice(cb) {
async.series(
[
function (callback) {
mouseCreate(
'Buttonful',
`You won't need a keyboard along this mouse thanks to its 136 buttons perfect for any situation`,
categories[0],
72,
42,
'/images/rica-naypa-7j-oAysJpo4-unsplash.jpg',
callback,
);
},
function (callback) {
mouseCreate(
'Buttonless',
'A mouse entirely made of a screen that is extremely responsive to your fingers',
categories[0],
340,
6,
'/images/shagal-sajid-Ox7mSmsljkc-unsplash.jpg',
callback,
);
},
],
// optional callback
cb,
);
}
function createTelevisions(cb) {
async.series(
[
function (callback) {
televisionCreate(
'Humungous',
'A 102 inches OLED panel that will immerse you for the rest of your life',
categories[1],
4341,
2,
'/images/diego-gonzalez--I8lDurtfAo-unsplash.jpg',
callback,
);
},
],
// optional callback
cb,
);
}
async.series(
[createCategories, createSmartphones, createMice, createTelevisions, createKeyboards],
// Optional callback
function (err, results) {
if (err) {
console.log('FINAL ERR: ' + err);
}
// All done, disconnect from database
mongoose.connection.close();
},
);