@naturalcycles/db-lib 10.37.0 → 10.38.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.
@@ -1,7 +1,6 @@
1
- import type { Transform } from 'node:stream';
2
1
  import { type BaseDBEntity, type NonNegativeInteger, type StringMap, type Unsaved } from '@naturalcycles/js-lib/types';
3
2
  import type { JsonSchema } from '@naturalcycles/nodejs-lib/ajv';
4
- import { type Pipeline } from '@naturalcycles/nodejs-lib/stream';
3
+ import type { Pipeline } from '@naturalcycles/nodejs-lib/stream';
5
4
  import type { CommonDBTransactionOptions, RunQueryResult } from '../db.model.js';
6
5
  import type { DBQuery } from '../query/dbQuery.js';
7
6
  import { RunnableDBQuery } from '../query/dbQuery.js';
@@ -92,8 +91,11 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
92
91
  * (of size opt.chunkSize, which defaults to 500),
93
92
  * and then executing db.saveBatch(chunk) with the concurrency
94
93
  * of opt.chunkConcurrency (which defaults to 32).
94
+ *
95
+ * It takes a Pipeline as input, appends necessary saving transforms to it,
96
+ * and calls .run() on it.
95
97
  */
96
- streamSaveTransforms(opt?: CommonDaoStreamSaveOptions<DBM>): Transform[];
98
+ streamSave(p: Pipeline<BM>, opt?: CommonDaoStreamSaveOptions<DBM>): Promise<void>;
97
99
  /**
98
100
  * @returns number of deleted items
99
101
  */
@@ -7,7 +7,6 @@ import { _filterUndefinedValues, _objectAssignExact, } from '@naturalcycles/js-l
7
7
  import { pMap } from '@naturalcycles/js-lib/promise/pMap.js';
8
8
  import { _passthroughPredicate, _stringMapEntries, _stringMapValues, _typeCast, } from '@naturalcycles/js-lib/types';
9
9
  import { stringId } from '@naturalcycles/nodejs-lib';
10
- import { transformChunk, transformFlatten, transformLogProgress, transformMap, } from '@naturalcycles/nodejs-lib/stream';
11
10
  import { DBLibError } from '../cnst.js';
12
11
  import { RunnableDBQuery } from '../query/dbQuery.js';
13
12
  import { CommonDaoTransaction } from './commonDaoTransaction.js';
