-
Notifications
You must be signed in to change notification settings - Fork 6
/
MongoHandler.js
146 lines (129 loc) · 3.74 KB
/
MongoHandler.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
const when = require('when');
const Promise = when.promise;
const MongoDB = require('mongodb');
const MongoClient = MongoDB.MongoClient;
let MongoHandler = class MongoHandler {
constructor(url, databaseName) {
this.url = url;
this.dbInstace = null;
this.client = null;
this.databaseName = databaseName;
}
connect() {
return Promise((resolve, reject) => {
MongoClient.connect(this.url, (err, client) => {
if (err) {
reject(err);
}
this.client = client;
this.dbInstace = client.db(this.databaseName, {useUnifiedTopology: true });
resolve();
});
})
}
findAll(collectionName) {
return Promise((resolve, reject) => {
try {
this.dbInstace.collection(collectionName)
.find({}).toArray(function (err, storageDocuments) {
if (err) {
reject(err);
}
if (storageDocuments == null) {
resolve({});
} else {
resolve(storageDocuments);
}
});
} catch (ex) {
reject(ex);
}
});
}
saveAll(collectionName, objects) {
return Promise(async (resolve, reject) => {
try {
await this.dropCollectionIfExists(collectionName);
if(objects.length > 0){
let bulk = this.dbInstace.collection(collectionName).initializeUnorderedBulkOp();
objects.forEach((obj) => {
bulk.insert(obj);
});
bulk.execute();
}
resolve();
} catch (ex) {
reject(ex);
}
});
}
async dropCollectionIfExists(collectionName){
let collectionList= await this.dbInstace.listCollections({
name: collectionName
}).toArray();
if(collectionList.length !== 0){
await this.dbInstace.collection(collectionName).drop();
}
}
findOneByPath(collectionName, path) {
return Promise((resolve, reject) => {
try {
this.dbInstace.collection(collectionName)
.findOne({path : path}, function (err, storageDocument) {
if (err) {
reject(err);
}
if (storageDocument == null) {
resolve({});
} else {
if (storageDocument.body) {
if (typeof(storageDocument.body) == "string") {
resolve(JSON.parse(storageDocument.body));
} else {
resolve(storageDocument.body);
}
} else {
resolve({});
}
}
});
} catch (ex) {
reject(ex);
}
});
}
saveOrUpdateByPath(collectionName, path, meta, body) {
return Promise((resolve, reject) => {
try {
this.dbInstace.collection(collectionName)
.findOne({
path: path
},
function (err, storageDocument) {
if (err) {
reject(err);
} else {
if (storageDocument == null) {
storageDocument = {
path: path
};
}
storageDocument.meta = JSON.stringify(meta);
storageDocument.body = JSON.stringify(body);
storageDocument.save(function (err, storageDocument) {
if (err) {
reject(err);
} else {
resolve();
}
});
}
}
);
} catch (ex) {
reject(ex);
}
});
}
};
module.exports = MongoHandler;