created inserse relation decorators

This commit is contained in:
Umed Khudoiberdiev 2016-03-19 14:03:02 +05:00
parent 6aaf1f515c
commit 7ccad582ff
22 changed files with 127 additions and 52 deletions

View File

@ -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<Post>(false, type => Post, post => post.categories)
@ManyToManyInverse<Post>(type => Post, post => post.categories)
posts: Post[];
@ManyToOne<PostDetails>(_ => PostDetails, postDetails => postDetails.categories)

View File

@ -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<ImageDetails>(true, () => ImageDetails, details => details.image, {
@OneToOne<ImageDetails>(() => ImageDetails, details => details.image, {
cascadeInsert: true,
cascadeUpdate: true,
cascadeRemove: true

View File

@ -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<Image>(false, () => Image, image => image.details)
@OneToOneInverse<Image>(() => Image, image => image.details)
image: Image;
}

View File

@ -22,7 +22,7 @@ export class Post {
})
text: string;
@OneToOne<PostDetails>(true, () => PostDetails, details => details.post, {
@OneToOne<PostDetails>(() => PostDetails, details => details.post, {
cascadeInsert: true,
cascadeUpdate: true,
cascadeRemove: true
@ -51,7 +51,7 @@ export class Post {
})
coverId: number;
@ManyToMany<Category>(true, type => Category, category => category.posts, {
@ManyToMany<Category>(type => Category, category => category.posts, {
cascadeInsert: true,
cascadeUpdate: true,
cascadeRemove: true

View File

@ -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<Post>(false, type => Post, post => post.details)
@OneToOneInverse<Post>(type => Post, post => post.details)
post: Post;
@OneToMany<Category>(type => Category, category => category.details, {

View File

@ -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<PostCategory>(true, () => PostCategory, {
@OneToOne<PostCategory>(() => 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<PostDetails>(true, () => PostDetails, details => details.post, {
@OneToOne<PostDetails>(() => 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<PostImage>(true, () => PostImage, image => image.post, {
@OneToOne<PostImage>(() => 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<PostMetadata>(true, () => PostMetadata, metadata => metadata.post, {
@OneToOne<PostMetadata>(() => PostMetadata, metadata => metadata.post, {
cascadeRemove: true
})
metadata: PostMetadata;
// post has relation with details. full cascades here
@OneToOne<PostInformation>(true, () => PostInformation, information => information.post, {
@OneToOne<PostInformation>(() => 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<PostAuthor>(true, () => PostAuthor, author => author.post)
@OneToOne<PostAuthor>(() => PostAuthor, author => author.post)
author: PostAuthor;
}

View File

@ -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<Post>(false, () => Post, post => post.author)
@OneToOneInverse
<Post>(() => Post, post => post.author)
post: Post;
}

View File

@ -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<Post>(false, () => Post, post => post.details, {
@OneToOneInverse<Post>(() => Post, post => post.details, {
cascadeInsert: true,
cascadeUpdate: true,
cascadeRemove: true

View File

@ -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<Post>(false, () => Post, post => post.image)
@OneToOneInverse<Post>(() => Post, post => post.image)
post: Post;
}

View File

@ -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<Post>(false, () => Post, post => post.information, {
@OneToOneInverse<Post>(() => Post, post => post.information, {
cascadeUpdate: true,
})
post: Post;

View File

@ -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<Post>(false, () => Post, post => post.metadata)
@OneToOneInverse<Post>(() => Post, post => post.metadata)
post: Post;
}

View File

@ -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<PostCategory>(true, () => PostCategory, {
@ManyToMany<PostCategory>(() => 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<PostDetails>(true, () => PostDetails, details => details.posts, {
@ManyToMany<PostDetails>(() => 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<PostImage>(true, () => PostImage, image => image.posts, {
@ManyToMany<PostImage>(() => 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<PostMetadata>(true, () => PostMetadata, metadata => metadata.posts, {
@ManyToMany<PostMetadata>(() => PostMetadata, metadata => metadata.posts, {
cascadeRemove: true
})
metadatas: PostMetadata[] = [];
// post has relation with details. full cascades here
@ManyToMany<PostInformation>(true, () => PostInformation, information => information.posts, {
@ManyToMany<PostInformation>(() => 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<PostAuthor>(true, () => PostAuthor, author => author.posts)
@ManyToMany<PostAuthor>(() => PostAuthor, author => author.posts)
authors: PostAuthor[] = [];
}

View File

@ -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<Post>(false, () => Post, post => post.authors)
@ManyToManyInverse<Post>(() => Post, post => post.authors)
posts: Post[];
}

View File

@ -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<Post>(false, () => Post, post => post.details, {
@ManyToManyInverse<Post>(() => Post, post => post.details, {
cascadeInsert: true,
cascadeUpdate: true,
cascadeRemove: true

View File

@ -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<Post>(false, () => Post, post => post.images)
@ManyToManyInverse<Post>(() => Post, post => post.images)
posts: Post[];
}

View File

@ -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<Post>(false, () => Post, post => post.informations, {
@ManyToManyInverse<Post>(() => Post, post => post.informations, {
cascadeUpdate: true,
})
posts: Post[];

View File

@ -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<Post>(false, () => Post, post => post.metadatas)
@ManyToManyInverse<Post>(() => Post, post => post.metadatas)
posts: Post[];
}

View File

@ -1,4 +1,6 @@
export * from "./relations/OneToOne";
export * from "./relations/OneToMany";
export * from "./relations/ManyToOne";
export * from "./relations/ManyToMany";
export * from "./relations/ManyToMany";
export * from "./relations/ManyToManyInverse";
export * from "./relations/OneToOneInverse";

View File

@ -6,10 +6,9 @@ import {
} from "../../metadata-builder/types/RelationTypes";
import {defaultMetadataStorage} from "../../metadata-builder/MetadataStorage";
export function ManyToMany<T>(isOwner: boolean, typeFunction: RelationTypeInFunction, options?: RelationOptions): Function;
export function ManyToMany<T>(isOwner: boolean, typeFunction: RelationTypeInFunction, inverseSide?: PropertyTypeInFunction<T>, options?: RelationOptions): Function;
export function ManyToMany<T>(isOwner: boolean,
typeFunction: RelationTypeInFunction,
export function ManyToMany<T>(typeFunction: RelationTypeInFunction, options?: RelationOptions): Function;
export function ManyToMany<T>(typeFunction: RelationTypeInFunction, inverseSide?: PropertyTypeInFunction<T>, options?: RelationOptions): Function;
export function ManyToMany<T>(typeFunction: RelationTypeInFunction,
inverseSideOrOptions: PropertyTypeInFunction<T>|RelationOptions,
options?: RelationOptions): Function {
let inverseSideProperty: PropertyTypeInFunction<T>;
@ -30,7 +29,7 @@ export function ManyToMany<T>(isOwner: boolean,
relationType: RelationTypes.MANY_TO_MANY,
type: typeFunction,
inverseSideProperty: inverseSideProperty,
isOwning: isOwner,
isOwning: true,
options: options
}));
};

View File

@ -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<T>(typeFunction: RelationTypeInFunction, options?: RelationOptions): Function;
export function ManyToManyInverse<T>(typeFunction: RelationTypeInFunction, inverseSide?: PropertyTypeInFunction<T>, options?: RelationOptions): Function;
export function ManyToManyInverse<T>(typeFunction: RelationTypeInFunction,
inverseSideOrOptions: PropertyTypeInFunction<T>|RelationOptions,
options?: RelationOptions): Function {
let inverseSideProperty: PropertyTypeInFunction<T>;
if (typeof inverseSideOrOptions === "object") {
options = <RelationOptions> inverseSideOrOptions;
} else {
inverseSideProperty = <PropertyTypeInFunction<T>> 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
}));
};
}

View File

@ -6,10 +6,9 @@ import {
} from "../../metadata-builder/types/RelationTypes";
import {defaultMetadataStorage} from "../../metadata-builder/MetadataStorage";
export function OneToOne<T>(isOwning: boolean, typeFunction: RelationTypeInFunction, options?: RelationOptions): Function;
export function OneToOne<T>(isOwning: boolean, typeFunction: RelationTypeInFunction, inverseSide?: PropertyTypeInFunction<T>, options?: RelationOptions): Function;
export function OneToOne<T>(isOwning: boolean,
typeFunction: RelationTypeInFunction,
export function OneToOne<T>(typeFunction: RelationTypeInFunction, options?: RelationOptions): Function;
export function OneToOne<T>(typeFunction: RelationTypeInFunction, inverseSide?: PropertyTypeInFunction<T>, options?: RelationOptions): Function;
export function OneToOne<T>(typeFunction: RelationTypeInFunction,
inverseSideOrOptions: PropertyTypeInFunction<T>|RelationOptions,
options?: RelationOptions): Function {
let inverseSideProperty: PropertyTypeInFunction<T>;
@ -30,7 +29,7 @@ export function OneToOne<T>(isOwning: boolean,
relationType: RelationTypes.ONE_TO_ONE,
type: typeFunction,
inverseSideProperty: inverseSideProperty,
isOwning: isOwning,
isOwning: true,
options: options
}));
};

View File

@ -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<T>(typeFunction: RelationTypeInFunction, options?: RelationOptions): Function;
export function OneToOneInverse<T>(typeFunction: RelationTypeInFunction, inverseSide?: PropertyTypeInFunction<T>, options?: RelationOptions): Function;
export function OneToOneInverse<T>(typeFunction: RelationTypeInFunction,
inverseSideOrOptions: PropertyTypeInFunction<T>|RelationOptions,
options?: RelationOptions): Function {
let inverseSideProperty: PropertyTypeInFunction<T>;
if (typeof inverseSideOrOptions === "object") {
options = <RelationOptions> inverseSideOrOptions;
} else {
inverseSideProperty = <PropertyTypeInFunction<T>> 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
}));
};
}