/
Unique.ts
55 lines (45 loc) · 2.26 KB
/
Unique.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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[]): 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[]): 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 })): 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 })): 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[]): 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[];
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;
}
}
const args: UniqueMetadataArgs = {
target: propertyName ? clsOrObject.constructor : clsOrObject as Function,
name: name,
columns,
};
getMetadataArgsStorage().uniques.push(args);
};
}