@naturalcycles/db-lib
Advanced tools
Comparing version 8.52.0 to 8.53.0
@@ -142,5 +142,2 @@ import { AnyObject, AsyncMapper, JsonSchemaObject, JsonSchemaRootObject, ObjectWithId, Promisable, Saved, Unsaved, ZodSchema } from '@naturalcycles/js-lib'; | ||
bmsToTM(bms: Saved<BM>[], opt?: CommonDaoOptions): TM[]; | ||
tmToBM(tm: undefined, opt?: CommonDaoOptions): undefined; | ||
tmToBM(tm?: TM, opt?: CommonDaoOptions): BM; | ||
tmsToBM(tms: TM[], opt?: CommonDaoOptions): BM[]; | ||
/** | ||
@@ -147,0 +144,0 @@ * Returns *converted value*. |
@@ -89,3 +89,2 @@ "use strict"; | ||
beforeBMToDBM: bm => bm, | ||
beforeTMToBM: tm => tm, | ||
beforeBMToTM: bm => bm, | ||
@@ -794,18 +793,2 @@ anonymize: dbm => dbm, | ||
} | ||
tmToBM(tm, opt = {}) { | ||
if (!tm) | ||
return; | ||
// optimization: 1 validation is enough | ||
// Validate/convert TM | ||
// bm gets assigned to the new reference | ||
// tm = this.validateAndConvert(tm, this.cfg.tmSchema, DBModelType.TM, opt) | ||
// TM > BM | ||
const bm = this.cfg.hooks.beforeTMToBM(tm); | ||
// Validate/convert BM | ||
return this.validateAndConvert(bm, this.cfg.bmSchema, db_model_1.DBModelType.BM, opt); | ||
} | ||
tmsToBM(tms, opt = {}) { | ||
// try/catch? | ||
return tms.map(tm => this.tmToBM(tm, opt)); | ||
} | ||
/** | ||
@@ -812,0 +795,0 @@ * Returns *converted value*. |
@@ -6,2 +6,7 @@ import { CommonLogger, ErrorMode, ObjectWithId, Saved, ZodError, ZodSchema } from '@naturalcycles/js-lib'; | ||
export interface CommonDaoHooks<BM extends Partial<ObjectWithId<ID>>, DBM extends ObjectWithId<ID>, TM, ID extends string | number> { | ||
/** | ||
* Allows to override the id generation function. | ||
* By default it uses `stringId` from nodejs-lib | ||
* (which uses lowercase alphanumberic alphabet and the size of 16). | ||
*/ | ||
createRandomId: () => ID; | ||
@@ -13,9 +18,38 @@ /** | ||
createNaturalId: (obj: DBM | BM) => ID; | ||
/** | ||
* It's a counter-part of `createNaturalId`. | ||
* Allows to provide a parser function to parse "natural id" into | ||
* DBM components (e.g accountId and some other property that is part of the id). | ||
*/ | ||
parseNaturalId: (id: ID) => Partial<DBM>; | ||
/** | ||
* It is called only on `dao.create` method. | ||
* Dao.create method is called in: | ||
* | ||
* - getByIdOrEmpty, getByIdAsDBMOrEmpty | ||
* - patch, patchAsDBM | ||
*/ | ||
beforeCreate: (bm: Partial<BM>) => Partial<BM>; | ||
/** | ||
* Called when loading things "as DBM" and validation is not skipped. | ||
* When loading things like BM/TM - other hooks get involved instead: | ||
* - beforeDBMToBM | ||
* - beforeBMToTM | ||
* | ||
* TODO: maybe rename those to `validateAs(model)` | ||
* as it only validates "final state", not intermediate | ||
*/ | ||
beforeDBMValidate: (dbm: Partial<DBM>) => Partial<DBM>; | ||
beforeDBMToBM: (dbm: DBM) => Partial<BM> | Promise<Partial<BM>>; | ||
beforeBMToDBM: (bm: BM) => Partial<DBM> | Promise<Partial<DBM>>; | ||
beforeTMToBM: (tm: TM) => Partial<BM>; | ||
beforeBMToTM: (bm: BM) => Partial<TM>; | ||
/** | ||
* Called in: | ||
* - dbmToBM (applied before DBM becomes BM) | ||
* - anyToDBM | ||
* | ||
* Hook only allows to apply anonymization to DBM (not to BM). | ||
* It still applies to BM "transitively", during dbmToBM | ||
* (e.g after loaded from the Database). | ||
*/ | ||
anonymize: (dbm: DBM) => DBM; | ||
@@ -22,0 +56,0 @@ /** |
@@ -44,3 +44,3 @@ { | ||
}, | ||
"version": "8.52.0", | ||
"version": "8.53.0", | ||
"description": "Lowest Common Denominator API to supported Databases", | ||
@@ -47,0 +47,0 @@ "keywords": [ |
@@ -26,3 +26,9 @@ import { | ||
> { | ||
/** | ||
* Allows to override the id generation function. | ||
* By default it uses `stringId` from nodejs-lib | ||
* (which uses lowercase alphanumberic alphabet and the size of 16). | ||
*/ | ||
createRandomId: () => ID | ||
/** | ||
@@ -33,9 +39,43 @@ * createNaturalId hook is called (tried) first. | ||
createNaturalId: (obj: DBM | BM) => ID | ||
/** | ||
* It's a counter-part of `createNaturalId`. | ||
* Allows to provide a parser function to parse "natural id" into | ||
* DBM components (e.g accountId and some other property that is part of the id). | ||
*/ | ||
parseNaturalId: (id: ID) => Partial<DBM> | ||
/** | ||
* It is called only on `dao.create` method. | ||
* Dao.create method is called in: | ||
* | ||
* - getByIdOrEmpty, getByIdAsDBMOrEmpty | ||
* - patch, patchAsDBM | ||
*/ | ||
beforeCreate: (bm: Partial<BM>) => Partial<BM> | ||
/** | ||
* Called when loading things "as DBM" and validation is not skipped. | ||
* When loading things like BM/TM - other hooks get involved instead: | ||
* - beforeDBMToBM | ||
* - beforeBMToTM | ||
* | ||
* TODO: maybe rename those to `validateAs(model)` | ||
* as it only validates "final state", not intermediate | ||
*/ | ||
beforeDBMValidate: (dbm: Partial<DBM>) => Partial<DBM> | ||
beforeDBMToBM: (dbm: DBM) => Partial<BM> | Promise<Partial<BM>> | ||
beforeBMToDBM: (bm: BM) => Partial<DBM> | Promise<Partial<DBM>> | ||
beforeTMToBM: (tm: TM) => Partial<BM> | ||
beforeBMToTM: (bm: BM) => Partial<TM> | ||
/** | ||
* Called in: | ||
* - dbmToBM (applied before DBM becomes BM) | ||
* - anyToDBM | ||
* | ||
* Hook only allows to apply anonymization to DBM (not to BM). | ||
* It still applies to BM "transitively", during dbmToBM | ||
* (e.g after loaded from the Database). | ||
*/ | ||
anonymize: (dbm: DBM) => DBM | ||
@@ -42,0 +82,0 @@ |
@@ -98,3 +98,2 @@ import { | ||
beforeBMToDBM: bm => bm as any, | ||
beforeTMToBM: tm => tm as any, | ||
beforeBMToTM: bm => bm as any, | ||
@@ -1065,24 +1064,2 @@ anonymize: dbm => dbm, | ||
tmToBM(tm: undefined, opt?: CommonDaoOptions): undefined | ||
tmToBM(tm?: TM, opt?: CommonDaoOptions): BM | ||
tmToBM(tm?: TM, opt: CommonDaoOptions = {}): BM | undefined { | ||
if (!tm) return | ||
// optimization: 1 validation is enough | ||
// Validate/convert TM | ||
// bm gets assigned to the new reference | ||
// tm = this.validateAndConvert(tm, this.cfg.tmSchema, DBModelType.TM, opt) | ||
// TM > BM | ||
const bm = this.cfg.hooks!.beforeTMToBM!(tm) as BM | ||
// Validate/convert BM | ||
return this.validateAndConvert<BM>(bm, this.cfg.bmSchema, DBModelType.BM, opt) | ||
} | ||
tmsToBM(tms: TM[], opt: CommonDaoOptions = {}): BM[] { | ||
// try/catch? | ||
return tms.map(tm => this.tmToBM(tm, opt)) | ||
} | ||
/** | ||
@@ -1089,0 +1066,0 @@ * Returns *converted value*. |
@@ -13,3 +13,2 @@ import { ObjectWithId } from '@naturalcycles/js-lib' | ||
// eslint-disable-next-line @typescript-eslint/no-empty-interface | ||
export interface CommonDBOptions {} | ||
@@ -16,0 +15,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
395098
10312