-
Notifications
You must be signed in to change notification settings - Fork 0
/
operations.js
104 lines (80 loc) · 2.43 KB
/
operations.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
/*
* Insert something in databse
*/
exports.insert = function(link) {
if (!link.data) {
link.send(400, { status: "Missing data" });
return;
}
M.datasource.resolve(link.params.ds, function(err, ds) {
if (err) {
link.send(400, err);
return;
}
M.database.open(ds, function(err, db) {
if (err) {
link.send(400, err);
return;
}
db.collection(ds.collection, function(err, collection) {
if (err) {
link.send(400, err);
return;
}
var data = link.data || {};
var docs = data.criteria || {};
var options = data.options || {};
// nothing to insert
if (["{}", "[]"].indexOf(JSON.strinigfy(docs) !== -1) {
link.send(200, []);
return;
}
collection.insert(docs, options, function(err, docsInserted) {
if (err) {
link.send(400, err);
return;
}
link.send(200, docsInserted);
});
});
});
});
};
/*
* Removes documents from database
*/
exports.remove = function(link) {
if (!link.data) {
link.send(400, { status: "Missing data" });
return;
}
M.datasource.resolve(link.params.ds, function(err, ds) {
if (err) {
link.send(400, err);
return;
}
M.database.open(ds, function(err, db) {
if (err) {
link.send(400, err);
return;
}
db.collection(ds.collection, function(err, collection) {
if (err) {
link.send(400, err);
return;
}
var data = link.data || {};
var criteria = data.criteria || {};
var update = data.update || {};
var options = data.options || {};
collection.update(criteria, update, options, function(err, results) {
if (err) {
link.send(400, err);
return;
}
link.send(200, { status: "OK" });
});
});
});
});
};