-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMongoDBModule.js
More file actions
348 lines (321 loc) · 11.8 KB
/
MongoDBModule.js
File metadata and controls
348 lines (321 loc) · 11.8 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
let MongoDBModule = function () {
var fs = require('fs');
var mongoose = require('mongoose');
mongoose.connect('mongodb://M4rk:0987@ds147799.mlab.com:47799/meme-place');
//CONNECTS TO USERS DB mongodb://M4rk:0987@ds147799.mlab.com:47799/meme-place
//localhost:27017
//M4rk:0987@ds147799.mlab.com:47799
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
// we're connected!
console.log("Connected to mongo sucessfully");
//Testing ephemeral FS
//convert binaries to images
});
//create User format
var usersSchema = mongoose.Schema({
username: {
type: String,
unique: [true, 'Why no eggs?']
},
password: String
});
var User = mongoose.model('User', usersSchema);
//create Image format
var imagesSchema = mongoose.Schema({
picAdress: {
type: String,
unique: [true, 'Why no eggs?']
},
x: Number,
y: Number,
mimeType: String,
imgBinary: Buffer
});
var Picture = mongoose.model('Picture', imagesSchema);
//1 unused
MongoDBModule.prototype.addObject = function (obj) {
let TempObj = new User({
username: obj.username,
password: obj.password
});
TempObj.save(function (err, tempObj) {
if (err) {
console.error(err);
return (null);
}
console.log("sucessfully saved " + tempObj.username);
return (tempObj);
});
}
//1.5
MongoDBModule.prototype.addObjectPromise = function (obj) {
return new Promise(function (resolve, reject) {
let TempObj = new User({
username: obj.username,
password: obj.password
});
TempObj.save(function (err, tempObj) {
if (err) {
console.error(err);
reject(null);
}
//console.log("sucessfully saved " + tempObj.username);
resolve(tempObj);
});
});
}
//2
MongoDBModule.prototype.getObjectWithUsername = function (_username) {
User.findOne({
'username': _username
}, function (err, result) {
if (err) {
console.error(err);
return (null);
}
if (result) {
console.log("in getobjwithuser" + result);
return (result);
} else
return (null);
});
}
//2.5
MongoDBModule.prototype.getObjectWithUsernamePromise = function (_username) {
return new Promise(function (resolve, reject) {
User.findOne({
'username': _username
}, function (err, result) {
if (err) {
console.error(err);
reject(null);
}
if (result) {
console.log("in getobjwithuser" + result);
resolve(result);
} else {
reject(null);
}
});
});
}
//3
MongoDBModule.prototype.deleteObjectWithUsername = function (_username) {
User.findOneAndRemove({
'username': _username
}, function (err, result) {
if (err) {
console.error(err);
return (null);
}
console.log("Maybee deleted objwithusername not sure if result is null or not");
return (result);
});
}
//3.5
MongoDBModule.prototype.deleteObjectWithUsernamePromise = function (_username) {
return new Promise(function (resolve, reject) {
User.findOneAndRemove({
'username': _username
}, function (err, result) {
if (err) {
console.error(err);
reject(null);
}
console.log("deleted Object:" + result.username);
resolve(result);
});
});
}
//4
MongoDBModule.prototype.getAllPicsObjects = function () {
Picture.find(function (err, pictures) {
if (err) {
console.error(err);
return (null);
}
//console.log("picsarray: " + pictures);
return (pictures);
});
}
//4.5
MongoDBModule.prototype.getAllPicsObjectsPromise = function () {
return new Promise(function (resolve, reject) {
Picture.find(function (err, pictures) {
if (err) {
console.error(err);
reject(null);
}
//console.log("picsarray: " + pictures);
resolve(pictures);
});
});
}
//5
MongoDBModule.prototype.addPicObj = function (picObj) {
let TempPicObj = new Picture({
picAdress: picObj.picAdress,
x: picObj.x,
y: picObj.y,
mimeType: picObj.mimeType,
imgBinary: picObj.imgBinary
});
TempPicObj.save(function (err, origCBPicObj) {
if (err) {
//pic with same name
console.log("SAME NAME HERE");
Picture.findOneAndRemove({
'picAdress': TempPicObj.picAdress
}, function (err, result) {
if (err) {
console.log("FindOneAndRemove");
console.error(err);
return (null);
}
console.log("deleted Object:" + result.picAdress);
//save again now that the document the document with same name has been deleted
TempPicObj.save(function (err, CallbackObj) {
if (err) {
console.log("Error Saving After Deleting");
console.error(err);
return (null);
}
console.log("sucessfully replaced: " + CallbackObj.picAdress);
return (CallbackObj);
});
return (result);
});
//
console.error(err);
return (null);
}
console.log("sucessfully saved " + origCBPicObj.picAdress);
return (origCBPicObj);
});
}
//5.5
///////////////////////////////////////
//////////////////////
////////
//
/* MongoDBModule.prototype.addPicObjPromise = function (picObj) {
return new Promise(function (resolve, reject) {
let TempPicObj = new Picture({
picAdress: picObj.picAdress,
x: picObj.x,
y: picObj.y,
mimeType: picObj.mimeType,
imgBinary: picObj.imgBinary
});
TempPicObj.save(function (err, origCBPicObj) {
if (err) {
//pic with same name
console.log("SAME NAME HERE");
Picture.findOneAndRemove({
'picAdress': TempPicObj.picAdress
}, function (err, result) {
if (err) {
console.log("FindOneAndRemove");
console.error(err);
reject(null);
}
console.log("deleted Object:" + result.picAdress);
//save again now that the document the document with same name has been deleted
TempPicObj.save(function (err, CallbackObj) {
if (err) {
console.log("Error Saving After Deleting");
console.error(err);
reject(null);
}
console.log("sucessfully replaced: " + CallbackObj.picAdress);
resolve(CallbackObj);
});
resolve(result);
});
//
console.error(err);
reject(null);
}
//uncoment when done console.log("sucessfully saved " + origCBPicObj.picAdress);
if (origCBPicObj != null) {
console.log("NULL here 6132456");
resolve(origCBPicObj);
}
});
});
}
*/
/////////////
////////////////
/////////////////
MongoDBModule.prototype.addPicObjPromise = function (picObj) {
return new Promise(function (resolve, reject) {
let TempPicObj = new Picture({
picAdress: picObj.picAdress,
x: picObj.x,
y: picObj.y,
mimeType: picObj.mimeType,
imgBinary: picObj.imgBinary
});
TempPicObj.save(function (err, origCBPicObj) {
if (err) {
console.log("A Dupe was recived");
resolve(TempPicObj);
} else {
console.log("sucessfully saved " + origCBPicObj.picAdress);
if (origCBPicObj != null) {
console.log("NULL here 6132456");
reject(origCBPicObj);
}
}
});
});
}
//
//
//
//
MongoDBModule.prototype.removePicObjPromise = function (newPicObj) {
return new Promise(function (resolve, reject) {
console.log("SAME NAME HERE " + newPicObj.picAdress);
Picture.findOneAndRemove({
'picAdress': newPicObj.picAdress
}, function (err, result) {
if (err) {
console.log("remove error");
console.error(err);
reject(null);
} else {
console.log("succsesfully removed : " + result.picAdress);
resolve(result);
}
});
});
}
//could be synchronus because it only runs at the start of the server
//no longer need to dump images to ephemeral File System
/*
this.getAllPicsObjectsPromise().then((StoredPics) => {
for (let i = 0; i < StoredPics.length; i++) {
fs.writeFile(__dirname + '/public/images/' + StoredPics[i].picAdress, StoredPics[i].imgBinary, (err) => {
if (err) throw err;
console.log(StoredPics[i].picAdress + ' binaries from the DB have been saved to the ephemeral FileSystem!');
});
}
}).catch((error) => {
console.error("Converting binaries failed: " + error);
});
*/
}
module.exports = MongoDBModule;
//nesasary functions
/*
1 ~~~db.addObject return null on error; return anything thats not null
2 ~~~db.getObjectWithUsername(_username) return null on error; return obj with .password and .username
3 ~~~db.deleteObjectWithUsername(_username); return null on error
4 ~~~db.getAllPicsObjects(); send all the PicObjects
5 ```db.addPicObj(picObj) return null on error; schema is .picAdress .x .y ; rework the way images are saved might need to add .binaryimgData
get all binary data and save it /^_^/
*/