quaerateum
Version:
Simple typescript ORM for node.js based on data-mapper, unit-of-work and identity-map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JS.
32 lines (27 loc) • 1.23 kB
text/typescript
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 prop = { name: propertyName, reference: ReferenceType.ONE_TO_MANY, cascade: [Cascade.PERSIST, Cascade.MERGE] };
Object.assign(prop, options);
Utils.renameKey(prop, 'fk', 'mappedBy');
meta.properties[propertyName] = prop as EntityProperty;
};
}
export type OneToManyOptions = PropertyOptions & {
entity: () => string | Function;
cascade?: Cascade[];
orderBy?: { [field: string]: QueryOrder },
joinColumn?: string;
inverseJoinColumn?: string;
referenceColumnName?: string;
} & ({ fk: string } | { mappedBy: string });