-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.js
467 lines (398 loc) · 12.5 KB
/
mongo.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//HTML part
var jsonfile = require('jsonfile');
var path = require('path');
var fs = require('fs');
var events = require('events');
var MongoClient = require('mongodb').MongoClient;
var backupModule = require('./mongodb_backup.js');
//Function to run the backup
var runBackup = function(time){
backupModule.backup(time);
}
//Function test
var testmongo = function(){
MongoClient.connect('mongodb://localhost:27017/det', function(err, db) { //To connect to 'det' database
if (err) {
throw err;
}
Find_all_id().then(function(items) {
let test = items;
console.log(items);
});
console.log("connection done");
//insertData(db, __dirname+"/data/res.json");
});
}
//FUNCTIONS
//Function to check conditions for insert detergents
/* Caracteristics category, _id, volume, color are needed (not null) :
* category : string
* _id : string
* volume : number
* color : object
*/
var checkConditionsInsert = function(detergent){
let check = true;
if(typeof(detergent.category) !== 'string' || detergent.category === 'null'){
check = 'The detergent category must be filled with string type';
return check;
}
if (typeof(detergent._id) !== 'string' || detergent.name === 'null'){
check = 'The detergent name must be filled with string type';
return check;
}
if (typeof(detergent.volume) !== 'number' || detergent.volume === 'null'){
check = 'The detergent volume must be filled with number type';
return check;
}
if (typeof(detergent.color) !== 'object' || detergent.color.length !== 3) {
check = 'The detergent color must be a list of 3 values';
return check;
}
return check; //Return true if conditions are met
}
//Function to normalize colors between 0 and 255
var modifyColor = function(detergent){
if (detergent[0] >= 0 && detergent[1] >= 0 && detergent[2] >= 0
&& detergent[0] <= 1 && detergent[1] <= 1 && detergent[2] <= 1){
detergent[0] = detergent[0]*255;
detergent[1] = detergent[1]*255;
detergent[2] = detergent[2]*255;
}
}
//Function to modify 'detBelt' format in 'mongo' format
/* Input : JSON in 'detBelt' format
* Output : Json in 'mongo' format (database format )
*/
var Json_detBelt_mongo = function(path) {
let dict = jsonfile.readFileSync(path,'utf8'); //contain the JSON
let write = [];
let values = Object.keys(dict.data).map(function(key) {
return dict.data[key];
});
for(let i=0; i<values.length; i++){ //for each class of detergent (eg: maltoside)
for(let j=0; j<values[i].length; j++){ //for each detergent
let det = values[i][j];
det.category = Object.keys(dict.data)[i];
if(det['name']){
det['_id'] = det['name']; //Rename key 'name' to '_id'
}
delete det['name'];
modifyColor(det.color); //Normalization of colors
write.push(det);
}
}
dict.data = write; //replace "data" values
return dict;
}
//Function to insert JSON file in database
/* path : path of the JSON file (in DetBelt format)
*/
var insertData = function(db, path) {
MongoClient.connect('mongodb://localhost:27017/det', function(err, db) {
if (err) {
throw err;
}
else{
let dict = Json_detBelt_mongo(path);
for(let i=0; i<dict.data.length; i++){ //for each detergent
let detergent = dict.data[i];
let check = checkConditionsInsert(detergent);
if(check === true){ //if conditions are check
db.collection('det').insert(detergent, function(err,result){
if(err){
if (err.code === 11000) { //if _id is not unique
let nameDet = err.errmsg.split('"')[1]; //id of the detergent error
console.log(nameDet, ': The detergent name must be unique');
}
else{
throw err;
}
}
});
}
else{
console.log(detergent._id, ' : ', checkConditionsInsert(detergent));
}
}
console.log('Insertion of detergents is finished !')
backupModule.control_backup(true); //variable backup = true, the database has been modified
}
})
}
//Function to return the database
var FindinDet = function() {
return MongoClient.connect('mongodb://localhost:27017/det').then(function(db) { //connexion to the database
//console.dir(db)
let collection = db.collection('det');
return collection.find().toArray();
}).then(function(items) {
return items; //items : the database
});
}
/////////// added during internship //////
var Find_a_Det = function(db,id) {
return MongoClient.connect('mongodb://localhost:27017/det').then(function(db) { //connexion to the database
let collection = db.collection('det');
return collection.find({"_id":id}).toArray();
}).then(function(items) {
if(items.length === 0){
return {"error":"this detergent is probably not in our database"};
}else{
return items;
}
});
}
var Find_all_id = function(){
return MongoClient.connect('mongodb://localhost:27017/det').then(function(db) { //connexion to the database
let collection = db.collection('det');
return collection.find({},{"id":1}).toArray();
}).then(function(items) {
let l_id = [];
items.forEach(function(doc){
l_id.push(doc["_id"]);
})
return l_id;
});
}
/////////////////////////////////////////////////
//Function to return all keys of the database
var getallkeys = function(){
let allKeys = {};
return MongoClient.connect('mongodb://localhost:27017/det').then(function(db) {
let collection = db.collection('det');
return collection.find().toArray();
}).then(function(items) {
items.forEach(function(doc){
Object.keys(doc).forEach(function(key){allKeys[key] = '1'})
})
let l_keys = Object.keys(allKeys)
return l_keys;
});
}
//Function to delete the database
/*
var _deleteData = function(db){
let emitter = new events.EventEmitter();
db.collection('det').drop(function(err,result){ //deletion of the database
if(err){
emitter.emit('errorCode', ['Error', 'Error in the deletion of ' + idDet]);
}
else{
let msg = 'The database has been deleted;'
console.log('The database has been deleted');
backupModule.control_backup(true);
emitter.emit('deleteOK', msg, result);
}
});
return emitter;
}
*/
var deleteData = function(db){
return new Promise((resolve,reject)=>{
MongoClient.connect('mongodb://localhost:27017/det', function(err, db) {
if (err) {
throw err;
}
else{
db.collection('det').drop(function(err,result){ //deletion of the database
if(err){
reject({"status":'Error', "data":'Error in the deletion of ' + idDet});
}
else{
let msg = 'The database has been deleted';
//console.log('The database has been deleted');
backupModule.control_backup(true);
resolve({"msg":msg, "result":result});
}
});
}
})
})
}
//Function to delete a detergent
/*idDet : variable with the _id of the detergent to delete
*/
var deleteDet = function(db, idDet){
return new Promise((resolve,reject)=>{
MongoClient.connect('mongodb://localhost:27017/det', function(err, db) { //To connect to 'det' database
if (err) {
throw err;
}
else{
db.collection('det').deleteOne({'_id' : idDet}, function(err, result) {
if (err){
reject({"status":'Error', "data":'Error in the deletion of ' + idDet});
}
else{
let msg = idDet + ': The detergent has been removed';
console.log(msg);
backupModule.control_backup(true);
resolve({"status":"OK_delete","data": msg});
}
});
}
})
})
}
//Function to add a new detergent
/*det : variable like { "_id" : "OM", "volume" : 391.1, "color" : [0,255,0], "category" : "maltoside"})
*/
var insertDet = function(db, det){
return new Promise((resolve,reject)=>{
MongoClient.connect('mongodb://localhost:27017/det', function(err, db) { //To connect to 'det' database
if (err) {
throw err;
}
modifyColor(det);
let check = checkConditionsInsert(det); //verification of conditions
if(check === true){ //if the conditions are verified
db.collection('det').insert(det, function(err,result){
if(err){
if (err.code == 11000) { //11000 : error code for an already existing identifier
console.log('The detergent name must be unique');
reject({"status":'Error', "data": "The detergent name must be unique"}); // in mongo, maybe not the same in tingo
}
else{
reject({"status":'Error', "data":"Error in the insertion of detergent"});
}
}
else{
console.log('The detergent has been added');
backupModule.control_backup(true);
resolve({"status":'OK_insert',"data":'The detergent has been added'});
}
});
}
})
})
}
//Function to modify a detegent
/*id : _id of the detergent to modify
* det : object contain object of the detergent { "_id" : "OM", "volume" : 391.1, "color" : [0,255,0], "category" : "maltoside"})
*/
var modifyDet = function(db, id, det){
return new Promise((resolve,reject)=>{
MongoClient.connect('mongodb://localhost:27017/det', function(err, db) { //To connect to 'det' database
if (err) {
throw err;
}
else{
modifyColor(det.color);
let check = checkConditionsInsert(det);
if(check === true){
db.collection('det').find({'_id' : id}).toArray((err, result) => {
if(err){
reject({"status":'Error',"data":'Error in the modification of ' + id});
}
if(result.length === 1){
db.collection('det').update({'_id' : id},{$set: det}, function(err,result){
if(err){
reject({"status":'Error',"data":'Error in the modification of ' + id});
}
else{
msg = 'The detergent ' + id + ' has been updated';
console.log(msg);
backupModule.control_backup(true);
resolve({"status":'OK_modif',"data":msg});
}
});
}
else{
msg = 'The detergent ' + id + ' is present more than one time';
console.log(msg);
reject({"status":'Error', "data": msg});
}
})
}
else{
console.log(check);
reject({"status":'Error', "data":check});
}
}
})
})
}
//Function to delete a caracteristic of detergents
/*caract : caracteristic to delete
the characteristic will be removed from all detergents containing it
TO DO: convert it with Promises
*/
var deleteCaract = function(db, caract){
let emitter = new events.EventEmitter();
let todelete = {};
todelete[caract] = 1;
db.collection('det').update({}, {$unset: todelete} , {multi: true}, function(err,result){
if(err){
emitter.emit('errorCode', ['Error', 'Error in the deletion of caracteristic ' + caract]);
}
else{
console.log('The caracteristic ' + db + ' has been deleted for all detergents');
backupModule.control_backup(true);
return ['OK_delete', 'The caracteristic ' + db + ' has been deleted for all detergents'];
}
});
}
//Function to rename a caracteristic of detergents
/*caract1 : name of the caracteristic in the database
*caract2 : new name
*/
var modifyCaract = function(db, caract1, caract2){
let rename = {};
rename[caract1] = caract2;
db.collection('det').update({}, {$rename: rename}, {multi: true}, function(err,result){
if(err){
emitter.emit('errorCode', ['Error', 'Error in the modification of caracteristic ' + caract1]);
}
else{
console.log('The caracteristic ' + caract1 + ' has been rename ' + caract2);
backupModule.control_backup(true);
emiter.emit(modifOK, ['OK_modif', 'The caracteristic ' + caract1 + ' has been rename ' + caract2])
}
});
return emitter;
}
//Function for a test
var testFront = function(){
var list = [{ "_id" : "OM", "volume" : 391.1, "color" : [0,255,0], "category" : "maltoside"}, { "_id" : "NM", "volume" : 408.9, "color" : [0,255,0], "category" : "maltoside"}];
console.log(list);
return list;
}
//Function that returns a list of all categories of detergents
var detCategory = function(db){
db.collection('det').distinct("category",(function(err, result){
if(err){
throw err;
}
else{
console.log(result);
return result;
}
}))
}
//Function to return the database
var db_for_detbelt = function(){
FindinDet().then(function(items) { //items contain all the detergents
items = {'items' : items};
return backupModule.Json_mongo_detBelt_format(items);
});
}
//EXPORT
module.exports = {
testFront: testFront,
FindinDet: FindinDet,
getallkeys: getallkeys,
insertData: insertData,
deleteData: deleteData,
deleteDet: deleteDet,
insertDet: insertDet,
modifyDet: modifyDet,
modifyCaract, modifyCaract,
deleteCaract: deleteCaract,
detCategory: detCategory,
db_for_detbelt: db_for_detbelt,
runBackup : runBackup,
testmongo : testmongo,
Find_a_Det : Find_a_Det,
Find_all_id : Find_all_id
};