From 7ccad582ff8a912c75b2b7054b1e1a43d845aa0e Mon Sep 17 00:00:00 2001 From: Umed Khudoiberdiev Date: Sat, 19 Mar 2016 14:03:02 +0500 Subject: [PATCH] created inserse relation decorators --- sample/sample10-mixed/entity/Category.ts | 4 +- sample/sample10-mixed/entity/Image.ts | 4 +- sample/sample10-mixed/entity/ImageDetails.ts | 4 +- sample/sample10-mixed/entity/Post.ts | 4 +- sample/sample10-mixed/entity/PostDetails.ts | 3 +- sample/sample2-one-to-one/entity/Post.ts | 12 +++--- .../sample2-one-to-one/entity/PostAuthor.ts | 5 ++- .../sample2-one-to-one/entity/PostDetails.ts | 4 +- sample/sample2-one-to-one/entity/PostImage.ts | 4 +- .../entity/PostInformation.ts | 4 +- .../sample2-one-to-one/entity/PostMetadata.ts | 4 +- sample/sample4-many-to-many/entity/Post.ts | 12 +++--- .../sample4-many-to-many/entity/PostAuthor.ts | 4 +- .../entity/PostDetails.ts | 4 +- .../sample4-many-to-many/entity/PostImage.ts | 4 +- .../entity/PostInformation.ts | 4 +- .../entity/PostMetadata.ts | 4 +- src/decorator/Relations.ts | 4 +- src/decorator/relations/ManyToMany.ts | 9 ++--- src/decorator/relations/ManyToManyInverse.ts | 37 +++++++++++++++++++ src/decorator/relations/OneToOne.ts | 9 ++--- src/decorator/relations/OneToOneInverse.ts | 36 ++++++++++++++++++ 22 files changed, 127 insertions(+), 52 deletions(-) create mode 100644 src/decorator/relations/ManyToManyInverse.ts create mode 100644 src/decorator/relations/OneToOneInverse.ts diff --git a/sample/sample10-mixed/entity/Category.ts b/sample/sample10-mixed/entity/Category.ts index b0e17b73b..4b58664bd 100644 --- a/sample/sample10-mixed/entity/Category.ts +++ b/sample/sample10-mixed/entity/Category.ts @@ -1,6 +1,6 @@ import {PrimaryColumn, Column} from "../../../src/decorator/Columns"; import {Table} from "../../../src/decorator/Tables"; -import {OneToMany, ManyToMany, ManyToOne} from "../../../src/decorator/Relations"; +import {ManyToOne, ManyToManyInverse} from "../../../src/decorator/Relations"; import {Post} from "./Post"; import {PostDetails} from "./PostDetails"; @@ -13,7 +13,7 @@ export class Category { @Column() description: string; - @ManyToMany(false, type => Post, post => post.categories) + @ManyToManyInverse(type => Post, post => post.categories) posts: Post[]; @ManyToOne(_ => PostDetails, postDetails => postDetails.categories) diff --git a/sample/sample10-mixed/entity/Image.ts b/sample/sample10-mixed/entity/Image.ts index a24aa599c..c8bd4ea95 100644 --- a/sample/sample10-mixed/entity/Image.ts +++ b/sample/sample10-mixed/entity/Image.ts @@ -1,6 +1,6 @@ import {PrimaryColumn, Column} from "../../../src/decorator/Columns"; import {Table} from "../../../src/decorator/Tables"; -import {ManyToOne, OneToMany, OneToOne} from "../../../src/decorator/Relations"; +import {ManyToOne, OneToOne} from "../../../src/decorator/Relations"; import {Post} from "./Post"; import {ImageDetails} from "./ImageDetails"; @@ -21,7 +21,7 @@ export class Image { }) secondaryPost: Post; - @OneToOne(true, () => ImageDetails, details => details.image, { + @OneToOne(() => ImageDetails, details => details.image, { cascadeInsert: true, cascadeUpdate: true, cascadeRemove: true diff --git a/sample/sample10-mixed/entity/ImageDetails.ts b/sample/sample10-mixed/entity/ImageDetails.ts index 54e9b2deb..aa11d2b2a 100644 --- a/sample/sample10-mixed/entity/ImageDetails.ts +++ b/sample/sample10-mixed/entity/ImageDetails.ts @@ -1,6 +1,6 @@ import {PrimaryColumn, Column} from "../../../src/decorator/Columns"; import {Table} from "../../../src/decorator/Tables"; -import {OneToOne} from "../../../src/decorator/Relations"; +import {OneToOneInverse} from "../../../src/decorator/Relations"; import {Image} from "./Image"; @Table("sample10_image_details") @@ -15,7 +15,7 @@ export class ImageDetails { @Column() comment: string; - @OneToOne(false, () => Image, image => image.details) + @OneToOneInverse(() => Image, image => image.details) image: Image; } \ No newline at end of file diff --git a/sample/sample10-mixed/entity/Post.ts b/sample/sample10-mixed/entity/Post.ts index 8aaa6fdeb..24d4d3434 100644 --- a/sample/sample10-mixed/entity/Post.ts +++ b/sample/sample10-mixed/entity/Post.ts @@ -22,7 +22,7 @@ export class Post { }) text: string; - @OneToOne(true, () => PostDetails, details => details.post, { + @OneToOne(() => PostDetails, details => details.post, { cascadeInsert: true, cascadeUpdate: true, cascadeRemove: true @@ -51,7 +51,7 @@ export class Post { }) coverId: number; - @ManyToMany(true, type => Category, category => category.posts, { + @ManyToMany(type => Category, category => category.posts, { cascadeInsert: true, cascadeUpdate: true, cascadeRemove: true diff --git a/sample/sample10-mixed/entity/PostDetails.ts b/sample/sample10-mixed/entity/PostDetails.ts index e3f1372d6..bf1a866d2 100644 --- a/sample/sample10-mixed/entity/PostDetails.ts +++ b/sample/sample10-mixed/entity/PostDetails.ts @@ -4,6 +4,7 @@ import {OneToOne, OneToMany, ManyToOne} from "../../../src/decorator/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 { @@ -17,7 +18,7 @@ export class PostDetails { @Column() comment: string; - @OneToOne(false, type => Post, post => post.details) + @OneToOneInverse(type => Post, post => post.details) post: Post; @OneToMany(type => Category, category => category.details, { diff --git a/sample/sample2-one-to-one/entity/Post.ts b/sample/sample2-one-to-one/entity/Post.ts index 178a0615d..4c59c50a8 100644 --- a/sample/sample2-one-to-one/entity/Post.ts +++ b/sample/sample2-one-to-one/entity/Post.ts @@ -21,7 +21,7 @@ export class Post { text: string; // post has relation with category, however inverse relation is not set (category does not have relation with post set) - @OneToOne(true, () => PostCategory, { + @OneToOne(() => PostCategory, { cascadeInsert: true, cascadeUpdate: true, cascadeRemove: true @@ -30,27 +30,27 @@ export class Post { // post has relation with details. cascade inserts here means if new PostDetails instance will be set to this // relation it will be inserted automatically to the db when you save this Post entity - @OneToOne(true, () => PostDetails, details => details.post, { + @OneToOne(() => PostDetails, details => details.post, { cascadeInsert: true }) details: PostDetails; // post has relation with details. cascade update here means if new PostDetail instance will be set to this relation // it will be inserted automatically to the db when you save this Post entity - @OneToOne(true, () => PostImage, image => image.post, { + @OneToOne(() => PostImage, image => image.post, { cascadeUpdate: true }) image: PostImage; // post has relation with details. cascade update here means if new PostDetail instance will be set to this relation // it will be inserted automatically to the db when you save this Post entity - @OneToOne(true, () => PostMetadata, metadata => metadata.post, { + @OneToOne(() => PostMetadata, metadata => metadata.post, { cascadeRemove: true }) metadata: PostMetadata; // post has relation with details. full cascades here - @OneToOne(true, () => PostInformation, information => information.post, { + @OneToOne(() => PostInformation, information => information.post, { cascadeInsert: true, cascadeUpdate: true, cascadeRemove: true @@ -58,7 +58,7 @@ export class Post { information: PostInformation; // post has relation with details. not cascades here. means cannot be persisted, updated or removed - @OneToOne(true, () => PostAuthor, author => author.post) + @OneToOne(() => PostAuthor, author => author.post) author: PostAuthor; } \ No newline at end of file diff --git a/sample/sample2-one-to-one/entity/PostAuthor.ts b/sample/sample2-one-to-one/entity/PostAuthor.ts index fc709a6e3..1fc961d82 100644 --- a/sample/sample2-one-to-one/entity/PostAuthor.ts +++ b/sample/sample2-one-to-one/entity/PostAuthor.ts @@ -1,7 +1,7 @@ import {PrimaryColumn, Column} from "../../../src/decorator/Columns"; import {Table} from "../../../src/decorator/Tables"; import {Post} from "./Post"; -import {OneToOne} from "../../../src/decorator/Relations"; +import {OneToOneInverse} from "../../../src/decorator/Relations"; @Table("sample2_post_author") export class PostAuthor { @@ -12,7 +12,8 @@ export class PostAuthor { @Column() name: string; - @OneToOne(false, () => Post, post => post.author) + @OneToOneInverse + (() => Post, post => post.author) post: Post; } \ No newline at end of file diff --git a/sample/sample2-one-to-one/entity/PostDetails.ts b/sample/sample2-one-to-one/entity/PostDetails.ts index 242f1a9bd..5e4b096c5 100644 --- a/sample/sample2-one-to-one/entity/PostDetails.ts +++ b/sample/sample2-one-to-one/entity/PostDetails.ts @@ -1,6 +1,6 @@ import {PrimaryColumn, Column} from "../../../src/decorator/Columns"; import {Table} from "../../../src/decorator/Tables"; -import {OneToOne} from "../../../src/decorator/Relations"; +import {OneToOneInverse} from "../../../src/decorator/Relations"; import {Post} from "./Post"; @Table("sample2_post_details") @@ -18,7 +18,7 @@ export class PostDetails { @Column() metadata: string; - @OneToOne(false, () => Post, post => post.details, { + @OneToOneInverse(() => Post, post => post.details, { cascadeInsert: true, cascadeUpdate: true, cascadeRemove: true diff --git a/sample/sample2-one-to-one/entity/PostImage.ts b/sample/sample2-one-to-one/entity/PostImage.ts index 56d42066b..3d2aa131e 100644 --- a/sample/sample2-one-to-one/entity/PostImage.ts +++ b/sample/sample2-one-to-one/entity/PostImage.ts @@ -1,7 +1,7 @@ import {PrimaryColumn, Column} from "../../../src/decorator/Columns"; import {Table} from "../../../src/decorator/Tables"; import {Post} from "./Post"; -import {OneToOne} from "../../../src/decorator/Relations"; +import {OneToOneInverse} from "../../../src/decorator/Relations"; @Table("sample2_post_image") export class PostImage { @@ -12,7 +12,7 @@ export class PostImage { @Column() url: string; - @OneToOne(false, () => Post, post => post.image) + @OneToOneInverse(() => Post, post => post.image) post: Post; } \ No newline at end of file diff --git a/sample/sample2-one-to-one/entity/PostInformation.ts b/sample/sample2-one-to-one/entity/PostInformation.ts index 6169600e2..69b22c44c 100644 --- a/sample/sample2-one-to-one/entity/PostInformation.ts +++ b/sample/sample2-one-to-one/entity/PostInformation.ts @@ -1,6 +1,6 @@ import {PrimaryColumn, Column} from "../../../src/decorator/Columns"; import {Table} from "../../../src/decorator/Tables"; -import {OneToOne} from "../../../src/decorator/Relations"; +import {OneToOneInverse} from "../../../src/decorator/Relations"; import {Post} from "./Post"; @Table("sample2_post_information") @@ -12,7 +12,7 @@ export class PostInformation { @Column() text: string; - @OneToOne(false, () => Post, post => post.information, { + @OneToOneInverse(() => Post, post => post.information, { cascadeUpdate: true, }) post: Post; diff --git a/sample/sample2-one-to-one/entity/PostMetadata.ts b/sample/sample2-one-to-one/entity/PostMetadata.ts index 99c042989..4c1b6d0d6 100644 --- a/sample/sample2-one-to-one/entity/PostMetadata.ts +++ b/sample/sample2-one-to-one/entity/PostMetadata.ts @@ -1,7 +1,7 @@ import {PrimaryColumn, Column} from "../../../src/decorator/Columns"; import {Table} from "../../../src/decorator/Tables"; import {Post} from "./Post"; -import {OneToOne} from "../../../src/decorator/Relations"; +import {OneToOneInverse} from "../../../src/decorator/Relations"; @Table("sample2_post_metadata") export class PostMetadata { @@ -12,7 +12,7 @@ export class PostMetadata { @Column() description: string; - @OneToOne(false, () => Post, post => post.metadata) + @OneToOneInverse(() => Post, post => post.metadata) post: Post; } \ No newline at end of file diff --git a/sample/sample4-many-to-many/entity/Post.ts b/sample/sample4-many-to-many/entity/Post.ts index 6370861e4..375c2c1fb 100644 --- a/sample/sample4-many-to-many/entity/Post.ts +++ b/sample/sample4-many-to-many/entity/Post.ts @@ -21,7 +21,7 @@ export class Post { text: string; // post has relation with category, however inverse relation is not set (category does not have relation with post set) - @ManyToMany(true, () => PostCategory, { + @ManyToMany(() => PostCategory, { cascadeInsert: true, cascadeUpdate: true, cascadeRemove: true @@ -30,27 +30,27 @@ export class Post { // post has relation with details. cascade inserts here means if new PostDetails instance will be set to this // relation it will be inserted automatically to the db when you save this Post entity - @ManyToMany(true, () => PostDetails, details => details.posts, { + @ManyToMany(() => PostDetails, details => details.posts, { cascadeInsert: true }) details: PostDetails[] = []; // post has relation with details. cascade update here means if new PostDetail instance will be set to this relation // it will be inserted automatically to the db when you save this Post entity - @ManyToMany(true, () => PostImage, image => image.posts, { + @ManyToMany(() => PostImage, image => image.posts, { cascadeUpdate: true }) images: PostImage[] = []; // post has relation with details. cascade update here means if new PostDetail instance will be set to this relation // it will be inserted automatically to the db when you save this Post entity - @ManyToMany(true, () => PostMetadata, metadata => metadata.posts, { + @ManyToMany(() => PostMetadata, metadata => metadata.posts, { cascadeRemove: true }) metadatas: PostMetadata[] = []; // post has relation with details. full cascades here - @ManyToMany(true, () => PostInformation, information => information.posts, { + @ManyToMany(() => PostInformation, information => information.posts, { cascadeInsert: true, cascadeUpdate: true, cascadeRemove: true @@ -58,7 +58,7 @@ export class Post { informations: PostInformation[] = []; // post has relation with details. not cascades here. means cannot be persisted, updated or removed - @ManyToMany(true, () => PostAuthor, author => author.posts) + @ManyToMany(() => PostAuthor, author => author.posts) authors: PostAuthor[] = []; } \ No newline at end of file diff --git a/sample/sample4-many-to-many/entity/PostAuthor.ts b/sample/sample4-many-to-many/entity/PostAuthor.ts index 96a752946..f8ae56026 100644 --- a/sample/sample4-many-to-many/entity/PostAuthor.ts +++ b/sample/sample4-many-to-many/entity/PostAuthor.ts @@ -1,7 +1,7 @@ import {PrimaryColumn, Column} from "../../../src/decorator/Columns"; import {Table} from "../../../src/decorator/Tables"; import {Post} from "./Post"; -import {ManyToMany} from "../../../src/decorator/Relations"; +import {ManyToManyInverse} from "../../../src/decorator/Relations"; @Table("sample4_post_author") export class PostAuthor { @@ -12,7 +12,7 @@ export class PostAuthor { @Column() name: string; - @ManyToMany(false, () => Post, post => post.authors) + @ManyToManyInverse(() => Post, post => post.authors) posts: Post[]; } \ No newline at end of file diff --git a/sample/sample4-many-to-many/entity/PostDetails.ts b/sample/sample4-many-to-many/entity/PostDetails.ts index 4ebecc98a..bf72b4bda 100644 --- a/sample/sample4-many-to-many/entity/PostDetails.ts +++ b/sample/sample4-many-to-many/entity/PostDetails.ts @@ -1,6 +1,6 @@ import {PrimaryColumn, Column} from "../../../src/decorator/Columns"; import {Table} from "../../../src/decorator/Tables"; -import {ManyToMany} from "../../../src/decorator/Relations"; +import {ManyToManyInverse} from "../../../src/decorator/Relations"; import {Post} from "./Post"; @Table("sample4_post_details") @@ -24,7 +24,7 @@ export class PostDetails { }) metadata: string; - @ManyToMany(false, () => Post, post => post.details, { + @ManyToManyInverse(() => Post, post => post.details, { cascadeInsert: true, cascadeUpdate: true, cascadeRemove: true diff --git a/sample/sample4-many-to-many/entity/PostImage.ts b/sample/sample4-many-to-many/entity/PostImage.ts index 1d4d33f58..b60301547 100644 --- a/sample/sample4-many-to-many/entity/PostImage.ts +++ b/sample/sample4-many-to-many/entity/PostImage.ts @@ -1,7 +1,7 @@ import {PrimaryColumn, Column} from "../../../src/decorator/Columns"; import {Table} from "../../../src/decorator/Tables"; import {Post} from "./Post"; -import {ManyToMany} from "../../../src/decorator/Relations"; +import {ManyToManyInverse} from "../../../src/decorator/Relations"; @Table("sample4_post_image") export class PostImage { @@ -12,7 +12,7 @@ export class PostImage { @Column() url: string; - @ManyToMany(false, () => Post, post => post.images) + @ManyToManyInverse(() => Post, post => post.images) posts: Post[]; } \ No newline at end of file diff --git a/sample/sample4-many-to-many/entity/PostInformation.ts b/sample/sample4-many-to-many/entity/PostInformation.ts index cfafaaa94..afce1e815 100644 --- a/sample/sample4-many-to-many/entity/PostInformation.ts +++ b/sample/sample4-many-to-many/entity/PostInformation.ts @@ -1,6 +1,6 @@ import {PrimaryColumn, Column} from "../../../src/decorator/Columns"; import {Table} from "../../../src/decorator/Tables"; -import {ManyToMany} from "../../../src/decorator/Relations"; +import {ManyToManyInverse} from "../../../src/decorator/Relations"; import {Post} from "./Post"; @Table("sample4_post_information") @@ -12,7 +12,7 @@ export class PostInformation { @Column() text: string; - @ManyToMany(false, () => Post, post => post.informations, { + @ManyToManyInverse(() => Post, post => post.informations, { cascadeUpdate: true, }) posts: Post[]; diff --git a/sample/sample4-many-to-many/entity/PostMetadata.ts b/sample/sample4-many-to-many/entity/PostMetadata.ts index a565a011c..d37552021 100644 --- a/sample/sample4-many-to-many/entity/PostMetadata.ts +++ b/sample/sample4-many-to-many/entity/PostMetadata.ts @@ -1,7 +1,7 @@ import {PrimaryColumn, Column} from "../../../src/decorator/Columns"; import {Table} from "../../../src/decorator/Tables"; import {Post} from "./Post"; -import {ManyToMany} from "../../../src/decorator/Relations"; +import {ManyToManyInverse} from "../../../src/decorator/Relations"; @Table("sample4_post_metadata") export class PostMetadata { @@ -12,7 +12,7 @@ export class PostMetadata { @Column() description: string; - @ManyToMany(false, () => Post, post => post.metadatas) + @ManyToManyInverse(() => Post, post => post.metadatas) posts: Post[]; } \ No newline at end of file diff --git a/src/decorator/Relations.ts b/src/decorator/Relations.ts index 37db862f3..83194af7d 100644 --- a/src/decorator/Relations.ts +++ b/src/decorator/Relations.ts @@ -1,4 +1,6 @@ export * from "./relations/OneToOne"; export * from "./relations/OneToMany"; export * from "./relations/ManyToOne"; -export * from "./relations/ManyToMany"; \ No newline at end of file +export * from "./relations/ManyToMany"; +export * from "./relations/ManyToManyInverse"; +export * from "./relations/OneToOneInverse"; \ No newline at end of file diff --git a/src/decorator/relations/ManyToMany.ts b/src/decorator/relations/ManyToMany.ts index 1ff5d843b..2896246ec 100644 --- a/src/decorator/relations/ManyToMany.ts +++ b/src/decorator/relations/ManyToMany.ts @@ -6,10 +6,9 @@ import { } from "../../metadata-builder/types/RelationTypes"; import {defaultMetadataStorage} from "../../metadata-builder/MetadataStorage"; -export function ManyToMany(isOwner: boolean, typeFunction: RelationTypeInFunction, options?: RelationOptions): Function; -export function ManyToMany(isOwner: boolean, typeFunction: RelationTypeInFunction, inverseSide?: PropertyTypeInFunction, options?: RelationOptions): Function; -export function ManyToMany(isOwner: boolean, - typeFunction: RelationTypeInFunction, +export function ManyToMany(typeFunction: RelationTypeInFunction, options?: RelationOptions): Function; +export function ManyToMany(typeFunction: RelationTypeInFunction, inverseSide?: PropertyTypeInFunction, options?: RelationOptions): Function; +export function ManyToMany(typeFunction: RelationTypeInFunction, inverseSideOrOptions: PropertyTypeInFunction|RelationOptions, options?: RelationOptions): Function { let inverseSideProperty: PropertyTypeInFunction; @@ -30,7 +29,7 @@ export function ManyToMany(isOwner: boolean, relationType: RelationTypes.MANY_TO_MANY, type: typeFunction, inverseSideProperty: inverseSideProperty, - isOwning: isOwner, + isOwning: true, options: options })); }; diff --git a/src/decorator/relations/ManyToManyInverse.ts b/src/decorator/relations/ManyToManyInverse.ts new file mode 100644 index 000000000..c63be987e --- /dev/null +++ b/src/decorator/relations/ManyToManyInverse.ts @@ -0,0 +1,37 @@ +import {RelationMetadata} from "../../metadata-builder/metadata/RelationMetadata"; +import {RelationOptions} from "../../metadata-builder/options/RelationOptions"; +import { + RelationTypeInFunction, PropertyTypeInFunction, + RelationTypes +} from "../../metadata-builder/types/RelationTypes"; +import {defaultMetadataStorage} from "../../metadata-builder/MetadataStorage"; + +export function ManyToManyInverse(typeFunction: RelationTypeInFunction, options?: RelationOptions): Function; +export function ManyToManyInverse(typeFunction: RelationTypeInFunction, inverseSide?: PropertyTypeInFunction, options?: RelationOptions): Function; +export function ManyToManyInverse(typeFunction: RelationTypeInFunction, + inverseSideOrOptions: PropertyTypeInFunction|RelationOptions, + options?: RelationOptions): Function { + let inverseSideProperty: PropertyTypeInFunction; + if (typeof inverseSideOrOptions === "object") { + options = inverseSideOrOptions; + } else { + inverseSideProperty = > inverseSideOrOptions; + } + + return function (object: Object, propertyName: string) { + + if (!options) + options = {}; + + defaultMetadataStorage.addRelationMetadata(new RelationMetadata({ + target: object.constructor, + propertyName: propertyName, + relationType: RelationTypes.MANY_TO_MANY, + type: typeFunction, + inverseSideProperty: inverseSideProperty, + isOwning: false, + options: options + })); + }; +} + diff --git a/src/decorator/relations/OneToOne.ts b/src/decorator/relations/OneToOne.ts index 37a6a6a32..61f126212 100644 --- a/src/decorator/relations/OneToOne.ts +++ b/src/decorator/relations/OneToOne.ts @@ -6,10 +6,9 @@ import { } from "../../metadata-builder/types/RelationTypes"; import {defaultMetadataStorage} from "../../metadata-builder/MetadataStorage"; -export function OneToOne(isOwning: boolean, typeFunction: RelationTypeInFunction, options?: RelationOptions): Function; -export function OneToOne(isOwning: boolean, typeFunction: RelationTypeInFunction, inverseSide?: PropertyTypeInFunction, options?: RelationOptions): Function; -export function OneToOne(isOwning: boolean, - typeFunction: RelationTypeInFunction, +export function OneToOne(typeFunction: RelationTypeInFunction, options?: RelationOptions): Function; +export function OneToOne(typeFunction: RelationTypeInFunction, inverseSide?: PropertyTypeInFunction, options?: RelationOptions): Function; +export function OneToOne(typeFunction: RelationTypeInFunction, inverseSideOrOptions: PropertyTypeInFunction|RelationOptions, options?: RelationOptions): Function { let inverseSideProperty: PropertyTypeInFunction; @@ -30,7 +29,7 @@ export function OneToOne(isOwning: boolean, relationType: RelationTypes.ONE_TO_ONE, type: typeFunction, inverseSideProperty: inverseSideProperty, - isOwning: isOwning, + isOwning: true, options: options })); }; diff --git a/src/decorator/relations/OneToOneInverse.ts b/src/decorator/relations/OneToOneInverse.ts new file mode 100644 index 000000000..2fa54e4ed --- /dev/null +++ b/src/decorator/relations/OneToOneInverse.ts @@ -0,0 +1,36 @@ +import {RelationMetadata} from "../../metadata-builder/metadata/RelationMetadata"; +import {RelationOptions} from "../../metadata-builder/options/RelationOptions"; +import { + PropertyTypeInFunction, RelationTypeInFunction, + RelationTypes +} from "../../metadata-builder/types/RelationTypes"; +import {defaultMetadataStorage} from "../../metadata-builder/MetadataStorage"; + +export function OneToOneInverse(typeFunction: RelationTypeInFunction, options?: RelationOptions): Function; +export function OneToOneInverse(typeFunction: RelationTypeInFunction, inverseSide?: PropertyTypeInFunction, options?: RelationOptions): Function; +export function OneToOneInverse(typeFunction: RelationTypeInFunction, + inverseSideOrOptions: PropertyTypeInFunction|RelationOptions, + options?: RelationOptions): Function { + let inverseSideProperty: PropertyTypeInFunction; + if (typeof inverseSideOrOptions === "object") { + options = inverseSideOrOptions; + } else { + inverseSideProperty = > inverseSideOrOptions; + } + + return function (object: Object, propertyName: string) { + + if (!options) + options = {}; + + defaultMetadataStorage.addRelationMetadata(new RelationMetadata({ + target: object.constructor, + propertyName: propertyName, + relationType: RelationTypes.ONE_TO_ONE, + type: typeFunction, + inverseSideProperty: inverseSideProperty, + isOwning: false, + options: options + })); + }; +} \ No newline at end of file