@naturalcycles/db-lib 10.14.0 → 10.15.1
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.
|
@@ -346,8 +346,9 @@ export class CommonDao {
|
|
|
346
346
|
const stream = this.cfg.db.streamQuery(q, opt);
|
|
347
347
|
if (partialQuery)
|
|
348
348
|
return stream;
|
|
349
|
-
return stream
|
|
350
|
-
|
|
349
|
+
return (stream
|
|
350
|
+
// the commented out line was causing RangeError: Maximum call stack size exceeded
|
|
351
|
+
// .on('error', err => stream.emit('error', err))
|
|
351
352
|
.pipe(transformMap(async (dbm) => {
|
|
352
353
|
if (this.cfg.hooks.afterLoad) {
|
|
353
354
|
dbm = (await this.cfg.hooks.afterLoad(dbm));
|
|
@@ -357,7 +358,7 @@ export class CommonDao {
|
|
|
357
358
|
return this.anyToDBM(dbm, opt);
|
|
358
359
|
}, {
|
|
359
360
|
errorMode: opt.errorMode,
|
|
360
|
-
}));
|
|
361
|
+
})));
|
|
361
362
|
}
|
|
362
363
|
/**
|
|
363
364
|
* Stream as Readable, to be able to .pipe() it further with support of backpressure.
|
|
@@ -392,7 +393,8 @@ export class CommonDao {
|
|
|
392
393
|
// optimization: 1 validation is enough
|
|
393
394
|
// .pipe(transformMap<any, DBM>(dbm => this.anyToDBM(dbm, opt), safeOpt))
|
|
394
395
|
// .pipe(transformMap<DBM, BM>(dbm => this.dbmToBM(dbm, opt), safeOpt))
|
|
395
|
-
|
|
396
|
+
// the commented out line was causing RangeError: Maximum call stack size exceeded
|
|
397
|
+
// .on('error', err => stream.emit('error', err))
|
|
396
398
|
.pipe(transformMap(async (dbm) => {
|
|
397
399
|
if (this.cfg.hooks.afterLoad) {
|
|
398
400
|
dbm = (await this.cfg.hooks.afterLoad(dbm));
|
|
@@ -77,7 +77,7 @@ export class InMemoryDB {
|
|
|
77
77
|
async saveBatch(_table, rows, opt = {}) {
|
|
78
78
|
const table = this.cfg.tablesPrefix + _table;
|
|
79
79
|
this.data[table] ||= {};
|
|
80
|
-
|
|
80
|
+
for (const r of rows) {
|
|
81
81
|
if (!r.id) {
|
|
82
82
|
this.cfg.logger.warn({ rows });
|
|
83
83
|
throw new Error(`InMemoryDB doesn't support id auto-generation in saveBatch, row without id was given`);
|
|
@@ -92,7 +92,7 @@ export class InMemoryDB {
|
|
|
92
92
|
// 1. Not store values "by reference" (avoid mutation bugs)
|
|
93
93
|
// 2. Simulate real DB that would do something like that in a transport layer anyway
|
|
94
94
|
this.data[table][r.id] = JSON.parse(JSON.stringify(r), bufferReviver);
|
|
95
|
-
}
|
|
95
|
+
}
|
|
96
96
|
}
|
|
97
97
|
async deleteByQuery(q, _opt) {
|
|
98
98
|
const table = this.cfg.tablesPrefix + q.table;
|
|
@@ -106,12 +106,12 @@ export class InMemoryDB {
|
|
|
106
106
|
if (!this.data[table])
|
|
107
107
|
return 0;
|
|
108
108
|
let count = 0;
|
|
109
|
-
|
|
109
|
+
for (const id of ids) {
|
|
110
110
|
if (!this.data[table][id])
|
|
111
|
-
|
|
111
|
+
continue;
|
|
112
112
|
delete this.data[table][id];
|
|
113
113
|
count++;
|
|
114
|
-
}
|
|
114
|
+
}
|
|
115
115
|
return count;
|
|
116
116
|
}
|
|
117
117
|
async patchByQuery(q, patch) {
|
|
@@ -119,7 +119,9 @@ export class InMemoryDB {
|
|
|
119
119
|
return 0;
|
|
120
120
|
const table = this.cfg.tablesPrefix + q.table;
|
|
121
121
|
const rows = queryInMemory(q, Object.values(this.data[table] || {}));
|
|
122
|
-
|
|
122
|
+
for (const row of rows) {
|
|
123
|
+
Object.assign(row, patch);
|
|
124
|
+
}
|
|
123
125
|
return rows.length;
|
|
124
126
|
}
|
|
125
127
|
async runQuery(q, _opt) {
|
|
@@ -14,7 +14,9 @@ export class InMemoryKeyValueDB {
|
|
|
14
14
|
async createTable(_table, _opt) { }
|
|
15
15
|
async deleteByIds(table, ids) {
|
|
16
16
|
this.data[table] ||= {};
|
|
17
|
-
|
|
17
|
+
for (const id of ids) {
|
|
18
|
+
delete this.data[table][id];
|
|
19
|
+
}
|
|
18
20
|
}
|
|
19
21
|
async getByIds(table, ids) {
|
|
20
22
|
this.data[table] ||= {};
|
|
@@ -22,7 +24,9 @@ export class InMemoryKeyValueDB {
|
|
|
22
24
|
}
|
|
23
25
|
async saveBatch(table, entries) {
|
|
24
26
|
this.data[table] ||= {};
|
|
25
|
-
|
|
27
|
+
for (const [id, v] of entries) {
|
|
28
|
+
this.data[table][id] = v;
|
|
29
|
+
}
|
|
26
30
|
}
|
|
27
31
|
streamIds(table, limit) {
|
|
28
32
|
return Readable.from(Object.keys(this.data[table] || {}).slice(0, limit));
|
package/package.json
CHANGED
|
@@ -457,23 +457,26 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, I
|
|
|
457
457
|
const stream = this.cfg.db.streamQuery<DBM>(q, opt)
|
|
458
458
|
if (partialQuery) return stream
|
|
459
459
|
|
|
460
|
-
return
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
if (
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
460
|
+
return (
|
|
461
|
+
stream
|
|
462
|
+
// the commented out line was causing RangeError: Maximum call stack size exceeded
|
|
463
|
+
// .on('error', err => stream.emit('error', err))
|
|
464
|
+
.pipe(
|
|
465
|
+
transformMap<any, DBM>(
|
|
466
|
+
async dbm => {
|
|
467
|
+
if (this.cfg.hooks!.afterLoad) {
|
|
468
|
+
dbm = (await this.cfg.hooks!.afterLoad(dbm))!
|
|
469
|
+
if (dbm === null) return SKIP
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
return this.anyToDBM(dbm, opt)
|
|
473
|
+
},
|
|
474
|
+
{
|
|
475
|
+
errorMode: opt.errorMode,
|
|
476
|
+
},
|
|
477
|
+
),
|
|
478
|
+
)
|
|
479
|
+
)
|
|
477
480
|
}
|
|
478
481
|
|
|
479
482
|
/**
|
|
@@ -512,7 +515,8 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, I
|
|
|
512
515
|
// optimization: 1 validation is enough
|
|
513
516
|
// .pipe(transformMap<any, DBM>(dbm => this.anyToDBM(dbm, opt), safeOpt))
|
|
514
517
|
// .pipe(transformMap<DBM, BM>(dbm => this.dbmToBM(dbm, opt), safeOpt))
|
|
515
|
-
|
|
518
|
+
// the commented out line was causing RangeError: Maximum call stack size exceeded
|
|
519
|
+
// .on('error', err => stream.emit('error', err))
|
|
516
520
|
.pipe(
|
|
517
521
|
transformMap<DBM, BM>(
|
|
518
522
|
async dbm => {
|
|
@@ -153,7 +153,7 @@ export class InMemoryDB implements CommonDB {
|
|
|
153
153
|
const table = this.cfg.tablesPrefix + _table
|
|
154
154
|
this.data[table] ||= {}
|
|
155
155
|
|
|
156
|
-
|
|
156
|
+
for (const r of rows) {
|
|
157
157
|
if (!r.id) {
|
|
158
158
|
this.cfg.logger!.warn({ rows })
|
|
159
159
|
throw new Error(
|
|
@@ -161,19 +161,19 @@ export class InMemoryDB implements CommonDB {
|
|
|
161
161
|
)
|
|
162
162
|
}
|
|
163
163
|
|
|
164
|
-
if (opt.saveMethod === 'insert' && this.data[table]
|
|
164
|
+
if (opt.saveMethod === 'insert' && this.data[table][r.id]) {
|
|
165
165
|
throw new Error(`InMemoryDB: INSERT failed, entity exists: ${table}.${r.id}`)
|
|
166
166
|
}
|
|
167
167
|
|
|
168
|
-
if (opt.saveMethod === 'update' && !this.data[table]
|
|
168
|
+
if (opt.saveMethod === 'update' && !this.data[table][r.id]) {
|
|
169
169
|
throw new Error(`InMemoryDB: UPDATE failed, entity doesn't exist: ${table}.${r.id}`)
|
|
170
170
|
}
|
|
171
171
|
|
|
172
172
|
// JSON parse/stringify (deep clone) is to:
|
|
173
173
|
// 1. Not store values "by reference" (avoid mutation bugs)
|
|
174
174
|
// 2. Simulate real DB that would do something like that in a transport layer anyway
|
|
175
|
-
this.data[table]
|
|
176
|
-
}
|
|
175
|
+
this.data[table][r.id] = JSON.parse(JSON.stringify(r), bufferReviver)
|
|
176
|
+
}
|
|
177
177
|
}
|
|
178
178
|
|
|
179
179
|
async deleteByQuery<ROW extends ObjectWithId>(
|
|
@@ -191,11 +191,11 @@ export class InMemoryDB implements CommonDB {
|
|
|
191
191
|
if (!this.data[table]) return 0
|
|
192
192
|
|
|
193
193
|
let count = 0
|
|
194
|
-
|
|
195
|
-
if (!this.data[table]
|
|
196
|
-
delete this.data[table]
|
|
194
|
+
for (const id of ids) {
|
|
195
|
+
if (!this.data[table][id]) continue
|
|
196
|
+
delete this.data[table][id]
|
|
197
197
|
count++
|
|
198
|
-
}
|
|
198
|
+
}
|
|
199
199
|
|
|
200
200
|
return count
|
|
201
201
|
}
|
|
@@ -207,7 +207,9 @@ export class InMemoryDB implements CommonDB {
|
|
|
207
207
|
if (_isEmptyObject(patch)) return 0
|
|
208
208
|
const table = this.cfg.tablesPrefix + q.table
|
|
209
209
|
const rows = queryInMemory(q, Object.values(this.data[table] || {}) as ROW[])
|
|
210
|
-
|
|
210
|
+
for (const row of rows) {
|
|
211
|
+
Object.assign(row, patch)
|
|
212
|
+
}
|
|
211
213
|
return rows.length
|
|
212
214
|
}
|
|
213
215
|
|
|
@@ -23,7 +23,9 @@ export class InMemoryKeyValueDB implements CommonKeyValueDB {
|
|
|
23
23
|
|
|
24
24
|
async deleteByIds(table: string, ids: string[]): Promise<void> {
|
|
25
25
|
this.data[table] ||= {}
|
|
26
|
-
|
|
26
|
+
for (const id of ids) {
|
|
27
|
+
delete this.data[table][id]
|
|
28
|
+
}
|
|
27
29
|
}
|
|
28
30
|
|
|
29
31
|
async getByIds(table: string, ids: string[]): Promise<KeyValueDBTuple[]> {
|
|
@@ -33,7 +35,9 @@ export class InMemoryKeyValueDB implements CommonKeyValueDB {
|
|
|
33
35
|
|
|
34
36
|
async saveBatch(table: string, entries: KeyValueDBTuple[]): Promise<void> {
|
|
35
37
|
this.data[table] ||= {}
|
|
36
|
-
|
|
38
|
+
for (const [id, v] of entries) {
|
|
39
|
+
this.data[table][id] = v
|
|
40
|
+
}
|
|
37
41
|
}
|
|
38
42
|
|
|
39
43
|
streamIds(table: string, limit?: number): ReadableTyped<string> {
|