Skip to content

Commit c23c888

Browse files
author
SakirSoft
authored
fix: decorators should implement the official TypeScript interface (#6398)
Closes: #5922
1 parent 874e573 commit c23c888

39 files changed

+111
-96
lines changed

src/decorator/Check.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,29 @@ import {CheckMetadataArgs} from "../metadata-args/CheckMetadataArgs";
66
* Can be used on entity property or on entity.
77
* Can create checks with composite columns when used on entity.
88
*/
9-
export function Check(expression: string): Function;
9+
export function Check(expression: string): ClassDecorator & PropertyDecorator;
1010

1111
/**
1212
* Creates a database check.
1313
* Can be used on entity property or on entity.
1414
* Can create checks with composite columns when used on entity.
1515
*/
16-
export function Check(name: string, expression: string): Function;
16+
export function Check(name: string, expression: string): ClassDecorator & PropertyDecorator;
1717

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

2525
const name = maybeExpression ? nameOrExpression : undefined;
2626
const expression = maybeExpression ? maybeExpression : nameOrExpression;
2727

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

31-
return function (clsOrObject: Function|Object, propertyName?: string) {
31+
return function (clsOrObject: Function|Object, propertyName?: string | symbol) {
3232

3333
getMetadataArgsStorage().checks.push({
3434
target: propertyName ? clsOrObject.constructor : clsOrObject as Function,

src/decorator/EntityRepository.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { EntitySchema } from "../entity-schema/EntitySchema";
77
* Custom repository can manage some specific entity or just be generic.
88
* Custom repository optionally can extend AbstractRepository, Repository or TreeRepository.
99
*/
10-
export function EntityRepository(entity?: Function | EntitySchema<any>): Function {
10+
export function EntityRepository(entity?: Function | EntitySchema<any>): ClassDecorator {
1111
return function (target: Function) {
1212

1313
getMetadataArgsStorage().entityRepositories.push({

src/decorator/Exclusion.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,29 @@ import {ExclusionMetadataArgs} from "../metadata-args/ExclusionMetadataArgs";
66
* Can be used on entity.
77
* Can create exclusions with composite columns when used on entity.
88
*/
9-
export function Exclusion(expression: string): Function;
9+
export function Exclusion(expression: string): ClassDecorator & PropertyDecorator;
1010

1111
/**
1212
* Creates a database exclusion.
1313
* Can be used on entity.
1414
* Can create exclusions with composite columns when used on entity.
1515
*/
16-
export function Exclusion(name: string, expression: string): Function;
16+
export function Exclusion(name: string, expression: string): ClassDecorator & PropertyDecorator;
1717

1818
/**
1919
* Creates a database exclusion.
2020
* Can be used on entity.
2121
* Can create exclusions with composite columns when used on entity.
2222
*/
23-
export function Exclusion(nameOrExpression: string, maybeExpression?: string): Function {
23+
export function Exclusion(nameOrExpression: string, maybeExpression?: string): ClassDecorator & PropertyDecorator {
2424

2525
const name = maybeExpression ? nameOrExpression : undefined;
2626
const expression = maybeExpression ? maybeExpression : nameOrExpression;
2727

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

31-
return function (clsOrObject: Function|Object, propertyName?: string) {
31+
return function (clsOrObject: Function|Object, propertyName?: string | symbol) {
3232

3333
getMetadataArgsStorage().exclusions.push({
3434
target: propertyName ? clsOrObject.constructor : clsOrObject as Function,

src/decorator/Generated.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {GeneratedMetadataArgs} from "../metadata-args/GeneratedMetadataArgs";
1010
*
1111
* Note, some databases do not support non-primary generation columns.
1212
*/
13-
export function Generated(strategy: "increment"|"uuid"|"rowid" = "increment"): Function {
13+
export function Generated(strategy: "increment"|"uuid"|"rowid" = "increment"): PropertyDecorator {
1414
return function (object: Object, propertyName: string) {
1515

1616
getMetadataArgsStorage().generations.push({

src/decorator/Index.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,49 @@ import {IndexMetadataArgs} from "../metadata-args/IndexMetadataArgs";
66
* Can be used on entity property or on entity.
77
* Can create indices with composite columns when used on entity.
88
*/
9-
export function Index(options?: IndexOptions): Function;
9+
export function Index(options?: IndexOptions): ClassDecorator & PropertyDecorator;
1010

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

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

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

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

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

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

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

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

69-
return function (clsOrObject: Function|Object, propertyName?: string) {
69+
return function (clsOrObject: Function|Object, propertyName?: string | symbol) {
7070

7171
getMetadataArgsStorage().indices.push({
7272
target: propertyName ? clsOrObject.constructor : clsOrObject as Function,

src/decorator/Unique.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,54 @@
1-
import {getMetadataArgsStorage} from "../index";
2-
import {UniqueMetadataArgs} from "../metadata-args/UniqueMetadataArgs";
1+
import { getMetadataArgsStorage } from "../index";
2+
import { UniqueMetadataArgs } from "../metadata-args/UniqueMetadataArgs";
33

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

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

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

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

2424
/**
2525
* Composite unique constraint must be set on entity classes and must specify entity's fields to be unique.
2626
*/
27-
export function Unique(nameOrFields?: string|string[]|((object: any) => (any[]|{ [key: string]: number })),
28-
maybeFields?: ((object?: any) => (any[]|{ [key: string]: number }))|string[]): Function {
27+
export function Unique(nameOrFields?: string | string[] | ((object: any) => (any[] | { [key: string]: number })),
28+
maybeFields?: ((object?: any) => (any[] | { [key: string]: number })) | string[]): ClassDecorator & PropertyDecorator {
2929
const name = typeof nameOrFields === "string" ? nameOrFields : undefined;
30-
const fields = typeof nameOrFields === "string" ? <((object?: any) => (any[]|{ [key: string]: number }))|string[]> maybeFields : nameOrFields as string[];
30+
const fields = typeof nameOrFields === "string" ? <((object?: any) => (any[] | { [key: string]: number })) | string[]>maybeFields : nameOrFields as string[];
31+
32+
return function (clsOrObject: Function | Object, propertyName?: string | symbol) {
33+
34+
let columns = fields;
35+
36+
if (propertyName !== undefined) {
37+
switch (typeof (propertyName)) {
38+
case "string":
39+
columns = [propertyName];
40+
break;
41+
42+
case "symbol":
43+
columns = [propertyName.toString()];
44+
break;
45+
}
46+
}
3147

32-
return function (clsOrObject: Function|Object, propertyName?: string) {
3348
const args: UniqueMetadataArgs = {
3449
target: propertyName ? clsOrObject.constructor : clsOrObject as Function,
3550
name: name,
36-
columns: propertyName ? [propertyName] : fields
51+
columns,
3752
};
3853
getMetadataArgsStorage().uniques.push(args);
3954
};

src/decorator/columns/Column.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,67 +20,67 @@ import { GeneratedMetadataArgs } from "../../metadata-args/GeneratedMetadataArgs
2020
* Column decorator is used to mark a specific class property as a table column. Only properties decorated with this
2121
* decorator will be persisted to the database when entity be saved.
2222
*/
23-
export function Column(): Function;
23+
export function Column(): PropertyDecorator;
2424

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

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

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

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

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

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

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

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

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

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

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

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

102102
// normalize parameters

src/decorator/columns/CreateDateColumn.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {ColumnMetadataArgs} from "../../metadata-args/ColumnMetadataArgs";
66
* Creation date is generated and inserted only once,
77
* at the first time when you create an object, the value is inserted into the table, and is never touched again.
88
*/
9-
export function CreateDateColumn(options?: ColumnOptions): Function {
9+
export function CreateDateColumn(options?: ColumnOptions): PropertyDecorator {
1010
return function (object: Object, propertyName: string) {
1111
getMetadataArgsStorage().columns.push({
1212
target: object.constructor,

src/decorator/columns/DeleteDateColumn.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ColumnMetadataArgs } from "../../metadata-args/ColumnMetadataArgs";
55
* This column will store a delete date of the soft-deleted object.
66
* This date is being updated each time you soft-delete the object.
77
*/
8-
export function DeleteDateColumn(options?: ColumnOptions): Function {
8+
export function DeleteDateColumn(options?: ColumnOptions): PropertyDecorator {
99
return function(object: Object, propertyName: string) {
1010
getMetadataArgsStorage().columns.push({
1111
target: object.constructor,

src/decorator/columns/ObjectIdColumn.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {ColumnMetadataArgs} from "../../metadata-args/ColumnMetadataArgs";
55
* Special type of column that is available only for MongoDB database.
66
* Marks your entity's column to be an object id.
77
*/
8-
export function ObjectIdColumn(options?: ColumnOptions): Function {
8+
export function ObjectIdColumn(options?: ColumnOptions): PropertyDecorator {
99
return function (object: Object, propertyName: string) {
1010

1111
// if column options are not given then create a new empty options

0 commit comments

Comments
 (0)