@naturalcycles/db-lib 9.3.0 → 9.3.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/commondao/common.dao.d.ts +4 -5
- package/dist/commondao/common.dao.js +6 -5
- package/dist/commondao/common.dao.model.d.ts +2 -2
- package/dist/query/dbQuery.d.ts +2 -2
- package/package.json +1 -1
- package/src/commondao/common.dao.model.ts +2 -1
- package/src/commondao/common.dao.ts +13 -15
- package/src/query/dbQuery.ts +2 -2
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { Transform } from 'node:stream';
|
|
3
|
-
import { AnyObject, AsyncMapper, CommonLogger, JsonSchemaObject, JsonSchemaRootObject, PartialObjectWithId, Saved, UnixTimestampMillisNumber, ZodSchema } from '@naturalcycles/js-lib';
|
|
3
|
+
import { AnyObject, AsyncMapper, BaseDBEntity, CommonLogger, JsonSchemaObject, JsonSchemaRootObject, PartialObjectWithId, Saved, UnixTimestampMillisNumber, ZodSchema } from '@naturalcycles/js-lib';
|
|
4
4
|
import { AjvSchema, ObjectSchema, ReadableTyped } from '@naturalcycles/nodejs-lib';
|
|
5
5
|
import { CommonDBTransactionOptions, DBModelType, DBPatch, DBTransaction, RunQueryResult } from '../db.model';
|
|
6
6
|
import { DBQuery, RunnableDBQuery } from '../query/dbQuery';
|
|
@@ -60,11 +60,11 @@ export declare class CommonDao<BM extends PartialObjectWithId, DBM extends Parti
|
|
|
60
60
|
runQueryExtendedAsTM(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<RunQueryResult<TM>>;
|
|
61
61
|
runQueryCount(q: DBQuery<DBM>, opt?: CommonDaoOptions): Promise<number>;
|
|
62
62
|
streamQueryForEach(q: DBQuery<DBM>, mapper: AsyncMapper<Saved<BM>, void>, opt?: CommonDaoStreamForEachOptions<Saved<BM>>): Promise<void>;
|
|
63
|
-
streamQueryAsDBMForEach(q: DBQuery<DBM>, mapper: AsyncMapper<DBM
|
|
63
|
+
streamQueryAsDBMForEach(q: DBQuery<DBM>, mapper: AsyncMapper<Saved<DBM>, void>, opt?: CommonDaoStreamForEachOptions<DBM>): Promise<void>;
|
|
64
64
|
/**
|
|
65
65
|
* Stream as Readable, to be able to .pipe() it further with support of backpressure.
|
|
66
66
|
*/
|
|
67
|
-
streamQueryAsDBM(q: DBQuery<DBM>, opt?: CommonDaoStreamOptions<DBM>): ReadableTyped<DBM
|
|
67
|
+
streamQueryAsDBM(q: DBQuery<DBM>, opt?: CommonDaoStreamOptions<DBM>): ReadableTyped<Saved<DBM>>;
|
|
68
68
|
/**
|
|
69
69
|
* Stream as Readable, to be able to .pipe() it further with support of backpressure.
|
|
70
70
|
*
|
|
@@ -82,8 +82,7 @@ export declare class CommonDao<BM extends PartialObjectWithId, DBM extends Parti
|
|
|
82
82
|
* Mutates!
|
|
83
83
|
* "Returns", just to have a type of "Saved"
|
|
84
84
|
*/
|
|
85
|
-
assignIdCreatedUpdated(obj:
|
|
86
|
-
assignIdCreatedUpdated(obj: Partial<BM>, opt?: CommonDaoOptions): Saved<Partial<BM>>;
|
|
85
|
+
assignIdCreatedUpdated<T extends BaseDBEntity>(obj: T, opt?: CommonDaoOptions): Saved<T>;
|
|
87
86
|
/**
|
|
88
87
|
* Mutates with id, created, updated
|
|
89
88
|
*/
|
|
@@ -479,16 +479,17 @@ class CommonDao {
|
|
|
479
479
|
this.cfg.logger?.log(`<< ${q.table}.${op}: ${count} id(s) in ${(0, js_lib_1._since)(started)}`);
|
|
480
480
|
}
|
|
481
481
|
}
|
|
482
|
+
/**
|
|
483
|
+
* Mutates!
|
|
484
|
+
* "Returns", just to have a type of "Saved"
|
|
485
|
+
*/
|
|
482
486
|
assignIdCreatedUpdated(obj, opt = {}) {
|
|
483
487
|
const now = Math.floor(Date.now() / 1000);
|
|
484
488
|
if (this.cfg.useCreatedProperty) {
|
|
485
|
-
;
|
|
486
|
-
obj['created'] ||= obj['updated'] || now;
|
|
489
|
+
obj.created ||= obj.updated || now;
|
|
487
490
|
}
|
|
488
491
|
if (this.cfg.useUpdatedProperty) {
|
|
489
|
-
;
|
|
490
|
-
obj['updated'] =
|
|
491
|
-
opt.preserveUpdatedCreated && obj['updated'] ? obj['updated'] : now;
|
|
492
|
+
obj.updated = opt.preserveUpdatedCreated && obj.updated ? obj.updated : now;
|
|
492
493
|
}
|
|
493
494
|
if (this.cfg.createId) {
|
|
494
495
|
obj.id ||= this.cfg.hooks.createNaturalId?.(obj) || this.cfg.hooks.createRandomId();
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CommonLogger, ErrorMode, PartialObjectWithId, Promisable, Saved, ZodError, ZodSchema } from '@naturalcycles/js-lib';
|
|
1
|
+
import { AnyObject, CommonLogger, ErrorMode, PartialObjectWithId, Promisable, Saved, ZodError, ZodSchema } from '@naturalcycles/js-lib';
|
|
2
2
|
import { AjvSchema, AjvValidationError, JoiValidationError, ObjectSchema, TransformLogProgressOptions, TransformMapOptions } from '@naturalcycles/nodejs-lib';
|
|
3
3
|
import { CommonDB } from '../common.db';
|
|
4
4
|
import { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions } from '../db.model';
|
|
@@ -104,7 +104,7 @@ export declare enum CommonDaoLogLevel {
|
|
|
104
104
|
*/
|
|
105
105
|
DATA_FULL = 30
|
|
106
106
|
}
|
|
107
|
-
export interface CommonDaoCfg<BM extends PartialObjectWithId, DBM extends PartialObjectWithId = BM, TM = BM> {
|
|
107
|
+
export interface CommonDaoCfg<BM extends PartialObjectWithId, DBM extends PartialObjectWithId = BM, TM extends AnyObject = BM> {
|
|
108
108
|
db: CommonDB;
|
|
109
109
|
table: string;
|
|
110
110
|
/**
|
package/dist/query/dbQuery.d.ts
CHANGED
|
@@ -100,9 +100,9 @@ export declare class RunnableDBQuery<BM extends PartialObjectWithId, DBM extends
|
|
|
100
100
|
runQueryCount(opt?: CommonDaoOptions): Promise<number>;
|
|
101
101
|
updateByQuery(patch: DBPatch<DBM>, opt?: CommonDaoOptions): Promise<number>;
|
|
102
102
|
streamQueryForEach(mapper: AsyncMapper<Saved<BM>, void>, opt?: CommonDaoStreamForEachOptions<Saved<BM>>): Promise<void>;
|
|
103
|
-
streamQueryAsDBMForEach(mapper: AsyncMapper<DBM
|
|
103
|
+
streamQueryAsDBMForEach(mapper: AsyncMapper<Saved<DBM>, void>, opt?: CommonDaoStreamForEachOptions<DBM>): Promise<void>;
|
|
104
104
|
streamQuery(opt?: CommonDaoStreamOptions<Saved<BM>>): ReadableTyped<Saved<BM>>;
|
|
105
|
-
streamQueryAsDBM(opt?: CommonDaoStreamOptions<DBM>): ReadableTyped<DBM
|
|
105
|
+
streamQueryAsDBM(opt?: CommonDaoStreamOptions<DBM>): ReadableTyped<Saved<DBM>>;
|
|
106
106
|
queryIds(opt?: CommonDaoOptions): Promise<string[]>;
|
|
107
107
|
streamQueryIds(opt?: CommonDaoStreamOptions<string>): ReadableTyped<string>;
|
|
108
108
|
streamQueryIdsForEach(mapper: AsyncMapper<string, void>, opt?: CommonDaoStreamForEachOptions<string>): Promise<void>;
|
package/package.json
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
+
AnyObject,
|
|
2
3
|
CommonLogger,
|
|
3
4
|
ErrorMode,
|
|
4
5
|
PartialObjectWithId,
|
|
@@ -138,7 +139,7 @@ export enum CommonDaoLogLevel {
|
|
|
138
139
|
export interface CommonDaoCfg<
|
|
139
140
|
BM extends PartialObjectWithId,
|
|
140
141
|
DBM extends PartialObjectWithId = BM,
|
|
141
|
-
TM = BM,
|
|
142
|
+
TM extends AnyObject = BM,
|
|
142
143
|
> {
|
|
143
144
|
db: CommonDB
|
|
144
145
|
table: string
|
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
AnyObject,
|
|
14
14
|
AppError,
|
|
15
15
|
AsyncMapper,
|
|
16
|
+
BaseDBEntity,
|
|
16
17
|
CommonLogger,
|
|
17
18
|
ErrorMode,
|
|
18
19
|
JsonSchemaObject,
|
|
@@ -116,7 +117,7 @@ export class CommonDao<
|
|
|
116
117
|
create(part: Partial<BM> = {}, opt: CommonDaoOptions = {}): Saved<BM> {
|
|
117
118
|
const bm = this.cfg.hooks!.beforeCreate!(part)
|
|
118
119
|
// First assignIdCreatedUpdated, then validate!
|
|
119
|
-
this.assignIdCreatedUpdated(bm
|
|
120
|
+
this.assignIdCreatedUpdated(bm, opt)
|
|
120
121
|
return this.validateAndConvert(bm, this.cfg.bmSchema, DBModelType.BM, opt)
|
|
121
122
|
}
|
|
122
123
|
|
|
@@ -483,7 +484,7 @@ export class CommonDao<
|
|
|
483
484
|
|
|
484
485
|
async streamQueryAsDBMForEach(
|
|
485
486
|
q: DBQuery<DBM>,
|
|
486
|
-
mapper: AsyncMapper<DBM
|
|
487
|
+
mapper: AsyncMapper<Saved<DBM>, void>,
|
|
487
488
|
opt: CommonDaoStreamForEachOptions<DBM> = {},
|
|
488
489
|
): Promise<void> {
|
|
489
490
|
q.table = opt.table || q.table
|
|
@@ -514,7 +515,7 @@ export class CommonDao<
|
|
|
514
515
|
errorMode: opt.errorMode,
|
|
515
516
|
},
|
|
516
517
|
),
|
|
517
|
-
transformMap<DBM
|
|
518
|
+
transformMap<Saved<DBM>, void>(mapper, {
|
|
518
519
|
...opt,
|
|
519
520
|
predicate: _passthroughPredicate, // to be able to logProgress
|
|
520
521
|
}),
|
|
@@ -534,7 +535,10 @@ export class CommonDao<
|
|
|
534
535
|
/**
|
|
535
536
|
* Stream as Readable, to be able to .pipe() it further with support of backpressure.
|
|
536
537
|
*/
|
|
537
|
-
streamQueryAsDBM(
|
|
538
|
+
streamQueryAsDBM(
|
|
539
|
+
q: DBQuery<DBM>,
|
|
540
|
+
opt: CommonDaoStreamOptions<DBM> = {},
|
|
541
|
+
): ReadableTyped<Saved<DBM>> {
|
|
538
542
|
q.table = opt.table || q.table
|
|
539
543
|
opt.skipValidation = opt.skipValidation !== false // default true
|
|
540
544
|
opt.skipConversion = opt.skipConversion !== false // default true
|
|
@@ -674,28 +678,22 @@ export class CommonDao<
|
|
|
674
678
|
* Mutates!
|
|
675
679
|
* "Returns", just to have a type of "Saved"
|
|
676
680
|
*/
|
|
677
|
-
assignIdCreatedUpdated(obj:
|
|
678
|
-
assignIdCreatedUpdated(obj: Partial<BM>, opt?: CommonDaoOptions): Saved<Partial<BM>>
|
|
679
|
-
assignIdCreatedUpdated(
|
|
680
|
-
obj: Partial<DBM> | Partial<BM>,
|
|
681
|
-
opt: CommonDaoOptions = {},
|
|
682
|
-
): Saved<Partial<DBM>> | Saved<Partial<BM>> {
|
|
681
|
+
assignIdCreatedUpdated<T extends BaseDBEntity>(obj: T, opt: CommonDaoOptions = {}): Saved<T> {
|
|
683
682
|
const now = Math.floor(Date.now() / 1000)
|
|
684
683
|
|
|
685
684
|
if (this.cfg.useCreatedProperty) {
|
|
686
|
-
|
|
685
|
+
obj.created ||= obj.updated || now
|
|
687
686
|
}
|
|
688
687
|
|
|
689
688
|
if (this.cfg.useUpdatedProperty) {
|
|
690
|
-
|
|
691
|
-
opt.preserveUpdatedCreated && (obj as any)['updated'] ? (obj as any)['updated'] : now
|
|
689
|
+
obj.updated = opt.preserveUpdatedCreated && obj.updated ? obj.updated : now
|
|
692
690
|
}
|
|
693
691
|
|
|
694
692
|
if (this.cfg.createId) {
|
|
695
|
-
obj.id ||= this.cfg.hooks!.createNaturalId?.(obj as
|
|
693
|
+
obj.id ||= this.cfg.hooks!.createNaturalId?.(obj as any) || this.cfg.hooks!.createRandomId!()
|
|
696
694
|
}
|
|
697
695
|
|
|
698
|
-
return obj as
|
|
696
|
+
return obj as Saved<T>
|
|
699
697
|
}
|
|
700
698
|
|
|
701
699
|
// SAVE
|
package/src/query/dbQuery.ts
CHANGED
|
@@ -297,7 +297,7 @@ export class RunnableDBQuery<
|
|
|
297
297
|
}
|
|
298
298
|
|
|
299
299
|
async streamQueryAsDBMForEach(
|
|
300
|
-
mapper: AsyncMapper<DBM
|
|
300
|
+
mapper: AsyncMapper<Saved<DBM>, void>,
|
|
301
301
|
opt?: CommonDaoStreamForEachOptions<DBM>,
|
|
302
302
|
): Promise<void> {
|
|
303
303
|
await this.dao.streamQueryAsDBMForEach(this, mapper, opt)
|
|
@@ -307,7 +307,7 @@ export class RunnableDBQuery<
|
|
|
307
307
|
return this.dao.streamQuery(this, opt)
|
|
308
308
|
}
|
|
309
309
|
|
|
310
|
-
streamQueryAsDBM(opt?: CommonDaoStreamOptions<DBM>): ReadableTyped<DBM
|
|
310
|
+
streamQueryAsDBM(opt?: CommonDaoStreamOptions<DBM>): ReadableTyped<Saved<DBM>> {
|
|
311
311
|
return this.dao.streamQueryAsDBM(this, opt)
|
|
312
312
|
}
|
|
313
313
|
|