diff --git a/package.json b/package.json index dca81376..6684ecf9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@unocha/hpc-api-core", - "version": "4.4.3", + "version": "4.4.4", "description": "Core libraries supporting HPC.Tools API Backend", "license": "Apache-2.0", "private": false, diff --git a/src/db/util/raw-model.ts b/src/db/util/raw-model.ts index 639ac5ce..040d4b2c 100644 --- a/src/db/util/raw-model.ts +++ b/src/db/util/raw-model.ts @@ -39,6 +39,11 @@ export type FindFn = ( limit?: number; offset?: number; orderBy?: OrderByCond | Array>; + /** + * WARNING: Only use this in very rare cases when performance gains + * from disabling validation are worth the risk! + */ + skipValidation?: boolean; trx?: Knex.Transaction; } & AdditionalArgs ) => Promise>>; @@ -53,6 +58,11 @@ export type FindOneFn = ( export type UpdateFn = (args: { values: Partial>; where: WhereCond; + /** + * WARNING: Only use this in very rare cases when performance gains + * from disabling validation are worth the risk! + */ + skipValidation?: boolean; trx?: Knex.Transaction; }) => Promise>>; @@ -138,6 +148,7 @@ export const defineRawModel = limit, offset, orderBy, + skipValidation = false, trx, } = {}) => { const builder = trx ? tbl().transacting(trx) : tbl(); @@ -159,6 +170,11 @@ export const defineRawModel = } const res = await query; + + if (skipValidation) { + return res as Instance[]; + } + return res.map(validateAndFilter); }; @@ -173,12 +189,22 @@ export const defineRawModel = } }; - const update: UpdateFn = async ({ values, where, trx }) => { + const update: UpdateFn = async ({ + values, + where, + skipValidation = false, + trx, + }) => { const builder = trx ? tbl().transacting(trx) : tbl(); const res = await builder .where(prepareCondition(where || {})) .update(values as any) .returning('*'); + + if (skipValidation) { + return res as Instance[]; + } + return (res as unknown[]).map(validateAndFilter); };