-
-
Notifications
You must be signed in to change notification settings - Fork 526
/
Embedded.ts
26 lines (23 loc) · 1.06 KB
/
Embedded.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
import { AnyEntity, EntityProperty } from '../typings';
import { MetadataStorage, MetadataValidator } from '../metadata';
import { Utils } from '../utils';
import { ReferenceType } from '../enums';
export function Embedded(type: EmbeddedOptions | (() => AnyEntity) = {}, options: EmbeddedOptions = {}) {
return function (target: AnyEntity, propertyName: string) {
const meta = MetadataStorage.getMetadataFromDecorator(target.constructor);
MetadataValidator.validateSingleDecorator(meta, propertyName, ReferenceType.EMBEDDED);
options = type instanceof Function ? { entity: type, ...options } : { ...type, ...options };
Utils.defaultValue(options, 'prefix', true);
const property = { name: propertyName, reference: ReferenceType.EMBEDDED } as EntityProperty;
meta.properties[propertyName] = Object.assign(property, options);
return Utils.propertyDecoratorReturnValue();
};
}
export type EmbeddedOptions = {
entity?: string | (() => AnyEntity);
type?: string;
prefix?: string | boolean;
nullable?: boolean;
object?: boolean;
array?: boolean;
};