@naturalcycles/db-lib 10.15.0 → 10.16.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 +1 -1
- package/dist/commondao/common.dao.js +9 -6
- package/dist/commondao/common.dao.model.d.ts +5 -2
- package/dist/pipeline/dbPipelineBackup.js +2 -2
- package/dist/pipeline/dbPipelineCopy.d.ts +1 -1
- package/dist/pipeline/dbPipelineCopy.js +2 -1
- package/dist/pipeline/dbPipelineRestore.js +2 -2
- package/package.json +1 -1
- package/src/commondao/common.dao.model.ts +5 -2
- package/src/commondao/common.dao.ts +25 -21
- package/src/pipeline/dbPipelineBackup.ts +2 -1
- package/src/pipeline/dbPipelineCopy.ts +5 -4
- package/src/pipeline/dbPipelineRestore.ts +2 -1
|
@@ -2,7 +2,7 @@ import type { Transform } from 'node:stream';
|
|
|
2
2
|
import type { JsonSchemaObject, JsonSchemaRootObject } from '@naturalcycles/js-lib/json-schema';
|
|
3
3
|
import type { CommonLogger } from '@naturalcycles/js-lib/log';
|
|
4
4
|
import type { AsyncIndexedMapper, BaseDBEntity, StringMap, UnixTimestampMillis, Unsaved } from '@naturalcycles/js-lib/types';
|
|
5
|
-
import type
|
|
5
|
+
import { type ReadableTyped } from '@naturalcycles/nodejs-lib/stream';
|
|
6
6
|
import type { CommonDBTransactionOptions, DBTransaction, RunQueryResult } from '../db.model.js';
|
|
7
7
|
import type { DBQuery } from '../query/dbQuery.js';
|
|
8
8
|
import { RunnableDBQuery } from '../query/dbQuery.js';
|
|
@@ -9,6 +9,7 @@ import { pMap } from '@naturalcycles/js-lib/promise/pMap.js';
|
|
|
9
9
|
import { _truncate } from '@naturalcycles/js-lib/string/string.util.js';
|
|
10
10
|
import { _passthroughPredicate, _typeCast, SKIP } from '@naturalcycles/js-lib/types';
|
|
11
11
|
import { stringId } from '@naturalcycles/nodejs-lib';
|
|
12
|
+
import { transformFlatten } from '@naturalcycles/nodejs-lib/stream';
|
|
12
13
|
import { _pipeline, transformChunk, transformLogProgress, transformMap, transformNoOp, writableVoid, } from '@naturalcycles/nodejs-lib/stream';
|
|
13
14
|
import { DBLibError } from '../cnst.js';
|
|
14
15
|
import { RunnableDBQuery } from '../query/dbQuery.js';
|
|
@@ -346,8 +347,9 @@ export class CommonDao {
|
|
|
346
347
|
const stream = this.cfg.db.streamQuery(q, opt);
|
|
347
348
|
if (partialQuery)
|
|
348
349
|
return stream;
|
|
349
|
-
return stream
|
|
350
|
-
|
|
350
|
+
return (stream
|
|
351
|
+
// the commented out line was causing RangeError: Maximum call stack size exceeded
|
|
352
|
+
// .on('error', err => stream.emit('error', err))
|
|
351
353
|
.pipe(transformMap(async (dbm) => {
|
|
352
354
|
if (this.cfg.hooks.afterLoad) {
|
|
353
355
|
dbm = (await this.cfg.hooks.afterLoad(dbm));
|
|
@@ -357,7 +359,7 @@ export class CommonDao {
|
|
|
357
359
|
return this.anyToDBM(dbm, opt);
|
|
358
360
|
}, {
|
|
359
361
|
errorMode: opt.errorMode,
|
|
360
|
-
}));
|
|
362
|
+
})));
|
|
361
363
|
}
|
|
362
364
|
/**
|
|
363
365
|
* Stream as Readable, to be able to .pipe() it further with support of backpressure.
|
|
@@ -392,7 +394,8 @@ export class CommonDao {
|
|
|
392
394
|
// optimization: 1 validation is enough
|
|
393
395
|
// .pipe(transformMap<any, DBM>(dbm => this.anyToDBM(dbm, opt), safeOpt))
|
|
394
396
|
// .pipe(transformMap<DBM, BM>(dbm => this.dbmToBM(dbm, opt), safeOpt))
|
|
395
|
-
|
|
397
|
+
// the commented out line was causing RangeError: Maximum call stack size exceeded
|
|
398
|
+
// .on('error', err => stream.emit('error', err))
|
|
396
399
|
.pipe(transformMap(async (dbm) => {
|
|
397
400
|
if (this.cfg.hooks.afterLoad) {
|
|
398
401
|
dbm = (await this.cfg.hooks.afterLoad(dbm));
|
|
@@ -471,7 +474,7 @@ export class CommonDao {
|
|
|
471
474
|
obj.created ||= obj.updated || now;
|
|
472
475
|
}
|
|
473
476
|
if (this.cfg.useUpdatedProperty) {
|
|
474
|
-
obj.updated = opt.
|
|
477
|
+
obj.updated = opt.preserveUpdated && obj.updated ? obj.updated : now;
|
|
475
478
|
}
|
|
476
479
|
if (this.cfg.generateId) {
|
|
477
480
|
obj.id ||= (this.cfg.hooks.createNaturalId?.(obj) ||
|
|
@@ -764,8 +767,8 @@ export class CommonDao {
|
|
|
764
767
|
}, {
|
|
765
768
|
concurrency: chunkConcurrency,
|
|
766
769
|
errorMode,
|
|
767
|
-
flattenArrayOutput: true,
|
|
768
770
|
}),
|
|
771
|
+
transformFlatten(),
|
|
769
772
|
transformLogProgress({
|
|
770
773
|
metric: 'saved',
|
|
771
774
|
...opt,
|
|
@@ -202,9 +202,12 @@ export interface CommonDaoOptions extends CommonDBOptions {
|
|
|
202
202
|
*/
|
|
203
203
|
mutateInput?: boolean;
|
|
204
204
|
/**
|
|
205
|
-
*
|
|
205
|
+
* Defaults to false.
|
|
206
|
+
*
|
|
207
|
+
* If false (default) - will set `updated` property to the current timestamp.
|
|
208
|
+
* If true - will NOT set it (preserve the original value).
|
|
206
209
|
*/
|
|
207
|
-
|
|
210
|
+
preserveUpdated?: boolean;
|
|
208
211
|
/**
|
|
209
212
|
* @default false (for streams). Setting to true enables deletion of immutable objects
|
|
210
213
|
*/
|
|
@@ -4,7 +4,7 @@ import { pMap } from '@naturalcycles/js-lib/promise/pMap.js';
|
|
|
4
4
|
import { _passthroughMapper } from '@naturalcycles/js-lib/types';
|
|
5
5
|
import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib/colors';
|
|
6
6
|
import { fs2 } from '@naturalcycles/nodejs-lib/fs2';
|
|
7
|
-
import { createWriteStreamAsNDJSON, } from '@naturalcycles/nodejs-lib/stream';
|
|
7
|
+
import { createWriteStreamAsNDJSON, transformFlatten, } from '@naturalcycles/nodejs-lib/stream';
|
|
8
8
|
import { _pipeline, NDJsonStats, transformLogProgress, transformMap, transformTap, } from '@naturalcycles/nodejs-lib/stream';
|
|
9
9
|
import { DBQuery } from '../query/dbQuery.js';
|
|
10
10
|
/**
|
|
@@ -65,10 +65,10 @@ export async function dbPipelineBackup(opt) {
|
|
|
65
65
|
}),
|
|
66
66
|
transformMap(mapperPerTable[table] || _passthroughMapper, {
|
|
67
67
|
errorMode,
|
|
68
|
-
flattenArrayOutput: true,
|
|
69
68
|
...transformMapOptions,
|
|
70
69
|
metric: table,
|
|
71
70
|
}),
|
|
71
|
+
transformFlatten(),
|
|
72
72
|
transformTap(() => {
|
|
73
73
|
rows++;
|
|
74
74
|
}),
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ErrorMode } from '@naturalcycles/js-lib/error/errorMode.js';
|
|
2
2
|
import type { AsyncMapper, UnixTimestamp } from '@naturalcycles/js-lib/types';
|
|
3
|
-
import type
|
|
3
|
+
import { type TransformLogProgressOptions, type TransformMapOptions } from '@naturalcycles/nodejs-lib/stream';
|
|
4
4
|
import { NDJsonStats } from '@naturalcycles/nodejs-lib/stream';
|
|
5
5
|
import type { CommonDB } from '../commondb/common.db.js';
|
|
6
6
|
import type { CommonDBSaveOptions } from '../db.model.js';
|
|
@@ -3,6 +3,7 @@ import { ErrorMode } from '@naturalcycles/js-lib/error/errorMode.js';
|
|
|
3
3
|
import { pMap } from '@naturalcycles/js-lib/promise/pMap.js';
|
|
4
4
|
import { _passthroughMapper } from '@naturalcycles/js-lib/types';
|
|
5
5
|
import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib/colors';
|
|
6
|
+
import { transformFlatten, } from '@naturalcycles/nodejs-lib/stream';
|
|
6
7
|
import { _pipeline, NDJsonStats, transformChunk, transformLogProgress, transformMap, transformTap, writableForEach, } from '@naturalcycles/nodejs-lib/stream';
|
|
7
8
|
import { DBQuery } from '../query/dbQuery.js';
|
|
8
9
|
/**
|
|
@@ -38,10 +39,10 @@ export async function dbPipelineCopy(opt) {
|
|
|
38
39
|
}),
|
|
39
40
|
transformMap(mapper, {
|
|
40
41
|
errorMode,
|
|
41
|
-
flattenArrayOutput: true,
|
|
42
42
|
...transformMapOptions,
|
|
43
43
|
metric: table,
|
|
44
44
|
}),
|
|
45
|
+
transformFlatten(),
|
|
45
46
|
transformTap(() => rows++),
|
|
46
47
|
transformChunk({ chunkSize }),
|
|
47
48
|
writableForEach(async (dbms) => {
|
|
@@ -6,7 +6,7 @@ import { pMap } from '@naturalcycles/js-lib/promise/pMap.js';
|
|
|
6
6
|
import { _passthroughMapper } from '@naturalcycles/js-lib/types';
|
|
7
7
|
import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib/colors';
|
|
8
8
|
import { fs2 } from '@naturalcycles/nodejs-lib/fs2';
|
|
9
|
-
import { createReadStreamAsNDJSON, } from '@naturalcycles/nodejs-lib/stream';
|
|
9
|
+
import { createReadStreamAsNDJSON, transformFlatten, } from '@naturalcycles/nodejs-lib/stream';
|
|
10
10
|
import { _pipeline, NDJsonStats, transformChunk, transformFilterSync, transformLogProgress, transformMap, transformTap, writableForEach, } from '@naturalcycles/nodejs-lib/stream';
|
|
11
11
|
/**
|
|
12
12
|
* Pipeline from NDJSON files in a folder (optionally gzipped) to CommonDB.
|
|
@@ -80,10 +80,10 @@ export async function dbPipelineRestore(opt) {
|
|
|
80
80
|
: []),
|
|
81
81
|
transformMap(mapperPerTable[table] || _passthroughMapper, {
|
|
82
82
|
errorMode,
|
|
83
|
-
flattenArrayOutput: true,
|
|
84
83
|
...transformMapOptions,
|
|
85
84
|
metric: table,
|
|
86
85
|
}),
|
|
86
|
+
transformFlatten(),
|
|
87
87
|
transformChunk({ chunkSize }),
|
|
88
88
|
writableForEach(async (dbms) => {
|
|
89
89
|
await db.saveBatch(table, dbms, saveOptions);
|
package/package.json
CHANGED
|
@@ -244,9 +244,12 @@ export interface CommonDaoOptions extends CommonDBOptions {
|
|
|
244
244
|
mutateInput?: boolean
|
|
245
245
|
|
|
246
246
|
/**
|
|
247
|
-
*
|
|
247
|
+
* Defaults to false.
|
|
248
|
+
*
|
|
249
|
+
* If false (default) - will set `updated` property to the current timestamp.
|
|
250
|
+
* If true - will NOT set it (preserve the original value).
|
|
248
251
|
*/
|
|
249
|
-
|
|
252
|
+
preserveUpdated?: boolean
|
|
250
253
|
|
|
251
254
|
/**
|
|
252
255
|
* @default false (for streams). Setting to true enables deletion of immutable objects
|
|
@@ -24,7 +24,7 @@ import type {
|
|
|
24
24
|
} from '@naturalcycles/js-lib/types'
|
|
25
25
|
import { _passthroughPredicate, _typeCast, SKIP } from '@naturalcycles/js-lib/types'
|
|
26
26
|
import { stringId } from '@naturalcycles/nodejs-lib'
|
|
27
|
-
import type
|
|
27
|
+
import { type ReadableTyped, transformFlatten } from '@naturalcycles/nodejs-lib/stream'
|
|
28
28
|
import {
|
|
29
29
|
_pipeline,
|
|
30
30
|
transformChunk,
|
|
@@ -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 => {
|
|
@@ -613,7 +617,7 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, I
|
|
|
613
617
|
}
|
|
614
618
|
|
|
615
619
|
if (this.cfg.useUpdatedProperty) {
|
|
616
|
-
obj.updated = opt.
|
|
620
|
+
obj.updated = opt.preserveUpdated && obj.updated ? obj.updated : now
|
|
617
621
|
}
|
|
618
622
|
|
|
619
623
|
if (this.cfg.generateId) {
|
|
@@ -979,9 +983,9 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, I
|
|
|
979
983
|
{
|
|
980
984
|
concurrency: chunkConcurrency,
|
|
981
985
|
errorMode,
|
|
982
|
-
flattenArrayOutput: true,
|
|
983
986
|
},
|
|
984
987
|
),
|
|
988
|
+
transformFlatten(),
|
|
985
989
|
transformLogProgress({
|
|
986
990
|
metric: 'saved',
|
|
987
991
|
...opt,
|
|
@@ -7,6 +7,7 @@ import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib/col
|
|
|
7
7
|
import { fs2 } from '@naturalcycles/nodejs-lib/fs2'
|
|
8
8
|
import {
|
|
9
9
|
createWriteStreamAsNDJSON,
|
|
10
|
+
transformFlatten,
|
|
10
11
|
type TransformLogProgressOptions,
|
|
11
12
|
type TransformMapOptions,
|
|
12
13
|
} from '@naturalcycles/nodejs-lib/stream'
|
|
@@ -220,10 +221,10 @@ export async function dbPipelineBackup(opt: DBPipelineBackupOptions): Promise<ND
|
|
|
220
221
|
}),
|
|
221
222
|
transformMap(mapperPerTable[table] || _passthroughMapper, {
|
|
222
223
|
errorMode,
|
|
223
|
-
flattenArrayOutput: true,
|
|
224
224
|
...transformMapOptions,
|
|
225
225
|
metric: table,
|
|
226
226
|
}),
|
|
227
|
+
transformFlatten(),
|
|
227
228
|
transformTap(() => {
|
|
228
229
|
rows++
|
|
229
230
|
}),
|
|
@@ -4,9 +4,10 @@ import { pMap } from '@naturalcycles/js-lib/promise/pMap.js'
|
|
|
4
4
|
import type { AsyncMapper, BaseDBEntity, UnixTimestamp } from '@naturalcycles/js-lib/types'
|
|
5
5
|
import { _passthroughMapper } from '@naturalcycles/js-lib/types'
|
|
6
6
|
import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib/colors'
|
|
7
|
-
import
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
import {
|
|
8
|
+
transformFlatten,
|
|
9
|
+
type TransformLogProgressOptions,
|
|
10
|
+
type TransformMapOptions,
|
|
10
11
|
} from '@naturalcycles/nodejs-lib/stream'
|
|
11
12
|
import {
|
|
12
13
|
_pipeline,
|
|
@@ -144,10 +145,10 @@ export async function dbPipelineCopy(opt: DBPipelineCopyOptions): Promise<NDJson
|
|
|
144
145
|
}),
|
|
145
146
|
transformMap(mapper, {
|
|
146
147
|
errorMode,
|
|
147
|
-
flattenArrayOutput: true,
|
|
148
148
|
...transformMapOptions,
|
|
149
149
|
metric: table,
|
|
150
150
|
}),
|
|
151
|
+
transformFlatten(),
|
|
151
152
|
transformTap(() => rows++),
|
|
152
153
|
transformChunk({ chunkSize }),
|
|
153
154
|
writableForEach(async dbms => {
|
|
@@ -10,6 +10,7 @@ import { boldWhite, dimWhite, grey, yellow } from '@naturalcycles/nodejs-lib/col
|
|
|
10
10
|
import { fs2 } from '@naturalcycles/nodejs-lib/fs2'
|
|
11
11
|
import {
|
|
12
12
|
createReadStreamAsNDJSON,
|
|
13
|
+
transformFlatten,
|
|
13
14
|
type TransformLogProgressOptions,
|
|
14
15
|
type TransformMapOptions,
|
|
15
16
|
} from '@naturalcycles/nodejs-lib/stream'
|
|
@@ -207,10 +208,10 @@ export async function dbPipelineRestore(opt: DBPipelineRestoreOptions): Promise<
|
|
|
207
208
|
: []),
|
|
208
209
|
transformMap(mapperPerTable[table] || _passthroughMapper, {
|
|
209
210
|
errorMode,
|
|
210
|
-
flattenArrayOutput: true,
|
|
211
211
|
...transformMapOptions,
|
|
212
212
|
metric: table,
|
|
213
213
|
}),
|
|
214
|
+
transformFlatten(),
|
|
214
215
|
transformChunk({ chunkSize }),
|
|
215
216
|
writableForEach(async dbms => {
|
|
216
217
|
await db.saveBatch(table, dbms, saveOptions)
|