@naturalcycles/db-lib 10.54.0 → 10.55.0
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/commondao/common.dao.d.ts +3 -0
- package/dist/commondb/common.db.d.ts +2 -2
- package/dist/inmemory/inMemory.db.js +34 -7
- package/package.json +3 -3
- package/src/adapter/cachedb/cache.db.ts +1 -1
- package/src/adapter/file/file.db.ts +1 -1
- package/src/commondao/common.dao.ts +3 -3
- package/src/commondb/common.db.ts +2 -2
- package/src/inmemory/inMemory.db.ts +39 -8
|
@@ -162,6 +162,9 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
|
|
|
162
162
|
* Converts multiple storage rows to DBMs.
|
|
163
163
|
*/
|
|
164
164
|
storageRowsToDBM(rows: ObjectWithId[]): DBM[];
|
|
165
|
+
/**
|
|
166
|
+
* Mutates `dbm`.
|
|
167
|
+
*/
|
|
165
168
|
private compress;
|
|
166
169
|
/**
|
|
167
170
|
* Mutates `dbm`.
|
|
@@ -165,8 +165,8 @@ export interface CommonDB {
|
|
|
165
165
|
incrementBatch: (table: string, prop: string, incrementMap: StringMap<number>, opt?: CommonDBOptions) => Promise<StringMap<number>>;
|
|
166
166
|
}
|
|
167
167
|
export declare enum CommonDBType {
|
|
168
|
-
|
|
169
|
-
|
|
168
|
+
document = "document",
|
|
169
|
+
relational = "relational"
|
|
170
170
|
}
|
|
171
171
|
/**
|
|
172
172
|
* Manifest of supported features.
|
|
@@ -4,7 +4,6 @@ import { _deepCopy, _sortObjectDeep } from '@naturalcycles/js-lib/object';
|
|
|
4
4
|
import { _stringMapEntries, _stringMapValues } from '@naturalcycles/js-lib/types';
|
|
5
5
|
import { generateJsonSchemaFromData } from '@naturalcycles/nodejs-lib/ajv';
|
|
6
6
|
import { Pipeline } from '@naturalcycles/nodejs-lib/stream';
|
|
7
|
-
import { bufferReviver } from '@naturalcycles/nodejs-lib/stream/ndjson/transformJsonParse.js';
|
|
8
7
|
import { commonDBFullSupport, CommonDBType } from '../commondb/common.db.js';
|
|
9
8
|
import { queryInMemory } from './queryInMemory.js';
|
|
10
9
|
export class InMemoryDB {
|
|
@@ -98,10 +97,7 @@ export class InMemoryDB {
|
|
|
98
97
|
if (isUpdate && !this.data[table][r.id]) {
|
|
99
98
|
throw new Error(`InMemoryDB: UPDATE failed, entity doesn't exist: ${table}.${r.id}`);
|
|
100
99
|
}
|
|
101
|
-
|
|
102
|
-
// 1. Not store values "by reference" (avoid mutation bugs)
|
|
103
|
-
// 2. Simulate real DB that would do something like that in a transport layer anyway
|
|
104
|
-
this.data[table][r.id] = JSON.parse(JSON.stringify(r), bufferReviver);
|
|
100
|
+
this.data[table][r.id] = deepCloneRow(r);
|
|
105
101
|
}
|
|
106
102
|
}
|
|
107
103
|
async multiSave(map, opt = {}) {
|
|
@@ -111,8 +107,8 @@ export class InMemoryDB {
|
|
|
111
107
|
}
|
|
112
108
|
async patchById(_table, id, patch, _opt) {
|
|
113
109
|
const table = this.cfg.tablesPrefix + _table;
|
|
114
|
-
_assert(
|
|
115
|
-
Object.assign(this.data[table][id], patch);
|
|
110
|
+
_assert(this.data[table]?.[id], `InMemoryDB: patchById failed, entity doesn't exist: ${table}.${id}`);
|
|
111
|
+
Object.assign(this.data[table][id], deepCloneRow(patch));
|
|
116
112
|
}
|
|
117
113
|
async deleteByQuery(q, _opt) {
|
|
118
114
|
const table = this.cfg.tablesPrefix + q.table;
|
|
@@ -261,3 +257,34 @@ export class InMemoryDBTransaction {
|
|
|
261
257
|
this.ops = [];
|
|
262
258
|
}
|
|
263
259
|
}
|
|
260
|
+
/**
|
|
261
|
+
* JSON parse/stringify (deep clone) is to:
|
|
262
|
+
* 1. Not store values "by reference" (avoid mutation bugs)
|
|
263
|
+
* 2. Simulate real DB that would do something like that in a transport layer anyway
|
|
264
|
+
*
|
|
265
|
+
* Buffers (e.g CommonDao `__compressed`) need special treatment: passing them through JSON
|
|
266
|
+
* (Buffer.toJSON => `{type: 'Buffer', data: [...bytes]}` => reviver)
|
|
267
|
+
* turns every byte into a JSON number and a reviver call — ~90ms for a single 300KB buffer.
|
|
268
|
+
* Instead, top-level Buffer fields are excluded from the JSON round-trip and copied directly.
|
|
269
|
+
*
|
|
270
|
+
* Known accepted limitation: only top-level Buffer fields are preserved as Buffers.
|
|
271
|
+
* A Buffer nested deeper goes through the JSON round-trip and is stored in its
|
|
272
|
+
* (slow to serialize) json form `{type: 'Buffer', data: [...bytes]}` — same as a real
|
|
273
|
+
* JSON transport layer without a reviver would do.
|
|
274
|
+
*/
|
|
275
|
+
function deepCloneRow(row) {
|
|
276
|
+
let bufferKeys;
|
|
277
|
+
for (const key of Object.keys(row)) {
|
|
278
|
+
if (Buffer.isBuffer(row[key]))
|
|
279
|
+
(bufferKeys ||= []).push(key);
|
|
280
|
+
}
|
|
281
|
+
if (!bufferKeys)
|
|
282
|
+
return JSON.parse(JSON.stringify(row));
|
|
283
|
+
const src = { ...row };
|
|
284
|
+
for (const key of bufferKeys)
|
|
285
|
+
src[key] = undefined; // JSON.stringify omits undefined values
|
|
286
|
+
const clone = JSON.parse(JSON.stringify(src));
|
|
287
|
+
for (const key of bufferKeys)
|
|
288
|
+
clone[key] = Buffer.from(row[key]);
|
|
289
|
+
return clone;
|
|
290
|
+
}
|
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naturalcycles/db-lib",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "10.
|
|
4
|
+
"version": "10.55.0",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@naturalcycles/js-lib": "^15",
|
|
7
7
|
"@naturalcycles/nodejs-lib": "^15"
|
|
8
8
|
},
|
|
9
9
|
"devDependencies": {
|
|
10
|
-
"
|
|
11
|
-
"@naturalcycles/dev-lib": "
|
|
10
|
+
"typescript": "^7",
|
|
11
|
+
"@naturalcycles/dev-lib": "0.0.0"
|
|
12
12
|
},
|
|
13
13
|
"files": [
|
|
14
14
|
"dist",
|
|
@@ -161,7 +161,7 @@ export class CacheDB extends BaseCommonDB implements CommonDB {
|
|
|
161
161
|
|
|
162
162
|
// Don't save to cache if it was a projection query
|
|
163
163
|
if (!opt.skipCache && !this.cfg.skipCache && !q._selectedFieldNames) {
|
|
164
|
-
const cacheResult = this.cfg.cacheDB.saveBatch(q.table, rows
|
|
164
|
+
const cacheResult = this.cfg.cacheDB.saveBatch(q.table, rows, opt)
|
|
165
165
|
if (this.cfg.awaitCache) await cacheResult
|
|
166
166
|
}
|
|
167
167
|
return { rows, ...queryResult }
|
|
@@ -79,7 +79,7 @@ export class CommonDao<
|
|
|
79
79
|
...cfg,
|
|
80
80
|
hooks: {
|
|
81
81
|
parseNaturalId: () => ({}),
|
|
82
|
-
beforeCreate: bm => bm
|
|
82
|
+
beforeCreate: bm => bm,
|
|
83
83
|
onValidationError: err => err,
|
|
84
84
|
...cfg.hooks,
|
|
85
85
|
} satisfies Partial<CommonDaoHooks<BM, DBM, ID>>,
|
|
@@ -922,7 +922,7 @@ export class CommonDao<
|
|
|
922
922
|
|
|
923
923
|
const inputName = opt.table || this.cfg.table
|
|
924
924
|
|
|
925
|
-
const [error, convertedValue] = this.cfg.validateBM(input
|
|
925
|
+
const [error, convertedValue] = this.cfg.validateBM(input, {
|
|
926
926
|
// Passing `mutateInput` through allows to opt-out of mutation
|
|
927
927
|
// for individual operations, e.g `someDao.save(myObj, { mutateInput: false })`
|
|
928
928
|
// Default is undefined (the validation function decides whether to mutate or not).
|
|
@@ -1083,7 +1083,7 @@ export class CommonDao<
|
|
|
1083
1083
|
}
|
|
1084
1084
|
}
|
|
1085
1085
|
|
|
1086
|
-
return bmsByProp
|
|
1086
|
+
return bmsByProp
|
|
1087
1087
|
}
|
|
1088
1088
|
|
|
1089
1089
|
/**
|
|
@@ -3,11 +3,15 @@ import { _assert } from '@naturalcycles/js-lib/error/assert.js'
|
|
|
3
3
|
import type { CommonLogger } from '@naturalcycles/js-lib/log'
|
|
4
4
|
import { _deepCopy, _sortObjectDeep } from '@naturalcycles/js-lib/object'
|
|
5
5
|
import { _stringMapEntries, _stringMapValues } from '@naturalcycles/js-lib/types'
|
|
6
|
-
import type {
|
|
6
|
+
import type {
|
|
7
|
+
AnyObject,
|
|
8
|
+
AnyObjectWithId,
|
|
9
|
+
ObjectWithId,
|
|
10
|
+
StringMap,
|
|
11
|
+
} from '@naturalcycles/js-lib/types'
|
|
7
12
|
import type { JsonSchema } from '@naturalcycles/nodejs-lib/ajv'
|
|
8
13
|
import { generateJsonSchemaFromData } from '@naturalcycles/nodejs-lib/ajv'
|
|
9
14
|
import { Pipeline } from '@naturalcycles/nodejs-lib/stream'
|
|
10
|
-
import { bufferReviver } from '@naturalcycles/nodejs-lib/stream/ndjson/transformJsonParse.js'
|
|
11
15
|
import type { CommonDB, CommonDBSupport } from '../commondb/common.db.js'
|
|
12
16
|
import { commonDBFullSupport, CommonDBType } from '../commondb/common.db.js'
|
|
13
17
|
import type {
|
|
@@ -174,10 +178,7 @@ export class InMemoryDB implements CommonDB {
|
|
|
174
178
|
throw new Error(`InMemoryDB: UPDATE failed, entity doesn't exist: ${table}.${r.id}`)
|
|
175
179
|
}
|
|
176
180
|
|
|
177
|
-
|
|
178
|
-
// 1. Not store values "by reference" (avoid mutation bugs)
|
|
179
|
-
// 2. Simulate real DB that would do something like that in a transport layer anyway
|
|
180
|
-
this.data[table][r.id] = JSON.parse(JSON.stringify(r), bufferReviver)
|
|
181
|
+
this.data[table][r.id] = deepCloneRow(r)
|
|
181
182
|
}
|
|
182
183
|
}
|
|
183
184
|
|
|
@@ -198,10 +199,10 @@ export class InMemoryDB implements CommonDB {
|
|
|
198
199
|
): Promise<void> {
|
|
199
200
|
const table = this.cfg.tablesPrefix + _table
|
|
200
201
|
_assert(
|
|
201
|
-
|
|
202
|
+
this.data[table]?.[id],
|
|
202
203
|
`InMemoryDB: patchById failed, entity doesn't exist: ${table}.${id}`,
|
|
203
204
|
)
|
|
204
|
-
Object.assign(this.data[table]
|
|
205
|
+
Object.assign(this.data[table][id], deepCloneRow(patch))
|
|
205
206
|
}
|
|
206
207
|
|
|
207
208
|
async deleteByQuery<ROW extends ObjectWithId>(
|
|
@@ -408,3 +409,33 @@ export class InMemoryDBTransaction implements DBTransaction {
|
|
|
408
409
|
this.ops = []
|
|
409
410
|
}
|
|
410
411
|
}
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* JSON parse/stringify (deep clone) is to:
|
|
415
|
+
* 1. Not store values "by reference" (avoid mutation bugs)
|
|
416
|
+
* 2. Simulate real DB that would do something like that in a transport layer anyway
|
|
417
|
+
*
|
|
418
|
+
* Buffers (e.g CommonDao `__compressed`) need special treatment: passing them through JSON
|
|
419
|
+
* (Buffer.toJSON => `{type: 'Buffer', data: [...bytes]}` => reviver)
|
|
420
|
+
* turns every byte into a JSON number and a reviver call — ~90ms for a single 300KB buffer.
|
|
421
|
+
* Instead, top-level Buffer fields are excluded from the JSON round-trip and copied directly.
|
|
422
|
+
*
|
|
423
|
+
* Known accepted limitation: only top-level Buffer fields are preserved as Buffers.
|
|
424
|
+
* A Buffer nested deeper goes through the JSON round-trip and is stored in its
|
|
425
|
+
* (slow to serialize) json form `{type: 'Buffer', data: [...bytes]}` — same as a real
|
|
426
|
+
* JSON transport layer without a reviver would do.
|
|
427
|
+
*/
|
|
428
|
+
function deepCloneRow<T extends AnyObject>(row: T): T {
|
|
429
|
+
let bufferKeys: string[] | undefined
|
|
430
|
+
for (const key of Object.keys(row)) {
|
|
431
|
+
if (Buffer.isBuffer(row[key])) (bufferKeys ||= []).push(key)
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
if (!bufferKeys) return JSON.parse(JSON.stringify(row))
|
|
435
|
+
|
|
436
|
+
const src: AnyObject = { ...row }
|
|
437
|
+
for (const key of bufferKeys) src[key] = undefined // JSON.stringify omits undefined values
|
|
438
|
+
const clone = JSON.parse(JSON.stringify(src))
|
|
439
|
+
for (const key of bufferKeys) clone[key] = Buffer.from(row[key])
|
|
440
|
+
return clone
|
|
441
|
+
}
|