integrated JoinColumn and JoinTable instead of Inversed versions of the decorators

This commit is contained in:
Umed Khudoiberdiev 2016-04-26 23:09:59 +05:00
parent 4ac7a4ba24
commit 30775c3820
39 changed files with 120 additions and 163 deletions

View File

@ -1,6 +1,6 @@
import {PrimaryColumn, Column} from "../../../src/columns";
import {Table} from "../../../src/tables";
import {ManyToOne, ManyToManyInverse} from "../../../src/relations";
import {ManyToOne, ManyToMany} from "../../../src/relations";
import {Post} from "./Post";
import {PostDetails} from "./PostDetails";
@ -13,7 +13,7 @@ export class Category {
@Column()
description: string;
@ManyToManyInverse(type => Post, post => post.categories)
@ManyToMany(type => Post, post => post.categories)
posts: Post[];
@ManyToOne(type => PostDetails, postDetails => postDetails.categories)

View File

@ -3,6 +3,7 @@ import {Table} from "../../../src/tables";
import {ManyToOne, OneToOne} from "../../../src/relations";
import {Post} from "./Post";
import {ImageDetails} from "./ImageDetails";
import {JoinColumn} from "../../../src/decorator/relations/JoinColumn";
@Table("sample10_image")
export class Image {
@ -26,6 +27,7 @@ export class Image {
cascadeUpdate: true,
cascadeRemove: true
})
@JoinColumn()
details: ImageDetails;
}

View File

@ -1,6 +1,6 @@
import {PrimaryColumn, Column} from "../../../src/columns";
import {Table} from "../../../src/tables";
import {OneToOneInverse} from "../../../src/relations";
import {OneToOne} from "../../../src/relations";
import {Image} from "./Image";
@Table("sample10_image_details")
@ -15,7 +15,7 @@ export class ImageDetails {
@Column()
comment: string;
@OneToOneInverse(type => Image, image => image.details)
@OneToOne(type => Image, image => image.details)
image: Image;
}

View File

@ -5,6 +5,8 @@ import {Image} from "./Image";
import {Cover} from "./Cover";
import {Category} from "./Category";
import {PostDetails} from "./PostDetails";
import {JoinColumn} from "../../../src/decorator/relations/JoinColumn";
import {JoinTable} from "../../../src/decorator/relations/JoinTable";
@Table("sample10_post")
export class Post {
@ -27,6 +29,7 @@ export class Post {
cascadeUpdate: true,
cascadeRemove: true
})
@JoinColumn()
details: PostDetails;
@OneToMany(type => Image, image => image.post, {
@ -56,6 +59,7 @@ export class Post {
cascadeUpdate: true,
cascadeRemove: true
})
@JoinTable()
categories: Category[];
}

View File

