From 74056bd892493176b363265e164e463696f38a24 Mon Sep 17 00:00:00 2001 From: Jeongho Nam Date: Wed, 3 Aug 2022 13:53:26 +0900 Subject: [PATCH] Close #68 --- package.json | 2 +- src/Model.ts | 12 +++++------ src/functional/getWhereArguments.ts | 20 +++++++++++-------- src/test/features/test_get_where_arguments.ts | 5 +++++ .../generators/generate_random_external_db.ts | 2 +- 5 files changed, 25 insertions(+), 16 deletions(-) create mode 100644 src/test/features/test_get_where_arguments.ts diff --git a/package.json b/package.json index b77467c..c370103 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "safe-typeorm", - "version": "1.0.12", + "version": "1.0.13", "description": "Safe Relationship Decorators for the TypeORM", "main": "lib/index.js", "typings": "lib/index.d.ts", diff --git a/src/Model.ts b/src/Model.ts index e7cf3ea..54dcee5 100644 --- a/src/Model.ts +++ b/src/Model.ts @@ -235,7 +235,7 @@ export abstract class Model extends orm.BaseEntity ( this: Model.Creator, fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>, - param: Field.MemberType | null + param: Field.MemberType | null | (() => string) ): [string, Record>]; /** @@ -270,9 +270,9 @@ export abstract class Model extends orm.BaseEntity this: Model.Creator, fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>, operator: OperatorType, - param: OperatorType extends "="|"!="|"<>" + param: (OperatorType extends "="|"!="|"<>" ? Field.MemberType | null - : Field.MemberType + : Field.MemberType) | (() => string) ): [string, Record, Field.ValueType]>]; /** @@ -304,7 +304,7 @@ export abstract class Model extends orm.BaseEntity this: Model.Creator, fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>, operator: "IN" | "NOT IN", - parameters: Array>, + parameters: Array> | (() => string), ): [string, Record>>]; /** @@ -339,8 +339,8 @@ export abstract class Model extends orm.BaseEntity this: Model.Creator, fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>, operator: "BETWEEN", - minimum: Field.MemberType, - maximum: Field.MemberType + minimum: Field.MemberType | (() => string), + maximum: Field.MemberType | (() => string) ): [string, { [key: string]: Array> }]; public static getWhereArguments< diff --git a/src/functional/getWhereArguments.ts b/src/functional/getWhereArguments.ts index cd013eb..f1596cf 100644 --- a/src/functional/getWhereArguments.ts +++ b/src/functional/getWhereArguments.ts @@ -41,7 +41,7 @@ export function getWhereArguments< ( creator: Creator, fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>, - param: Field.MemberType | null + param: Field.MemberType | null | (() => string) ): [string, Record>]; /** @@ -77,9 +77,9 @@ export function getWhereArguments< creator: Creator, fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>, operator: OperatorType, - param: OperatorType extends "="|"!="|"<>" + param: (OperatorType extends "="|"!="|"<>" ? Field.MemberType | null - : Field.MemberType + : Field.MemberType) | (() => string) ): [string, Record>]; /** @@ -114,7 +114,7 @@ export function getWhereArguments< creator: Creator, fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>, operator: "IN" | "NOT IN", - parameters: Array> + parameters: Array> | (() => string) ): [string, Record, Field.ValueType]>]; /** @@ -150,8 +150,8 @@ export function getWhereArguments< creator: Creator, fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>, operator: "BETWEEN", - minimum: Field.MemberType, - maximum: Field.MemberType + minimum: Field.MemberType | (() => string), + maximum: Field.MemberType | (() => string) ): [string, Record>>]; export function getWhereArguments< @@ -207,6 +207,10 @@ export function getWhereArguments< throw new InvalidArgument(`Error on ${creator.name}.getColumn(): unable to bind null value for the ${operator} operator.`); } + // RETURNS RAW QUERY + if (typeof param === "function") + return [`${left} ${operator} ${param()}`, {}]; + // RETURNS WITH BINDING const uuid: string = crypto.randomBytes(64).toString("hex"); const binding: string = (operator === "IN" || operator === "NOT IN") @@ -221,7 +225,7 @@ export function getWhereArguments< const minimum: Field.ValueType = _Decompose_entity(rest[1]); const maximum: Field.ValueType = _Decompose_entity(rest[2]); - return [`${left} BETWEEN :${from} AND :${to}`, + return [`${left} BETWEEN ${typeof minimum === "function" ? minimum() : `:${from}`} AND ${typeof maximum === "function" ? maximum() : `:${to}`}`, { [from]: minimum, [to]: maximum @@ -238,7 +242,7 @@ function _Decompose_parameter(param: any): any function _Decompose_entity(param: any): any { - if (param instanceof Object && !(param instanceof Date)) + if (typeof param !== "function" && param instanceof Object && !(param instanceof Date)) { if (param instanceof BelongsAccessorBase) param = param.id; diff --git a/src/test/features/test_get_where_arguments.ts b/src/test/features/test_get_where_arguments.ts new file mode 100644 index 0000000..89bb4ee --- /dev/null +++ b/src/test/features/test_get_where_arguments.ts @@ -0,0 +1,5 @@ +import { BbsArticle } from "../models/bbs/BbsArticle"; + +export function test_get_where_arguments(): void { + BbsArticle.getWhereArguments("created_at", "<", () => "NOW()"); +} \ No newline at end of file diff --git a/src/test/internal/generators/generate_random_external_db.ts b/src/test/internal/generators/generate_random_external_db.ts index 348b9f4..f8470fc 100644 --- a/src/test/internal/generators/generate_random_external_db.ts +++ b/src/test/internal/generators/generate_random_external_db.ts @@ -10,7 +10,7 @@ export async function generate_random_external_db() await collection.execute(); return { - group: await BbsGroup.findOneOrFail(group.id), + group: await BbsGroup.findOneOrFail({ id: group.id }), users: await BlogUser.findByIds(users.map(u => u.id)) }; } \ No newline at end of file