/
repo.js
375 lines (340 loc) · 8.59 KB
/
repo.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
var fs = require('fs-extra');
var path = require('path');
var uuid = require('uuid');
var async = require('async');
var rimraf = require('rimraf');
var util = require('util');
/**
* DataRepo: Represents a data repository. Each collection is persisted in a different folder,within rootdir.
* @param config: Config object, format:
* rootdir String: represents de root dir for the repo - default is './dr'
* @constructor
*/
function DataRepo(config) {
if (config && config.rootdir) {
this.rootdir = config.rootdir
} else {
this.rootdir = './dr/'
}
this.filters = {};
fs.mkdirsSync(this.rootdir)
}
/**
* collist - lists all collections within repo..
* @param cb
*/
DataRepo.prototype.collist = function (cb) {
fs.readdir(this.rootdir, cb);
}
/**
* colcreate - creates a new collection
* @param name
* @param cb
*/
DataRepo.prototype.colcreate = function (name, cb) {
var fullpath = path.join(this['rootdir'], name);
fs.mkdirs(fullpath, (err)=> {
if (err) {
return cb(err);
} else {
return cb(null, new DRCol(this, name));
}
});
}
/**
* coldelete - deletes a collection within a repo; drops all documents;
* @param name
* @param cb
*/
DataRepo.prototype.coldelete = function (name, cb) {
var fullpath = path.join(this.rootdir, name);
rimraf(fullpath, cb);
//fs.rmdir(fullpath, cb);
}
/**
* SYNC Version - coldelete - deletes a collection within a repo; drops all documents;
* @param name
* @param cb
*/
DataRepo.prototype.coldeleteSync = function (name, cb) {
var fullpath = path.join(this.rootdir, name);
rimraf.sync(fullpath);
}
/**
* returns a named collection out of repo. Creates it in case does not exist;
* @param name
* @returns {DRCol}
*/
DataRepo.prototype.col = function (name) {
var ret = new DRCol(this, name);
return ret;
}
/**
* represents a Collection within a repo. Constructor should not be used directly and is not exposed by module.
* @param p {DataRepo} - Parent repo.
* @param name {String} - Col name
* @constructor
*/
function DRCol(p, name) {
this.parent = p;
this.name = name;
this.fullpath = path.join(this.parent.rootdir, name);
fs.mkdirsSync(this.fullpath);
}
/**
* executes upsert operation in collection for object obj. In case obj has id field, whole object update will be performed;
* Otherwise, insert will be done
* @param obj {Object} - Object to be persisted
* @param cb - Callback to be called on op is finished, expects (err,doc) params where error is any error that may occour and doc is the doc as after upsert.
*/
DRCol.prototype.upsert = function (obj, cb) {
if (!obj._id) {
obj._id = uuid.v4();
}
if (!cb) {
cb = function () {
}
}
delete obj._col;
delete obj._delete;
delete obj._save;
var fqdn = path.join(this.fullpath, obj._id);
fs.writeFile(fqdn, JSON.stringify(obj), (err)=> {
if (err) {
return cb(err, null);
} else {
obj._col = this;
obj._delete = function (cb) {
this._col.delete(this._id, cb);
};
obj._save = function (cb) {
this._col.upsert(this, cb);
}
return cb(null, obj);
}
});
}
/**
* SYNC Version - executes upsert operation in collection for object obj. In case obj has id field, whole object update will be performed;
* Otherwise, insert will be done
* @param obj {Object} - Object to be persisted
*/
DRCol.prototype.upsertSync = function (obj) {
if (!obj._id) {
obj._id = uuid.v4();
}
delete obj._col;
delete obj._delete;
delete obj._save;
var fqdn = path.join(this.fullpath, obj._id);
fs.writeFileSync(fqdn, JSON.stringify(obj));
obj._col = this;
obj._delete = function (cb) {
this._col.delete(this._id, cb);
};
obj._save = function (cb) {
this._col.upsert(this, cb);
}
obj;
}
/**
* Deletes an object from collection
* @param id {String} - id of object to be deleted.
* @param cb - Callback one op is finished.
*/
DRCol.prototype.delete = function (id, cb) {
var fqdn = path.join(this.fullpath, id);
fs.unlink(fqdn, cb)
}
/**
* SYNC VERSION - Deletes an object from collection
* @param id {String} - id of object to be deleted.
*/
DRCol.prototype.deleteSync = function (id) {
var fqdn = path.join(this.fullpath, id);
fs.unlinkSync(fqdn);
}
/**
* Retrieves the content of a collection.
* @param filter - A function for new filter os String for pre-registered ones. Register is done using :
* repo.filters.myfilter = function(obj)..., than the string 'myfilter' can be used.
* @param cb - Callback with params (err,docs) where docs is the collection of returned objs.
*/
DRCol.prototype.get = function (filter, cb) {
if (!cb) {
cb = filter;
filter = function (o) {
return true;
}
} else if (util.isString(filter)) {
filter = this.parent.filters[filter];
}
fs.readdir(this.fullpath, (err, files)=> {
if (err) {
return cb(err, null);
}
var ret = [];
async.each(files, (f, cb)=> {
fs.readFile(path.join(this.fullpath, f), function (err, data) {
if (err) {
return cb(err);
} else {
obj = JSON.parse(data.toString(), reviver);
if (filter(obj)) {
obj._col = this;
obj._delete = function (cb) {
this._col.delete(this._id, cb);
};
obj._save = function (cb) {
this._col.upsert(this, cb);
}
ret.push(obj);
}
cb();
}
});
}, function (err) {
if (err) {
return cb(err, null);
} else {
return cb(null, ret);
}
});
});
}
/**
* SYNC VERSION - Retrieves the content of a collection.
* @param filter - A function for new filter os String for pre-registered ones. Register is done using :
* repo.filters.myfilter = function(obj)..., than the string 'myfilter' can be used.
*/
DRCol.prototype.getSync = function (filter) {
if (!filter) {
filter = function (o) {
return true;
}
} else if (util.isString(filter)) {
filter = this.parent.filters[filter];
}
var files = fs.readdirSync(this.fullpath);
var ret = [];
for (f of files) {
var data = fs.readFileSync(path.join(this.fullpath, f));
obj = JSON.parse(data.toString(), reviver);
if (filter(obj)) {
obj._col = this;
obj._delete = function (cb) {
this._col.delete(this._id, cb);
};
obj._save = function (cb) {
this._col.upsert(this, cb);
}
ret.push(obj);
}
}
return ret;
}
/**
* Get a single doc by id
* @param id
* @param cb
*/
DRCol.prototype.getbyid = function (id, cb) {
fs.readFile(path.join(this.fullpath, id), function (err, data) {
if (err) {
return cb(err);
} else {
obj = JSON.parse(data.toString());
obj._col = this;
obj._delete = function (cb) {
this._col.delete(this._id, cb);
};
obj._save = function (cb) {
this._col.upsert(this, cb);
}
cb(null, obj);
}
});
}
/**
* SYNC VERSION - Get a single doc by id
* @param id
*/
DRCol.prototype.getbyidSync = function (id) {
var data = fs.readFileSync(path.join(this.fullpath, id));
var obj = JSON.parse(data.toString());
obj._col = this;
obj._delete = function (cb) {
this._col.delete(this._id, cb);
};
obj._save = function (cb) {
this._col.upsert(this, cb);
}
return obj;
}
/**
* each - iterates over a collection.
* @param filter {String|function}- name or function for filtering
* @param cbeach {function} - callback for each object in collection
* @param cbend {function} - callback called once all objects have been interacted.
*/
DRCol.prototype.each = function (filter, cbeach, cbend) {
if (!cbend) {
cbend = cbeach;
cbeach = filter;
filter = function (o) {
return true;
}
} else if (util.isString(filter)) {
filter = this.parent.filters[filter];
}
fs.readdir(this.fullpath, (err, files)=> {
if (err) {
return cb(err, null);
}
var ret = [];
async.each(files, (f, cb)=> {
fs.readFile(path.join(this.fullpath, f), function (err, data) {
if (err) {
return cb(err);
} else {
obj = JSON.parse(data.toString(), reviver);
obj._col = this;
obj._delete = function (cb) {
this._col.delete(this._id, cb);
};
obj._save = function (cb) {
this._col.upsert(this, cb);
}
if (filter(obj)) {
process.nextTick(function () {
cbeach(obj)
});
}
cb();
}
});
}, function (err) {
if (err) {
return cbend(err);
} else {
process.nextTick(function () {
return cbend(null);
})
}
});
});
}
/**
* reviver: Internal function used to revive JSON with some extras
* @param k {String} Key to be used
* @param v {Multi} Value to be used
* @returns {*} Value post conversion
*/
function reviver(k, v) {
if (util.isString(v) && v.match(/\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d/g)) {
return new Date(v);
} else {
return v;
}
}
module.exports = DataRepo;