@ -4,7 +4,6 @@ import {OneToOne, OneToMany, ManyToOne} from "../../../src/relations";
import {Post} from "./Post";
import {Chapter} from "./Chapter";
import {Category} from "./Category";
import {OneToOneInverse} from "../../../src/decorator/relations/OneToOneInverse";
@Table("sample10_post_details")
export class PostDetails {
@ -18,7 +17,7 @@ export class PostDetails {
@Column()
comment: string;
@OneToOneInverse(type => Post, post => post.details)
@OneToOne(type => Post, post => post.details)
post: Post;
@OneToMany(type => Category, category => category.details, {

View File

@ -7,6 +7,7 @@ import {PostAuthor} from "./PostAuthor";
import {PostInformation} from "./PostInformation";
import {PostImage} from "./PostImage";
import {PostMetadata} from "./PostMetadata";
import {JoinColumn} from "../../../src/decorator/relations/JoinColumn";
@Table("sample2_post")
export class Post {
@ -26,6 +27,7 @@ export class Post {
cascadeUpdate: true,
cascadeRemove: true
})
@JoinColumn()
category: PostCategory;
// post has relation with details. cascade inserts here means if new PostDetails instance will be set to this
@ -33,6 +35,7 @@ export class Post {
@OneToOne(type => PostDetails, details => details.post, {
cascadeInsert: true
})
@JoinColumn()
details: PostDetails;
// post has relation with details. cascade update here means if new PostDetail instance will be set to this relation
@ -40,6 +43,7 @@ export class Post {
@OneToOne(type => PostImage, image => image.post, {
cascadeUpdate: true
})
@JoinColumn()
image: PostImage;
// post has relation with details. cascade update here means if new PostDetail instance will be set to this relation
@ -47,6 +51,7 @@ export class Post {
@OneToOne(type => PostMetadata, metadata => metadata.post, {
cascadeRemove: true
})
@JoinColumn()
metadata: PostMetadata|undefined;
// post has relation with details. full cascades here
@ -55,10 +60,12 @@ export class Post {
cascadeUpdate: true,
cascadeRemove: true
})
@JoinColumn()
information: PostInformation;
// post has relation with details. not cascades here. means cannot be persisted, updated or removed
@OneToOne(type => PostAuthor, author => author.post)
@JoinColumn()
author: PostAuthor;
}

View File

@ -1,7 +1,7 @@
import {PrimaryColumn, Column} from "../../../src/columns";
import {Table} from "../../../src/tables";
import {Post} from "./Post";
import {OneToOneInverse} from "../../../src/relations";
import {OneToOne} from "../../../src/relations";
@Table("sample2_post_author")
export class PostAuthor {
@ -12,7 +12,7 @@ export class PostAuthor {
@Column()
name: string;
@OneToOneInverse(type => Post, post => post.author)
@OneToOne(type => Post, post => post.author)
post: Post;
}

View File

@ -1,6 +1,6 @@
import {PrimaryColumn, Column} from "../../../src/columns";
import {Table} from "../../../src/tables";
import {OneToOneInverse} from "../../../src/relations";
import {OneToOne} from "../../../src/relations";
import {Post} from "./Post";
@Table("sample2_post_details")
@ -18,7 +18,7 @@ export class PostDetails {
@Column()
metadata: string;
@OneToOneInverse(type => Post, post => post.details, {
@OneToOne(type => Post, post => post.details, {
cascadeInsert: true,
cascadeUpdate: true,
cascadeRemove: true

View File

@ -1,7 +1,7 @@
import {PrimaryColumn, Column} from "../../../src/columns";
import {Table} from "../../../src/tables";
import {Post} from "./Post";
import {OneToOneInverse} from "../../../src/relations";
import {OneToOne} from "../../../src/relations";
@Table("sample2_post_image")
export class PostImage {
@ -12,7 +12,7 @@ export class PostImage {
@Column()
url: string;
@OneToOneInverse(type => Post, post => post.image)
@OneToOne(type => Post, post => post.image)
post: Post;
}

View File

@ -1,6 +1,6 @@
import {PrimaryColumn, Column} from "../../../src/columns";
import {Table} from "../../../src/tables";
import {OneToOneInverse} from "../../../src/relations";
import {OneToOne} from "../../../src/relations";
import {Post} from "./Post";
@Table("sample2_post_information")
@ -12,7 +12,7 @@ export class PostInformation {
@Column()
text: string;
@OneToOneInverse(type => Post, post => post.information, {
@OneToOne(type => Post, post => post.information, {
cascadeUpdate: true,
})
post: Post;

View File

@ -1,7 +1,7 @@
import {PrimaryColumn, Column} from "../../../src/columns";
import {Table} from "../../../src/tables";
import {Post} from "./Post";
import {OneToOneInverse} from "../../../src/relations";
import {OneToOne} from "../../../src/relations";
@Table("sample2_post_metadata")
export class PostMetadata {
@ -12,7 +12,7 @@ export class PostMetadata {
@Column()
description: string;
@OneToOneInverse(type => Post, post => post.metadata)
@OneToOne(type => Post, post => post.metadata)
post: Post;
}

View File

@ -7,6 +7,7 @@ import {PostAuthor} from "./PostAuthor";
import {PostInformation} from "./PostInformation";
import {PostImage} from "./PostImage";
import {PostMetadata} from "./PostMetadata";
import {JoinTable} from "../../../src/decorator/relations/JoinTable";
@Table("sample4_post")
export class Post {
@ -26,6 +27,7 @@ export class Post {
cascadeUpdate: true,
cascadeRemove: true
})
@JoinTable()
categories: PostCategory[] = [];
// post has relation with details. cascade inserts here means if new PostDetails instance will be set to this
@ -33,6 +35,7 @@ export class Post {
@ManyToMany(type => PostDetails, details => details.posts, {
cascadeInsert: true
})
@JoinTable()
details: PostDetails[] = [];
// post has relation with details. cascade update here means if new PostDetail instance will be set to this relation
@ -40,6 +43,7 @@ export class Post {
@ManyToMany(type => PostImage, image => image.posts, {
cascadeUpdate: true
})
@JoinTable()
images: PostImage[] = [];
// post has relation with details. cascade update here means if new PostDetail instance will be set to this relation
@ -47,6 +51,7 @@ export class Post {
@ManyToMany(type => PostMetadata, metadata => metadata.posts, {
cascadeRemove: true
})
@JoinTable()
metadatas: PostMetadata[] = [];
// post has relation with details. full cascades here
@ -55,10 +60,12 @@ export class Post {
cascadeUpdate: true,
cascadeRemove: true
})
@JoinTable()
informations: PostInformation[] = [];
// post has relation with details. not cascades here. means cannot be persisted, updated or removed
@ManyToMany(type => PostAuthor, author => author.posts)
@JoinTable()
authors: PostAuthor[] = [];
}

View File

@ -1,7 +1,7 @@
import {PrimaryColumn, Column} from "../../../src/columns";
import {Table} from "../../../src/tables";
import {Post} from "./Post";
import {ManyToManyInverse} from "../../../src/relations";
import {ManyToMany} from "../../../src/relations";
@Table("sample4_post_author")
export class PostAuthor {
@ -12,7 +12,7 @@ export class PostAuthor {
@Column()
name: string;
@ManyToManyInverse(type => Post, post => post.authors)
@ManyToMany(type => Post, post => post.authors)
posts: Post[];
}

View File

@ -1,6 +1,6 @@
import {PrimaryColumn, Column} from "../../../src/columns";
import {Table} from "../../../src/tables";
import {ManyToManyInverse} from "../../../src/relations";
import {ManyToMany} from "../../../src/relations";
import {Post} from "./Post";
@Table("sample4_post_details")
@ -24,7 +24,7 @@ export class PostDetails {
})
metadata: string;
@ManyToManyInverse(type => Post, post => post.details, {
@ManyToMany(type => Post, post => post.details, {
cascadeInsert: true,
cascadeUpdate: true,
cascadeRemove: true

View File

@ -1,7 +1,7 @@
import {PrimaryColumn, Column} from "../../../src/columns";
import {Table} from "../../../src/tables";
import {Post} from "./Post";
import {ManyToManyInverse} from "../../../src/relations";
import {ManyToMany} from "../../../src/relations";
@Table("sample4_post_image")
export class PostImage {
@ -12,7 +12,7 @@ export class PostImage {
@Column()
url: string;
@ManyToManyInverse(type => Post, post => post.images)
@ManyToMany(type => Post, post => post.images)
posts: Post[];
}

View File

@ -1,6 +1,6 @@
import {PrimaryColumn, Column} from "../../../src/columns";
import {Table} from "../../../src/tables";
import {ManyToManyInverse} from "../../../src/relations";
import {ManyToMany} from "../../../src/relations";
import {Post} from "./Post";
@Table("sample4_post_information")
@ -12,7 +12,7 @@ export class PostInformation {
@Column()
text: string;
@ManyToManyInverse(type => Post, post => post.informations, {
@ManyToMany(type => Post, post => post.informations, {
cascadeUpdate: true,
})
posts: Post[];

View File

@ -1,7 +1,7 @@
import {PrimaryColumn, Column} from "../../../src/columns";
import {Table} from "../../../src/tables";
import {Post} from "./Post";
import {ManyToManyInverse} from "../../../src/relations";
import {ManyToMany} from "../../../src/relations";
@Table("sample4_post_metadata")
export class PostMetadata {
@ -12,7 +12,7 @@ export class PostMetadata {
@Column()
description: string;
@ManyToManyInverse(type => Post, post => post.metadatas)
@ManyToMany(type => Post, post => post.metadatas)
posts: Post[];
}

View File

@ -5,6 +5,7 @@ import {PostCategory} from "./PostCategory";
import {PostAuthor} from "./PostAuthor";
import {OneToMany} from "../../../src/decorator/relations/OneToMany";
import {ManyToOne} from "../../../src/decorator/relations/ManyToOne";
import {JoinTable} from "../../../src/decorator/relations/JoinTable";
@Table("sample5_post")
export class Post {
@ -30,6 +31,7 @@ export class Post {
cascadeUpdate: true,
cascadeRemove: true
})
@JoinTable()
categories: PostCategory[] = [];
}

View File

@ -1,7 +1,7 @@
import {PrimaryColumn, Column} from "../../../src/columns";
import {Table} from "../../../src/tables";
import {Post} from "./Post";
import {ManyToManyInverse} from "../../../src/decorator/relations/ManyToManyInverse";
import {ManyToMany} from "../../../src/decorator/relations/ManyToMany";
@Table("sample5_post_category")
export class PostCategory {
@ -12,7 +12,7 @@ export class PostCategory {
@Column()
name: string;
@ManyToManyInverse(type => Post, post => post.categories, {
@ManyToMany(type => Post, post => post.categories, {
cascadeInsert: true,
cascadeUpdate: true,
cascadeRemove: true

View File

@ -5,6 +5,7 @@ import {ManyToOne} from "../../../src/decorator/relations/ManyToOne";
import {PostAuthor} from "./PostAuthor";
import {ManyToMany} from "../../../src/decorator/relations/ManyToMany";
import {PostCategory} from "./PostCategory";
import {JoinTable} from "../../../src/decorator/relations/JoinTable";
@Table("sample6_blog")
export class Blog extends BasePost {
@ -24,6 +25,7 @@ export class Blog extends BasePost {
cascadeUpdate: true,
cascadeRemove: true
})
@JoinTable()
categories: PostCategory[] = [];
}

View File

@ -5,6 +5,7 @@ import {PostCategory} from "./PostCategory";
import {ManyToMany} from "../../../src/decorator/relations/ManyToMany";
import {PostAuthor} from "./PostAuthor";
import {ManyToOne} from "../../../src/decorator/relations/ManyToOne";
import {JoinTable} from "../../../src/decorator/relations/JoinTable";
@Table("sample6_post")
export class Post extends BasePost {
@ -24,6 +25,7 @@ export class Post extends BasePost {
cascadeUpdate: true,
cascadeRemove: true
})
@JoinTable()
categories: PostCategory[] = [];
}

View File

@ -1,7 +1,7 @@
import {PrimaryColumn, Column} from "../../../src/columns";
import {Table} from "../../../src/tables";
import {Post} from "./Post";
import {ManyToManyInverse} from "../../../src/decorator/relations/ManyToManyInverse";
import {ManyToMany} from "../../../src/decorator/relations/ManyToMany";
@Table("sample6_post_category")
export class PostCategory {
@ -12,7 +12,7 @@ export class PostCategory {
@Column()
name: string;
@ManyToManyInverse(type => Post, post => post.categories, {
@ManyToMany(type => Post, post => post.categories, {
cascadeInsert: true,
cascadeUpdate: true,
cascadeRemove: true

View File

@ -4,6 +4,7 @@ import {ManyToMany} from "../../../src/relations";
import {PostCategory} from "./PostCategory";
import {PostAuthor} from "./PostAuthor";
import {ManyToOne} from "../../../src/decorator/relations/ManyToOne";
import {JoinTable} from "../../../src/decorator/relations/JoinTable";
@Table("sample7_post")
export class Post {
@ -29,6 +30,7 @@ export class Post {
cascadeUpdate: true,
cascadeRemove: true
})
@JoinTable()
categories: PostCategory[] = [];
}

View File

@ -1,7 +1,7 @@
import {PrimaryColumn, Column} from "../../../src/columns";
import {Table} from "../../../src/tables";
import {Post} from "./Post";
import {ManyToManyInverse} from "../../../src/decorator/relations/ManyToManyInverse";
import {ManyToMany} from "../../../src/decorator/relations/ManyToMany";
@Table("sample7_post_category")
export class PostCategory {
@ -12,7 +12,7 @@ export class PostCategory {
@Column()
name: string;
@ManyToManyInverse(type => Post, post => post.categories, {
@ManyToMany(type => Post, post => post.categories, {
cascadeInsert: true,
cascadeUpdate: true,
cascadeRemove: true

View File

@ -3,9 +3,9 @@ import {Table} from "../../../src/tables";
import {ManyToMany} from "../../../src/relations";
import {ManyToOne} from "../../../src/decorator/relations/ManyToOne";
import {OneToMany} from "../../../src/decorator/relations/OneToMany";
import {ManyToManyInverse} from "../../../src/decorator/relations/ManyToManyInverse";
import {OneToOne} from "../../../src/decorator/relations/OneToOne";
import {OneToOneInverse} from "../../../src/decorator/relations/OneToOneInverse";
import {JoinColumn} from "../../../src/decorator/relations/JoinColumn";
import {JoinTable} from "../../../src/decorator/relations/JoinTable";
@Table("sample8_category")
export class Category {
@ -21,9 +21,10 @@ export class Category {
cascadeUpdate: true,
cascadeRemove: true
})
@JoinColumn()
oneCategory: Category;
@OneToOneInverse(type => Category, category => category.oneCategory, {
@OneToOne(type => Category, category => category.oneCategory, {
cascadeInsert: true,
cascadeUpdate: true,
cascadeRemove: true
@ -49,9 +50,10 @@ export class Category {
cascadeUpdate: true,
cascadeRemove: true
})
@JoinTable()
manyCategories: Category[] = [];
@ManyToManyInverse(type => Category, category => category.manyCategories, {
@ManyToMany(type => Category, category => category.manyCategories, {
cascadeInsert: true,
cascadeUpdate: true,
cascadeRemove: true

View File

@ -11,6 +11,7 @@ import {BeforeUpdate} from "../../../src/decorator/listeners/BeforeUpdate";
import {AfterUpdate} from "../../../src/decorator/listeners/AfterUpdate";
import {BeforeRemove} from "../../../src/decorator/listeners/BeforeRemove";
import {AfterRemove} from "../../../src/decorator/listeners/AfterRemove";
import {JoinTable} from "../../../src/decorator/relations/JoinTable";
@Table("sample9_post")
export class Post {
@ -35,6 +36,7 @@ export class Post {
cascadeUpdate: true,
cascadeRemove: true
})
@JoinTable()
categories: PostCategory[] = [];
uid: number;

View File

@ -1,7 +1,7 @@
import {PrimaryColumn, Column} from "../../../src/columns";
import {Table} from "../../../src/tables";
import {Post} from "./Post";
import {ManyToManyInverse} from "../../../src/decorator/relations/ManyToManyInverse";
import {ManyToMany} from "../../../src/decorator/relations/ManyToMany";
import {AfterRemove} from "../../../src/decorator/listeners/AfterRemove";
import {BeforeRemove} from "../../../src/decorator/listeners/BeforeRemove";
import {AfterUpdate} from "../../../src/decorator/listeners/AfterUpdate";
@ -18,7 +18,7 @@ export class PostCategory {
@Column()
name: string;
@ManyToManyInverse(type => Post, post => post.categories, {
@ManyToMany(type => Post, post => post.categories, {
cascadeInsert: true,
cascadeUpdate: true,
cascadeRemove: true

View File

@ -4,8 +4,9 @@ import {JoinColumnMetadata} from "../../metadata/JoinColumnMetadata";
/**
*/
export function JoinColumn<T>(options: JoinTableOptions): Function {
export function JoinColumn(options?: JoinTableOptions): Function {
return function (object: Object, propertyName: string) {
options = options || {} as JoinTableOptions;
const metadata = new JoinColumnMetadata(object.constructor, propertyName, options);
defaultMetadataStorage().addJoinColumnMetadata(metadata);
};

View File

@ -4,8 +4,9 @@ import {JoinTableMetadata} from "../../metadata/JoinTableMetadata";
/**
*/
export function JoinTable<T>(options: JoinTableOptions): Function {
export function JoinTable(options?: JoinTableOptions): Function {
return function (object: Object, propertyName: string) {
options = options || {} as JoinTableOptions;
const metadata = new JoinTableMetadata(object.constructor, propertyName, options);
defaultMetadataStorage().addJoinTableMetadata(metadata);
};

View File

@ -44,7 +44,6 @@ export function ManyToMany<T>(typeFunction: (type?: any) => ConstructorFunction<
relationType: RelationTypes.MANY_TO_MANY,
type: typeFunction,
inverseSideProperty: inverseSideProperty,
isOwning: true,
options: options
}));
};

View File

@ -1,52 +0,0 @@
import {RelationMetadata} from "../../metadata/RelationMetadata";
import {RelationOptions} from "../../metadata/options/RelationOptions";
import {RelationTypes} from "../../metadata/types/RelationTypes";
import {defaultMetadataStorage} from "../../typeorm";
import {ConstructorFunction} from "../../common/ConstructorFunction";
/**
* Many-to-many is a type of relationship when Entity1 can have multiple instances of Entity2, and Entity2 can have
* multiple instances of Entity1. To achieve it, this type of relation creates a junction table, where it storage
* entity1 and entity2 ids. This is inverse side of the relationship.
*/
export function ManyToManyInverse<T>(typeFunction: (type?: any) => ConstructorFunction<T>, options?: RelationOptions): Function;
/**
* Many-to-many is a type of relationship when Entity1 can have multiple instances of Entity2, and Entity2 can have
* multiple instances of Entity1. To achieve it, this type of relation creates a junction table, where it storage
* entity1 and entity2 ids. This is inverse side of the relationship.
*/
export function ManyToManyInverse<T>(typeFunction: (type?: any) => ConstructorFunction<T>,
inverseSide?: string|((object: T) => any),
options?: RelationOptions): Function;
/**
* Many-to-many is a type of relationship when Entity1 can have multiple instances of Entity2, and Entity2 can have
* multiple instances of Entity1. To achieve it, this type of relation creates a junction table, where it storage
* entity1 and entity2 ids. This is inverse side of the relationship.
*/
export function ManyToManyInverse<T>(typeFunction: (type?: any) => ConstructorFunction<T>,
inverseSideOrOptions?: string|((object: T) => any)|RelationOptions,
options?: RelationOptions): Function {
let inverseSideProperty: string|((object: T) => any);
if (typeof inverseSideOrOptions === "object") {
options = <RelationOptions> inverseSideOrOptions;
} else {
inverseSideProperty = <string|((object: T) => any)> inverseSideOrOptions;
}
return function (object: Object, propertyName: string) {
if (!options) options = {} as RelationOptions;
defaultMetadataStorage().addRelationMetadata(new RelationMetadata({
target: object.constructor,
propertyName: propertyName,
relationType: RelationTypes.MANY_TO_MANY,
type: typeFunction,
inverseSideProperty: inverseSideProperty,
isOwning: false,
options: options
}));
};
}

View File

@ -44,7 +44,6 @@ export function ManyToOne<T>(typeFunction: (type?: any) => ConstructorFunction<T
relationType: RelationTypes.MANY_TO_ONE,
type: typeFunction,
inverseSideProperty: inverseSideProperty,
isOwning: true,
options: options
}));
};

View File

@ -41,7 +41,6 @@ export function OneToMany<T>(typeFunction: (type?: any) => ConstructorFunction<T
relationType: RelationTypes.ONE_TO_MANY,
type: typeFunction,
inverseSideProperty: inverseSideProperty,
isOwning: false,
options: options
}));
};

View File

@ -41,7 +41,6 @@ export function OneToOne<T>(typeFunction: (type?: any) => ConstructorFunction<T>
relationType: RelationTypes.ONE_TO_ONE,
type: typeFunction,
inverseSideProperty: inverseSideProperty,
isOwning: true,
options: options
}));
};

View File

@ -1,51 +0,0 @@
import {RelationMetadata} from "../../metadata/RelationMetadata";
import {RelationOptions} from "../../metadata/options/RelationOptions";
import {RelationTypes} from "../../metadata/types/RelationTypes";
import {defaultMetadataStorage} from "../../typeorm";
import {ConstructorFunction} from "../../common/ConstructorFunction";
/**
* Inverse side of the one-to-one relation. One-to-one relation allows to create direct relation between two entities.
* Entity2 have only one Entity1. Entity2 is inverse side of the relation on Entity1. Does not storage id of the
* Entity1. Entity1's id is storage on the one-to-one owner side.
*/
export function OneToOneInverse<T>(typeFunction: (type?: any) => ConstructorFunction<T>, options?: RelationOptions): Function;
/**
* Inverse side of the one-to-one relation. One-to-one relation allows to create direct relation between two entities.
* Entity2 have only one Entity1. Entity2 is inverse side of the relation on Entity1. Does not storage id of the
* Entity1. Entity1's id is storage on the one-to-one owner side.
*/
export function OneToOneInverse<T>(typeFunction: (type?: any) => ConstructorFunction<T>,
inverseSide?: string|((object: T) => any),
options?: RelationOptions): Function;
/**
* Inverse side of the one-to-one relation. One-to-one relation allows to create direct relation between two entities.
* Entity2 have only one Entity1. Entity2 is inverse side of the relation on Entity1. Does not storage id of the
* Entity1. Entity1's id is storage on the one-to-one owner side.
*/
export function OneToOneInverse<T>(typeFunction: (type?: any) => ConstructorFunction<T>,
inverseSideOrOptions?: string|((object: T) => any)|RelationOptions,
options?: RelationOptions): Function {
let inverseSideProperty: string|((object: T) => any);
if (typeof inverseSideOrOptions === "object") {
options = <RelationOptions> inverseSideOrOptions;
} else {
inverseSideProperty = <string|((object: T) => any)> inverseSideOrOptions;
}
return function (object: Object, propertyName: string) {
if (!options) options = {} as RelationOptions;
defaultMetadataStorage().addRelationMetadata(new RelationMetadata({
target: object.constructor,
propertyName: propertyName,
relationType: RelationTypes.ONE_TO_ONE,
type: typeFunction,
inverseSideProperty: inverseSideProperty,
isOwning: false,
options: options
}));
};
}

View File

@ -41,6 +41,8 @@ export class EntityMetadataBuilder {
const abstractTableMetadatas = this.metadataStorage.findAbstractTableMetadatasForClasses(entityClasses);
const columnMetadatas = this.metadataStorage.findFieldMetadatasForClasses(entityClasses);
const relationMetadatas = this.metadataStorage.findRelationMetadatasForClasses(entityClasses);
const joinTableMetadatas = this.metadataStorage.findJoinTableMetadatasForClasses(entityClasses);
const joinColumnMetadatas = this.metadataStorage.findJoinColumnMetadatasForClasses(entityClasses);
const indexMetadatas = this.metadataStorage.findIndexMetadatasForClasses(entityClasses);
const compoundIndexMetadatas = this.metadataStorage.findCompoundIndexMetadatasForClasses(entityClasses);
@ -53,6 +55,8 @@ export class EntityMetadataBuilder {
let entityRelations = relationMetadatas.filter(constructorChecker);
let entityCompoundIndices = compoundIndexMetadatas.filter(constructorChecker2);
let entityIndices = indexMetadatas.filter(constructorChecker);
let entityJoinTables = joinTableMetadatas.filter(constructorChecker);
let entityJoinColumns = joinColumnMetadatas.filter(constructorChecker);
// merge all columns in the abstract table extendings of this table
abstractTableMetadatas.forEach(abstractMetadata => {
@ -77,6 +81,20 @@ export class EntityMetadataBuilder {
const entityMetadata = new EntityMetadata(tableMetadata, entityColumns, entityRelations, entityIndices, entityCompoundIndices, []);
// find entity's relations join tables
entityRelations.forEach(relation => {
const relationJoinTable = entityJoinTables.find(joinTable => joinTable.propertyName === relation.propertyName);
if (relationJoinTable)
relation.joinTable = relationJoinTable;
});
// find entity's relations join columns
entityRelations.forEach(relation => {
const relationJoinColumn = entityJoinColumns.find(joinColumn => joinColumn.propertyName === relation.propertyName);
if (relationJoinColumn)
relation.joinColumn = relationJoinColumn;
});
// set naming strategies
tableMetadata.namingStrategy = this.namingStrategy;
entityColumns.forEach(column => column.namingStrategy = this.namingStrategy);

View File

@ -152,9 +152,12 @@ export class MetadataStorage {
return this.relationMetadatas.filter(metadata => classes.indexOf(metadata.target) !== -1);
}
findNamingStrategy(name: string): NamingStrategyMetadata {
// todo: throw error if naming strategy is not found.
return this.namingStrategyMetadatas.find(metadata => metadata.name === name);
findJoinTableMetadatasForClasses(classes: Function[]): JoinTableMetadata[] {
return this.joinTableMetadatas.filter(metadata => classes.indexOf(metadata.target) !== -1);
}
findJoinColumnMetadatasForClasses(classes: Function[]): JoinColumnMetadata[] {
return this.joinColumnMetadatas.filter(metadata => classes.indexOf(metadata.target) !== -1);
}
findNamingStrategiesForClasses(classes: Function[]): NamingStrategyMetadata[] {

View File

@ -4,6 +4,8 @@ import {RelationOptions} from "./options/RelationOptions";
import {NamingStrategyInterface} from "../naming-strategy/NamingStrategy";
import {EntityMetadata} from "./EntityMetadata";
import {OnDeleteType} from "./ForeignKeyMetadata";
import {JoinTableMetadata} from "./JoinTableMetadata";
import {JoinColumnMetadata} from "./JoinColumnMetadata";
/**
* Function that returns a type of the field. Returned value must be a class used on the relation.
@ -46,11 +48,6 @@ export interface RelationMetadataArgs {
*/
inverseSideProperty: PropertyTypeInFunction<any>;
/**
* Indicates if this relation is owner side of the relation between entities.
*/
isOwning: boolean;
/**
* Additional relation options.
*/
@ -81,6 +78,16 @@ export class RelationMetadata extends PropertyMetadata {
*/
junctionEntityMetadata: EntityMetadata;
/**
* Join table metadata.
*/
joinTable: JoinTableMetadata;
/**
* Join column metadata.
*/
joinColumn: JoinColumnMetadata;
// ---------------------------------------------------------------------
// Readonly Properties
// ---------------------------------------------------------------------
@ -90,11 +97,6 @@ export class RelationMetadata extends PropertyMetadata {
*/
readonly relationType: RelationType;
/**
* Indicates if this side is an owner of this relation.
*/
readonly isOwning: boolean;
/**
* If set to true then it means that related object can be allowed to be inserted to the db.
*/
@ -151,7 +153,6 @@ export class RelationMetadata extends PropertyMetadata {
constructor(args: RelationMetadataArgs) {
super(args.target, args.propertyName);
this.relationType = args.relationType;
this.isOwning = args.isOwning;
this._inverseSideProperty = args.inverseSideProperty;
if (args.options.name)
@ -183,6 +184,15 @@ export class RelationMetadata extends PropertyMetadata {
return this.namingStrategy ? this.namingStrategy.relationName(this._name) : this._name;
}
/**
* Indicates if this side is an owner of this relation.
*/
get isOwning() {
return this.isManyToOne ||
(this.isManyToMany && this.joinTable) ||
(this.isOneToOne && this.joinColumn);
}
get type(): Function {
return this._type();
}

View File

@ -2,5 +2,5 @@ export * from "./decorator/relations/OneToOne";
export * from "./decorator/relations/OneToMany";
export * from "./decorator/relations/ManyToOne";
export * from "./decorator/relations/ManyToMany";
export * from "./decorator/relations/ManyToManyInverse";
export * from "./decorator/relations/OneToOneInverse";
export * from "./decorator/relations/JoinTable";
export * from "./decorator/relations/JoinColumn";