@@ -401,8 +400,11 @@ export class CommonDao {
401
400
  * (of size opt.chunkSize, which defaults to 500),
402
401
  * and then executing db.saveBatch(chunk) with the concurrency
403
402
  * of opt.chunkConcurrency (which defaults to 32).
403
+ *
404
+ * It takes a Pipeline as input, appends necessary saving transforms to it,
405
+ * and calls .run() on it.
404
406
  */
405
- streamSaveTransforms(opt = {}) {
407
+ async streamSave(p, opt = {}) {
406
408
  this.requireWriteAccess();
407
409
  const table = opt.table || this.cfg.table;
408
410
  opt.skipValidation ??= true;
@@ -413,32 +415,29 @@ export class CommonDao {
413
415
  const excludeFromIndexes = opt.excludeFromIndexes || this.cfg.excludeFromIndexes;
414
416
  const { beforeSave } = this.cfg.hooks;
415
417
  const { chunkSize = 500, chunkConcurrency = 32, errorMode } = opt;
416
- return [
417
- transformMap(async (bm) => {
418
- this.assignIdCreatedUpdated(bm, opt);
419
- const dbm = await this.bmToDBM(bm, opt);
420
- beforeSave?.(dbm);
421
- return dbm;
422
- }, {
423
- errorMode,
424
- }),
425
- transformChunk(chunkSize),
426
- transformMap(async (batch) => {
427
- await this.cfg.db.saveBatch(table, batch, {
428
- ...opt,
429
- excludeFromIndexes,
430
- });
431
- return batch;
432
- }, {
433
- concurrency: chunkConcurrency,
434
- errorMode,
435
- }),
436
- transformFlatten(),
437
- transformLogProgress({
438
- metric: 'saved',
418
+ await p
419
+ .map(async (bm) => {
420
+ this.assignIdCreatedUpdated(bm, opt);
421
+ const dbm = await this.bmToDBM(bm, opt);
422
+ beforeSave?.(dbm);
423
+ return dbm;
424
+ }, { errorMode })
425
+ .chunk(chunkSize)
426
+ .map(async (batch) => {
427
+ await this.cfg.db.saveBatch(table, batch, {
439
428
  ...opt,
440
- }),
441
- ];
429
+ excludeFromIndexes,
430
+ });
431
+ return batch;
432
+ }, {
433
+ concurrency: chunkConcurrency,
434
+ errorMode,
435
+ })
436
+ .logProgress({
437
+ metric: 'saved',
438
+ ...opt,
439
+ })
440
+ .run();
442
441
  }
443
442
  // DELETE
444
443
  /**
@@ -205,10 +205,10 @@ export async function runCommonDaoTest(db, quirks = {}) {
205
205
  ids = ids.sort();
206
206
  expectMatch(expectedItems.map(i => i.id), ids, quirks);
207
207
  });
208
- test('streamSaveTransform', async () => {
208
+ test('streamSave', async () => {
209
209
  const items2 = createTestItemsBM(2).map(i => ({ ...i, id: i.id + '_str' }));
210
210
  const ids = items2.map(i => i.id);
211
- await Pipeline.fromArray(items2).transformMany(dao.streamSaveTransforms()).run();
211
+ await dao.streamSave(Pipeline.fromArray(items2));
212
212
  const items2Loaded = await dao.getByIds(ids);
213
213
  expectMatch(items2, items2Loaded, quirks);
214
214
  // cleanup
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@naturalcycles/db-lib",
3
3
  "type": "module",
4
- "version": "10.37.0",
4
+ "version": "10.38.0",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@naturalcycles/nodejs-lib": "^15"
8
8
  },
9
9
  "devDependencies": {
10
- "@naturalcycles/dev-lib": "20.15.0"
10
+ "@naturalcycles/dev-lib": "18.4.2"
11
11
  },
12
12
  "files": [
13
13
  "dist",
@@ -1,4 +1,3 @@
1
- import type { Transform } from 'node:stream'
2
1
  import { _isTruthy } from '@naturalcycles/js-lib'
3
2
  import { _uniqBy } from '@naturalcycles/js-lib/array/array.util.js'
4
3
  import { localTime } from '@naturalcycles/js-lib/datetime/localTime.js'
@@ -22,13 +21,7 @@ import {
22
21
  } from '@naturalcycles/js-lib/types'
23
22
  import { stringId } from '@naturalcycles/nodejs-lib'
24
23
  import type { JsonSchema } from '@naturalcycles/nodejs-lib/ajv'
25
- import {
26
- type Pipeline,
27
- transformChunk,
28
- transformFlatten,
29
- transformLogProgress,
30
- transformMap,
31
- } from '@naturalcycles/nodejs-lib/stream'
24
+ import type { Pipeline } from '@naturalcycles/nodejs-lib/stream'
32
25
  import { DBLibError } from '../cnst.js'
33
26
  import type {
34
27
  CommonDBSaveOptions,
@@ -542,8 +535,11 @@ export class CommonDao<
542
535
  * (of size opt.chunkSize, which defaults to 500),
543
536
  * and then executing db.saveBatch(chunk) with the concurrency
544
537
  * of opt.chunkConcurrency (which defaults to 32).
538
+ *
539
+ * It takes a Pipeline as input, appends necessary saving transforms to it,
540
+ * and calls .run() on it.
545
541
  */
546
- streamSaveTransforms(opt: CommonDaoStreamSaveOptions<DBM> = {}): Transform[] {
542
+ async streamSave(p: Pipeline<BM>, opt: CommonDaoStreamSaveOptions<DBM> = {}): Promise<void> {
547
543
  this.requireWriteAccess()
548
544
 
549
545
  const table = opt.table || this.cfg.table
@@ -559,20 +555,18 @@ export class CommonDao<
559
555
 
560
556
  const { chunkSize = 500, chunkConcurrency = 32, errorMode } = opt
561
557
 
562
- return [
563
- transformMap<BM, DBM>(
558
+ await p
559
+ .map(
564
560
  async bm => {
565
561
  this.assignIdCreatedUpdated(bm, opt)
566
562
  const dbm = await this.bmToDBM(bm, opt)
567
563
  beforeSave?.(dbm)
568
564
  return dbm
569
565
  },
570
- {
571
- errorMode,
572
- },
573
- ),
574
- transformChunk<DBM>(chunkSize),
575
- transformMap<DBM[], DBM[]>(
566
+ { errorMode },
567
+ )
568
+ .chunk(chunkSize)
569
+ .map(
576
570
  async batch => {
577
571
  await this.cfg.db.saveBatch(table, batch, {
578
572
  ...opt,
@@ -584,13 +578,12 @@ export class CommonDao<
584
578
  concurrency: chunkConcurrency,
585
579
  errorMode,
586
580
  },
587
- ),
588
- transformFlatten(),
589
- transformLogProgress({
581
+ )
582
+ .logProgress({
590
583
  metric: 'saved',
591
584
  ...opt,
592
- }),
593
- ]
585
+ })
586
+ .run()
594
587
  }
595
588
 
596
589
  // DELETE
@@ -285,11 +285,11 @@ export async function runCommonDaoTest(
285
285
  )
286
286
  })
287
287
 
288
- test('streamSaveTransform', async () => {
288
+ test('streamSave', async () => {
289
289
  const items2 = createTestItemsBM(2).map(i => ({ ...i, id: i.id + '_str' }))
290
290
  const ids = items2.map(i => i.id)
291
291
 
292
- await Pipeline.fromArray(items2).transformMany(dao.streamSaveTransforms()).run()
292
+ await dao.streamSave(Pipeline.fromArray(items2))
293
293
 
294
294
  const items2Loaded = await dao.getByIds(ids)
295
295
  expectMatch(items2, items2Loaded, quirks)