mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
small tweaks to support null inverse sides
This commit is contained in:
parent
37c2731f89
commit
7ee006760e
@ -4,18 +4,20 @@ import {RelationTypes} from "../../metadata/types/RelationTypes";
|
||||
import {defaultMetadataStorage} from "../../typeorm";
|
||||
import {ConstructorFunction} from "../../common/ConstructorFunction";
|
||||
|
||||
// todo: make decorators which use inverse side string separate
|
||||
|
||||
/**
|
||||
* One-to-many relation allows to create type of relation when Entity2 can have multiple instances of Entity1.
|
||||
* Entity1 have only one Entity2. Entity1 is an owner of the relationship, and storages Entity2 id on its own side.
|
||||
*/
|
||||
export function OneToMany<T>(typeFunction: (type?: any) => ConstructorFunction<T>, options?: RelationOptions): Function;
|
||||
// export function OneToMany<T>(typeFunction: (type?: any) => ConstructorFunction<T>, options?: RelationOptions): Function;
|
||||
|
||||
/**
|
||||
* One-to-many relation allows to create type of relation when Entity2 can have multiple instances of Entity1.
|
||||
* Entity1 have only one Entity2. Entity1 is an owner of the relationship, and storages Entity2 id on its own side.
|
||||
*/
|
||||
export function OneToMany<T>(typeFunction: (type?: any) => ConstructorFunction<T>,
|
||||
inverseSide?: string|((object: T) => any),
|
||||
inverseSide: string|((object: T) => any),
|
||||
options?: RelationOptions): Function;
|
||||
|
||||
/**
|
||||
@ -23,7 +25,7 @@ export function OneToMany<T>(typeFunction: (type?: any) => ConstructorFunction<T
|
||||
* Entity1 have only one Entity2. Entity1 is an owner of the relationship, and storages Entity2 id on its own side.
|
||||
*/
|
||||
export function OneToMany<T>(typeFunction: (type?: any) => ConstructorFunction<T>,
|
||||
inverseSideOrOptions?: string|((object: T) => any)|RelationOptions,
|
||||
inverseSideOrOptions: string|((object: T) => any)|RelationOptions,
|
||||
options?: RelationOptions): Function {
|
||||
let inverseSideProperty: string|((object: T) => any);
|
||||
if (typeof inverseSideOrOptions === "object") {
|
||||
@ -31,6 +33,9 @@ export function OneToMany<T>(typeFunction: (type?: any) => ConstructorFunction<T
|
||||
} else {
|
||||
inverseSideProperty = <string|((object: T) => any)> inverseSideOrOptions;
|
||||
}
|
||||
|
||||
// todo: for OneToMany having inverse side is required because otherwise its not possible to do anything (selections/persisment)
|
||||
// todo: validate it somehow?
|
||||
|
||||
return function (object: Object, propertyName: string) {
|
||||
if (!options) options = {} as RelationOptions;
|
||||
|
||||
@ -193,7 +193,7 @@ export class EntityMetadataBuilder {
|
||||
const junctionEntityMetadata = new EntityMetadata(tableMetadata, columns, [], [], foreignKeys);
|
||||
junctionEntityMetadatas.push(junctionEntityMetadata);
|
||||
relation.junctionEntityMetadata = junctionEntityMetadata;
|
||||
if (relation.inverseRelation)
|
||||
if (relation.hasInverseSide)
|
||||
relation.inverseRelation.junctionEntityMetadata = junctionEntityMetadata;
|
||||
});
|
||||
});
|
||||
|
||||
@ -64,13 +64,20 @@ export class EntityMetadataValidator {
|
||||
|
||||
// if its a one-to-one relation and JoinColumn is missing on both sides of the relation
|
||||
// or its one-side relation without JoinColumn we should give an error
|
||||
if (!relation.joinColumn && relation.isOneToOne && (!relation.inverseRelation || !relation.inverseRelation.joinColumn))
|
||||
if (!relation.joinColumn && relation.isOneToOne && (!relation.hasInverseSide || !relation.inverseRelation.joinColumn))
|
||||
throw new MissingJoinColumnError(entityMetadata, relation);
|
||||
|
||||
// if its a many-to-many relation and JoinTable is missing on both sides of the relation
|
||||
// or its one-side relation without JoinTable we should give an error
|
||||
if (!relation.joinTable && relation.isManyToMany && (!relation.inverseRelation || !relation.inverseRelation.joinTable))
|
||||
if (!relation.joinTable && relation.isManyToMany && (!relation.hasInverseSide || !relation.inverseRelation.joinTable))
|
||||
throw new MissingJoinTableError(entityMetadata, relation);
|
||||
|
||||
|
||||
// todo: validate if its one-to-one and side which does not have join column MUST have inverse side
|
||||
// todo: validate if its many-to-many and side which does not have join table MUST have inverse side
|
||||
// todo: if there is a relation, and inverse side is specified only on one side, shall we give error
|
||||
// todo: with message like: "Inverse side is specified only on one side of the relationship. Specify on other side too to prevent confusion".
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -193,7 +193,7 @@ export class RelationMetadata extends PropertyMetadata {
|
||||
}
|
||||
|
||||
get hasInverseSide(): boolean {
|
||||
return !!this.inverseRelation;
|
||||
return this.relatedEntityMetadata && !!this.inverseRelation;
|
||||
}
|
||||
|
||||
get isLazy(): boolean {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user