Skip to content
This repository has been archived by the owner on May 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #69 from samchon/v1.0
Browse files Browse the repository at this point in the history
Close #68
  • Loading branch information
samchon authored Aug 3, 2022
2 parents 15450b1 + 74056bd commit 2193e02
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
12 changes: 6 additions & 6 deletions src/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ export abstract class Model extends orm.BaseEntity
(
this: Model.Creator<T>,
fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>,
param: Field.MemberType<T, Literal> | null
param: Field.MemberType<T, Literal> | null | (() => string)
): [string, Record<string, Field.ValueType<T[Literal]>>];

/**
Expand Down Expand Up @@ -270,9 +270,9 @@ export abstract class Model extends orm.BaseEntity
this: Model.Creator<T>,
fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>,
operator: OperatorType,
param: OperatorType extends "="|"!="|"<>"
param: (OperatorType extends "="|"!="|"<>"
? Field.MemberType<T, Literal> | null
: Field.MemberType<T, Literal>
: Field.MemberType<T, Literal>) | (() => string)
): [string, Record<string, [Field.ValueType<T[Literal]>, Field.ValueType<T[Literal]>]>];

/**
Expand Down Expand Up @@ -304,7 +304,7 @@ export abstract class Model extends orm.BaseEntity
this: Model.Creator<T>,
fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>,
operator: "IN" | "NOT IN",
parameters: Array<Field.MemberType<T, Literal>>,
parameters: Array<Field.MemberType<T, Literal>> | (() => string),
): [string, Record<string, Array<Field.ValueType<T[Literal]>>>];

/**
Expand Down Expand Up @@ -339,8 +339,8 @@ export abstract class Model extends orm.BaseEntity
this: Model.Creator<T>,
fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>,
operator: "BETWEEN",
minimum: Field.MemberType<T, Literal>,
maximum: Field.MemberType<T, Literal>
minimum: Field.MemberType<T, Literal> | (() => string),
maximum: Field.MemberType<T, Literal> | (() => string)
): [string, { [key: string]: Array<Field.ValueType<T[Literal]>> }];

public static getWhereArguments<
Expand Down
20 changes: 12 additions & 8 deletions src/functional/getWhereArguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function getWhereArguments<
(
creator: Creator<T>,
fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>,
param: Field.MemberType<T, Literal> | null
param: Field.MemberType<T, Literal> | null | (() => string)
): [string, Record<string, Field.ValueType<T[Literal]>>];

/**
Expand Down Expand Up @@ -77,9 +77,9 @@ export function getWhereArguments<
creator: Creator<T>,
fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>,
operator: OperatorType,
param: OperatorType extends "="|"!="|"<>"
param: (OperatorType extends "="|"!="|"<>"
? Field.MemberType<T, Literal> | null
: Field.MemberType<T, Literal>
: Field.MemberType<T, Literal>) | (() => string)
): [string, Record<string, Field.ValueType<T[Literal]>>];

/**
Expand Down Expand Up @@ -114,7 +114,7 @@ export function getWhereArguments<
creator: Creator<T>,
fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>,
operator: "IN" | "NOT IN",
parameters: Array<Field.MemberType<T, Literal>>
parameters: Array<Field.MemberType<T, Literal>> | (() => string)
): [string, Record<string, [Field.ValueType<T[Literal]>, Field.ValueType<T[Literal]>]>];

/**
Expand Down Expand Up @@ -150,8 +150,8 @@ export function getWhereArguments<
creator: Creator<T>,
fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>,
operator: "BETWEEN",
minimum: Field.MemberType<T, Literal>,
maximum: Field.MemberType<T, Literal>
minimum: Field.MemberType<T, Literal> | (() => string),
maximum: Field.MemberType<T, Literal> | (() => string)
): [string, Record<string, Array<Field.ValueType<T[Literal]>>>];

export function getWhereArguments<
Expand Down Expand Up @@ -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")
Expand All @@ -221,7 +225,7 @@ export function getWhereArguments<
const minimum: Field.ValueType<T[Literal]> = _Decompose_entity(rest[1]);
const maximum: Field.ValueType<T[Literal]> = _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
Expand All @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/test/features/test_get_where_arguments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { BbsArticle } from "../models/bbs/BbsArticle";

export function test_get_where_arguments(): void {
BbsArticle.getWhereArguments("created_at", "<", () => "NOW()");
}
Original file line number Diff line number Diff line change
Expand Up @@ -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))
};
}

0 comments on commit 2193e02

Please sign in to comment.