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

Close #66 #67

Merged
merged 1 commit into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.11",
"version": "1.0.12",
"description": "Safe Relationship Decorators for the TypeORM",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
11 changes: 6 additions & 5 deletions src/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -233,7 +234,7 @@ export abstract class Model extends orm.BaseEntity
Literal extends SpecialFields<T, Field>>
(
this: Model.Creator<T>,
fieldLike: `${Literal}` | `${string}.${Literal}`,
fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>,
param: Field.MemberType<T, Literal> | null
): [string, Record<string, Field.ValueType<T[Literal]>>];

Expand Down Expand Up @@ -267,7 +268,7 @@ export abstract class Model extends orm.BaseEntity
OperatorType extends Operator>
(
this: Model.Creator<T>,
fieldLike: `${Literal}` | `${string}.${Literal}`,
fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>,
operator: OperatorType,
param: OperatorType extends "="|"!="|"<>"
? Field.MemberType<T, Literal> | null
Expand Down Expand Up @@ -301,7 +302,7 @@ export abstract class Model extends orm.BaseEntity
Literal extends SpecialFields<T, Field>>
(
this: Model.Creator<T>,
fieldLike: `${Literal}` | `${string}.${Literal}`,
fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>,
operator: "IN" | "NOT IN",
parameters: Array<Field.MemberType<T, Literal>>,
): [string, Record<string, Array<Field.ValueType<T[Literal]>>>];
Expand Down Expand Up @@ -336,7 +337,7 @@ export abstract class Model extends orm.BaseEntity
Literal extends SpecialFields<T, Field>>
(
this: Model.Creator<T>,
fieldLike: `${Literal}` | `${string}.${Literal}`,
fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>,
operator: "BETWEEN",
minimum: Field.MemberType<T, Literal>,
maximum: Field.MemberType<T, Literal>
Expand All @@ -347,7 +348,7 @@ export abstract class Model extends orm.BaseEntity
Literal extends SpecialFields<T, Field>>
(
this: Model.Creator<T>,
fieldLike: `${Literal}` | `${string}.${Literal}`,
fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>,
...rest: any[]
): [string, any]
{
Expand Down
34 changes: 22 additions & 12 deletions src/functional/getWhereArguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -39,7 +40,7 @@ export function getWhereArguments<
Literal extends SpecialFields<T, Field>>
(
creator: Creator<T>,
fieldLike: `${Literal}` | `${string}.${Literal}`,
fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>,
param: Field.MemberType<T, Literal> | null
): [string, Record<string, Field.ValueType<T[Literal]>>];

Expand Down Expand Up @@ -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, Field>,
T extends { [P in Literal]: Field; },
Literal extends SpecialFields<T, Field>,
OperatorType extends Operator>
(
creator: Creator<T>,
fieldLike: `${Literal}` | `${string}.${Literal}`,
fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>,
operator: OperatorType,
param: OperatorType extends "="|"!="|"<>"
? Field.MemberType<T, Literal> | null
Expand Down Expand Up @@ -111,7 +112,7 @@ export function getWhereArguments<
Literal extends SpecialFields<T, Field>>
(
creator: Creator<T>,
fieldLike: `${Literal}` | `${string}.${Literal}`,
fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>,
operator: "IN" | "NOT IN",
parameters: Array<Field.MemberType<T, Literal>>
): [string, Record<string, [Field.ValueType<T[Literal]>, Field.ValueType<T[Literal]>]>];
Expand Down Expand Up @@ -147,7 +148,7 @@ export function getWhereArguments<
Literal extends SpecialFields<T, Field>>
(
creator: Creator<T>,
fieldLike: `${Literal}` | `${string}.${Literal}`,
fieldLike: WhereColumnType<`${Literal}` | `${string}.${Literal}`>,
operator: "BETWEEN",
minimum: Field.MemberType<T, Literal>,
maximum: Field.MemberType<T, Literal>
Expand All @@ -158,14 +159,23 @@ export function getWhereArguments<
Literal extends SpecialFields<T, Field>>
(
creator: Creator<T>,
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)
Expand All @@ -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.`);
}
Expand All @@ -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
Expand All @@ -211,7 +221,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 [`${column} BETWEEN :${from} AND :${to}`,
return [`${left} BETWEEN :${from} AND :${to}`,
{
[from]: minimum,
[to]: maximum
Expand Down
6 changes: 4 additions & 2 deletions src/typings/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ export type Field
| boolean
| Date
| Belongs.ManyToOne<any, PrimaryColumnType>
| Belongs.External.ManyToOne<any, PrimaryColumnType>
| Belongs.OneToOne<any, PrimaryColumnType>
| Belongs.External.OneToOne<any, PrimaryColumnType>
| null;

export namespace Field
{
export type ValueType<Type extends Field>
= Type extends string ? Type
: Type extends Belongs.ManyToOne<infer Target, infer KeyType, infer Options>
: Type extends (Belongs.ManyToOne<infer Target, infer KeyType, infer Options> | Belongs.External.ManyToOne<infer Target, infer KeyType, infer Options>)
? Options extends { nullable: true }
? (ModelLike<Target, KeyType, true> | PrimaryColumnType.ValueType<KeyType> | null)
: (ModelLike<Target, KeyType, false> | PrimaryColumnType.ValueType<KeyType>)
: Type extends Belongs.OneToOne<infer Target, infer KeyType, infer Options>
: Type extends (Belongs.OneToOne<infer Target, infer KeyType, infer Options> | Belongs.External.OneToOne<infer Target, infer KeyType, infer Options>)
? Options extends { nullable: true }
? (ModelLike<Target, KeyType, true> | PrimaryColumnType.ValueType<KeyType> | null)
: (ModelLike<Target, KeyType, false> | PrimaryColumnType.ValueType<KeyType>)
Expand Down
3 changes: 3 additions & 0 deletions src/typings/WhereColumnType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type WhereColumnType<Literal extends string>
= Literal
| [Literal, (str: string) => string];
3 changes: 2 additions & 1 deletion src/typings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export * from "./Primitive";
export * from "./Relationship";
export * from "./Same";
export * from "./SpecialFields";
export * from "./StringColumnType";
export * from "./StringColumnType";
export * from "./WhereColumnType";