@naturalcycles/db-lib 8.52.0 → 8.53.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.
|
@@ -139,9 +139,6 @@ export declare class CommonDao<BM extends Partial<ObjectWithId<ID>>, DBM extends
|
|
|
139
139
|
bmToTM(bm: undefined, opt?: CommonDaoOptions): TM | undefined;
|
|
140
140
|
bmToTM(bm?: Saved<BM>, opt?: CommonDaoOptions): TM;
|
|
141
141
|
bmsToTM(bms: Saved<BM>[], opt?: CommonDaoOptions): TM[];
|
|
142
|
-
tmToBM(tm: undefined, opt?: CommonDaoOptions): undefined;
|
|
143
|
-
tmToBM(tm?: TM, opt?: CommonDaoOptions): BM;
|
|
144
|
-
tmsToBM(tms: TM[], opt?: CommonDaoOptions): BM[];
|
|
145
142
|
/**
|
|
146
143
|
* Returns *converted value*.
|
|
147
144
|
* Validates (unless `skipValidation=true` passed).
|
|
@@ -86,7 +86,6 @@ class CommonDao {
|
|
|
86
86
|
beforeDBMValidate: dbm => dbm,
|
|
87
87
|
beforeDBMToBM: dbm => dbm,
|
|
88
88
|
beforeBMToDBM: bm => bm,
|
|
89
|
-
beforeTMToBM: tm => tm,
|
|
90
89
|
beforeBMToTM: bm => bm,
|
|
91
90
|
anonymize: dbm => dbm,
|
|
92
91
|
onValidationError: err => err,
|
|
@@ -790,22 +789,6 @@ class CommonDao {
|
|
|
790
789
|
// try/catch?
|
|
791
790
|
return bms.map(bm => this.bmToTM(bm, opt));
|
|
792
791
|
}
|
|
793
|
-
tmToBM(tm, opt = {}) {
|
|
794
|
-
if (!tm)
|
|
795
|
-
return;
|
|
796
|
-
// optimization: 1 validation is enough
|
|
797
|
-
// Validate/convert TM
|
|
798
|
-
// bm gets assigned to the new reference
|
|
799
|
-
// tm = this.validateAndConvert(tm, this.cfg.tmSchema, DBModelType.TM, opt)
|
|
800
|
-
// TM > BM
|
|
801
|
-
const bm = this.cfg.hooks.beforeTMToBM(tm);
|
|
802
|
-
// Validate/convert BM
|
|
803
|
-
return this.validateAndConvert(bm, this.cfg.bmSchema, db_model_1.DBModelType.BM, opt);
|
|
804
|
-
}
|
|
805
|
-
tmsToBM(tms, opt = {}) {
|
|
806
|
-
// try/catch?
|
|
807
|
-
return tms.map(tm => this.tmToBM(tm, opt));
|
|
808
|
-
}
|
|
809
792
|
/**
|
|
810
793
|
* Returns *converted value*.
|
|
811
794
|
* Validates (unless `skipValidation=true` passed).
|
|
@@ -3,19 +3,53 @@ import { AjvSchema, AjvValidationError, JoiValidationError, ObjectSchemaTyped, T
|
|
|
3
3
|
import { CommonDB } from '../common.db';
|
|
4
4
|
import { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions } from '../db.model';
|
|
5
5
|
export interface CommonDaoHooks<BM extends Partial<ObjectWithId<ID>>, DBM extends ObjectWithId<ID>, TM, ID extends string | number> {
|
|
6
|
+
/**
|
|
7
|
+
* Allows to override the id generation function.
|
|
8
|
+
* By default it uses `stringId` from nodejs-lib
|
|
9
|
+
* (which uses lowercase alphanumberic alphabet and the size of 16).
|
|
10
|
+
*/
|
|
6
11
|
createRandomId: () => ID;
|
|
7
12
|
/**
|
|
8
13
|
* createNaturalId hook is called (tried) first.
|
|
9
14
|
* If it doesn't exist - createRandomId is called.
|
|
10
15
|
*/
|
|
11
16
|
createNaturalId: (obj: DBM | BM) => ID;
|
|
17
|
+
/**
|
|
18
|
+
* It's a counter-part of `createNaturalId`.
|
|
19
|
+
* Allows to provide a parser function to parse "natural id" into
|
|
20
|
+
* DBM components (e.g accountId and some other property that is part of the id).
|
|
21
|
+
*/
|
|
12
22
|
parseNaturalId: (id: ID) => Partial<DBM>;
|
|
23
|
+
/**
|
|
24
|
+
* It is called only on `dao.create` method.
|
|
25
|
+
* Dao.create method is called in:
|
|
26
|
+
*
|
|
27
|
+
* - getByIdOrEmpty, getByIdAsDBMOrEmpty
|
|
28
|
+
* - patch, patchAsDBM
|
|
29
|
+
*/
|
|
13
30
|
beforeCreate: (bm: Partial<BM>) => Partial<BM>;
|
|
31
|
+
/**
|
|
32
|
+
* Called when loading things "as DBM" and validation is not skipped.
|
|
33
|
+
* When loading things like BM/TM - other hooks get involved instead:
|
|
34
|
+
* - beforeDBMToBM
|
|
35
|
+
* - beforeBMToTM
|
|
36
|
+
*
|
|
37
|
+
* TODO: maybe rename those to `validateAs(model)`
|
|
38
|
+
* as it only validates "final state", not intermediate
|
|
39
|
+
*/
|
|
14
40
|
beforeDBMValidate: (dbm: Partial<DBM>) => Partial<DBM>;
|
|
15
41
|
beforeDBMToBM: (dbm: DBM) => Partial<BM> | Promise<Partial<BM>>;
|
|
16
42
|
beforeBMToDBM: (bm: BM) => Partial<DBM> | Promise<Partial<DBM>>;
|
|
17
|
-
beforeTMToBM: (tm: TM) => Partial<BM>;
|
|
18
43
|
beforeBMToTM: (bm: BM) => Partial<TM>;
|
|
44
|
+
/**
|
|
45
|
+
* Called in:
|
|
46
|
+
* - dbmToBM (applied before DBM becomes BM)
|
|
47
|
+
* - anyToDBM
|
|
48
|
+
*
|
|
49
|
+
* Hook only allows to apply anonymization to DBM (not to BM).
|
|
50
|
+
* It still applies to BM "transitively", during dbmToBM
|
|
51
|
+
* (e.g after loaded from the Database).
|
|
52
|
+
*/
|
|
19
53
|
anonymize: (dbm: DBM) => DBM;
|
|
20
54
|
/**
|
|
21
55
|
* If hook is defined - allows to prevent or modify the error thrown.
|
package/package.json
CHANGED
|
@@ -23,19 +23,59 @@ export interface CommonDaoHooks<
|
|
|
23
23
|
TM,
|
|
24
24
|
ID extends string | number,
|
|
25
25
|
> {
|
|
26
|
+
/**
|
|
27
|
+
* Allows to override the id generation function.
|
|
28
|
+
* By default it uses `stringId` from nodejs-lib
|
|
29
|
+
* (which uses lowercase alphanumberic alphabet and the size of 16).
|
|
30
|
+
*/
|
|
26
31
|
createRandomId: () => ID
|
|
32
|
+
|
|
27
33
|
/**
|
|
28
34
|
* createNaturalId hook is called (tried) first.
|
|
29
35
|
* If it doesn't exist - createRandomId is called.
|
|
30
36
|
*/
|
|
31
37
|
createNaturalId: (obj: DBM | BM) => ID
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* It's a counter-part of `createNaturalId`.
|
|
41
|
+
* Allows to provide a parser function to parse "natural id" into
|
|
42
|
+
* DBM components (e.g accountId and some other property that is part of the id).
|
|
43
|
+
*/
|
|
32
44
|
parseNaturalId: (id: ID) => Partial<DBM>
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* It is called only on `dao.create` method.
|
|
48
|
+
* Dao.create method is called in:
|
|
49
|
+
*
|
|
50
|
+
* - getByIdOrEmpty, getByIdAsDBMOrEmpty
|
|
51
|
+
* - patch, patchAsDBM
|
|
52
|
+
*/
|
|
33
53
|
beforeCreate: (bm: Partial<BM>) => Partial<BM>
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Called when loading things "as DBM" and validation is not skipped.
|
|
57
|
+
* When loading things like BM/TM - other hooks get involved instead:
|
|
58
|
+
* - beforeDBMToBM
|
|
59
|
+
* - beforeBMToTM
|
|
60
|
+
*
|
|
61
|
+
* TODO: maybe rename those to `validateAs(model)`
|
|
62
|
+
* as it only validates "final state", not intermediate
|
|
63
|
+
*/
|
|
34
64
|
beforeDBMValidate: (dbm: Partial<DBM>) => Partial<DBM>
|
|
65
|
+
|
|
35
66
|
beforeDBMToBM: (dbm: DBM) => Partial<BM> | Promise<Partial<BM>>
|
|
36
67
|
beforeBMToDBM: (bm: BM) => Partial<DBM> | Promise<Partial<DBM>>
|
|
37
|
-
beforeTMToBM: (tm: TM) => Partial<BM>
|
|
38
68
|
beforeBMToTM: (bm: BM) => Partial<TM>
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Called in:
|
|
72
|
+
* - dbmToBM (applied before DBM becomes BM)
|
|
73
|
+
* - anyToDBM
|
|
74
|
+
*
|
|
75
|
+
* Hook only allows to apply anonymization to DBM (not to BM).
|
|
76
|
+
* It still applies to BM "transitively", during dbmToBM
|
|
77
|
+
* (e.g after loaded from the Database).
|
|
78
|
+
*/
|
|
39
79
|
anonymize: (dbm: DBM) => DBM
|
|
40
80
|
|
|
41
81
|
/**
|
|
@@ -95,7 +95,6 @@ export class CommonDao<
|
|
|
95
95
|
beforeDBMValidate: dbm => dbm,
|
|
96
96
|
beforeDBMToBM: dbm => dbm as any,
|
|
97
97
|
beforeBMToDBM: bm => bm as any,
|
|
98
|
-
beforeTMToBM: tm => tm as any,
|
|
99
98
|
beforeBMToTM: bm => bm as any,
|
|
100
99
|
anonymize: dbm => dbm,
|
|
101
100
|
onValidationError: err => err,
|
|
@@ -1061,28 +1060,6 @@ export class CommonDao<
|
|
|
1061
1060
|
return bms.map(bm => this.bmToTM(bm, opt))
|
|
1062
1061
|
}
|
|
1063
1062
|
|
|
1064
|
-
tmToBM(tm: undefined, opt?: CommonDaoOptions): undefined
|
|
1065
|
-
tmToBM(tm?: TM, opt?: CommonDaoOptions): BM
|
|
1066
|
-
tmToBM(tm?: TM, opt: CommonDaoOptions = {}): BM | undefined {
|
|
1067
|
-
if (!tm) return
|
|
1068
|
-
|
|
1069
|
-
// optimization: 1 validation is enough
|
|
1070
|
-
// Validate/convert TM
|
|
1071
|
-
// bm gets assigned to the new reference
|
|
1072
|
-
// tm = this.validateAndConvert(tm, this.cfg.tmSchema, DBModelType.TM, opt)
|
|
1073
|
-
|
|
1074
|
-
// TM > BM
|
|
1075
|
-
const bm = this.cfg.hooks!.beforeTMToBM!(tm) as BM
|
|
1076
|
-
|
|
1077
|
-
// Validate/convert BM
|
|
1078
|
-
return this.validateAndConvert<BM>(bm, this.cfg.bmSchema, DBModelType.BM, opt)
|
|
1079
|
-
}
|
|
1080
|
-
|
|
1081
|
-
tmsToBM(tms: TM[], opt: CommonDaoOptions = {}): BM[] {
|
|
1082
|
-
// try/catch?
|
|
1083
|
-
return tms.map(tm => this.tmToBM(tm, opt))
|
|
1084
|
-
}
|
|
1085
|
-
|
|
1086
1063
|
/**
|
|
1087
1064
|
* Returns *converted value*.
|
|
1088
1065
|
* Validates (unless `skipValidation=true` passed).
|
package/src/db.model.ts
CHANGED