-
Notifications
You must be signed in to change notification settings - Fork 4
/
nosql-crud-adapter.js
70 lines (65 loc) · 1.86 KB
/
nosql-crud-adapter.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
function NoSqlCrudAdapter (dbConnectFunction, getId) {
function getObjects (collectionName, filter, callback) {
filter = filter || [];
let query = {};
filter.forEach(fi => query[fi.name] = fi.value);
dbConnectFunction((db, finalizeCallback) => {
db.collection(collectionName).find(query).toArray()
.then((results) => {
callback(results);
finalizeCallback(results);
})
.catch(() => {
console.error(JSON.stringify(arguments));
});
}
);
}
function deleteObject (collectionName, idValue, callback) {
dbConnectFunction((db, finalizeCallback) => {
db.collection(collectionName).deleteMany({ id: idValue })
.then((results) => {
callback(results);
finalizeCallback(results);
})
.catch(() => {
console.error(JSON.stringify(arguments));
});
}
);
}
function createObject (collectionName, object, callback) {
object.id = object.id || getId();
dbConnectFunction((db, finalizeCallback) => {
db.collection(collectionName).insertOne(object)
.then((results) => {
callback(object.id);
finalizeCallback(results);
})
.catch(() => {
console.error(JSON.stringify(arguments));
});
}
);
}
function updateObject (collectionName, object, callback) {
dbConnectFunction((db, finalizeCallback) => {
db.collection(collectionName).updateOne({ id: object.id }, { $set: object })
.then((results) => {
callback(results);
finalizeCallback(results);
})
.catch(() => {
console.error(JSON.stringify(arguments));
});
}
);
}
return {
create: createObject,
retrieve: getObjects,
update: updateObject,
delete: deleteObject
}
}
module.exports = NoSqlCrudAdapter;