-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.js
131 lines (98 loc) · 3.48 KB
/
model.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
// Mongo
var mongodb = require('mongodb')
var MongoClient = mongodb.MongoClient;
var Templates = require("./templates");
module.exports = objToReturn = {
// find
read: function (options, callback) {
// process options
processOptions(options, [
{ key: "q", defaultVal: {} },
{ key: "o", defaultVal: {} },
{ key: "t", defaultVal: "" }
]);
getCollectionFromTemplate (options.t, function (err, collection) {
if (err) { return callback (err); }
// and finally, find
collection.find(options.q, options.o).toArray(callback);
});
},
// create
create: function (options, callback) {
// process options
processOptions(options, [
{ key: "d", defaultVal: {} },
{ key: "t", defaultVal: "" }
]);
getCollectionFromTemplate (options.t, function (err, collection) {
if (err) { return callback (err); }
// and finally, insert something there
collection.insert(options.d, callback);
});
},
// update
update: function (options, callback) {
// process options
processOptions(options, [
{ key: "q", defaultVal: {} },
{ key: "o", defaultVal: {} },
{ key: "d", defaultVal: {} },
{ key: "t", defaultVal: "" }
]);
getCollectionFromTemplate (options.t, function (err, collection) {
if (err) { return callback (err); }
// and finally, update something there
collection.update(options.q, options.d, options.o, callback);
});
},
// remove
delete: function (options, callback) {
// process options
processOptions(options, [
{ key: "q", defaultVal: {} },
{ key: "t", defaultVal: "" }
]);
getCollectionFromTemplate (options.t, function (err, collection) {
if (err) { return callback (err); }
// and finally, remove something
collection.remove(options.q, callback);
});
}
};
function processOptions (options, fields) {
fields = fields || [];
for (var i = 0; i < fields.length; ++i) {
var cField = fields[i];
var defaultVal = cField.defaultVal;
var key = cField.key;
options[key] = options[key] || defaultVal;
}
}
var dbCache = {};
function getCollectionFromTemplate (templateId, callback) {
if (!templateId) {
return callback("Template id not provided.");
}
if (dbCache[templateId]) {
return callback(null, dbCache[templateId]);
}
if (typeof templateId._id === "string") {
templateId = templateId._id;
}
var templObject = Templates[templateId];
if (templateId.constructor.name === "Object") {
templObject = templateId;
}
if (!templObject) {
return callback ("Template not found.");
}
if (typeof templObject.connectionStr !== "string" || templObject.connectionStr === "") {
return callback ("connectionStrnig must be a non empty string.");
}
console.log(templObject);
MongoClient.connect(templObject.connectionStr, function (err, db) {
if (err) { return callback (err); }
var col = dbCache[templateId] = db.collection(templObject.collection);
callback(null, col);
});
}