generated from dr-btd-student/p5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadDatabase.js
More file actions
146 lines (136 loc) · 4.87 KB
/
loadDatabase.js
File metadata and controls
146 lines (136 loc) · 4.87 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
/**
* This Node.js program loads the Project 7 model data into Mongoose
* defined objects in a MongoDB database. It can be run with the command:
* node loadDatabase.js
* Be sure to have an instance of the MongoDB running on the localhost.
*
* This script loads the data into the MongoDB database named 'project6'.
* In loads into collections named User and Photos. The Comments are added in
* the Photos of the comments. Any previous objects in those collections are
* discarded.
*/
// We use the Mongoose to define the schema stored in MongoDB.
const mongoose = require("mongoose");
mongoose.Promise = require("bluebird");
mongoose.set("strictQuery", false);
mongoose.connect("mongodb://127.0.0.1/project6", {
useNewUrlParser: true,
useUnifiedTopology: true,
});
// Get the magic models we used in the previous projects.
const models = require("./modelData/photoApp.js").models;
// Load the Mongoose schema for Use and Photo
const User = require("./schema/user.js");
const Photo = require("./schema/photo.js");
const SchemaInfo = require("./schema/schemaInfo.js");
const versionString = "1.0";
// We start by removing anything that existing in the collections.
const removePromises = [
User.deleteMany({}),
Photo.deleteMany({}),
SchemaInfo.deleteMany({}),
];
Promise.all(removePromises)
.then(function () {
// Load the users into the User. Mongo assigns ids to objects so we record
// the assigned '_id' back into the model.userListModels so we have it
// later in the script.
const userModels = models.userListModel();
const mapFakeId2RealId = {};
const userPromises = userModels.map(function (user) {
return User.create({
first_name: user.first_name,
last_name: user.last_name,
location: user.location,
description: user.description,
occupation: user.occupation,
login_name: user.last_name.toLowerCase(),
password: "weak",
})
.then(function (userObj) {
// 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.save();
mapFakeId2RealId[user._id] = userObj._id;
user.objectID = userObj._id;
console.log(
"Adding user:",
user.first_name + " " + user.last_name,
" with ID ",
user.objectID
);
})
.catch(function (err) {
console.error("Error create user", err);
});
});
const 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.
const photoModels = [];
const userIDs = Object.keys(mapFakeId2RealId);
userIDs.forEach(function (id) {
photoModels.push(...models.photoOfUserModel(id));
});
const photoPromises = photoModels.map(function (photo) {
return Photo.create({
file_name: photo.file_name,
date_time: photo.date_time,
user_id: mapFakeId2RealId[photo.user_id],
})
.then(function (photoObj) {
photo.objectID = photoObj._id;
if (photo.comments) {
photo.comments.forEach(function (comment) {
photoObj.comments = photoObj.comments.concat([
{
comment: comment.comment,
date_time: comment.date_time,
user_id: comment.user.objectID,
},
]);
console.log(
"Adding comment of length %d by user %s to photo %s",
comment.comment.length,
comment.user.objectID,
photo.file_name
);
});
}
photoObj.save();
console.log(
"Adding photo:",
photo.file_name,
" of user ID ",
photoObj.user_id
);
})
.catch(function (err) {
console.error("Error create user", err);
});
});
return Promise.all(photoPromises).then(function () {
// Create the SchemaInfo object
return SchemaInfo.create({
version: versionString,
})
.then(function (schemaInfo) {
console.log(
"SchemaInfo object created with version ",
schemaInfo.version
);
})
.catch(function (err) {
console.error("Error create schemaInfo", err);
});
});
});
allPromises.then(function () {
mongoose.disconnect();
});
})
.catch(function (err) {
console.error("Error create schemaInfo", err);
});