-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloadDatabase.js
131 lines (115 loc) · 5.26 KB
/
loadDatabase.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
"use strict";
/* jshint node: true */
/* global Promise */
/*
* This Node.js program loads the CS142 Project #5 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 'cs142project6'. In loads
* into collections named User and Photos. The Comments are added in the Photos of the
* comments. Any previous objects in those collections is discarded.
*
* 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.
*
*/
// Get the magic models we used in the previous projects.
var cs142models = require('./modelData/photoApp.js').cs142models;
// 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 = '1.0';
// We start by removing anything that existing in the collections.
var removePromises = [User.remove({}), Photo.remove({}), SchemaInfo.remove({})];
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 cs142model.userListModels so we have it
// later in the script.
var userModels = cs142models.userListModel();
var userIDs = []; // Collect the fake IDs of the user we load
var userPromises = userModels.map(function (user) {
userIDs.push(user.id);
return User.create({
first_name: user.first_name,
last_name: user.last_name,
location: user.location,
description: user.description,
occupation: user.occupation
}, 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();
user.objectID = userObj._id;
console.log('Adding user:', user.first_name + ' ' + user.last_name, ' with ID ',
user.objectID);
}
});
});
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 = [];
for (var i = 0; i < userIDs.length; i++) {
photoModels = photoModels.concat(cs142models.photoOfUserModel(userIDs[i]));
}
var photoPromises = photoModels.map(function (photo) {
return Photo.create({
file_name: photo.file_name,
date_time: photo.date_time,
user_id: photo.user.objectID
}, 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;
photoObj.save();
photo.objectID = photoObj._id;
if (photo.comments) {
photo.comments.forEach(function (comment) {
photoObj.comments.push({
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);
});
}
console.log('Adding photo:', photo.file_name, ' of user ID ', photo.user.objectID);
}
});
});
return Promise.all(photoPromises).then(function () {
// Create the SchemaInfo object
return SchemaInfo.create({
version: versionString
}, function (err, schemaInfo) {
if (err) {
console.error('Error create schemaInfo', err);
} else {
console.log('SchemaInfo object created with version ', versionString);
}
});
});
});
allPromises.then(function () {
mongoose.disconnect();
});
});