ueberdb2 5.0.48 → 6.0.2
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/dist/{AbstractDatabase-a4jdIEuT.js → AbstractDatabase-DfQ39Ry6.js} +6 -13
- package/dist/{cassandra_db-C5K2zZew.js → cassandra_db-B_5ANRzM.js} +6 -7
- package/dist/{couch_db-BVd81sS7.js → couch_db-BXyzauwJ.js} +5 -20
- package/dist/databases/cassandra_db.d.ts +5 -3
- package/dist/databases/cassandra_db.d.ts.map +1 -1
- package/dist/databases/couch_db.d.ts +2 -4
- package/dist/databases/couch_db.d.ts.map +1 -1
- package/dist/databases/dirty_db.d.ts +1 -1
- package/dist/databases/dirty_db.d.ts.map +1 -1
- package/dist/databases/dirty_git_db.d.ts +1 -1
- package/dist/databases/dirty_git_db.d.ts.map +1 -1
- package/dist/databases/elasticsearch_db.d.ts +2 -2
- package/dist/databases/elasticsearch_db.d.ts.map +1 -1
- package/dist/databases/memory_db.d.ts +1 -1
- package/dist/databases/memory_db.d.ts.map +1 -1
- package/dist/databases/mock_db.d.ts +1 -1
- package/dist/databases/mock_db.d.ts.map +1 -1
- package/dist/databases/mongodb_db.d.ts +4 -3
- package/dist/databases/mongodb_db.d.ts.map +1 -1
- package/dist/databases/mssql_db.d.ts +3 -3
- package/dist/databases/mssql_db.d.ts.map +1 -1
- package/dist/databases/mysql_db.d.ts +3 -3
- package/dist/databases/mysql_db.d.ts.map +1 -1
- package/dist/databases/postgres_db.d.ts +2 -2
- package/dist/databases/postgres_db.d.ts.map +1 -1
- package/dist/databases/postgrespool_db.d.ts +1 -1
- package/dist/databases/postgrespool_db.d.ts.map +1 -1
- package/dist/databases/redis_db.d.ts +3 -3
- package/dist/databases/redis_db.d.ts.map +1 -1
- package/dist/databases/rethink_db.d.ts +2 -2
- package/dist/databases/rethink_db.d.ts.map +1 -1
- package/dist/databases/sqlite_db.d.ts +2 -2
- package/dist/databases/sqlite_db.d.ts.map +1 -1
- package/dist/databases/surrealdb_db.d.ts +3 -3
- package/dist/databases/surrealdb_db.d.ts.map +1 -1
- package/dist/{dirty_db-C9zUhXIG.js → dirty_db-CocfsG5w.js} +5 -7
- package/dist/{dirty_git_db-Cj3oEEyZ.js → dirty_git_db-BYnH74Hk.js} +8 -10
- package/dist/{elasticsearch_db-DjnZ1DHG.js → elasticsearch_db-C6G-Wnou.js} +12 -14
- package/dist/index.d.ts +44 -91
- package/dist/index.js +106 -282
- package/dist/lib/AbstractDatabase.d.ts +13 -15
- package/dist/lib/AbstractDatabase.d.ts.map +1 -1
- package/dist/lib/CacheAndBufferLayer.d.ts +95 -77
- package/dist/lib/CacheAndBufferLayer.d.ts.map +1 -1
- package/dist/lib/logging.d.ts +12 -2
- package/dist/lib/logging.d.ts.map +1 -1
- package/dist/logging-DOvFhjsQ.js +22 -0
- package/dist/{memory_db-SlQsh4tk.js → memory_db-CGTi0Ck-.js} +3 -3
- package/dist/{mock_db-BBijMErd.js → mock_db-DCUmGtUq.js} +3 -5
- package/dist/{mongodb_db-ByZscnbs.js → mongodb_db-DgkIuEMZ.js} +5 -6
- package/dist/{mssql_db-C9-gmVgP.js → mssql_db-LiEvv4Sm.js} +19 -22
- package/dist/{mysql_db-CRYtP-Mk.js → mysql_db-48mMBbKZ.js} +7 -10
- package/dist/{postgres_db-CVDQiNwN.js → postgres_db-7Hiezhpi.js} +6 -9
- package/dist/{postgrespool_db-CynysVC1.js → postgrespool_db-CdF-1S34.js} +3 -3
- package/dist/{redis_db-CDma40om.js → redis_db-DKRC8sR9.js} +7 -8
- package/dist/{rethink_db-D7NvPe_4.js → rethink_db-BGxY_SA3.js} +15 -18
- package/dist/{rusty_db-CaVGu0z7.js → rusty_db-Msjxckfm.js} +5 -6
- package/dist/{sqlite_db-B-nmfVje.js → sqlite_db-Cy8OT62U.js} +5 -6
- package/dist/{surrealdb_db-JmcyCabz.js → surrealdb_db-XnhMQEwL.js} +10 -10
- package/package.json +88 -38
- package/dist/chunk-8l464Juk.js +0 -28
- package/dist/logging-DmqnZRde.js +0 -25
package/dist/index.js
CHANGED
|
@@ -1,12 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
__esModule: { value: true },
|
|
4
|
-
[Symbol.toStringTag]: { value: "Module" }
|
|
5
|
-
});
|
|
6
|
-
const require_chunk = require("./chunk-8l464Juk.js");
|
|
7
|
-
const require_logging = require("./logging-DmqnZRde.js");
|
|
8
|
-
let util = require("util");
|
|
9
|
-
util = require_chunk.__toESM(util);
|
|
1
|
+
import { t as normalizeLogger } from "./logging-DOvFhjsQ.js";
|
|
2
|
+
import { promisify } from "node:util";
|
|
10
3
|
//#region lib/CacheAndBufferLayer.ts
|
|
11
4
|
/**
|
|
12
5
|
* 2011 Peter 'Pita' Martischka
|
|
@@ -23,54 +16,32 @@ util = require_chunk.__toESM(util);
|
|
|
23
16
|
* See the License for the specific language governing permissions and
|
|
24
17
|
* limitations under the License.
|
|
25
18
|
*/
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
* module user.
|
|
34
|
-
* * cache reads. A amount of KeyValues are hold in the memory, so that reading is faster.
|
|
35
|
-
* * Buffer DB Writings. Sets and deletes should be buffered to make them in a setted interval
|
|
36
|
-
* with a bulk. This reduces the overhead of database transactions and makes the database
|
|
37
|
-
* faster. But there is also a danger to loose data integrity, to keep that, we should provide a
|
|
38
|
-
* flush function.
|
|
39
|
-
*
|
|
40
|
-
* All Features can be disabled or configured. The Wrapper provides default settings that can be
|
|
41
|
-
* overwriden by the driver and by the module user.
|
|
42
|
-
*/
|
|
43
|
-
/**
|
|
44
|
-
* Cache with Least Recently Used eviction policy.
|
|
45
|
-
*/
|
|
19
|
+
const defaultSettings = {
|
|
20
|
+
bulkLimit: 0,
|
|
21
|
+
cache: 1e4,
|
|
22
|
+
writeInterval: 100,
|
|
23
|
+
json: true,
|
|
24
|
+
charset: "utf8mb4"
|
|
25
|
+
};
|
|
46
26
|
var LRU = class {
|
|
27
|
+
_capacity;
|
|
28
|
+
_evictable;
|
|
29
|
+
_cache;
|
|
47
30
|
/**
|
|
48
31
|
* @param evictable Optional predicate that dictates whether it is permissable to evict the entry
|
|
49
|
-
* if it is old and the cache is over capacity.
|
|
50
|
-
*
|
|
51
|
-
* entries can cause the cache to go over capacity. If the number of non-evictable entries is
|
|
52
|
-
* greater than or equal to the capacity, all new evictable entries will be evicted
|
|
53
|
-
* immediately.
|
|
32
|
+
* if it is old and the cache is over capacity. Warning: Non-evictable entries can cause the
|
|
33
|
+
* cache to go over capacity.
|
|
54
34
|
*/
|
|
55
|
-
constructor(capacity, evictable = (
|
|
35
|
+
constructor(capacity, evictable = () => true) {
|
|
56
36
|
this._capacity = capacity;
|
|
57
37
|
this._evictable = evictable;
|
|
58
38
|
this._cache = /* @__PURE__ */ new Map();
|
|
59
39
|
}
|
|
60
|
-
/**
|
|
61
|
-
* The entries accessed via this iterator are not considered to have been "used" (for purposes of
|
|
62
|
-
* determining least recently used).
|
|
63
|
-
*/
|
|
64
40
|
[Symbol.iterator]() {
|
|
65
41
|
return this._cache.entries();
|
|
66
42
|
}
|
|
67
|
-
/**
|
|
68
|
-
* @param isUse Optional boolean indicating whether this get() should be considered a "use" of the
|
|
69
|
-
* entry (for determining least recently used). Defaults to true.
|
|
70
|
-
* @returns undefined if there is no entry matching the given key.
|
|
71
|
-
*/
|
|
72
43
|
get(k, isUse = true) {
|
|
73
|
-
if (!this._cache.has(k)) return;
|
|
44
|
+
if (!this._cache.has(k)) return void 0;
|
|
74
45
|
const v = this._cache.get(k);
|
|
75
46
|
if (isUse) {
|
|
76
47
|
this._cache.delete(k);
|
|
@@ -78,19 +49,11 @@ var LRU = class {
|
|
|
78
49
|
}
|
|
79
50
|
return v;
|
|
80
51
|
}
|
|
81
|
-
/**
|
|
82
|
-
* Adds or updates an entry in the cache. This marks the entry as the most recently used entry.
|
|
83
|
-
*/
|
|
84
52
|
set(k, v) {
|
|
85
53
|
this._cache.delete(k);
|
|
86
54
|
this._cache.set(k, v);
|
|
87
55
|
this.evictOld();
|
|
88
56
|
}
|
|
89
|
-
/**
|
|
90
|
-
* Evicts the oldest evictable entries until the number of entries is equal to or less than the
|
|
91
|
-
* cache's capacity. This method is automatically called by set(). Call this if you need to evict
|
|
92
|
-
* newly evictable entries before the next call to set().
|
|
93
|
-
*/
|
|
94
57
|
evictOld() {
|
|
95
58
|
for (const [k, v] of this._cache.entries()) {
|
|
96
59
|
if (this._cache.size <= this._capacity) break;
|
|
@@ -100,31 +63,31 @@ var LRU = class {
|
|
|
100
63
|
}
|
|
101
64
|
};
|
|
102
65
|
var SelfContainedPromise = class extends Promise {
|
|
66
|
+
done;
|
|
103
67
|
constructor(executor = null) {
|
|
104
68
|
let done;
|
|
105
69
|
super((resolve, reject) => {
|
|
106
|
-
done = (err
|
|
107
|
-
|
|
70
|
+
done = (err) => err != null ? reject(err) : resolve();
|
|
71
|
+
executor?.(resolve, reject);
|
|
108
72
|
});
|
|
109
73
|
this.done = done;
|
|
110
74
|
}
|
|
111
75
|
};
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
*/
|
|
76
|
+
var Database$1 = class {
|
|
77
|
+
wrappedDB;
|
|
78
|
+
logger;
|
|
79
|
+
settings;
|
|
80
|
+
buffer;
|
|
81
|
+
_flushPaused = null;
|
|
82
|
+
_flushPausedCount = 0;
|
|
83
|
+
_locks = /* @__PURE__ */ new Map();
|
|
84
|
+
_flushDone = null;
|
|
85
|
+
metrics;
|
|
86
|
+
flushInterval;
|
|
124
87
|
constructor(wrappedDB, settings, logger) {
|
|
125
88
|
if (wrappedDB.isAsync) this.wrappedDB = wrappedDB;
|
|
126
89
|
else {
|
|
127
|
-
|
|
90
|
+
const promisified = {};
|
|
128
91
|
for (const fn of [
|
|
129
92
|
"close",
|
|
130
93
|
"doBulk",
|
|
@@ -136,18 +99,17 @@ const Database$1 = class {
|
|
|
136
99
|
]) {
|
|
137
100
|
const f = wrappedDB[fn];
|
|
138
101
|
if (typeof f !== "function") continue;
|
|
139
|
-
|
|
102
|
+
promisified[fn] = promisify(f.bind(wrappedDB));
|
|
140
103
|
}
|
|
104
|
+
this.wrappedDB = promisified;
|
|
141
105
|
}
|
|
142
106
|
this.logger = logger;
|
|
143
107
|
this.settings = Object.freeze({
|
|
144
108
|
...defaultSettings,
|
|
145
|
-
...wrappedDB.settings
|
|
146
|
-
...settings
|
|
109
|
+
...wrappedDB.settings ?? {},
|
|
110
|
+
...settings ?? {}
|
|
147
111
|
});
|
|
148
112
|
this.buffer = new LRU(this.settings.cache, (k, v) => !v.dirty && !v.writingInProgress);
|
|
149
|
-
this._flushPaused = null;
|
|
150
|
-
this._locks = /* @__PURE__ */ new Map();
|
|
151
113
|
this.metrics = {
|
|
152
114
|
lockAwaits: 0,
|
|
153
115
|
lockAcquires: 0,
|
|
@@ -168,7 +130,9 @@ const Database$1 = class {
|
|
|
168
130
|
writesToDbFinished: 0,
|
|
169
131
|
writesToDbRetried: 0
|
|
170
132
|
};
|
|
171
|
-
this.flushInterval = this.settings.writeInterval > 0 ? setInterval(() =>
|
|
133
|
+
this.flushInterval = this.settings.writeInterval > 0 ? setInterval(() => {
|
|
134
|
+
this.flush();
|
|
135
|
+
}, this.settings.writeInterval) : null;
|
|
172
136
|
}
|
|
173
137
|
async _lock(key) {
|
|
174
138
|
while (true) {
|
|
@@ -180,7 +144,7 @@ const Database$1 = class {
|
|
|
180
144
|
++this.metrics.lockAcquires;
|
|
181
145
|
this._locks.set(key, new SelfContainedPromise());
|
|
182
146
|
}
|
|
183
|
-
|
|
147
|
+
_unlock(key) {
|
|
184
148
|
++this.metrics.lockReleases;
|
|
185
149
|
this._locks.get(key).done();
|
|
186
150
|
this._locks.delete(key);
|
|
@@ -188,33 +152,24 @@ const Database$1 = class {
|
|
|
188
152
|
_pauseFlush() {
|
|
189
153
|
if (this._flushPaused == null) {
|
|
190
154
|
this._flushPaused = new SelfContainedPromise();
|
|
191
|
-
this.
|
|
155
|
+
this._flushPausedCount = 0;
|
|
192
156
|
}
|
|
193
|
-
++this.
|
|
157
|
+
++this._flushPausedCount;
|
|
194
158
|
}
|
|
195
159
|
_resumeFlush() {
|
|
196
|
-
if (--this.
|
|
160
|
+
if (--this._flushPausedCount > 0) return;
|
|
197
161
|
this._flushPaused.done();
|
|
198
162
|
this._flushPaused = null;
|
|
199
163
|
}
|
|
200
|
-
/**
|
|
201
|
-
* wraps the init function of the original DB
|
|
202
|
-
*/
|
|
203
164
|
async init() {
|
|
204
165
|
await this.wrappedDB.init();
|
|
205
166
|
}
|
|
206
|
-
/**
|
|
207
|
-
* wraps the close function of the original DB
|
|
208
|
-
*/
|
|
209
167
|
async close() {
|
|
210
|
-
clearInterval(this.flushInterval);
|
|
168
|
+
clearInterval(this.flushInterval ?? void 0);
|
|
211
169
|
await this.flush();
|
|
212
170
|
await this.wrappedDB.close();
|
|
213
171
|
this.wrappedDB = null;
|
|
214
172
|
}
|
|
215
|
-
/**
|
|
216
|
-
* Gets the value trough the wrapper.
|
|
217
|
-
*/
|
|
218
173
|
async get(key) {
|
|
219
174
|
let v;
|
|
220
175
|
await this._lock(key);
|
|
@@ -245,7 +200,7 @@ const Database$1 = class {
|
|
|
245
200
|
++this.metrics.readsFromDbFinished;
|
|
246
201
|
}
|
|
247
202
|
if (this.settings.json) try {
|
|
248
|
-
value = JSON.parse(value);
|
|
203
|
+
value = value != null ? JSON.parse(value) : null;
|
|
249
204
|
} catch (err) {
|
|
250
205
|
this.logger.error(`JSON-PROBLEM:${value}`);
|
|
251
206
|
throw err;
|
|
@@ -264,26 +219,16 @@ const Database$1 = class {
|
|
|
264
219
|
++this.metrics.readsFinished;
|
|
265
220
|
}
|
|
266
221
|
}
|
|
267
|
-
/**
|
|
268
|
-
* Find keys function searches the db sets for matching entries and
|
|
269
|
-
* returns the key entries via callback.
|
|
270
|
-
*/
|
|
271
222
|
async findKeys(key, notKey) {
|
|
272
223
|
await this.flush();
|
|
273
224
|
const keyValues = await this.wrappedDB.findKeys(key, notKey);
|
|
274
225
|
if (this.logger.isDebugEnabled()) this.logger.debug(`GET - ${key}-${notKey} - ${JSON.stringify(keyValues)} - from database `);
|
|
275
226
|
return clone(keyValues);
|
|
276
227
|
}
|
|
277
|
-
/**
|
|
278
|
-
* Remove a record from the database
|
|
279
|
-
*/
|
|
280
228
|
async remove(key) {
|
|
281
229
|
if (this.logger.isDebugEnabled()) this.logger.debug(`DELETE - ${key} - from database `);
|
|
282
230
|
await this.set(key, null);
|
|
283
231
|
}
|
|
284
|
-
/**
|
|
285
|
-
* Sets the value trough the wrapper
|
|
286
|
-
*/
|
|
287
232
|
async set(key, value) {
|
|
288
233
|
value = clone(value);
|
|
289
234
|
let p;
|
|
@@ -304,7 +249,11 @@ const Database$1 = class {
|
|
|
304
249
|
++this.metrics.writes;
|
|
305
250
|
try {
|
|
306
251
|
let entry = this.buffer.get(key);
|
|
307
|
-
if (!entry || entry.writingInProgress) entry = {
|
|
252
|
+
if (!entry || entry.writingInProgress) entry = {
|
|
253
|
+
value: void 0,
|
|
254
|
+
dirty: null,
|
|
255
|
+
writingInProgress: false
|
|
256
|
+
};
|
|
308
257
|
else if (entry.dirty) ++this.metrics.writesObsoleted;
|
|
309
258
|
entry.value = value;
|
|
310
259
|
if (!entry.dirty) entry.dirty = new SelfContainedPromise();
|
|
@@ -320,9 +269,6 @@ const Database$1 = class {
|
|
|
320
269
|
++this.metrics.writesFinished;
|
|
321
270
|
}
|
|
322
271
|
}
|
|
323
|
-
/**
|
|
324
|
-
* Sets a subvalue
|
|
325
|
-
*/
|
|
326
272
|
async setSub(key, sub, value) {
|
|
327
273
|
value = clone(value);
|
|
328
274
|
if (this.logger.isDebugEnabled()) this.logger.debug(`SETSUB - ${key}${JSON.stringify(sub)} - ${JSON.stringify(value)}`);
|
|
@@ -357,18 +303,13 @@ const Database$1 = class {
|
|
|
357
303
|
}
|
|
358
304
|
p = this._setLocked(key, base.fullValue);
|
|
359
305
|
} finally {
|
|
360
|
-
|
|
306
|
+
this._unlock(key);
|
|
361
307
|
}
|
|
362
308
|
} finally {
|
|
363
309
|
this._resumeFlush();
|
|
364
310
|
}
|
|
365
311
|
await p;
|
|
366
312
|
}
|
|
367
|
-
/**
|
|
368
|
-
* Returns a sub value of the object
|
|
369
|
-
* @param sub is a array, for example if you want to access object.test.bla, the array is ["test",
|
|
370
|
-
* "bla"]
|
|
371
|
-
*/
|
|
372
313
|
async getSub(key, sub) {
|
|
373
314
|
await this._lock(key);
|
|
374
315
|
try {
|
|
@@ -384,9 +325,6 @@ const Database$1 = class {
|
|
|
384
325
|
this._unlock(key);
|
|
385
326
|
}
|
|
386
327
|
}
|
|
387
|
-
/**
|
|
388
|
-
* Writes all dirty values to the database
|
|
389
|
-
*/
|
|
390
328
|
async flush() {
|
|
391
329
|
if (this._flushDone == null) this._flushDone = (async () => {
|
|
392
330
|
while (true) {
|
|
@@ -416,18 +354,19 @@ const Database$1 = class {
|
|
|
416
354
|
const ops = [];
|
|
417
355
|
const entries = [];
|
|
418
356
|
for (const [key, entry] of dirtyEntries) {
|
|
419
|
-
let
|
|
357
|
+
let serialized;
|
|
420
358
|
try {
|
|
421
|
-
|
|
359
|
+
if (this.settings.json && entry.value != null) serialized = JSON.stringify(entry.value);
|
|
360
|
+
else serialized = clone(entry.value);
|
|
422
361
|
} catch (err) {
|
|
423
362
|
markDone(entry, err);
|
|
424
363
|
continue;
|
|
425
364
|
}
|
|
426
365
|
entry.writingInProgress = true;
|
|
427
366
|
ops.push({
|
|
428
|
-
type:
|
|
367
|
+
type: serialized == null ? "remove" : "set",
|
|
429
368
|
key,
|
|
430
|
-
value
|
|
369
|
+
value: serialized
|
|
431
370
|
});
|
|
432
371
|
entries.push(entry);
|
|
433
372
|
}
|
|
@@ -436,30 +375,24 @@ const Database$1 = class {
|
|
|
436
375
|
const writeOneOp = async (op, entry) => {
|
|
437
376
|
let writeErr = null;
|
|
438
377
|
try {
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
await this.wrappedDB.remove(op.key);
|
|
442
|
-
break;
|
|
443
|
-
case "set":
|
|
444
|
-
await this.wrappedDB.set(op.key, op.value);
|
|
445
|
-
break;
|
|
446
|
-
default: throw new Error(`unsupported operation type: ${op.type}`);
|
|
447
|
-
}
|
|
378
|
+
if (op.type === "remove") await this.wrappedDB.remove(op.key);
|
|
379
|
+
else await this.wrappedDB.set(op.key, op.value);
|
|
448
380
|
} catch (err) {
|
|
449
|
-
writeErr = err
|
|
381
|
+
writeErr = err instanceof Error ? err : new Error(String(err));
|
|
450
382
|
}
|
|
451
383
|
markDone(entry, writeErr);
|
|
452
384
|
};
|
|
453
385
|
if (ops.length === 1) await writeOneOp(ops[0], entries[0]);
|
|
386
|
+
else if (typeof this.wrappedDB.doBulk !== "function") await Promise.all(ops.map(async (op, i) => writeOneOp(op, entries[i])));
|
|
454
387
|
else {
|
|
455
388
|
let success = false;
|
|
456
389
|
try {
|
|
457
390
|
await this.wrappedDB.doBulk(ops);
|
|
458
391
|
success = true;
|
|
459
392
|
} catch (err) {
|
|
460
|
-
this.logger.error(`Bulk write of ${ops.length} ops failed, retrying individually: ${err.stack
|
|
393
|
+
this.logger.error(`Bulk write of ${ops.length} ops failed, retrying individually: ${err.stack ?? String(err)}`);
|
|
461
394
|
this.metrics.writesToDbRetried += ops.length;
|
|
462
|
-
await Promise.all(ops.map(async (op, i) =>
|
|
395
|
+
await Promise.all(ops.map(async (op, i) => writeOneOp(op, entries[i])));
|
|
463
396
|
}
|
|
464
397
|
if (success) entries.forEach((entry) => markDone(entry, null));
|
|
465
398
|
}
|
|
@@ -467,21 +400,17 @@ const Database$1 = class {
|
|
|
467
400
|
}
|
|
468
401
|
};
|
|
469
402
|
const clone = (obj, key = "") => {
|
|
470
|
-
if (
|
|
403
|
+
if (obj == null || typeof obj !== "object") return obj;
|
|
471
404
|
if (typeof obj.toJSON === "function") return clone(obj.toJSON(key));
|
|
472
405
|
if (obj instanceof Date) {
|
|
473
406
|
const copy = /* @__PURE__ */ new Date();
|
|
474
407
|
copy.setTime(obj.getTime());
|
|
475
408
|
return copy;
|
|
476
409
|
}
|
|
477
|
-
if (obj
|
|
478
|
-
const copy = [];
|
|
479
|
-
for (let i = 0, len = obj.length; i < len; ++i) copy[i] = clone(obj[i], String(i));
|
|
480
|
-
return copy;
|
|
481
|
-
}
|
|
410
|
+
if (Array.isArray(obj)) return obj.map((item, i) => clone(item, String(i)));
|
|
482
411
|
if (obj instanceof Object) {
|
|
483
412
|
const copy = {};
|
|
484
|
-
for (const attr
|
|
413
|
+
for (const attr of Object.keys(obj)) copy[attr] = clone(obj[attr], attr);
|
|
485
414
|
return copy;
|
|
486
415
|
}
|
|
487
416
|
throw new Error("Unable to copy obj! Its type isn't supported.");
|
|
@@ -504,48 +433,18 @@ const clone = (obj, key = "") => {
|
|
|
504
433
|
* See the License for the specific language governing permissions and
|
|
505
434
|
* limitations under the License.
|
|
506
435
|
*/
|
|
507
|
-
const cbDb = {
|
|
508
|
-
init: () => {},
|
|
509
|
-
flush: () => {},
|
|
510
|
-
set: () => {},
|
|
511
|
-
get: () => {},
|
|
512
|
-
remove: () => {},
|
|
513
|
-
findKeys: () => {},
|
|
514
|
-
close: () => {},
|
|
515
|
-
getSub: () => {},
|
|
516
|
-
setSub: () => {}
|
|
517
|
-
};
|
|
518
|
-
for (const fn of [
|
|
519
|
-
"close",
|
|
520
|
-
"findKeys",
|
|
521
|
-
"flush",
|
|
522
|
-
"get",
|
|
523
|
-
"getSub",
|
|
524
|
-
"init",
|
|
525
|
-
"remove",
|
|
526
|
-
"set",
|
|
527
|
-
"setSub"
|
|
528
|
-
]) if (fn in cbDb) cbDb[fn] = (0, util.callbackify)(Database$1.prototype[fn]);
|
|
529
|
-
const makeDoneCallback = (callback, deprecated) => (err) => {
|
|
530
|
-
if (callback) callback(err);
|
|
531
|
-
if (deprecated) deprecated(err);
|
|
532
|
-
if (err != null && callback == null && deprecated == null) throw err;
|
|
533
|
-
};
|
|
534
436
|
var Database = class {
|
|
535
437
|
type;
|
|
536
438
|
dbSettings;
|
|
537
439
|
wrapperSettings;
|
|
538
|
-
|
|
440
|
+
_logger;
|
|
539
441
|
db;
|
|
540
442
|
metrics;
|
|
541
443
|
/**
|
|
542
444
|
* @param type The type of the database
|
|
543
445
|
* @param dbSettings The settings for that specific database type
|
|
544
|
-
* @param wrapperSettings
|
|
545
|
-
* @param logger Optional logger
|
|
546
|
-
* The logger object is expected to be a log4js logger object or `console`. A logger object
|
|
547
|
-
* from another logging library should also work, but performance may be reduced if the logger
|
|
548
|
-
* object does not have is${Level}Enabled() methods (isDebugEnabled(), etc.).
|
|
446
|
+
* @param wrapperSettings Cache/buffer layer settings (cache size, write interval, etc.)
|
|
447
|
+
* @param logger Optional logger (log4js, console, or any object with debug/info/warn/error methods)
|
|
549
448
|
*/
|
|
550
449
|
constructor(type, dbSettings, wrapperSettings, logger = null) {
|
|
551
450
|
if (!type) {
|
|
@@ -555,141 +454,66 @@ var Database = class {
|
|
|
555
454
|
}
|
|
556
455
|
this.type = type;
|
|
557
456
|
this.dbSettings = dbSettings;
|
|
558
|
-
this.wrapperSettings = wrapperSettings;
|
|
559
|
-
this.
|
|
457
|
+
this.wrapperSettings = wrapperSettings ?? null;
|
|
458
|
+
this._logger = normalizeLogger(logger);
|
|
560
459
|
}
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
this.db = new Database$1(db, this.wrapperSettings, this.logger);
|
|
568
|
-
this.metrics = this.db.metrics;
|
|
569
|
-
return this.db.init();
|
|
570
|
-
});
|
|
571
|
-
if (callback != null) return cbDb.init.call({ init: () => p });
|
|
572
|
-
return p;
|
|
460
|
+
async init() {
|
|
461
|
+
const db = await this.initDB();
|
|
462
|
+
db.logger = this._logger;
|
|
463
|
+
this.db = new Database$1(db, this.wrapperSettings, this._logger);
|
|
464
|
+
this.metrics = this.db.metrics;
|
|
465
|
+
await this.db.init();
|
|
573
466
|
}
|
|
574
467
|
async initDB() {
|
|
575
468
|
switch (this.type) {
|
|
576
|
-
case "mysql": return new (await (
|
|
577
|
-
case "postgres": return new (await (
|
|
578
|
-
case "sqlite": return new (await (
|
|
579
|
-
case "rustydb": return new (await (
|
|
580
|
-
case "mongodb": return new (await (
|
|
581
|
-
case "redis": return new (await (
|
|
582
|
-
case "cassandra": return new (await (
|
|
583
|
-
case "dirty": return new (await (
|
|
584
|
-
case "dirtygit": return new (await (
|
|
585
|
-
case "elasticsearch": return new (await (
|
|
586
|
-
case "memory": return new (await (
|
|
587
|
-
case "mock": return new (await (
|
|
588
|
-
case "mssql": return new (await (
|
|
589
|
-
case "postgrespool": return new (await (
|
|
590
|
-
case "rethink": return new (await (
|
|
591
|
-
case "couch": return new (await (
|
|
592
|
-
case "surrealdb": return new (await (
|
|
593
|
-
default: throw new Error(
|
|
469
|
+
case "mysql": return new (await (import("./mysql_db-48mMBbKZ.js"))).default(this.dbSettings);
|
|
470
|
+
case "postgres": return new (await (import("./postgres_db-7Hiezhpi.js"))).default(this.dbSettings);
|
|
471
|
+
case "sqlite": return new (await (import("./sqlite_db-Cy8OT62U.js"))).default(this.dbSettings);
|
|
472
|
+
case "rustydb": return new (await (import("./rusty_db-Msjxckfm.js"))).default(this.dbSettings);
|
|
473
|
+
case "mongodb": return new (await (import("./mongodb_db-DgkIuEMZ.js"))).default(this.dbSettings);
|
|
474
|
+
case "redis": return new (await (import("./redis_db-DKRC8sR9.js"))).default(this.dbSettings);
|
|
475
|
+
case "cassandra": return new (await (import("./cassandra_db-B_5ANRzM.js"))).default(this.dbSettings);
|
|
476
|
+
case "dirty": return new (await (import("./dirty_db-CocfsG5w.js"))).default(this.dbSettings);
|
|
477
|
+
case "dirtygit": return new (await (import("./dirty_git_db-BYnH74Hk.js"))).default(this.dbSettings);
|
|
478
|
+
case "elasticsearch": return new (await (import("./elasticsearch_db-C6G-Wnou.js"))).default(this.dbSettings);
|
|
479
|
+
case "memory": return new (await (import("./memory_db-CGTi0Ck-.js"))).default(this.dbSettings);
|
|
480
|
+
case "mock": return new (await (import("./mock_db-DCUmGtUq.js"))).default(this.dbSettings);
|
|
481
|
+
case "mssql": return new (await (import("./mssql_db-LiEvv4Sm.js"))).default(this.dbSettings);
|
|
482
|
+
case "postgrespool": return new (await (import("./postgrespool_db-CdF-1S34.js"))).default(this.dbSettings);
|
|
483
|
+
case "rethink": return new (await (import("./rethink_db-BGxY_SA3.js"))).default(this.dbSettings);
|
|
484
|
+
case "couch": return new (await (import("./couch_db-BXyzauwJ.js"))).default(this.dbSettings);
|
|
485
|
+
case "surrealdb": return new (await (import("./surrealdb_db-XnhMQEwL.js"))).default(this.dbSettings);
|
|
486
|
+
default: throw new Error(`Invalid database type: ${this.type}`);
|
|
594
487
|
}
|
|
595
488
|
}
|
|
596
|
-
|
|
597
|
-
* Wrapper functions
|
|
598
|
-
*/
|
|
599
|
-
/**
|
|
600
|
-
* Deprecated synonym of flush().
|
|
601
|
-
*
|
|
602
|
-
* @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
|
|
603
|
-
*/
|
|
604
|
-
doShutdown(callback = null) {
|
|
605
|
-
return this.flush(callback);
|
|
606
|
-
}
|
|
607
|
-
/**
|
|
608
|
-
* Writes any unsaved changes to the underlying database.
|
|
609
|
-
*
|
|
610
|
-
* @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
|
|
611
|
-
*/
|
|
612
|
-
flush(callback = null) {
|
|
613
|
-
if (!cbDb || !cbDb.flush === void 0) return null;
|
|
614
|
-
if (callback != null) return cbDb.flush.call(this.db, callback);
|
|
489
|
+
async flush() {
|
|
615
490
|
return this.db.flush();
|
|
616
491
|
}
|
|
617
|
-
/**
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
get(key
|
|
622
|
-
if (callback != null) return cbDb.get.call(this.db, key, callback);
|
|
492
|
+
/** @deprecated Use flush() */
|
|
493
|
+
doShutdown() {
|
|
494
|
+
return this.flush();
|
|
495
|
+
}
|
|
496
|
+
async get(key) {
|
|
623
497
|
return this.db.get(key);
|
|
624
498
|
}
|
|
625
|
-
|
|
626
|
-
* @param key
|
|
627
|
-
* @param notKey
|
|
628
|
-
* @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
|
|
629
|
-
*/
|
|
630
|
-
findKeys(key, notKey, callback = null) {
|
|
631
|
-
if (callback != null) return cbDb.findKeys.call(this.db, key, notKey, callback);
|
|
499
|
+
async findKeys(key, notKey) {
|
|
632
500
|
return this.db.findKeys(key, notKey);
|
|
633
501
|
}
|
|
634
|
-
|
|
635
|
-
* Removes an entry from the database if present.
|
|
636
|
-
*
|
|
637
|
-
* @param key
|
|
638
|
-
* @param cb Deprecated. Node-style callback. Called when the write has been committed to the
|
|
639
|
-
* underlying database driver. If null, a Promise is returned.
|
|
640
|
-
* @param deprecated Deprecated callback that is called just after cb. Ignored if cb is null.
|
|
641
|
-
*/
|
|
642
|
-
remove(key, cb = null, deprecated = null) {
|
|
643
|
-
if (cb != null) return cbDb.remove.call(this.db, key, makeDoneCallback(cb, deprecated));
|
|
502
|
+
async remove(key) {
|
|
644
503
|
return this.db.remove(key);
|
|
645
504
|
}
|
|
646
|
-
|
|
647
|
-
* Adds or changes the value of an entry.
|
|
648
|
-
*
|
|
649
|
-
* @param key
|
|
650
|
-
* @param value
|
|
651
|
-
* @param cb Deprecated. Node-style callback. Called when the write has been committed to the
|
|
652
|
-
* underlying database driver. If null, a Promise is returned.
|
|
653
|
-
* @param deprecated Deprecated callback that is called just after cb. Ignored if cb is null.
|
|
654
|
-
*/
|
|
655
|
-
set(key, value, cb = null, deprecated = null) {
|
|
656
|
-
if (cb != null) return cbDb.set.call(this.db, key, value, makeDoneCallback(cb, deprecated));
|
|
505
|
+
async set(key, value) {
|
|
657
506
|
return this.db.set(key, value);
|
|
658
507
|
}
|
|
659
|
-
|
|
660
|
-
* @param key
|
|
661
|
-
* @param sub
|
|
662
|
-
* @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
|
|
663
|
-
*/
|
|
664
|
-
getSub(key, sub, callback = null) {
|
|
665
|
-
if (callback != null) return cbDb.getSub.call(this.db, key, sub, callback);
|
|
508
|
+
async getSub(key, sub) {
|
|
666
509
|
return this.db.getSub(key, sub);
|
|
667
510
|
}
|
|
668
|
-
|
|
669
|
-
* Adds or changes a subvalue of an entry.
|
|
670
|
-
*
|
|
671
|
-
* @param key
|
|
672
|
-
* @param sub
|
|
673
|
-
* @param value
|
|
674
|
-
* @param cb Deprecated. Node-style callback. Called when the write has been committed to the
|
|
675
|
-
* underlying database driver. If null, a Promise is returned.
|
|
676
|
-
* @param deprecated Deprecated callback that is called just after cb. Ignored if cb is null.
|
|
677
|
-
*/
|
|
678
|
-
setSub(key, sub, value, cb = null, deprecated = null) {
|
|
679
|
-
if (cb != null) return cbDb.setSub.call(this.db, key, sub, value, makeDoneCallback(cb, deprecated));
|
|
511
|
+
async setSub(key, sub, value) {
|
|
680
512
|
return this.db.setSub(key, sub, value);
|
|
681
513
|
}
|
|
682
|
-
|
|
683
|
-
* Flushes unwritten changes then closes the connection to the underlying database. After this
|
|
684
|
-
* returns, any future call to a method on this object may result in an error.
|
|
685
|
-
*
|
|
686
|
-
* @param callback - Deprecated. Node-style callback. If null, a Promise is returned.
|
|
687
|
-
*/
|
|
688
|
-
close(callback = null) {
|
|
689
|
-
if (callback != null) return cbDb.close.call(this.db, callback);
|
|
514
|
+
async close() {
|
|
690
515
|
return this.db.close();
|
|
691
516
|
}
|
|
692
517
|
};
|
|
693
518
|
//#endregion
|
|
694
|
-
|
|
695
|
-
exports.default = Database;
|
|
519
|
+
export { Database, Database as default };
|