Skip to content

Commit

Permalink
Merge pull request #102 from UN-OCHA/skip-validation
Browse files Browse the repository at this point in the history
Add `skipValidation` support to `find` and `update` methods
  • Loading branch information
Pl217 authored Jul 15, 2022
2 parents 601de5a + c4dea05 commit 7808585
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
28 changes: 27 additions & 1 deletion src/db/util/raw-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ export type FindFn<F extends FieldDefinition, AdditionalArgs = {}> = (
limit?: number;
offset?: number;
orderBy?: OrderByCond<F> | Array<OrderByCond<F>>;
/**
* WARNING: Only use this in very rare cases when performance gains
* from disabling validation are worth the risk!
*/
skipValidation?: boolean;
trx?: Knex.Transaction<any, any>;
} & AdditionalArgs
) => Promise<Array<InstanceDataOf<F>>>;
Expand All @@ -53,6 +58,11 @@ export type FindOneFn<F extends FieldDefinition, AdditionalArgs = {}> = (
export type UpdateFn<F extends FieldDefinition> = (args: {
values: Partial<UserDataOf<F>>;
where: WhereCond<F>;
/**
* WARNING: Only use this in very rare cases when performance gains
* from disabling validation are worth the risk!
*/
skipValidation?: boolean;
trx?: Knex.Transaction<any, any>;
}) => Promise<Array<InstanceDataOf<F>>>;

Expand Down Expand Up @@ -138,6 +148,7 @@ export const defineRawModel =
limit,
offset,
orderBy,
skipValidation = false,
trx,
} = {}) => {
const builder = trx ? tbl().transacting(trx) : tbl();
Expand All @@ -159,6 +170,11 @@ export const defineRawModel =
}

const res = await query;

if (skipValidation) {
return res as Instance[];
}

return res.map(validateAndFilter);
};

Expand All @@ -173,12 +189,22 @@ export const defineRawModel =
}
};

const update: UpdateFn<F> = async ({ values, where, trx }) => {
const update: UpdateFn<F> = 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);
};

Expand Down

0 comments on commit 7808585

Please sign in to comment.