/
index.js
199 lines (177 loc) · 6.12 KB
/
index.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
(function() {
var Db, EasyMongo, ObjectID, Server, ensureObjectId, isFunction, isObject, _ref;
_ref = require('mongodb'), Db = _ref.Db, Server = _ref.Server, ObjectID = _ref.ObjectID;
ensureObjectId = function(id) {
if (typeof id === 'string') {
return new ObjectID(id);
} else {
return id;
}
};
isFunction = function(obj) {
return toString.call(obj) === '[object Function]';
};
isObject = function(obj) {
return obj === Object(obj);
};
EasyMongo = (function() {
EasyMongo.prototype.db = null;
EasyMongo.prototype.collection = {
name: null,
object: null
};
EasyMongo.prototype._closeAfterRequest = true;
function EasyMongo(options) {
this.options = options;
if (this.options.host == null) this.options.host = '127.0.0.1';
if (this.options.port == null) this.options.port = 27017;
}
EasyMongo.prototype.getInstance = function(table, after) {
var instance,
_this = this;
if (this.options.db == null) {
throw new Error('The database name must be configured (options.db)');
}
if (this.db !== null) {
return this.getCollection(table, this.db, after);
} else {
instance = new Db(this.options.db, new Server(this.options.host, this.options.port, {}));
return instance.open(function(error, db) {
if (error) {
console.log('Error with connection to MongoDB server: ' + error);
}
_this.db = db;
return _this.getCollection(table, db, after);
});
}
};
EasyMongo.prototype.getCollection = function(table, db, after) {
var _this = this;
if (this.collection.object !== null && this.collection.name === table) {
return after(this.db, this.collection.object);
} else {
return db.collection(table, function(error, collection) {
if (error) console.log('Error with fetching collection: ' + error);
_this.collection = {
name: table,
object: collection
};
return after(_this.db, _this.collection.object);
});
}
};
EasyMongo.prototype.findById = function(table, id, after) {
var _this = this;
if (after == null) after = function() {};
return this.getInstance(table, function(db, collection) {
var params;
try {
params = {
_id: ensureObjectId(id)
};
} catch (exception) {
console.log('Error with fetching document by id: ' + exception);
_this.close();
return after(false);
}
return collection.find(params).toArray(function(error, results) {
if (error) console.log('Error with fetching document by id: ' + error);
if (this._closeAfterRequest) this.close();
return after(results.length === 1 ? results[0] : false);
});
});
};
EasyMongo.prototype.find = function(table, params, options, after) {
var _ref2,
_this = this;
_ref2 = this._normalizeArguments(params, options, after), params = _ref2[0], options = _ref2[1], after = _ref2[2];
return this.getInstance(table, function(db, collection) {
var cursor;
try {
if ((params != null ? params._id : void 0) != null) {
if (isObject(params._id) && (params._id.$in != null)) {
params._id.$in = params._id.$in.map(function(value) {
return ensureObjectId(value);
});
} else {
params._id = ensureObjectId(params._id);
}
}
} catch (exception) {
console.log('Error with fetching documents: ' + exception);
_this.close();
return after([]);
}
cursor = collection.find(params);
if (options.sort) cursor.sort(options.sort);
if (options.limit) cursor.limit(options.limit);
if (options.skip) cursor.skip(options.skip);
return cursor.toArray(function(error, results) {
if (error) console.log('Error with fetching documents: ' + error);
if (_this._closeAfterRequest) _this.close();
return after(results);
});
});
};
EasyMongo.prototype.count = function(table, params, after) {
var _this = this;
if (isFunction(params)) {
after = params;
params = null;
}
return this.getInstance(table, function(db, collection) {
return collection.count(params, function(error, results) {
if (error) console.log('Error with fetching counts: ' + error);
if (_this._closeAfterRequest) _this.close();
return after(parseInt(results, 10));
});
});
};
EasyMongo.prototype.save = function(table, params, after) {
var _this = this;
if (after == null) after = function() {};
return this.getInstance(table, function(db, collection) {
if (params._id != null) params._id = ensureObjectId(params._id);
return collection.save(params, {
safe: true
}, function(error, results) {
if (error) console.log('Error with saving data: ' + error);
if (_this._closeAfterRequest) _this.close();
return after(params._id != null ? params : results[0]);
});
});
};
EasyMongo.prototype.closeAfterRequest = function(value) {
this._closeAfterRequest = value;
return this;
};
EasyMongo.prototype.close = function() {
this._closeAfterRequest = true;
if (this.db !== null) {
this.db.close();
this.collection = {
name: null,
object: null
};
this.db = null;
}
return this;
};
EasyMongo.prototype._normalizeArguments = function(params, options, after) {
if (isFunction(params)) {
after = params;
params = null;
options = {};
}
if (isFunction(options)) {
after = options;
options = {};
}
if (!after) after = (function() {});
if (!options) options = {};
return [params, options, after];
};
return EasyMongo;
})();
module.exports = EasyMongo;
}).call(this);