@abtnode/db 1.8.14 → 1.8.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/base.d.ts +2 -0
- package/lib/base.js +12 -0
- package/lib/db.js +39 -100
- package/lib/factory.d.ts +8 -0
- package/package.json +5 -5
package/lib/base.d.ts
ADDED
package/lib/base.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const { DataStore } = require('@nedb/core');
|
|
2
|
+
const { createDataStore } = require('@nedb/multi');
|
|
3
|
+
|
|
4
|
+
const selectDataStore = () => {
|
|
5
|
+
if (process.env.NODE_ENV === 'test' || !process.env.NEDB_MULTI_PORT) {
|
|
6
|
+
return DataStore;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
return createDataStore(Number(process.env.NEDB_MULTI_PORT));
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
module.exports.DataStore = selectDataStore();
|
package/lib/db.js
CHANGED
|
@@ -1,70 +1,40 @@
|
|
|
1
1
|
const fs = require('fs');
|
|
2
|
+
const EventEmitter = require('events');
|
|
2
3
|
const path = require('path');
|
|
3
|
-
const util = require('util');
|
|
4
4
|
const cloneDeep = require('lodash.clonedeep');
|
|
5
|
-
|
|
5
|
+
|
|
6
6
|
const packageName = require('../package.json').name;
|
|
7
|
+
const { DataStore } = require('./base');
|
|
7
8
|
|
|
8
|
-
const DataStore =
|
|
9
|
-
process.env.NODE_ENV === 'test' ? require('@nedb/core') : require('@nedb/multi')(Number(process.env.NEDB_MULTI_PORT));
|
|
10
9
|
// eslint-disable-next-line import/order
|
|
11
10
|
const logger = require('@abtnode/logger')(packageName);
|
|
12
11
|
|
|
13
|
-
class DB extends
|
|
14
|
-
constructor(baseDir,
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
this.options = Object.freeze(cloneDeep(options));
|
|
20
|
-
this.db = new DataStore({
|
|
21
|
-
filename: this.filename,
|
|
22
|
-
timestampData: true,
|
|
23
|
-
...dbOptions,
|
|
24
|
-
});
|
|
12
|
+
class DB extends DataStore {
|
|
13
|
+
constructor(baseDir, config) {
|
|
14
|
+
const dbOptions = config.db || {};
|
|
15
|
+
const filename = path.join(baseDir, config.filename);
|
|
16
|
+
|
|
17
|
+
super({ filename, timestampData: true, ...dbOptions });
|
|
25
18
|
|
|
26
|
-
|
|
19
|
+
this.filename = filename;
|
|
20
|
+
this.config = Object.freeze(cloneDeep(config));
|
|
21
|
+
|
|
22
|
+
this._patchEvents();
|
|
27
23
|
|
|
28
24
|
this.ready = false;
|
|
29
25
|
this.readyCallbacks = [];
|
|
30
|
-
this.
|
|
26
|
+
this.loadDatabase((err) => {
|
|
31
27
|
if (err) {
|
|
32
28
|
logger.error(`failed to load disk database ${this.filename}`, { error: err });
|
|
33
29
|
console.error(err);
|
|
34
30
|
} else {
|
|
31
|
+
logger.info('loaded', { filename: this.filename });
|
|
35
32
|
this.ready = true;
|
|
36
33
|
if (this.readyCallbacks.length) {
|
|
37
34
|
this.readyCallbacks.forEach((x) => x());
|
|
38
35
|
}
|
|
39
36
|
}
|
|
40
37
|
});
|
|
41
|
-
|
|
42
|
-
this.asyncDB = new Proxy(this.db, {
|
|
43
|
-
get(target, property) {
|
|
44
|
-
if (typeof target[property] === 'function') {
|
|
45
|
-
return util
|
|
46
|
-
.promisify((...args) => {
|
|
47
|
-
const cb = args[args.length - 1];
|
|
48
|
-
const rest = args.slice(0, args.length - 1);
|
|
49
|
-
|
|
50
|
-
target[property](...rest, (err, ...result) => {
|
|
51
|
-
if (err) {
|
|
52
|
-
return cb(err);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
if (result.length === 1) {
|
|
56
|
-
return cb(null, result[0]);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return cb(null, result);
|
|
60
|
-
});
|
|
61
|
-
})
|
|
62
|
-
.bind(target);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
return target[property];
|
|
66
|
-
},
|
|
67
|
-
});
|
|
68
38
|
}
|
|
69
39
|
|
|
70
40
|
onReady(cb) {
|
|
@@ -80,10 +50,9 @@ class DB extends EventEmitter {
|
|
|
80
50
|
const pageSize = Math.min(100, size);
|
|
81
51
|
|
|
82
52
|
return new Promise((resolve, reject) => {
|
|
83
|
-
this.
|
|
84
|
-
.find(conditions)
|
|
53
|
+
this.cursor(conditions)
|
|
85
54
|
.sort(sort)
|
|
86
|
-
.exec((err, docs) => {
|
|
55
|
+
.exec((err, docs = []) => {
|
|
87
56
|
if (err) {
|
|
88
57
|
return reject(err);
|
|
89
58
|
}
|
|
@@ -112,7 +81,7 @@ class DB extends EventEmitter {
|
|
|
112
81
|
}
|
|
113
82
|
|
|
114
83
|
async updateById(id, updates, options = {}) {
|
|
115
|
-
const [, doc] = await
|
|
84
|
+
const [, doc] = await super.update({ _id: id }, updates, {
|
|
116
85
|
multi: false,
|
|
117
86
|
upsert: false,
|
|
118
87
|
returnUpdatedDocs: true,
|
|
@@ -122,73 +91,43 @@ class DB extends EventEmitter {
|
|
|
122
91
|
return doc;
|
|
123
92
|
}
|
|
124
93
|
|
|
125
|
-
update(
|
|
126
|
-
if (
|
|
127
|
-
throw new Error('param
|
|
94
|
+
update(query, updates, options = {}) {
|
|
95
|
+
if (arguments.length === 0) {
|
|
96
|
+
throw new Error('query and update param are required to update database record');
|
|
128
97
|
}
|
|
129
98
|
|
|
130
|
-
if (typeof
|
|
131
|
-
return this.updateById(
|
|
99
|
+
if (typeof query === 'string') {
|
|
100
|
+
return this.updateById(query, updates, options);
|
|
132
101
|
}
|
|
133
102
|
|
|
134
|
-
return
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
count(conditions = {}) {
|
|
138
|
-
return new Promise((resolve, reject) => {
|
|
139
|
-
this.db.count(conditions, (err, num) => {
|
|
140
|
-
if (err) {
|
|
141
|
-
return reject(err);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
return resolve(num);
|
|
145
|
-
});
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
remove(conditions = {}, options = { multi: false }) {
|
|
150
|
-
return new Promise((resolve, reject) => {
|
|
151
|
-
this.db.remove(conditions, options, (err, num) => {
|
|
152
|
-
if (err) {
|
|
153
|
-
return reject(err);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
return resolve(num);
|
|
157
|
-
});
|
|
158
|
-
});
|
|
103
|
+
return super.update(query, updates, { returnUpdatedDocs: true, ...options });
|
|
159
104
|
}
|
|
160
105
|
|
|
161
106
|
reset() {
|
|
162
107
|
fs.unlinkSync(this.filename);
|
|
163
108
|
}
|
|
164
109
|
|
|
165
|
-
find(...args) {
|
|
166
|
-
if (args.length === 0) {
|
|
167
|
-
return this.asyncDB.find({});
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
return this.asyncDB.find(...args);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
findOne(...args) {
|
|
174
|
-
if (args.length === 0) {
|
|
175
|
-
return this.asyncDB.findOne({});
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
return this.asyncDB.findOne(...args);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
insert(...args) {
|
|
182
|
-
return this.asyncDB.insert(...args);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
110
|
compactDatafile() {
|
|
186
|
-
this.
|
|
111
|
+
this.persistence.compactDatafile((err) => {
|
|
187
112
|
if (err) {
|
|
188
113
|
console.error(`failed to compact datafile: ${this.filename}`, err);
|
|
189
114
|
}
|
|
190
115
|
});
|
|
191
116
|
}
|
|
117
|
+
|
|
118
|
+
// In case we are using the concurrent version
|
|
119
|
+
_patchEvents() {
|
|
120
|
+
if (typeof this.emit === 'function') {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const events = new EventEmitter();
|
|
125
|
+
|
|
126
|
+
this.on = events.on.bind(events);
|
|
127
|
+
this.once = events.once.bind(events);
|
|
128
|
+
this.off = events.off.bind(events);
|
|
129
|
+
this.emit = events.emit.bind(events);
|
|
130
|
+
}
|
|
192
131
|
}
|
|
193
132
|
|
|
194
133
|
/**
|
package/lib/factory.d.ts
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@abtnode/db",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.17",
|
|
4
4
|
"description": "@nedb/core wrapper",
|
|
5
5
|
"author": "polunzh <polunzh@gmail.com>",
|
|
6
6
|
"homepage": "https://github.com/ArcBlock/blocklet-server#readme",
|
|
@@ -29,13 +29,13 @@
|
|
|
29
29
|
"url": "https://github.com/ArcBlock/blocklet-server/issues"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@abtnode/logger": "1.8.
|
|
33
|
-
"@nedb/core": "^
|
|
34
|
-
"@nedb/multi": "^
|
|
32
|
+
"@abtnode/logger": "1.8.17",
|
|
33
|
+
"@nedb/core": "^2.0.3",
|
|
34
|
+
"@nedb/multi": "^2.0.3",
|
|
35
35
|
"lodash.clonedeep": "^4.5.0"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"jest": "^27.5.1"
|
|
39
39
|
},
|
|
40
|
-
"gitHead": "
|
|
40
|
+
"gitHead": "aa1854b3b71a6250182bfcad794235099f38a514"
|
|
41
41
|
}
|