-
-
Notifications
You must be signed in to change notification settings - Fork 496
/
OneToMany.ts
27 lines (23 loc) · 1.05 KB
/
OneToMany.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
import { PropertyOptions } from './Property';
import { EntityProperty, IEntity } from './Entity';
import { MetadataStorage } from '../metadata';
import { Utils } from '../utils';
import { Cascade, ReferenceType } from '../entity';
import { QueryOrder } from '../query';
export function OneToMany(options: OneToManyOptions): Function {
return function (target: IEntity, propertyName: string) {
const meta = MetadataStorage.getMetadata(target.constructor.name);
Utils.lookupPathFromDecorator(meta);
if (!options.entity) {
throw new Error(`'@OneToMany({ entity: string | Function })' is required in '${target.constructor.name}.${propertyName}'`);
}
const property = { name: propertyName, reference: ReferenceType.ONE_TO_MANY, cascade: [Cascade.PERSIST, Cascade.MERGE] };
meta.properties[propertyName] = Object.assign(property, options) as EntityProperty;
};
}
export interface OneToManyOptions extends PropertyOptions {
entity: () => string | Function;
fk: string;
cascade?: Cascade[];
orderBy?: { [field: string]: QueryOrder },
}