model.js
1.1 KB
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
/**
* Base Model Class
*/
'use strict';
const DataBase = require('nedb-promise');
const path = require('path');
const config = require('../config');
class Model {
constructor(name) {
this.name = name;
this.db = new DataBase({
filename: path.join(config.dbDir, `${name}.json`),
autoload: true
});
}
_seeds() {
return [];
}
get _name() {
return this.name;
}
insert(...docs) {
return this.db.insert(docs);
}
find(doc) {
return this.db.find(doc);
}
cfind(doc) {
return this.db.cfind(doc);
}
findOne(doc) {
return this.db.findOne(doc);
}
findById(id) {
return this.db.findOne({
_id: id
});
}
findAll() {
return this.db.find({});
}
count(doc) {
return this.db.count(doc);
}
update(query, update, options = {
multi: true,
upsert: false
}) {
return this.db.update(query, update, options);
}
remove(query, options = {
multi: true
}) {
return this.db.remove(query, options);
}
removeById(id) {
return this.remove({
_id: id
});
}
}
module.exports = Model;