improved relation describe style in relation decorators

This commit is contained in:
Umed Khudoiberdiev 2016-03-20 20:16:24 +05:00
parent 7aa02a5e8d
commit 3764579896
39 changed files with 192 additions and 153 deletions

View File

@ -13,10 +13,10 @@ export class Category {
@Column()
description: string;
@ManyToManyInverse<Post>(type => Post, post => post.categories)
@ManyToManyInverse(type => Post, post => post.categories)
posts: Post[];
@ManyToOne<PostDetails>(_ => PostDetails, postDetails => postDetails.categories)
@ManyToOne(type => PostDetails, postDetails => postDetails.categories)
details: PostDetails;
}

View File

@ -12,7 +12,7 @@ export class Chapter {
@Column()
about: string;
@OneToMany<PostDetails>(type => PostDetails, postDetails => postDetails.chapter)
@OneToMany(type => PostDetails, postDetails => postDetails.chapter)
postDetails: PostDetails[];
}

View File

@ -12,7 +12,7 @@ export class Cover {
@Column()
url: string;
@OneToMany<Post>(() => Post, post => post.cover)
@OneToMany(type => Post, post => post.cover)
posts: Post[];
}

View File

@ -13,15 +13,15 @@ export class Image {
@Column()
name: string;
@ManyToOne<Post>(() => Post, post => post.images)
@ManyToOne(type => Post, post => post.images)
post: Post;
@ManyToOne<Post>(() => Post, post => post.secondaryImages, {
@ManyToOne(type => Post, post => post.secondaryImages, {
cascadeInsert: true
})
secondaryPost: Post;
@OneToOne<ImageDetails>(() => ImageDetails, details => details.image, {
@OneToOne(type => ImageDetails, details => details.image, {
cascadeInsert: true,
cascadeUpdate: true,
cascadeRemove: true

View File

@ -15,7 +15,7 @@ export class ImageDetails {
@Column()
comment: string;
@OneToOneInverse<Image>(() => Image, image => image.details)
@OneToOneInverse(type => Image, image => image.details)
image: Image;
}

View File

@ -22,24 +22,24 @@ export class Post {
})
text: string;
@OneToOne<PostDetails>(() => PostDetails, details => details.post, {
@OneToOne(type => PostDetails, details => details.post, {
cascadeInsert: true,
cascadeUpdate: true,
cascadeRemove: true
})
details: PostDetails;
@OneToMany<Image>(type => Image, image => image.post, {
@OneToMany(type => Image, image => image.post, {
cascadeInsert: true,
cascadeUpdate: true,
cascadeRemove: true
})
images: Image[] = [];
@OneToMany<Image>(type => Image, image => image.secondaryPost)
@OneToMany(type => Image, image => image.secondaryPost)
secondaryImages: Image[];
@ManyToOne<Cover>(type => Cover, cover => cover.posts, {
@ManyToOne(type => Cover, cover => cover.posts, {
name: "coverId",
cascadeInsert: true,
cascadeRemove: true
@ -51,7 +51,7 @@ export class Post {
})
coverId: number;
@ManyToMany<Category>(type => Category, category => category.posts, {
@ManyToMany(type => Category, category => category.posts, {
cascadeInsert: true,
cascadeUpdate: true,
cascadeRemove: true

View File

@ -18,16 +18,16 @@ export class PostDetails {
@Column()
comment: string;
@OneToOneInverse<Post>(type => Post, post => post.details)
@OneToOneInverse(type => Post, post => post.details)
post: Post;
@OneToMany<Category>(type => Category, category => category.details, {
@OneToMany(type => Category, category => category.details, {
cascadeInsert: true,
cascadeRemove: true
})
categories: Category[];
@ManyToOne<Chapter>(_ => Chapter, chapter => chapter.postDetails, {
@ManyToOne(type => Chapter, chapter => chapter.postDetails, {
cascadeInsert: true,
cascadeRemove: true
})

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

View File

@ -12,8 +12,7 @@ export class PostAuthor {
@Column()
name: string;
@OneToOneInverse
<Post>(() => Post, post => post.author)
@OneToOneInverse(type => Post, post => post.author)
post: Post;
}

View File

@ -18,7 +18,7 @@ export class PostDetails {
@Column()
metadata: string;
@OneToOneInverse<Post>(() => Post, post => post.details, {
@OneToOneInverse(type => Post, post => post.details, {
cascadeInsert: true,
cascadeUpdate: true,
cascadeRemove: true

View File

@ -12,7 +12,7 @@ export class PostImage {
@Column()
url: string;
@OneToOneInverse<Post>(() => Post, post => post.image)
@OneToOneInverse(type => Post, post => post.image)
post: Post;
}

View File

@ -12,7 +12,7 @@ export class PostInformation {
@Column()
text: string;
@OneToOneInverse<Post>(() => Post, post => post.information, {
@OneToOneInverse(type => Post, post => post.information, {
cascadeUpdate: true,
})
post: Post;

View File

@ -12,7 +12,7 @@ export class PostMetadata {
@Column()
description: string;
@OneToOneInverse<Post>(() => Post, post => post.metadata)
@OneToOneInverse(type => 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)
@ManyToOne<PostCategory>(() => PostCategory, {
@ManyToOne(type => 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
@ManyToOne<PostDetails>(() => PostDetails, details => details.posts, {
@ManyToOne(type => 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
@ManyToOne<PostImage>(() => PostImage, image => image.posts, {
@ManyToOne(type => PostImage, image => image.posts, {
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
@ManyToOne<PostMetadata>(() => PostMetadata, metadata => metadata.posts, {
@ManyToOne(type => PostMetadata, metadata => metadata.posts, {
cascadeRemove: true
})
metadata: PostMetadata;
// post has relation with details. full cascades here
@ManyToOne<PostInformation>(() => PostInformation, information => information.posts, {
@ManyToOne(type => PostInformation, information => information.posts, {
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
@ManyToOne<PostAuthor>(() => PostAuthor, author => author.posts)
@ManyToOne(type => PostAuthor, author => author.posts)
author: PostAuthor;
}

View File

@ -12,7 +12,7 @@ export class PostAuthor {
@Column()
name: string;
@OneToMany<Post>(() => Post, post => post.author)
@OneToMany(type => Post, post => post.author)
posts: Post[];
}

View File

@ -24,7 +24,7 @@ export class PostDetails {
})
metadata: string;
@OneToMany<Post>(() => Post, post => post.details, {
@OneToMany(type => Post, post => post.details, {
cascadeInsert: true,
cascadeUpdate: true,
cascadeRemove: true

View File

@ -12,7 +12,7 @@ export class PostImage {
@Column()
url: string;
@OneToMany<Post>(() => Post, post => post.image)
@OneToMany(type => Post, post => post.image)
posts: Post[];
}

View File

@ -12,7 +12,7 @@ export class PostInformation {
@Column()
text: string;
@OneToMany<Post>(() => Post, post => post.information, {
@OneToMany(type => Post, post => post.information, {
cascadeUpdate: true,
})
posts: Post[];

View File

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

View File

@ -12,7 +12,7 @@ export class PostAuthor {
@Column()
name: string;
@ManyToManyInverse<Post>(() => Post, post => post.authors)
@ManyToManyInverse(type => Post, post => post.authors)
posts: Post[];
}

View File

@ -24,7 +24,7 @@ export class PostDetails {
})
metadata: string;
@ManyToManyInverse<Post>(() => Post, post => post.details, {
@ManyToManyInverse(type => Post, post => post.details, {
cascadeInsert: true,
cascadeUpdate: true,
cascadeRemove: true

View File

@ -12,7 +12,7 @@ export class PostImage {
@Column()
url: string;
@ManyToManyInverse<Post>(() => Post, post => post.images)
@ManyToManyInverse(type => Post, post => post.images)
posts: Post[];
}

View File

@ -12,7 +12,7 @@ export class PostInformation {
@Column()
text: string;
@ManyToManyInverse<Post>(() => Post, post => post.informations, {
@ManyToManyInverse(type => Post, post => post.informations, {
cascadeUpdate: true,
})
posts: Post[];

View File

@ -12,7 +12,7 @@ export class PostMetadata {
@Column()
description: string;
@ManyToManyInverse<Post>(() => Post, post => post.metadatas)
@ManyToManyInverse(type => Post, post => post.metadatas)
posts: Post[];
}

View File

@ -1,10 +1,6 @@
import {RelationMetadata} from "../../metadata-builder/metadata/RelationMetadata";
import {RelationOptions} from "../../metadata-builder/options/RelationOptions";
import {
RelationTypeInFunction,
PropertyTypeInFunction,
RelationTypes
} from "../../metadata-builder/types/RelationTypes";
import {ObjectConstructor, RelationTypes} from "../../metadata-builder/types/RelationTypes";
import {defaultMetadataStorage} from "../../metadata-builder/MetadataStorage";
/**
@ -12,28 +8,30 @@ import {defaultMetadataStorage} from "../../metadata-builder/MetadataStorage";
* multiple instances of Entity1. To achieve it, this type of relation creates a junction table, where it storage
* entity1 and entity2 ids. This is owner side of the relationship.
*/
export function ManyToMany<T>(typeFunction: RelationTypeInFunction, options?: RelationOptions): Function;
export function ManyToMany<T>(typeFunction: (type?: any) => ObjectConstructor<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 owner side of the relationship.
*/
export function ManyToMany<T>(typeFunction: RelationTypeInFunction, inverseSide?: PropertyTypeInFunction<T>, options?: RelationOptions): Function;
export function ManyToMany<T>(typeFunction: (type?: any) => ObjectConstructor<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 owner side of the relationship.
*/
export function ManyToMany<T>(typeFunction: RelationTypeInFunction,
inverseSideOrOptions: PropertyTypeInFunction<T>|RelationOptions,
export function ManyToMany<T>(typeFunction: (type?: any) => ObjectConstructor<T>,
inverseSideOrOptions?: string|((object: T) => any)|RelationOptions,
options?: RelationOptions): Function {
let inverseSideProperty: PropertyTypeInFunction<T>;
let inverseSideProperty: string|((object: T) => any);
if (typeof inverseSideOrOptions === "object") {
options = <RelationOptions> inverseSideOrOptions;
} else {
inverseSideProperty = <PropertyTypeInFunction<T>> inverseSideOrOptions;
inverseSideProperty = <string|((object: T) => any)> inverseSideOrOptions;
}
return function (object: Object, propertyName: string) {

View File

@ -1,10 +1,6 @@
import {RelationMetadata} from "../../metadata-builder/metadata/RelationMetadata";
import {RelationOptions} from "../../metadata-builder/options/RelationOptions";
import {
RelationTypeInFunction,
PropertyTypeInFunction,
RelationTypes
} from "../../metadata-builder/types/RelationTypes";
import {ObjectConstructor, RelationTypes} from "../../metadata-builder/types/RelationTypes";
import {defaultMetadataStorage} from "../../metadata-builder/MetadataStorage";
/**
@ -12,28 +8,30 @@ import {defaultMetadataStorage} from "../../metadata-builder/MetadataStorage";
* 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: RelationTypeInFunction, options?: RelationOptions): Function;
export function ManyToManyInverse<T>(typeFunction: (type?: any) => ObjectConstructor<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: RelationTypeInFunction, inverseSide?: PropertyTypeInFunction<T>, options?: RelationOptions): Function;
export function ManyToManyInverse<T>(typeFunction: (type?: any) => ObjectConstructor<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: RelationTypeInFunction,
inverseSideOrOptions: PropertyTypeInFunction<T>|RelationOptions,
export function ManyToManyInverse<T>(typeFunction: (type?: any) => ObjectConstructor<T>,
inverseSideOrOptions?: string|((object: T) => any)|RelationOptions,
options?: RelationOptions): Function {
let inverseSideProperty: PropertyTypeInFunction<T>;
let inverseSideProperty: string|((object: T) => any);
if (typeof inverseSideOrOptions === "object") {
options = <RelationOptions> inverseSideOrOptions;
} else {
inverseSideProperty = <PropertyTypeInFunction<T>> inverseSideOrOptions;
inverseSideProperty = <string|((object: T) => any)> inverseSideOrOptions;
}
return function (object: Object, propertyName: string) {

View File

@ -1,10 +1,6 @@
import {RelationMetadata} from "../../metadata-builder/metadata/RelationMetadata";
import {RelationOptions} from "../../metadata-builder/options/RelationOptions";
import {
PropertyTypeInFunction,
RelationTypeInFunction,
RelationTypes
} from "../../metadata-builder/types/RelationTypes";
import {RelationTypes, ObjectConstructor} from "../../metadata-builder/types/RelationTypes";
import {defaultMetadataStorage} from "../../metadata-builder/MetadataStorage";
/**
@ -12,28 +8,30 @@ import {defaultMetadataStorage} from "../../metadata-builder/MetadataStorage";
* Entity2 can have a multiple instances of Entity1. Entity1 is an owner of the relationship, and storages Entity2 id
* on its own side.
*/
export function ManyToOne<T>(typeFunction: RelationTypeInFunction, options?: RelationOptions): Function;
export function ManyToOne<T>(typeFunction: (type?: any) => ObjectConstructor<T>, options?: RelationOptions): Function;
/**
* Many-to-one relation allows to create type of relation when Entity1 can have single instance of Entity2, but
* Entity2 can have a multiple instances of Entity1. Entity1 is an owner of the relationship, and storages Entity2 id
* on its own side.
*/
export function ManyToOne<T>(typeFunction: RelationTypeInFunction, inverseSide?: PropertyTypeInFunction<T>, options?: RelationOptions): Function;
export function ManyToOne<T>(typeFunction: (type?: any) => ObjectConstructor<T>,
inverseSide?: string|((object: T) => any),
options?: RelationOptions): Function;
/**
* Many-to-one relation allows to create type of relation when Entity1 can have single instance of Entity2, but
* Entity2 can have a multiple instances of Entity1. Entity1 is an owner of the relationship, and storages Entity2 id
* on its own side.
*/
export function ManyToOne<T>(typeFunction: RelationTypeInFunction,
inverseSideOrOptions: PropertyTypeInFunction<T>|RelationOptions,
options?: RelationOptions): Function {
let inverseSideProperty: PropertyTypeInFunction<T>;
export function ManyToOne<T>(typeFunction: (type?: any) => ObjectConstructor<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 = <PropertyTypeInFunction<T>> inverseSideOrOptions;
inverseSideProperty = <string|((object: T) => any)> inverseSideOrOptions;
}
return function (object: Object, propertyName: string) {

View File

@ -1,36 +1,34 @@
import {RelationMetadata} from "../../metadata-builder/metadata/RelationMetadata";
import {RelationOptions} from "../../metadata-builder/options/RelationOptions";
import {
PropertyTypeInFunction,
RelationTypeInFunction,
RelationTypes
} from "../../metadata-builder/types/RelationTypes";
import {ObjectConstructor, RelationTypes} from "../../metadata-builder/types/RelationTypes";
import {defaultMetadataStorage} from "../../metadata-builder/MetadataStorage";
/**
* 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: RelationTypeInFunction, options?: RelationOptions): Function;
export function OneToMany<T>(typeFunction: (type?: any) => ObjectConstructor<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: RelationTypeInFunction, inverseSide?: PropertyTypeInFunction<T>, options?: RelationOptions): Function;
export function OneToMany<T>(typeFunction: (type?: any) => ObjectConstructor<T>,
inverseSide?: string|((object: T) => any),
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: RelationTypeInFunction,
inverseSideOrOptions: PropertyTypeInFunction<T>|RelationOptions,
export function OneToMany<T>(typeFunction: (type?: any) => ObjectConstructor<T>,
inverseSideOrOptions?: string|((object: T) => any)|RelationOptions,
options?: RelationOptions): Function {
let inverseSideProperty: PropertyTypeInFunction<T>;
let inverseSideProperty: string|((object: T) => any);
if (typeof inverseSideOrOptions === "object") {
options = <RelationOptions> inverseSideOrOptions;
} else {
inverseSideProperty = <PropertyTypeInFunction<T>> inverseSideOrOptions;
inverseSideProperty = <string|((object: T) => any)> inverseSideOrOptions;
}
return function (object: Object, propertyName: string) {

View File

@ -1,36 +1,34 @@
import {RelationMetadata} from "../../metadata-builder/metadata/RelationMetadata";
import {RelationOptions} from "../../metadata-builder/options/RelationOptions";
import {
PropertyTypeInFunction,
RelationTypeInFunction,
RelationTypes
} from "../../metadata-builder/types/RelationTypes";
import {ObjectConstructor, RelationTypes} from "../../metadata-builder/types/RelationTypes";
import {defaultMetadataStorage} from "../../metadata-builder/MetadataStorage";
/**
* One-to-one relation allows to create direct relation between two entities. Entity1 have only one Entity2.
* Entity1 is an owner of the relationship, and storages Entity1 id on its own side.
*/
export function OneToOne<T>(typeFunction: RelationTypeInFunction, options?: RelationOptions): Function;
export function OneToOne<T>(typeFunction: (type?: any) => ObjectConstructor<T>, options?: RelationOptions): Function;
/**
* One-to-one relation allows to create direct relation between two entities. Entity1 have only one Entity2.
* Entity1 is an owner of the relationship, and storages Entity1 id on its own side.
*/
export function OneToOne<T>(typeFunction: RelationTypeInFunction, inverseSide?: PropertyTypeInFunction<T>, options?: RelationOptions): Function;
export function OneToOne<T>(typeFunction: (type?: any) => ObjectConstructor<T>,
inverseSide?: string|((object: T) => any),
options?: RelationOptions): Function;
/**
* One-to-one relation allows to create direct relation between two entities. Entity1 have only one Entity2.
* Entity1 is an owner of the relationship, and storages Entity1 id on its own side.
*/
export function OneToOne<T>(typeFunction: RelationTypeInFunction,
inverseSideOrOptions: PropertyTypeInFunction<T>|RelationOptions,
export function OneToOne<T>(typeFunction: (type?: any) => ObjectConstructor<T>,
inverseSideOrOptions?: string|((object: T) => any)|RelationOptions,
options?: RelationOptions): Function {
let inverseSideProperty: PropertyTypeInFunction<T>;
let inverseSideProperty: string|((object: T) => any);
if (typeof inverseSideOrOptions === "object") {
options = <RelationOptions> inverseSideOrOptions;
} else {
inverseSideProperty = <PropertyTypeInFunction<T>> inverseSideOrOptions;
inverseSideProperty = <string|((object: T) => any)> inverseSideOrOptions;
}
return function (object: Object, propertyName: string) {

View File

@ -1,10 +1,6 @@
import {RelationMetadata} from "../../metadata-builder/metadata/RelationMetadata";
import {RelationOptions} from "../../metadata-builder/options/RelationOptions";
import {
PropertyTypeInFunction,
RelationTypeInFunction,
RelationTypes
} from "../../metadata-builder/types/RelationTypes";
import {RelationTypes, ObjectConstructor} from "../../metadata-builder/types/RelationTypes";
import {defaultMetadataStorage} from "../../metadata-builder/MetadataStorage";
/**
@ -12,28 +8,30 @@ import {defaultMetadataStorage} from "../../metadata-builder/MetadataStorage";
* 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: RelationTypeInFunction, options?: RelationOptions): Function;
export function OneToOneInverse<T>(typeFunction: (type?: any) => ObjectConstructor<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: RelationTypeInFunction, inverseSide?: PropertyTypeInFunction<T>, options?: RelationOptions): Function;
export function OneToOneInverse<T>(typeFunction: (type?: any) => ObjectConstructor<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: RelationTypeInFunction,
inverseSideOrOptions: PropertyTypeInFunction<T>|RelationOptions,
export function OneToOneInverse<T>(typeFunction: (type?: any) => ObjectConstructor<T>,
inverseSideOrOptions?: string|((object: T) => any)|RelationOptions,
options?: RelationOptions): Function {
let inverseSideProperty: PropertyTypeInFunction<T>;
let inverseSideProperty: string|((object: T) => any);
if (typeof inverseSideOrOptions === "object") {
options = <RelationOptions> inverseSideOrOptions;
} else {
inverseSideProperty = <PropertyTypeInFunction<T>> inverseSideOrOptions;
inverseSideProperty = <string|((object: T) => any)> inverseSideOrOptions;
}
return function (object: Object, propertyName: string) {

View File

@ -5,7 +5,6 @@ import {EntityMetadata} from "./metadata/EntityMetadata";
import {NamingStrategy} from "../naming-strategy/NamingStrategy";
import {ColumnMetadata} from "./metadata/ColumnMetadata";
import {ColumnOptions} from "./options/ColumnOptions";
import {RelationTypes} from "./types/RelationTypes";
import {ForeignKeyMetadata} from "./metadata/ForeignKeyMetadata";
/**

View File

@ -19,7 +19,7 @@ export class MetadataStorage {
private _tableMetadatas: TableMetadata[] = [];
private _ormEventSubscriberMetadatas: OrmEventSubscriberMetadata[] = [];
private _fieldMetadatas: ColumnMetadata[] = [];
private _columnMetadatas: ColumnMetadata[] = [];
private _indexMetadatas: IndexMetadata[] = [];
private _compoundIndexMetadatas: CompoundIndexMetadata[] = [];
private _relationMetadatas: RelationMetadata[] = [];
@ -36,8 +36,8 @@ export class MetadataStorage {
return this._ormEventSubscriberMetadatas;
}
get fieldMetadatas(): ColumnMetadata[] {
return this._fieldMetadatas;
get columnMetadatas(): ColumnMetadata[] {
return this._columnMetadatas;
}
get indexMetadatas(): IndexMetadata[] {
@ -83,7 +83,7 @@ export class MetadataStorage {
if (metadata.name && this.hasFieldMetadataWithName(metadata.target, metadata.name))
throw new MetadataWithSuchNameAlreadyExistsError("Column", metadata.name);
this.fieldMetadatas.push(metadata);
this.columnMetadatas.push(metadata);
}
addOrmEventSubscriberMetadata(metadata: OrmEventSubscriberMetadata) {
@ -136,7 +136,7 @@ export class MetadataStorage {
}
findFieldMetadatasForClasses(classes: Function[]): ColumnMetadata[] {
return this.fieldMetadatas.filter(metadata => classes.indexOf(metadata.target) !== -1);
return this.columnMetadatas.filter(metadata => classes.indexOf(metadata.target) !== -1);
}
findRelationMetadatasForClasses(classes: Function[]): RelationMetadata[] {
@ -148,43 +148,35 @@ export class MetadataStorage {
// -------------------------------------------------------------------------
private hasTableMetadataWithObjectConstructor(constructor: Function): boolean {
return this.tableMetadatas.reduce((found, metadata) => metadata.target === constructor ? metadata : found, null) !== null;
return !!this.tableMetadatas.find(metadata => metadata.target === constructor);
}
private hasCompoundIndexMetadataWithObjectConstructor(constructor: Function): boolean {
return this.compoundIndexMetadatas.reduce((found, metadata) => metadata.target === constructor ? metadata : found, null) !== null;
return !!this.compoundIndexMetadatas.find(metadata => metadata.target === constructor);
}
private hasOrmEventSubscriberWithObjectConstructor(constructor: Function): boolean {
return this.ormEventSubscriberMetadatas.reduce((found, metadata) => metadata.target === constructor ? metadata : found, null) !== null;
return !!this.ormEventSubscriberMetadatas.find(metadata => metadata.target === constructor);
}
private hasFieldMetadataOnProperty(constructor: Function, propertyName: string): boolean {
return this.fieldMetadatas.reduce((found, metadata) => {
return metadata.target === constructor && metadata.propertyName === propertyName ? metadata : found;
}, null) !== null;
return !!this.columnMetadatas.find(metadata => metadata.target === constructor && metadata.propertyName === propertyName);
}
private hasRelationWithOneMetadataOnProperty(constructor: Function, propertyName: string): boolean {
return this.relationMetadatas.reduce((found, metadata) => {
return metadata.target === constructor && metadata.propertyName === propertyName ? metadata : found;
}, null) !== null;
return !!this.relationMetadatas.find(metadata => metadata.target === constructor && metadata.propertyName === propertyName);
}
private hasTableMetadataWithName(name: string): boolean {
return this.tableMetadatas.reduce((found, metadata) => metadata.name === name ? metadata : found, null) !== null;
return !!this.tableMetadatas.find(metadata => metadata.name === name);
}
private hasFieldMetadataWithName(constructor: Function, name: string): boolean {
return this.fieldMetadatas.reduce((found, metadata) => {
return metadata.target === constructor && metadata.name === name ? metadata : found;
}, null) !== null;
return !!this.columnMetadatas.find(metadata => metadata.target === constructor && metadata.name === name);
}
private hasRelationWithOneMetadataWithName(constructor: Function, name: string): boolean {
return this.relationMetadatas.reduce((found, metadata) => {
return metadata.target === constructor && metadata.name === name ? metadata : found;
}, null) !== null;
return !!this.relationMetadatas.find(metadata => metadata.target === constructor && metadata.name === name);
}
}
@ -192,4 +184,4 @@ export class MetadataStorage {
/**
* Default metadata storage used as singleton and can be used to storage all metadatas in the system.
*/
export let defaultMetadataStorage = new MetadataStorage();
export const defaultMetadataStorage = new MetadataStorage();

View File

@ -3,14 +3,26 @@
*/
export abstract class PropertyMetadata {
// ---------------------------------------------------------------------
// Properties
// ---------------------------------------------------------------------
private _target: Function;
private _propertyName: string;
// ---------------------------------------------------------------------
// Constructor
// ---------------------------------------------------------------------
constructor(target: Function, propertyName: string) {
this._target = target;
this._propertyName = propertyName;
}
// ---------------------------------------------------------------------
// Accessors
// ---------------------------------------------------------------------
/**
* The object class to which this metadata is attached.
*/

View File

@ -1,17 +1,58 @@
import {PropertyMetadata} from "./PropertyMetadata";
import {RelationTypes, RelationType, RelationTypeInFunction, PropertyTypeInFunction} from "../types/RelationTypes";
import {RelationTypes, RelationType} from "../types/RelationTypes";
import {RelationOptions} from "../options/RelationOptions";
import {NamingStrategy} from "../../naming-strategy/NamingStrategy";
import {TableMetadata} from "./TableMetadata";
import {EntityMetadata} from "./EntityMetadata";
/**
* Function that returns a type of the field. Returned value must be a class used on the relation.
*/
type RelationTypeInFunction = ((type?: any) => Function);
/**
* Contains the name of the property of the object, or the function that returns this name.
*/
type PropertyTypeInFunction<T> = string|((t: T) => string|any);
/**
* Relation metadata constructor arguments.
*/
export interface RelationMetadataArgs {
/**
* Class to which this relation is applied.
*/
target: Function;
/**
* Class's property name to which this relation is applied.
*/
propertyName: string;
/**
* Type of relation. Can be one of the value of the RelationTypes class.
*/
relationType: RelationType;
/**
* Type of the relation. This type is in function because of language specifics and problems with recursive
* referenced classes.
*/
type: RelationTypeInFunction;
/**
* Inverse side of the relation.
*/
inverseSideProperty: PropertyTypeInFunction<any>;
/**
* Indicates if this relation is owner side of the relation between entities.
*/
isOwning: boolean;
/**
* Additional relation options.
*/
options: RelationOptions;
}

View File

@ -27,12 +27,12 @@ export interface ColumnOptions {
autoIncrement?: boolean;
/**
* Specifies if column's value must be unqiue or not.
* Specifies if column's value must be unique or not.
*/
unique?: boolean;
/**
* Indicates if column must be nullable or not.
* Indicates if column's value can be set to NULL.
*/
nullable?: boolean;

View File

@ -1,3 +1,7 @@
/**
* Describes all relation's options.
*/
export interface RelationOptions {
/**
@ -21,7 +25,8 @@ export interface RelationOptions {
cascadeRemove?: boolean;
/**
* Old column name. Used to make safe schema updates.
* Column name used previously for this column. Used to make safe schema updates. Experimental and most probably
* will be removed in the future. Avoid using it.
*/
oldColumnName?: string;
@ -30,4 +35,9 @@ export interface RelationOptions {
*/
nullable?: boolean;
/**
* Database cascade action on delete.
*/
onDelete?: boolean;
}

View File

@ -1,3 +1,4 @@
/**
* All types that column can be.
*/

View File

@ -4,14 +4,11 @@
export type RelationType = "one-to-one"|"one-to-many"|"many-to-one"|"many-to-many";
/**
* Function that returns a type of the field. Returned value must be a class used on the relation.
* Used to get a type of the creating Function.
*/
export type RelationTypeInFunction = ((type?: any) => Function);
/**
* Contains the name of the property of the object, or the function that returns this name.
*/
export type PropertyTypeInFunction<T> = string|((t: T) => string|any);
export interface ObjectConstructor<T> {
new (): T;
}
/**
* Provides a constants for each relation type.
@ -21,4 +18,4 @@ export class RelationTypes {
static ONE_TO_MANY: RelationType = "one-to-many";
static MANY_TO_ONE: RelationType = "many-to-one";
static MANY_TO_MANY: RelationType = "many-to-many";
}
}