/
PrimaryKey.ts
31 lines (25 loc) · 1.34 KB
/
PrimaryKey.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
import { MetadataStorage, MetadataValidator } from '../metadata';
import { ReferenceKind } from '../enums';
import type { PropertyOptions } from './Property';
import type { AnyEntity, EntityKey, EntityProperty } from '../typings';
import { Utils } from '../utils/Utils';
function createDecorator<T extends object>(options: PrimaryKeyOptions<T> | SerializedPrimaryKeyOptions<T>, serialized: boolean) {
return function (target: AnyEntity, propertyName: string) {
const meta = MetadataStorage.getMetadataFromDecorator(target.constructor as T);
MetadataValidator.validateSingleDecorator(meta, propertyName, ReferenceKind.SCALAR);
const k = serialized ? 'serializedPrimaryKey' as const : 'primary' as const;
options[k] = true;
meta.properties[propertyName as EntityKey<T>] = { name: propertyName, kind: ReferenceKind.SCALAR, ...options } as EntityProperty<T>;
return Utils.propertyDecoratorReturnValue();
};
}
export function PrimaryKey<T extends object>(options: PrimaryKeyOptions<T> = {}) {
return createDecorator(options, false);
}
export function SerializedPrimaryKey<T extends object>(options: SerializedPrimaryKeyOptions<T> = {}) {
return createDecorator(options, true);
}
export interface PrimaryKeyOptions<T> extends PropertyOptions<T> { }
export interface SerializedPrimaryKeyOptions<T> extends PropertyOptions<T> {
type?: any;
}