Skip to content

Commit

Permalink
fix: decorators should implement the official TypeScript interface (#…
Browse files Browse the repository at this point in the history
…6398)

Closes: #5922
  • Loading branch information
SakirSoft committed Jul 17, 2020
1 parent 874e573 commit c23c888
Show file tree
Hide file tree
Showing 39 changed files with 111 additions and 96 deletions.
8 changes: 4 additions & 4 deletions src/decorator/Check.ts
Expand Up @@ -6,29 +6,29 @@ import {CheckMetadataArgs} from "../metadata-args/CheckMetadataArgs";
* Can be used on entity property or on entity.
* Can create checks with composite columns when used on entity.
*/
export function Check(expression: string): Function;
export function Check(expression: string): ClassDecorator & PropertyDecorator;

/**
* Creates a database check.
* Can be used on entity property or on entity.
* Can create checks with composite columns when used on entity.
*/
export function Check(name: string, expression: string): Function;
export function Check(name: string, expression: string): ClassDecorator & PropertyDecorator;

/**
* Creates a database check.
* Can be used on entity property or on entity.
* Can create checks with composite columns when used on entity.
*/
export function Check(nameOrExpression: string, maybeExpression?: string): Function {
export function Check(nameOrExpression: string, maybeExpression?: string): ClassDecorator & PropertyDecorator {

const name = maybeExpression ? nameOrExpression : undefined;
const expression = maybeExpression ? maybeExpression : nameOrExpression;

if (!expression)
throw new Error(`Check expression is required`);

return function (clsOrObject: Function|Object, propertyName?: string) {
return function (clsOrObject: Function|Object, propertyName?: string | symbol) {

getMetadataArgsStorage().checks.push({
target: propertyName ? clsOrObject.constructor : clsOrObject as Function,
Expand Down
2 changes: 1 addition & 1 deletion src/decorator/EntityRepository.ts
Expand Up @@ -7,7 +7,7 @@ import { EntitySchema } from "../entity-schema/EntitySchema";
* Custom repository can manage some specific entity or just be generic.
* Custom repository optionally can extend AbstractRepository, Repository or TreeRepository.
*/
export function EntityRepository(entity?: Function | EntitySchema<any>): Function {
export function EntityRepository(entity?: Function | EntitySchema<any>): ClassDecorator {
return function (target: Function) {

getMetadataArgsStorage().entityRepositories.push({
Expand Down
8 changes: 4 additions & 4 deletions src/decorator/Exclusion.ts
Expand Up @@ -6,29 +6,29 @@ import {ExclusionMetadataArgs} from "../metadata-args/ExclusionMetadataArgs";
* Can be used on entity.
* Can create exclusions with composite columns when used on entity.
*/
export function Exclusion(expression: string): Function;
export function Exclusion(expression: string): ClassDecorator & PropertyDecorator;

/**
* Creates a database exclusion.
* Can be used on entity.
* Can create exclusions with composite columns when used on entity.
*/
export function Exclusion(name: string, expression: string): Function;
export function Exclusion(name: string, expression: string): ClassDecorator & PropertyDecorator;

/**
* Creates a database exclusion.
* Can be used on entity.
* Can create exclusions with composite columns when used on entity.
*/
export function Exclusion(nameOrExpression: string, maybeExpression?: string): Function {
export function Exclusion(nameOrExpression: string, maybeExpression?: string): ClassDecorator & PropertyDecorator {

const name = maybeExpression ? nameOrExpression : undefined;
const expression = maybeExpression ? maybeExpression : nameOrExpression;

if (!expression)
throw new Error(`Exclusion expression is required`);

return function (clsOrObject: Function|Object, propertyName?: string) {
return function (clsOrObject: Function|Object, propertyName?: string | symbol) {

getMetadataArgsStorage().exclusions.push({
target: propertyName ? clsOrObject.constructor : clsOrObject as Function,
Expand Down
2 changes: 1 addition & 1 deletion src/decorator/Generated.ts
Expand Up @@ -10,7 +10,7 @@ import {GeneratedMetadataArgs} from "../metadata-args/GeneratedMetadataArgs";
*
* Note, some databases do not support non-primary generation columns.
*/
export function Generated(strategy: "increment"|"uuid"|"rowid" = "increment"): Function {
export function Generated(strategy: "increment"|"uuid"|"rowid" = "increment"): PropertyDecorator {
return function (object: Object, propertyName: string) {

getMetadataArgsStorage().generations.push({
Expand Down
18 changes: 9 additions & 9 deletions src/decorator/Index.ts
Expand Up @@ -6,49 +6,49 @@ import {IndexMetadataArgs} from "../metadata-args/IndexMetadataArgs";
* Can be used on entity property or on entity.
* Can create indices with composite columns when used on entity.
*/
export function Index(options?: IndexOptions): Function;
export function Index(options?: IndexOptions): ClassDecorator & PropertyDecorator;

/**
* Creates a database index.
* Can be used on entity property or on entity.
* Can create indices with composite columns when used on entity.
*/
export function Index(name: string, options?: IndexOptions): Function;
export function Index(name: string, options?: IndexOptions): ClassDecorator & PropertyDecorator;

/**
* Creates a database index.
* Can be used on entity property or on entity.
* Can create indices with composite columns when used on entity.
*/
export function Index(name: string, options: { synchronize: false }): Function;
export function Index(name: string, options: { synchronize: false }): ClassDecorator & PropertyDecorator;

/**
* Creates a database index.
* Can be used on entity property or on entity.
* Can create indices with composite columns when used on entity.
*/
export function Index(name: string, fields: string[], options?: IndexOptions): Function;
export function Index(name: string, fields: string[], options?: IndexOptions): ClassDecorator & PropertyDecorator;

/**
* Creates a database index.
* Can be used on entity property or on entity.
* Can create indices with composite columns when used on entity.
*/
export function Index(fields: string[], options?: IndexOptions): Function;
export function Index(fields: string[], options?: IndexOptions): ClassDecorator & PropertyDecorator;

/**
* Creates a database index.
* Can be used on entity property or on entity.
* Can create indices with composite columns when used on entity.
*/
export function Index(fields: (object?: any) => (any[]|{ [key: string]: number }), options?: IndexOptions): Function;
export function Index(fields: (object?: any) => (any[]|{ [key: string]: number }), options?: IndexOptions): ClassDecorator & PropertyDecorator;

/**
* Creates a database index.
* Can be used on entity property or on entity.
* Can create indices with composite columns when used on entity.
*/
export function Index(name: string, fields: (object?: any) => (any[]|{ [key: string]: number }), options?: IndexOptions): Function;
export function Index(name: string, fields: (object?: any) => (any[]|{ [key: string]: number }), options?: IndexOptions): ClassDecorator & PropertyDecorator;

/**
* Creates a database index.
Expand All @@ -57,7 +57,7 @@ export function Index(name: string, fields: (object?: any) => (any[]|{ [key: str
*/
export function Index(nameOrFieldsOrOptions?: string|string[]|((object: any) => (any[]|{ [key: string]: number }))|IndexOptions,
maybeFieldsOrOptions?: ((object?: any) => (any[]|{ [key: string]: number }))|IndexOptions|string[]|{ synchronize: false },
maybeOptions?: IndexOptions): Function {
maybeOptions?: IndexOptions): ClassDecorator & PropertyDecorator {

// normalize parameters
const name = typeof nameOrFieldsOrOptions === "string" ? nameOrFieldsOrOptions : undefined;
Expand All @@ -66,7 +66,7 @@ export function Index(nameOrFieldsOrOptions?: string|string[]|((object: any) =>
if (!options)
options = (typeof maybeFieldsOrOptions === "object" && !Array.isArray(maybeFieldsOrOptions)) ? maybeFieldsOrOptions as IndexOptions : maybeOptions;

return function (clsOrObject: Function|Object, propertyName?: string) {
return function (clsOrObject: Function|Object, propertyName?: string | symbol) {

getMetadataArgsStorage().indices.push({
target: propertyName ? clsOrObject.constructor : clsOrObject as Function,
Expand Down
37 changes: 26 additions & 11 deletions src/decorator/Unique.ts
@@ -1,39 +1,54 @@
import {getMetadataArgsStorage} from "../index";
import {UniqueMetadataArgs} from "../metadata-args/UniqueMetadataArgs";
import { getMetadataArgsStorage } from "../index";
import { UniqueMetadataArgs } from "../metadata-args/UniqueMetadataArgs";

/**
* Composite unique constraint must be set on entity classes and must specify entity's fields to be unique.
*/
export function Unique(name: string, fields: string[]): Function;
export function Unique(name: string, fields: string[]): ClassDecorator & PropertyDecorator;

/**
* Composite unique constraint must be set on entity classes and must specify entity's fields to be unique.
*/
export function Unique(fields: string[]): Function;
export function Unique(fields: string[]): ClassDecorator & PropertyDecorator;

/**
* Composite unique constraint must be set on entity classes and must specify entity's fields to be unique.
*/
export function Unique(fields: (object?: any) => (any[]|{ [key: string]: number })): Function;
export function Unique(fields: (object?: any) => (any[] | { [key: string]: number })): ClassDecorator & PropertyDecorator;

/**
* Composite unique constraint must be set on entity classes and must specify entity's fields to be unique.
*/
export function Unique(name: string, fields: (object?: any) => (any[]|{ [key: string]: number })): Function;
export function Unique(name: string, fields: (object?: any) => (any[] | { [key: string]: number })): ClassDecorator & PropertyDecorator;

/**
* Composite unique constraint must be set on entity classes and must specify entity's fields to be unique.
*/
export function Unique(nameOrFields?: string|string[]|((object: any) => (any[]|{ [key: string]: number })),
maybeFields?: ((object?: any) => (any[]|{ [key: string]: number }))|string[]): Function {
export function Unique(nameOrFields?: string | string[] | ((object: any) => (any[] | { [key: string]: number })),
maybeFields?: ((object?: any) => (any[] | { [key: string]: number })) | string[]): ClassDecorator & PropertyDecorator {
const name = typeof nameOrFields === "string" ? nameOrFields : undefined;
const fields = typeof nameOrFields === "string" ? <((object?: any) => (any[]|{ [key: string]: number }))|string[]> maybeFields : nameOrFields as string[];
const fields = typeof nameOrFields === "string" ? <((object?: any) => (any[] | { [key: string]: number })) | string[]>maybeFields : nameOrFields as string[];

return function (clsOrObject: Function | Object, propertyName?: string | symbol) {

let columns = fields;

if (propertyName !== undefined) {
switch (typeof (propertyName)) {
case "string":
columns = [propertyName];
break;

case "symbol":
columns = [propertyName.toString()];
break;
}
}

return function (clsOrObject: Function|Object, propertyName?: string) {
const args: UniqueMetadataArgs = {
target: propertyName ? clsOrObject.constructor : clsOrObject as Function,
name: name,
columns: propertyName ? [propertyName] : fields
columns,
};
getMetadataArgsStorage().uniques.push(args);
};
Expand Down
26 changes: 13 additions & 13 deletions src/decorator/columns/Column.ts
Expand Up @@ -20,67 +20,67 @@ import { GeneratedMetadataArgs } from "../../metadata-args/GeneratedMetadataArgs
* Column decorator is used to mark a specific class property as a table column. Only properties decorated with this
* decorator will be persisted to the database when entity be saved.
*/
export function Column(): Function;
export function Column(): PropertyDecorator;

/**
* Column decorator is used to mark a specific class property as a table column.
* Only properties decorated with this decorator will be persisted to the database when entity be saved.
*/
export function Column(options: ColumnOptions): Function;
export function Column(options: ColumnOptions): PropertyDecorator;

/**
* Column decorator is used to mark a specific class property as a table column.
* Only properties decorated with this decorator will be persisted to the database when entity be saved.
*/
export function Column(type: SimpleColumnType, options?: ColumnCommonOptions): Function;
export function Column(type: SimpleColumnType, options?: ColumnCommonOptions): PropertyDecorator;

/**
* Column decorator is used to mark a specific class property as a table column.
* Only properties decorated with this decorator will be persisted to the database when entity be saved.
*/
export function Column(type: SpatialColumnType, options?: ColumnCommonOptions & SpatialColumnOptions): Function;
export function Column(type: SpatialColumnType, options?: ColumnCommonOptions & SpatialColumnOptions): PropertyDecorator;

/**
* Column decorator is used to mark a specific class property as a table column.
* Only properties decorated with this decorator will be persisted to the database when entity be saved.
*/
export function Column(type: WithLengthColumnType, options?: ColumnCommonOptions & ColumnWithLengthOptions): Function;
export function Column(type: WithLengthColumnType, options?: ColumnCommonOptions & ColumnWithLengthOptions): PropertyDecorator;

/**
* Column decorator is used to mark a specific class property as a table column.
* Only properties decorated with this decorator will be persisted to the database when entity be saved.
*/
export function Column(type: WithWidthColumnType, options?: ColumnCommonOptions & ColumnWithWidthOptions): Function;
export function Column(type: WithWidthColumnType, options?: ColumnCommonOptions & ColumnWithWidthOptions): PropertyDecorator;

/**
* Column decorator is used to mark a specific class property as a table column.
* Only properties decorated with this decorator will be persisted to the database when entity be saved.
*/
export function Column(type: WithPrecisionColumnType, options?: ColumnCommonOptions & ColumnNumericOptions): Function;
export function Column(type: WithPrecisionColumnType, options?: ColumnCommonOptions & ColumnNumericOptions): PropertyDecorator;

/**
* Column decorator is used to mark a specific class property as a table column.
* Only properties decorated with this decorator will be persisted to the database when entity be saved.
*/
export function Column(type: "enum", options?: ColumnCommonOptions & ColumnEnumOptions): Function;
export function Column(type: "enum", options?: ColumnCommonOptions & ColumnEnumOptions): PropertyDecorator;

/**
* Column decorator is used to mark a specific class property as a table column.
* Only properties decorated with this decorator will be persisted to the database when entity be saved.
*/
export function Column(type: "simple-enum", options?: ColumnCommonOptions & ColumnEnumOptions): Function;
export function Column(type: "simple-enum", options?: ColumnCommonOptions & ColumnEnumOptions): PropertyDecorator;

/**
* Column decorator is used to mark a specific class property as a table column.
* Only properties decorated with this decorator will be persisted to the database when entity be saved.
*/
export function Column(type: "set", options?: ColumnCommonOptions & ColumnEnumOptions): Function;
export function Column(type: "set", options?: ColumnCommonOptions & ColumnEnumOptions): PropertyDecorator;

/**
* Column decorator is used to mark a specific class property as a table column.
* Only properties decorated with this decorator will be persisted to the database when entity be saved.
*/
export function Column(type: "hstore", options?: ColumnCommonOptions & ColumnHstoreOptions): Function;
export function Column(type: "hstore", options?: ColumnCommonOptions & ColumnHstoreOptions): PropertyDecorator;

/**
* Column decorator is used to mark a specific class property as a table column.
Expand All @@ -90,13 +90,13 @@ export function Column(type: "hstore", options?: ColumnCommonOptions & ColumnHst
* single table of the entity where Embedded is used. And on hydration all columns which supposed to be in the
* embedded will be mapped to it from the single table.
*/
export function Column(type: (type?: any) => Function, options?: ColumnEmbeddedOptions): Function;
export function Column(type: (type?: any) => Function, options?: ColumnEmbeddedOptions): PropertyDecorator;

/**
* Column decorator is used to mark a specific class property as a table column.
* Only properties decorated with this decorator will be persisted to the database when entity be saved.
*/
export function Column(typeOrOptions?: ((type?: any) => Function)|ColumnType|(ColumnOptions&ColumnEmbeddedOptions), options?: (ColumnOptions&ColumnEmbeddedOptions)): Function {
export function Column(typeOrOptions?: ((type?: any) => Function)|ColumnType|(ColumnOptions&ColumnEmbeddedOptions), options?: (ColumnOptions&ColumnEmbeddedOptions)): PropertyDecorator {
return function (object: Object, propertyName: string) {

// normalize parameters
Expand Down
2 changes: 1 addition & 1 deletion src/decorator/columns/CreateDateColumn.ts
Expand Up @@ -6,7 +6,7 @@ import {ColumnMetadataArgs} from "../../metadata-args/ColumnMetadataArgs";
* Creation date is generated and inserted only once,
* at the first time when you create an object, the value is inserted into the table, and is never touched again.
*/
export function CreateDateColumn(options?: ColumnOptions): Function {
export function CreateDateColumn(options?: ColumnOptions): PropertyDecorator {
return function (object: Object, propertyName: string) {
getMetadataArgsStorage().columns.push({
target: object.constructor,
Expand Down
2 changes: 1 addition & 1 deletion src/decorator/columns/DeleteDateColumn.ts
Expand Up @@ -5,7 +5,7 @@ import { ColumnMetadataArgs } from "../../metadata-args/ColumnMetadataArgs";
* This column will store a delete date of the soft-deleted object.
* This date is being updated each time you soft-delete the object.
*/
export function DeleteDateColumn(options?: ColumnOptions): Function {
export function DeleteDateColumn(options?: ColumnOptions): PropertyDecorator {
return function(object: Object, propertyName: string) {
getMetadataArgsStorage().columns.push({
target: object.constructor,
Expand Down
2 changes: 1 addition & 1 deletion src/decorator/columns/ObjectIdColumn.ts
Expand Up @@ -5,7 +5,7 @@ import {ColumnMetadataArgs} from "../../metadata-args/ColumnMetadataArgs";
* Special type of column that is available only for MongoDB database.
* Marks your entity's column to be an object id.
*/
export function ObjectIdColumn(options?: ColumnOptions): Function {
export function ObjectIdColumn(options?: ColumnOptions): PropertyDecorator {
return function (object: Object, propertyName: string) {

// if column options are not given then create a new empty options
Expand Down
6 changes: 3 additions & 3 deletions src/decorator/columns/PrimaryColumn.ts
Expand Up @@ -9,21 +9,21 @@ import { GeneratedMetadataArgs } from "../../metadata-args/GeneratedMetadataArgs
* Only properties decorated with this decorator will be persisted to the database when entity be saved.
* Primary columns also creates a PRIMARY KEY for this column in a db.
*/
export function PrimaryColumn(options?: ColumnOptions): Function;
export function PrimaryColumn(options?: ColumnOptions): PropertyDecorator;

/**
* Column decorator is used to mark a specific class property as a table column.
* Only properties decorated with this decorator will be persisted to the database when entity be saved.
* Primary columns also creates a PRIMARY KEY for this column in a db.
*/
export function PrimaryColumn(type?: ColumnType, options?: ColumnOptions): Function;
export function PrimaryColumn(type?: ColumnType, options?: ColumnOptions): PropertyDecorator;

/**
* Column decorator is used to mark a specific class property as a table column.
* Only properties decorated with this decorator will be persisted to the database when entity be saved.
* Primary columns also creates a PRIMARY KEY for this column in a db.
*/
export function PrimaryColumn(typeOrOptions?: ColumnType|ColumnOptions, options?: ColumnOptions): Function {
export function PrimaryColumn(typeOrOptions?: ColumnType|ColumnOptions, options?: ColumnOptions): PropertyDecorator {
return function (object: Object, propertyName: string) {

// normalize parameters
Expand Down

0 comments on commit c23c888

Please sign in to comment.