forked from markhoney/express-nedb-rest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
332 lines (293 loc) · 10.9 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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
var express = require('express');
var bodyParser = require("body-parser");
var nedb = require('nedb');
var filter = require('./filter');
var order = require('./order');
function expressNedbRest(cfg) {
var router = express.Router();
// initialize configuration object
router.cfg = (typeof(cfg) == 'object') ? cfg : {};
router.cfg.collections = []; //no collections yet
if (typeof(router.cfg.convertToDate) != 'boolean') {
router.cfg.convertToDate = true;
}
// define reviver function to parse date strings, if option convertToDate==true
var reviverFunc = null;
if (router.cfg.convertToDate) {
reviverFunc = function(key, value) {
// convert date string (ISO 8601) to date object
// i.e. "2017-04-07T18:00:00.000Z"
if(typeof(value)=='string' &&
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|(\+|-)\d{2}:\d{2})$/.test(value)) {
return new Date(value);
}
else {
return value;
}
};
}
// parse body of request
router.use(bodyParser.json({"reviver":reviverFunc}));
// add configuration to each request
router.use(function (req, res, next) {
req.cfg = router.cfg;
next();
});
// find datastore of collection and add it to request object
router.param('collection', function collectionParam(req, res, next, collection) {
// call validator function, if configured
if (req.cfg.validator) {
req.cfg.validator(req, res, next);
}
// add collection information to request object
req.collection = collection;
req.nedb = req.cfg.collections[collection];
if (!req.nedb) {
next({ status: 404, // Bad Request
message: "unknown collection " + req.collection }) ;
}
else {
// parse filter
try {
req.$filter = filter(req.query.$filter);
next();
}
catch (e) {
// parser error
next({ status: 400, // Bad Request
message: "unvalid $filter " + e.message });
}
}
});
// add object id from uri to request
router.param('id', function (req, res, next, id) {
req.id = id;
next();
});
// register methods for GET, POST, ...
addRestMethods(router);
// at last send json result or error
router.use(function(req, res, next){
if(res.locals.count) {
res.append('X-Total-Count', res.locals.count);
}
if (res.locals.json) {
res.json(res.locals.json);
}
next();
});
// error handling
router.use(function(err, req, res, next){
if ( typeof(err) ==='object') {
res.status(err.status || 400);
res.send(err.message || 'unknown error');
}
else {
res.status(400);
res.send(err.toString());
}
});
/**
* add a NeDB datastore to REST collections
* @param {string} collection's name, wich is used for publication in REST calls
* @param {Datastore) NeDB Datastore object
* @public
*/
router.addDatastore = function(collection, store) {
this.cfg.collections[collection] = store;
};
/**
* add a callback function, which will be called before each NeDB database call.
* @param {function) callback function with expressJS signature (req, res, next)
* @public
*/
router.setValidator = function(f) {
if (typeof(f) == "function") {
this.cfg.validator = f;
}
else {
this.cfg.validator = null;
}
};
// return router
return router;
};
function fullUrl(req) {
return req.protocol + '://' + req.get('host') + req.originalUrl;
}
function addRestMethods(router) {
//--------------------------------------------------------------------------
router.get('/', function (req, res, next) {
// return an array with all collection's names
res.locals.json = [];
for(var name in req.cfg.collections) {
res.locals.json.push({
"name": name,
"link": fullUrl(req) + name
});
}
res.locals.count = res.locals.json.length;
next();
});
//--------------------------------------------------------------------------
router.get('/:collection', function (req, res, next) {
if (typeof(req.query.$count) == "undefined") {
if (typeof(req.query.$single) == 'undefined') {
// normal query
var query = req.nedb.find(req.$filter);
// parse orderby
if (req.query.$orderby) {
try {
var $order = order(req.query.$orderby);
if ($order) query.sort($order);
}
catch (e) {
// parser error
next({ status: 400, // Bad Request
message: "unvalid $orderby " + e.message });
}
}
if (!isNaN(req.query.$skip)) query.skip(parseInt(req.query.$skip));
if (!isNaN(req.query.$limit)) query.limit(parseInt(req.query.$limit));
query.exec(function(err, docs) {
if (err) {
return next(err);
}
res.locals.count = docs.length;
res.locals.json = docs;
next();
}); }
else {
// find single document
query = req.nedb.findOne(req.$filter, function(err, doc) {
if (err) {
return next(err);
}
if (doc) {
res.locals.count = 1;
res.locals.json = doc;
next();
}
else {
next({ status: 404, // Not found
message: "document not found" });
}
});
}
}
else {
// count of documents requested
req.nedb.count(req.$filter, function(err, count) {
if (err) {
return next(err);
}
res.locals.count = count;
res.status(200).send(count.toString());
});
}
});
//--------------------------------------------------------------------------
router.get('/:collection/:id', function (req, res, next) {
req.nedb.findOne({ _id: req.id }, function (err, doc) {
if (err) {
return next(err);
}
if (!doc) {
return next({status:404, message:"document "+req.collection+" _id="+req.id+" not found"});
}
res.locals.json = doc;
next();
});
});
//--------------------------------------------------------------------------
router.post('/:collection', function (req, res, next) {
if (!req.body || typeof(req.body) != 'object') {
return next({ status: 400, message: 'No Request Body' }); // Bad Request
}
req.nedb.insert(req.body, function (err, doc) {
if (err) {
return next(err);
}
res.append('Location', fullUrl(req) + '/' + doc._id);
res.status(201); // Created
res.locals.json = doc;
next();
});
});
//--------------------------------------------------------------------------
router.delete('/:collection/:id', function (req, res, next) {
req.nedb.remove({ _id: req.id}, { multi: false }, function (err, count) {
if (err) {
return next(err);
}
if (count != 1) {
return next({status:404, message:"document "+req.collection+" _id="+req.id+" not found"});
}
else {
res.locals.count = count;
res.status(204).send("deleted entries: "+count);
}
});
});
//--------------------------------------------------------------------------
router.delete('/:collection', function (req, res, next) {
if (!req.$filter || Object.keys(req.$filter).length == 0) {
return next({ status: 405, message: '$filter ist missing' });
}
req.nedb.remove(req.$filter, { multi: true }, function (err, count) {
if (err) {
return next(err);
}
if (count == 0) {
return next({status:404, message:"no document found to delete"});
}
res.locals.count = count;
res.status(204).send("deleted entries: "+count);
});
});
//--------------------------------------------------------------------------
router.put('/:collection/:id', function (req, res, next) {
if (!req.body || typeof(req.body) != 'object') {
return next({ status: 400, message: 'No Request Body' }); // Bad Request
}
req.nedb.update({_id:req.id}, req.body, {multi:false}, function (err, count) {
if (err) {
return next(err);
}
if (count != 1) {
return next({status:404, message:"document "+req.collection+" _id="+req.id+" not found"});
}
res.locals.count = count;
res.status(204).send("updated entries: "+count);
});
});
//--------------------------------------------------------------------------
router.put('/:collection', function (req, res, next) {
if (!req.body || typeof(req.body) != 'object') {
return next({ status: 400, message: 'No Request Body' }); // Bad Request
}
req.nedb.update(req.$filter, req.body, {multi:true}, function (err, count, docs) {
if (err) {
return next(err);
}
if (count == 0) {
return next({status:404, message:"no document found to update"});
}
res.locals.count = count;
res.status(204).send("updated entries: "+count);
});
});
//--------------------------------------------------------------------------
router.patch('/:collection', function (req, res, next) {
res.status(405).send(); // Method Not Allowed
});
//--------------------------------------------------------------------------
router.delete('/:collection', function (req, res, next) {
res.status(405).send(); // Method Not Allowed
});
//--------------------------------------------------------------------------
router.post('/:collection/:id', function (req, res, next) {
res.status(405).send(); // Method Not Allowed
});
}
module.exports = expressNedbRest;