-
-
Notifications
You must be signed in to change notification settings - Fork 495
/
Entity.ts
37 lines (33 loc) · 1.34 KB
/
Entity.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
import { MetadataStorage } from '../metadata';
import { Utils } from '../utils';
import type { Constructor, Dictionary, FilterQuery } from '../typings';
import type { FindOptions } from '../drivers/IDatabaseDriver';
export function Entity(options: EntityOptions<any> = {}) {
return function <T>(target: T & Dictionary) {
const meta = MetadataStorage.getMetadataFromDecorator(target);
Utils.merge(meta, options);
meta.class = target as unknown as Constructor<T>;
if (!options.abstract || meta.discriminatorColumn) {
meta.name = target.name;
}
return target;
};
}
export type EntityOptions<T> = {
tableName?: string;
schema?: string;
collection?: string;
discriminatorColumn?: string;
discriminatorMap?: Dictionary<string>;
discriminatorValue?: number | string;
comment?: string;
abstract?: boolean;
readonly?: boolean;
virtual?: boolean;
// we need to use `em: any` here otherwise an expression would not be assignable with more narrow type like `SqlEntityManager`
// also return type is unknown as it can be either QB instance (which we cannot type here) or array of POJOs (e.g. for mongodb)
expression?: string | ((em: any, where: FilterQuery<T>, options: FindOptions<T, any>) => object);
customRepository?: () => Constructor;
/** shortcut for `customRepository` */
repository?: () => Constructor;
};