Skip to content

Commit

Permalink
feat: unify skipValidation and skipConversion
Browse files Browse the repository at this point in the history
skipValidation now works as skipConversion.
skipConversion is no longer supported (it's skipValidation or nothing).
This is for the sake of simplicity.
  • Loading branch information
kirillgroshkov committed Mar 25, 2024
1 parent 7ce6508 commit a02efba
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 30 deletions.
6 changes: 0 additions & 6 deletions scripts/cannon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,6 @@ async function register2(): Promise<any> {
}

async function register3(): Promise<any> {
let item = createTestItemsBM(1).map(r => _omit(r, ['id']))[0]!
item = await dao.save(item, { skipConversion: true })
return { item }
}

async function register4(): Promise<any> {
let item = createTestItemsBM(1).map(r => _omit(r, ['id']))[0]!
item = await dao.save(item, { skipValidation: true })
return { item }
Expand Down
20 changes: 3 additions & 17 deletions src/commondao/common.dao.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,22 +215,13 @@ export interface CommonDaoCfg<BM extends BaseDBEntity, DBM extends BaseDBEntity
*/
export interface CommonDaoOptions extends CommonDBOptions {
/**
* If true - will ignore the validation result, but will STILL DO the validation step, which will DO conversion
* (according to Joi schema).
*
* Set skipConversion=true (or raw=true) to bypass conversion step as well (e.g for performance reasons).
* Defaults to false.
*
* @default false
* If set to true - will disable validation (and conversion).
* One possible use case of doing this is - performance (as validation/conversion takes time, especially with Joi).
*/
skipValidation?: boolean

/**
* If true - will SKIP the joi validation AND conversion steps alltogether. To improve performance of DAO.
*
* @default false
*/
skipConversion?: boolean

/**
* @default false
*/
Expand Down Expand Up @@ -314,11 +305,6 @@ export interface CommonDaoStreamOptions<IN>
*/
skipValidation?: boolean

/**
* @default true (for streams)
*/
skipConversion?: boolean

/**
* @default ErrorMode.SUPPRESS for returning ReadableStream, because .pipe() has no concept of "error propagation"
* @default ErrorMode.SUPPRESS for .forEach() streams as well, but overridable
Expand Down
9 changes: 2 additions & 7 deletions src/commondao/common.dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,6 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
): Promise<void> {
q.table = opt.table || q.table
opt.skipValidation = opt.skipValidation !== false // default true
opt.skipConversion = opt.skipConversion !== false // default true
opt.errorMode ||= ErrorMode.SUPPRESS

const partialQuery = !!q._selectedFieldNames
Expand Down Expand Up @@ -405,7 +404,6 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
): Promise<void> {
q.table = opt.table || q.table
opt.skipValidation = opt.skipValidation !== false // default true
opt.skipConversion = opt.skipConversion !== false // default true
opt.errorMode ||= ErrorMode.SUPPRESS

const partialQuery = !!q._selectedFieldNames
Expand Down Expand Up @@ -454,7 +452,6 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
streamQueryAsDBM(q: DBQuery<DBM>, opt: CommonDaoStreamOptions<DBM> = {}): ReadableTyped<DBM> {
q.table = opt.table || q.table
opt.skipValidation = opt.skipValidation !== false // default true
opt.skipConversion = opt.skipConversion !== false // default true
opt.errorMode ||= ErrorMode.SUPPRESS

const partialQuery = !!q._selectedFieldNames
Expand Down Expand Up @@ -493,7 +490,6 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
streamQuery(q: DBQuery<DBM>, opt: CommonDaoStreamOptions<BM> = {}): ReadableTyped<BM> {
q.table = opt.table || q.table
opt.skipValidation = opt.skipValidation !== false // default true
opt.skipConversion = opt.skipConversion !== false // default true
opt.errorMode ||= ErrorMode.SUPPRESS

const stream = this.cfg.db.streamQuery<DBM>(q, opt)
Expand Down Expand Up @@ -902,7 +898,6 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {

const table = opt.table || this.cfg.table
opt.skipValidation ??= true
opt.skipConversion ??= true
opt.errorMode ||= ErrorMode.SUPPRESS

if (this.cfg.immutable && !opt.allowMutability && !opt.saveMethod) {
Expand Down Expand Up @@ -1157,7 +1152,7 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
// Return as is if no schema is passed or if `skipConversion` is set
if (
!schema ||
opt.skipConversion ||
opt.skipValidation ||
(op === 'load' && !this.cfg.validateOnLoad) ||
(op === 'save' && !this.cfg.validateOnSave)
) {
Expand Down Expand Up @@ -1191,7 +1186,7 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
}

// If we care about validation and there's an error
if (error && !opt.skipValidation) {
if (error) {
const processedError = this.cfg.hooks!.onValidationError!(error)

if (processedError) throw processedError
Expand Down

0 comments on commit a02efba

Please sign in to comment.