-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
44 lines (37 loc) · 1.19 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
var Persistence = require('nedb/lib/persistence')
, model = require('nedb/lib/model')
, customUtils = require('nedb/lib/customUtils')
, mkdirp = require('mkdirp')
, fs = require('fs')
, path = require('path')
, util = require('util')
;
function Logger (dbOptions) {
dbOptions.inMemoryOnly = false;
dbOptions.autoload = false;
this.persistence = new Persistence({ db: dbOptions });
// Make sure file and containing directory exist, create them if they don't
mkdirp.sync(path.dirname(dbOptions.filename));
if (!fs.existsSync(dbOptions.filename)) { fs.writeFileSync(dbOptions.filename, '', 'utf8'); }
}
// docs can be one document or an array of documents
Logger.prototype.insert = function (_docs, cb) {
var callback = cb || function () {}
, docs = util.isArray(_docs) ? _docs : [_docs]
, preparedDocs = []
;
try {
docs.forEach(function (doc) {
preparedDocs.push(model.deepCopy(doc));
});
preparedDocs.forEach(function(doc) {
doc._id = customUtils.uid(16);
model.checkObject(doc);
});
this.persistence.persistNewState(preparedDocs, callback);
} catch (err) {
return callback(err);
}
};
// Interface
module.exports = Logger;