diff --git a/package.json b/package.json index 5899369..b77467c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "safe-typeorm", - "version": "1.0.11", + "version": "1.0.12", "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 f07ab5c..e7cf3ea 100644 --- a/src/Model.ts +++ b/src/Model.ts @@ -22,6 +22,7 @@ import { initialize } from "./functional/initialize"; import { insert } from "./functional/insert"; import { toPrimitive } from "./functional/toPrimitive"; import { update } from "./functional/update"; +import { WhereColumnType } from "./typings/WhereColumnType"; /** * The basic model class. @@ -233,7 +234,7 @@ export abstract class Model extends orm.BaseEntity Literal extends SpecialFields> ( this: Model.Creator, - fieldLike: `${Literal}` | `${string}.${Literal}`, + fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>, param: Field.MemberType | null ): [string, Record>]; @@ -267,7 +268,7 @@ export abstract class Model extends orm.BaseEntity OperatorType extends Operator> ( this: Model.Creator, - fieldLike: `${Literal}` | `${string}.${Literal}`, + fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>, operator: OperatorType, param: OperatorType extends "="|"!="|"<>" ? Field.MemberType | null @@ -301,7 +302,7 @@ export abstract class Model extends orm.BaseEntity Literal extends SpecialFields> ( this: Model.Creator, - fieldLike: `${Literal}` | `${string}.${Literal}`, + fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>, operator: "IN" | "NOT IN", parameters: Array>, ): [string, Record>>]; @@ -336,7 +337,7 @@ export abstract class Model extends orm.BaseEntity Literal extends SpecialFields> ( this: Model.Creator, - fieldLike: `${Literal}` | `${string}.${Literal}`, + fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>, operator: "BETWEEN", minimum: Field.MemberType, maximum: Field.MemberType @@ -347,7 +348,7 @@ export abstract class Model extends orm.BaseEntity Literal extends SpecialFields> ( this: Model.Creator, - fieldLike: `${Literal}` | `${string}.${Literal}`, + fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>, ...rest: any[] ): [string, any] { diff --git a/src/functional/getWhereArguments.ts b/src/functional/getWhereArguments.ts index 815ebff..cd013eb 100644 --- a/src/functional/getWhereArguments.ts +++ b/src/functional/getWhereArguments.ts @@ -9,6 +9,7 @@ import { SpecialFields } from "../typings/SpecialFields"; import { BelongsAccessorBase } from "../decorators/base/BelongsAccessorBase"; import { findRepository } from "./findRepository"; import { get_column_name_tuple } from "./internal/get_column_name_tuple"; +import { WhereColumnType } from "../typings/WhereColumnType"; /** * Get arguments for the where-equal query. @@ -39,7 +40,7 @@ export function getWhereArguments< Literal extends SpecialFields> ( creator: Creator, - fieldLike: `${Literal}` | `${string}.${Literal}`, + fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>, param: Field.MemberType | null ): [string, Record>]; @@ -69,12 +70,12 @@ export function getWhereArguments< * never can be the runtime error */ export function getWhereArguments< - T extends { [P in Literal]: Field; }, - Literal extends SpecialFields, + T extends { [P in Literal]: Field; }, + Literal extends SpecialFields, OperatorType extends Operator> ( creator: Creator, - fieldLike: `${Literal}` | `${string}.${Literal}`, + fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>, operator: OperatorType, param: OperatorType extends "="|"!="|"<>" ? Field.MemberType | null @@ -111,7 +112,7 @@ export function getWhereArguments< Literal extends SpecialFields> ( creator: Creator, - fieldLike: `${Literal}` | `${string}.${Literal}`, + fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>, operator: "IN" | "NOT IN", parameters: Array> ): [string, Record, Field.ValueType]>]; @@ -147,7 +148,7 @@ export function getWhereArguments< Literal extends SpecialFields> ( creator: Creator, - fieldLike: `${Literal}` | `${string}.${Literal}`, + fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>, operator: "BETWEEN", minimum: Field.MemberType, maximum: Field.MemberType @@ -158,14 +159,23 @@ export function getWhereArguments< Literal extends SpecialFields> ( creator: Creator, - fieldLike: `${Literal}` | `${string}.${Literal}`, + fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>, ...rest: any[] ): [string, any] { - const tuple: [string, string] = get_column_name_tuple(creator, fieldLike); + const tuple: [string, string] = get_column_name_tuple + ( + creator, + typeof fieldLike === "string" + ? fieldLike + : fieldLike[0] + ); const column: string = tuple[0] ? `${tuple[0]}.${tuple[1]}` : tuple[1]; + const left: string = typeof fieldLike === "string" + ? column + : fieldLike[1](column); // MOST OPERATORS if (rest.length <= 2) @@ -190,9 +200,9 @@ export function getWhereArguments< if (param === null) { if (operator === "=") - return [`${column} IS NULL`] as any; + return [`${left} IS NULL`] as any; else if (operator === "!=" || operator === "<>") - return [`${column} IS NOT NULL`] as any; + return [`${left} IS NOT NULL`] as any; else throw new InvalidArgument(`Error on ${creator.name}.getColumn(): unable to bind null value for the ${operator} operator.`); } @@ -202,7 +212,7 @@ export function getWhereArguments< const binding: string = (operator === "IN" || operator === "NOT IN") ? `(:...${uuid})` : `:${uuid}`; - return [`${column} ${operator} ${binding}`, { [uuid]: param }]; + return [`${left} ${operator} ${binding}`, { [uuid]: param }]; } // BETWEEN OPERATOR @@ -211,7 +221,7 @@ export function getWhereArguments< const minimum: Field.ValueType = _Decompose_entity(rest[1]); const maximum: Field.ValueType = _Decompose_entity(rest[2]); - return [`${column} BETWEEN :${from} AND :${to}`, + return [`${left} BETWEEN :${from} AND :${to}`, { [from]: minimum, [to]: maximum diff --git a/src/typings/Field.ts b/src/typings/Field.ts index 20341e7..93e8ca3 100644 --- a/src/typings/Field.ts +++ b/src/typings/Field.ts @@ -9,18 +9,20 @@ export type Field | boolean | Date | Belongs.ManyToOne + | Belongs.External.ManyToOne | Belongs.OneToOne + | Belongs.External.OneToOne | null; export namespace Field { export type ValueType = Type extends string ? Type - : Type extends Belongs.ManyToOne + : Type extends (Belongs.ManyToOne | Belongs.External.ManyToOne) ? Options extends { nullable: true } ? (ModelLike | PrimaryColumnType.ValueType | null) : (ModelLike | PrimaryColumnType.ValueType) - : Type extends Belongs.OneToOne + : Type extends (Belongs.OneToOne | Belongs.External.OneToOne) ? Options extends { nullable: true } ? (ModelLike | PrimaryColumnType.ValueType | null) : (ModelLike | PrimaryColumnType.ValueType) diff --git a/src/typings/WhereColumnType.ts b/src/typings/WhereColumnType.ts new file mode 100644 index 0000000..b8be0da --- /dev/null +++ b/src/typings/WhereColumnType.ts @@ -0,0 +1,3 @@ +export type WhereColumnType + = Literal + | [Literal, (str: string) => string]; \ No newline at end of file diff --git a/src/typings/index.ts b/src/typings/index.ts index 84a2424..5827cb5 100644 --- a/src/typings/index.ts +++ b/src/typings/index.ts @@ -10,4 +10,5 @@ export * from "./Primitive"; export * from "./Relationship"; export * from "./Same"; export * from "./SpecialFields"; -export * from "./StringColumnType"; \ No newline at end of file +export * from "./StringColumnType"; +export * from "./WhereColumnType"; \ No newline at end of file