-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaddFakeData.js
140 lines (121 loc) · 5.21 KB
/
addFakeData.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
"use strict";
/* jshint node: true */
/* global Promise */
/*
* This Node.js program loads a fake Photo App datasset in Mongoose defined objects
* in a MongoDB database. It can be run with the command:
* node loadFakeData.js
* be sure to have an instance of the MongoDB running on the localhost.
*
* This script loads the data into the MongoDB database named 'cs142project6'. In loads
* into collections named User and Photos. The Comments are added in the Photos of the
* comments.
*
* NOTE: This scripts uses Promise abstraction for handling the async calls to
* the database. We are not teaching Promises in CS142 so strongly suggest you don't
* use them in your solution.
*
*/
var faker = require('faker');
// We use the Mongoose to define the schema stored in MongoDB.
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/cs142project6');
// Load the Mongoose schema for Use and Photo
var User = require('./schema/user.js');
var Photo = require('./schema/photo.js');
var SchemaInfo = require('./schema/schemaInfo.js');
var versionString = '2.0';
var numUsers = 100;
var maxPhotos = 50;
var maxComments = 30;
// Generate the users
var userPromises = [];
var users = [];
console.log('Creating ', numUsers, 'users');
for (var i = 0; i < numUsers; i++) {
var args = {
first_name: faker.name.firstName(),
last_name: faker.name.lastName(),
location: faker.address.city() + ', ' + faker.address.state(),
description: faker.company.catchPhrase(),
occupation: faker.name.jobTitle()
};
userPromises.push(User.create(args, function (err, userObj) {
if (err) {
console.error('Error create user', err);
} else {
// Set the unique ID of the object. We use the MongoDB generated _id for now
// but we keep it distinct from the MongoDB ID so we can go to something
// prettier in the future since these show up in URLs, etc.
userObj.id = userObj._id;
userObj.save();
users.push(userObj);
console.log('Adding user:', userObj.first_name + ' ' + userObj.last_name, ' with ID ',
userObj.id);
}
}));
}
var allPromises = Promise.all(userPromises).then(function () {
// Once we've loaded all the users into the User collection we add all the photos. Note
// that the user_id of the photo is the MongoDB assigned id in the User object.
var photoModels = [];
var photoPromises = [];
for (var u = 0; u < numUsers; u++) {
var numPhotos = faker.random.number({min: 0, max: maxPhotos});
(function (user) {
for (var p = 0; p < numPhotos; p++) {
photoPromises.push(Photo.create({
file_name: faker.image.image(),
date_time: new Date(),
user_id: user.id
}, function (err, photoObj) {
if (err) {
console.error('Error create user', err);
} else {
// Set the unique ID of the object. We use the MongoDB generated _id for now
// but we keep it distinct from the MongoDB ID so we can go to something
// prettier in the future since these show up in URLs, etc.
photoObj.id = photoObj._id;
photoModels.push(photoObj);
var numComments = faker.random.number({min: 0, max: maxComments});
for (var c = 0; c < numComments; c++) {
var author = users[faker.random.number({
min: 0,
max: numUsers - 1
})];
var comment = {
comment: faker.lorem.paragraph(),
date_time: new Date(),
user_id: author.id
};
photoObj.comments.push(comment);
console.log("Adding comment of length %d by user %s to photo %s",
comment.comment.length,
comment.user_id,
photoObj.file_name);
}
photoObj.save();
console.log('Adding photo:', photoObj.file_name, ' of user ID ', user.id);
}
}));
}
} )(users[u]);
}
return Promise.all(photoPromises).then(function () {
// Update the SchemaInfo object to the new versionString
return SchemaInfo.find({}, function (err, schemaInfos) {
if (err || !schemaInfos || !schemaInfos.length) {
console.error('Error updating schemaInfo', err);
} else {
var schemaInfo = schemaInfos[0];
schemaInfo.version = versionString;
schemaInfo.load_date_time = new Date();
schemaInfo.save();
console.log('SchemaInfo object updated with version ', versionString);
}
});
});
});
allPromises.then(function () {
mongoose.disconnect();
});