-
Notifications
You must be signed in to change notification settings - Fork 0
/
populatedb.js
278 lines (263 loc) · 6.44 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
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
#! /usr/bin/env node
console.log(
"This script populates some test items, brands and categories to the database. Specified database as argument - e.g.: populatedb mongodb+srv://beautifulchaos:[email protected]/inventory?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
}
*/
var async = require("async");
var Item = require("./models/item");
var Category = require("./models/category");
var Brand = require("./models/brand");
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:"));
var items = [];
var brands = [];
var categories = [];
function itemCreate(
name,
description,
category,
price,
brand,
stock,
filename,
cb
) {
item_detail = {
name: name,
description: description,
price: price,
category: category,
brand: brand,
stock: stock,
};
if (filename !== null) {
item_detail.filename = filename;
}
var item = new Item(item_detail);
item.save(function (err) {
if (err) {
cb(err, null);
return;
}
console.log("New Item: " + item);
items.push(item);
cb(null, item);
});
}
function brandCreate(name, cb) {
var brand = new Brand({ name: name });
brand.save(function (err) {
if (err) {
cb(err, null);
return;
}
console.log("New Brand: " + brand);
brands.push(brand);
cb(null, brand);
});
}
function categoryCreate(name, cb) {
var category = new Category({ name: name });
category.save(function (err) {
if (err) {
cb(err, null);
return;
}
console.log("New Category: " + category);
categories.push(category);
cb(null, category);
});
}
function createBrands(cb) {
async.series(
[
function (callback) {
brandCreate("ASOS", callback);
},
function (callback) {
brandCreate("Fashion Nova", callback);
},
function (callback) {
brandCreate("SHEIN", callback);
},
function (callback) {
brandCreate("Pretty Little Thing", callback);
},
function (callback) {
brandCreate("PayPorte", callback);
},
function (callback) {
brandCreate("Victoria's Secret", callback);
},
function (callback) {
brandCreate("Ivy Park", callback);
},
function (callback) {
brandCreate("NordStrum", callback);
},
function (callback) {
brandCreate("Good American", callback);
},
function (callback) {
brandCreate("American Eagle", callback);
},
],
// optional callback
cb
);
}
function createCategories(cb) {
async.series(
[
function (callback) {
categoryCreate("Dresses", callback);
},
function (callback) {
categoryCreate("Skirts", callback);
},
function (callback) {
categoryCreate("Tops", callback);
},
function (callback) {
categoryCreate("Shirts", callback);
},
function (callback) {
categoryCreate("Jackets", callback);
},
function (callback) {
categoryCreate("Athleisure", callback);
},
function (callback) {
categoryCreate("Bottoms", callback);
},
],
// optional callback
cb
);
}
function createItems(cb) {
async.parallel(
[
function (callback) {
itemCreate(
"Floral Knitwear Sweater",
"The Floral Knitwear Sweater is a comfortable beige colored sweater. It is perfect for a cold weather.",
categories[2],
100,
brands[0],
0,
"sweater.jpeg",
callback
);
},
function (callback) {
itemCreate(
"Mom Jeans",
"The Mom Jeans provides comfort and a chic style. Perfect a night out with friends. The jeans comes in a faded blue color. Sizes Small(S), Medium(M), Large(L) and Extra Large(XL) are available.",
categories[6],
400,
brands[8],
3,
"mom-jeans.jpeg",
callback
);
},
function (callback) {
itemCreate(
"Butterfly Ankara Top",
"The Butterfly Ankara Top is perfect to wear on jeans.",
categories[2],
150,
brands[0],
100,
"butterfly.jpeg",
callback
);
},
function (callback) {
itemCreate(
"Leather Biker Jacket",
"If you're looking to look badass, then this is the jacket for you. The jacket is made from real leather",
categories[4],
200,
brands[1],
50,
"biker.jpeg",
callback
);
},
function (callback) {
itemCreate(
"Bandage Dress",
"This dress is the talk of the town and it's only right to have something that makes you feel sexy.",
categories[0],
1200,
brands[2],
1000,
"bandage-dress.jpg",
callback
);
},
function (callback) {
itemCreate(
"Ruched Pencil Drawstrings Skirt",
"This skirt is avilable in red, black and white.",
categories[1],
240,
brands[4],
90,
"drawstring.jpg",
callback
);
},
function (callback) {
itemCreate(
"Why We Should All Be Feminists Shirt",
"This shirt has the popular Chimamanda Adichie's book name.",
categories[4],
150,
brands[3],
200,
"feminist.png",
callback
);
},
function (callback) {
itemCreate(
"Beige Athleisure Set",
"Perfect for gym day",
categories[5],
200,
brands[5],
280,
"athleisure.jpeg",
callback
);
},
],
// optional callback
cb
);
}
async.series(
[createBrands, createCategories, createItems],
// Optional callback
function (err, results) {
if (err) {
console.log("FINAL ERR: " + err);
}
// All done, disconnect from database
mongoose.connection.close();
}
);