-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrud-mongo.js
279 lines (242 loc) · 9.01 KB
/
crud-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
module.exports.create = function(dbs,collection, itemorlist, extra_options={}) {
return createfn(dbs, collection, itemorlist, extra_options);
}
// when string is given criteria transforms to {_id:new ObjectId()}
module.exports.read = function(dbs, collection, criteria={}, justOne=false, extra_options={}) {
return readfn(dbs, collection,criteria, justOne, extra_options);
}
module.exports.update = function(dbs, collection, criteria={}, itemorlist, justOne=false, extra_options={}) {
return updatefn(dbs, collection,criteria, itemorlist, justOne, extra_options);
}
module.exports.delete = function(dbs, collection, criteria={}, justOne=false) {
return deletefn(dbs, collection,criteria, justOne);
}
// returns dbs (client.db())
module.exports.connect = function(dbname) {
return connectfn(dbname);
}
// returns both dbs and client {"dbs":..,"client":""} in case you need to close the connection using client.close()
module.exports.connectWithClient = function(dbname) {
return connectwithclientfn(dbname);
}
// Returns items that have keyword in collection.searchField
module.exports.search = function(dbs,collection,searchField, keyword, additional_criteria={}) {
return searchfn(dbs,collection,searchField, keyword, additional_criteria);
}
function createfn(dbs,collection,itemOrList,extra_options={}){
if(isList(itemOrList)) {
return createMany(dbs, collection, itemOrList, extra_options);
} else {
return createOne(dbs,collection,itemOrList, extra_options);
}
}
// returns _id as String of the created object
function createMany(dbs, collection, list, extra_options) {
const datalist = list.map((data) => {
// prevent an insertion to mess up your _id order. Can be overwritten using extra_options["allow_id"] = 1
if(data._id && !extra_options["allow_id"]) {
delete data._id;
}
const date = +new Date();
data.date = date;
data.last_updated = date;
})
return new Promise((resolve, reject) => {
dbs.collection(collection).insertMany(datalist, function(err, result) {
if (err) {
reject(err);
} else {
try {
const lastId = String(result.insertedId);
resolve(lastId);
} catch(err2) {
reject(err2);
}
}
});
});
}
// returns _id as String of the created object
function createOne(dbs, collection, data, extra_options) {
// prevent an insertion to mess up your _id order. Can be overwritten using extra_options["allow_id"] = 1
if(data._id && !extra_options["allow_id"]) {
delete data._id;
}
const date = +new Date();
data.date = date;
data.last_updated = date;
return new Promise((resolve, reject) => {
dbs.collection(collection).insertOne(data, function(err, result) {
if (err) {
reject(err);
} else {
try {
const lastId = String(result.insertedId);
resolve(lastId);
} catch(err2) {
reject(err2);
}
}
});
});
}
async function readfn(dbs, collection,criteria={}, justOne=false, extra_options={}) {
// transforms String and _id:String keys to _id:ObjectId
const t1_criteria = transformCriteria(criteria);
if(justOne) {
return findOne(dbs, collection, t1_criteria, extra_options);
} else {
return readmanyfn(dbs,collection,t1_criteria,extra_options);
}
}
async function readmanyfn(dbs,collection,criteria,extra_options={}) {
return new Promise(function(resolve, reject) {
dbs.collection(collection).find(criteria).toArray(function(err, result) {
if (err) {
return reject(err);
} else {
return resolve(result);
}
});
});
}
function findOne(dbs, collection, criteria, extra_options={}) {
return new Promise((resolve, reject) => {
dbs.collection(collection).findOne(criteria, function(err, result) {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
}
function updatefn(dbs, collection, criteria, data, justOne=false, extra_options) {
// transforms String and _id:String keys to _id:ObjectId
const t1_criteria = transformCriteria(criteria);
// by default updates last_updated value
if(!(extra_options['no_date'])) {
data.last_updated = +new Date();
}
const updateDoc = { $set: data };
if(justOne) {
return updateonefn(dbs, collection, t1_criteria, updateDoc);
} else {
return updatemanyfn(dbs, collection, t1_criteria, updateDoc);
}
}
// also updated last_updated value if extra_options['no_date']=1
function updatemanyfn(dbs, collection, t1_criteria, updateDoc) {
return dbs.collection(collection).updateMany(t1_criteria, updateDoc);
}
// also updated last_updated value if extra_options['no_date']=1
function updateonefn(dbs, collection, t1_criteria, updateDoc) {
return dbs.collection(collection).updateOne(t1_criteria, updateDoc);
}
function deletefn(dbs,collection,criteria,justOne=false) {
if(justOne) {
return deleteonefn(dbs, collection, criteria);
} else {
return deletemanyfn(dbs,collection, criteria);
}
}
function deletemanyfn(dbs,collection,criteria) {
// transforms String and _id:String keys to _id:ObjectId
const t1_criteria = transformCriteria(criteria);
return new Promise((resolve, reject) => {
dbs.collection(collection).deleteMany(t1_criteria, function(err, result) {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
}
function deleteonefn(dbs,collection,criteria) {
// transforms String and _id:String keys to _id:ObjectId
const t1_criteria = transformCriteria(criteria);
return new Promise((resolve, reject) => {
dbs.collection(collection).deleteMany(t1_criteria, function(err, result) {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
}
const ObjectId = require('mongodb').ObjectId;
const MongoClient = require('mongodb').MongoClient;
function connectfn(dbname, dburl = "mongodb://localhost:27017") {
return new Promise((resolve, reject) => {
MongoClient.connect(dburl + "/" + dbname, { useNewUrlParser: true, useUnifiedTopology: true }).then((client)=>{
resolve(client.db())
}).catch((err)=>{
reject(err);
});
});
}
function connectwithclientfn(dbname, dburl = "mongodb://localhost:27017") {
return new Promise((resolve, reject) => {
MongoClient.connect(dburl + "/" + dbname, { useNewUrlParser: true, useUnifiedTopology: true }).then((client)=>{
resolve({"dbs":client.db(),"client":client})
}).catch((err)=>{
reject(err);
});
});
}
// transforms String and _id keys to {"_id":ObjectId(_id)}
function transformCriteria(criteria){
const t1 = transformStringCriteria(criteria);
const t2 = transformIdKeysCriteria(t1);
return t2;
}
// transforms String to {"_id":String}
function transformStringCriteria(criteria) {
if(!isObject(criteria)) {
return {"_id":criteria};
} else {
return criteria;
}
}
// for transforming _id key that are String to ObjectId(_id)
function transformIdKeysCriteria(criteria) {
const editCriteria = {};
let keys = Object.keys(criteria);
for(let i=0; i < keys.length; i++) {
let k = keys[i];
let v = criteria[k];
if(k === '_id' && !(v instanceof Object)){
editCriteria['_id'] = new ObjectId(v);
} else {
editCriteria[k] = v;
}
}
return editCriteria;
}
// basic isList and isObject distinction
function isList(obj) {
return Array.isArray(obj) && obj.length !== 0;
}
function isObject(obj) {
return obj.constructor == Object;
}
// from https://stackoverflow.com/questions/3115150/how-to-escape-regular-expression-special-characters-using-javascript
function escapeRegexfn(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
};
// Returns items that have keyword in collection.searchField
function searchfn(dbs,collection,searchField, keyword, additional_criteria={}) {
const t1_criteria = transformCriteria(additional_criteria);
t1_criteria[searchField] = new RegExp(escapeRegexfn(keyword),'gi');
return new Promise(function(resolve, reject) {
dbs.collection(collection).find(t1_criteria).toArray(function(err, result) {
if (err) {
return reject(err);
} else {
return resolve(result);
}
});
});
}