Add lazy-relation-tests, update to latest master

This commit is contained in:
Luchillo 2017-09-13 13:24:28 -05:00
parent a466de4e4f
commit f625467f42
17 changed files with 1315 additions and 595 deletions

View File

@ -150,8 +150,7 @@ export class Gulpfile {
@Task()
browserClearPackageDirectory(cb: Function) {
return del([
"./build/systemjs/**",
"./build/browser/**"
"./build/systemjs/**"
]);
}

View File

@ -1,28 +1,22 @@
import "reflect-metadata";
import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../utils/test-utils";
import {Connection} from "../../../src/connection/Connection";
import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../../utils/test-utils";
import {Connection} from "../../../../src/connection/Connection";
import {
Post,
PostNamedAll,
PostNamedTable,
PostNamedColumn,
} from "./entity/Post";
import {
Category,
CategoryNamedAll,
CategoryNamedTable,
CategoryNamedColumn,
} from "./entity/Category";
/**
* Because lazy relations are overriding prototype is impossible to run these tests on multiple connections.
* So we run tests only for mysql.
*/
describe("lazy-relations", () => {
describe("basic-lazy-relations", () => {
let userSchema: any, profileSchema: any;
try {
const resourceDir = __dirname + "/../../../../../test/functional/lazy-relations/";
const resourceDir = __dirname + "/../../../../../test/functional/lazy-relations/basic-lazy-relation/";
userSchema = require(resourceDir + "schema/user.json");
profileSchema = require(resourceDir + "schema/profile.json");
} catch (err) {
@ -35,14 +29,7 @@ describe("lazy-relations", () => {
before(async () => connections = await createTestingConnections({
entities: [
Post,
PostNamedAll,
PostNamedTable,
PostNamedColumn,
Category,
CategoryNamedAll,
CategoryNamedTable,
CategoryNamedColumn,
],
entitySchemas: [ userSchema, profileSchema ],
schemaCreate: true,

View File

@ -0,0 +1,28 @@
import { Entity } from "../../../../../src/decorator/entity/Entity";
import { PrimaryGeneratedColumn } from "../../../../../src/decorator/columns/PrimaryGeneratedColumn";
import { Column } from "../../../../../src/decorator/columns/Column";
import { ManyToMany } from "../../../../../src/decorator/relations/ManyToMany";
import { OneToMany } from "../../../../../src/decorator/relations/OneToMany";
import { OneToOne } from "../../../../../src/decorator/relations/OneToOne";
import {
Post,
} from "./Post";
@Entity()
export class Category {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@OneToOne(type => Post, post => post.oneCategory)
onePost: Promise<Post>;
@ManyToMany(type => Post, post => post.twoSideCategories)
twoSidePosts: Promise<Post[]>;
@OneToMany(type => Post, post => post.twoSideCategory)
twoSidePosts2: Promise<Post[]>;
}

View File

@ -0,0 +1,45 @@
import { Entity } from "../../../../../src/decorator/entity/Entity";
import { PrimaryGeneratedColumn } from "../../../../../src/decorator/columns/PrimaryGeneratedColumn";
import { Column } from "../../../../../src/decorator/columns/Column";
import { ManyToMany } from "../../../../../src/decorator/relations/ManyToMany";
import { JoinTable } from "../../../../../src/decorator/relations/JoinTable";
import { ManyToOne } from "../../../../../src/decorator/relations/ManyToOne";
import { OneToOne } from "../../../../../src/decorator/relations/OneToOne";
import { JoinColumn } from "../../../../../src/decorator/relations/JoinColumn";
import {
Category,
} from "./Category";
@Entity()
export class Post {
@PrimaryGeneratedColumn()
id: number;
@Column()
title: string;
@Column()
text: string;
@ManyToMany(type => Category)
@JoinTable()
categories: Promise<Category[]>;
@ManyToMany(type => Category, category => category.twoSidePosts)
@JoinTable()
twoSideCategories: Promise<Category[]>;
@Column()
viewCount: number = 0;
@ManyToOne(type => Category)
category: Promise<Category>;
@OneToOne(type => Category, category => category.onePost)
@JoinColumn()
oneCategory: Promise<Category>;
@ManyToOne(type => Category, category => category.twoSidePosts2)
twoSideCategory: Promise<Category>;
}

View File

@ -1,222 +0,0 @@
import { Entity } from "../../../../src/decorator/entity/Entity";
import { PrimaryGeneratedColumn } from "../../../../src/decorator/columns/PrimaryGeneratedColumn";
import { PrimaryColumn } from "../../../../src/decorator/columns/PrimaryColumn";
import { Column } from "../../../../src/decorator/columns/Column";
import { ManyToMany } from "../../../../src/decorator/relations/ManyToMany";
import { OneToMany } from "../../../../src/decorator/relations/OneToMany";
import { OneToOne } from "../../../../src/decorator/relations/OneToOne";
import {
Post,
PostNamedAll,
PostNamedTable,
PostNamedColumn,
} from "./Post";
@Entity()
export class Category {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@OneToOne(type => Post, post => post.oneCategory)
onePost: Promise<Post>;
@ManyToMany(type => Post, post => post.twoSideCategories)
twoSidePosts: Promise<Post[]>;
@OneToMany(type => Post, post => post.twoSideCategory)
twoSidePosts2: Promise<Post[]>;
// ManyToMany with named properties
@ManyToMany(type => PostNamedTable, post => post.categoriesNamedTable)
postsNamedTable: Promise<PostNamedTable[]>;
@ManyToMany(type => PostNamedColumn, post => post.categoriesNamedColumn)
postsNamedColumn: Promise<PostNamedColumn[]>;
@ManyToMany(type => PostNamedAll, post => post.categoriesNamedAll)
postsNamedAll: Promise<PostNamedAll[]>;
// OneToMany with named properties
@OneToMany(type => PostNamedTable, post => post.categoryNamedTable)
onePostsNamedTable: Promise<PostNamedTable[]>;
@OneToMany(type => PostNamedColumn, post => post.categoryNamedColumn)
onePostsNamedColumn: Promise<PostNamedColumn[]>;
@OneToMany(type => PostNamedAll, post => post.categoryNamedAll)
onePostsNamedAll: Promise<PostNamedAll[]>;
// OneToOne with named properties
@OneToOne(type => PostNamedTable, post => post.oneCategoryNamedTable)
onePostNamedTable: Promise<PostNamedTable>;
@OneToOne(type => PostNamedColumn, post => post.oneCategoryNamedColumn)
onePostNamedColumn: Promise<PostNamedColumn>;
@OneToOne(type => PostNamedAll, post => post.oneCategoryNamedAll)
onePostNamedAll: Promise<PostNamedAll>;
}
@Entity("s_category", {
orderBy: {
id: "ASC",
}
})
export class CategoryNamedTable {
@PrimaryColumn("int")
id: number;
@Column()
name: string;
@OneToOne(type => Post, post => post.oneCategory)
onePost: Promise<Post>;
@ManyToMany(type => Post, post => post.twoSideCategories)
twoSidePosts: Promise<Post[]>;
@OneToMany(type => Post, post => post.twoSideCategory)
twoSidePosts2: Promise<Post[]>;
// ManyToMany with named properties
@ManyToMany(type => PostNamedTable, post => post.categoriesNamedTable)
postsNamedTable: Promise<PostNamedTable[]>;
@ManyToMany(type => PostNamedColumn, post => post.categoriesNamedColumn)
postsNamedColumn: Promise<PostNamedColumn[]>;
@ManyToMany(type => PostNamedAll, post => post.categoriesNamedAll)
postsNamedAll: Promise<PostNamedAll[]>;
// OneToMany with named properties
@OneToMany(type => PostNamedTable, post => post.categoryNamedTable)
onePostsNamedTable: Promise<PostNamedTable[]>;
@OneToMany(type => PostNamedColumn, post => post.categoryNamedColumn)
onePostsNamedColumn: Promise<PostNamedColumn[]>;
@OneToMany(type => PostNamedAll, post => post.categoryNamedAll)
onePostsNamedAll: Promise<PostNamedAll[]>;
// OneToOne with named properties
@OneToOne(type => PostNamedTable, post => post.oneCategoryNamedTable)
onePostNamedTable: Promise<PostNamedTable>;
@OneToOne(type => PostNamedColumn, post => post.oneCategoryNamedColumn)
onePostNamedColumn: Promise<PostNamedColumn>;
@OneToOne(type => PostNamedAll, post => post.oneCategoryNamedAll)
onePostNamedAll: Promise<PostNamedAll>;
}
@Entity()
export class CategoryNamedColumn {
@PrimaryColumn("int", {
name: "s_category_id",
})
id: number;
@Column()
name: string;
@OneToOne(type => Post, post => post.oneCategory)
onePost: Promise<Post>;
@ManyToMany(type => Post, post => post.twoSideCategories)
twoSidePosts: Promise<Post[]>;
@OneToMany(type => Post, post => post.twoSideCategory)
twoSidePosts2: Promise<Post[]>;
// ManyToMany with named properties
@ManyToMany(type => PostNamedTable, post => post.categoriesNamedTable)
postsNamedTable: Promise<PostNamedTable[]>;
@ManyToMany(type => PostNamedColumn, post => post.categoriesNamedColumn)
postsNamedColumn: Promise<PostNamedColumn[]>;
@ManyToMany(type => PostNamedAll, post => post.categoriesNamedAll)
postsNamedAll: Promise<PostNamedAll[]>;
// OneToMany with named properties
@OneToMany(type => PostNamedTable, post => post.categoryNamedTable)
onePostsNamedTable: Promise<PostNamedTable[]>;
@OneToMany(type => PostNamedColumn, post => post.categoryNamedColumn)
onePostsNamedColumn: Promise<PostNamedColumn[]>;
@OneToMany(type => PostNamedAll, post => post.categoryNamedAll)
onePostsNamedAll: Promise<PostNamedAll[]>;
// OneToOne with named properties
@OneToOne(type => PostNamedTable, post => post.oneCategoryNamedTable)
onePostNamedTable: Promise<PostNamedTable>;
@OneToOne(type => PostNamedColumn, post => post.oneCategoryNamedColumn)
onePostNamedColumn: Promise<PostNamedColumn>;
@OneToOne(type => PostNamedAll, post => post.oneCategoryNamedAll)
onePostNamedAll: Promise<PostNamedAll>;
}
@Entity("s_category_named_all", {
orderBy: {
id: "ASC",
}
})
export class CategoryNamedAll {
@PrimaryColumn("int", {
name: "s_category_id",
})
id: number;
@Column()
name: string;
@OneToOne(type => Post, post => post.oneCategory)
onePost: Promise<Post>;
@ManyToMany(type => Post, post => post.twoSideCategories)
twoSidePosts: Promise<Post[]>;
@OneToMany(type => Post, post => post.twoSideCategory)
twoSidePosts2: Promise<Post[]>;
// ManyToMany with named properties
@ManyToMany(type => PostNamedTable, post => post.categoriesNamedTable)
postsNamedTable: Promise<PostNamedTable[]>;
@ManyToMany(type => PostNamedColumn, post => post.categoriesNamedColumn)
postsNamedColumn: Promise<PostNamedColumn[]>;
@ManyToMany(type => PostNamedAll, post => post.categoriesNamedAll)
postsNamedAll: Promise<PostNamedAll[]>;
// OneToMany with named properties
@OneToMany(type => PostNamedTable, post => post.categoryNamedTable)
onePostsNamedTable: Promise<PostNamedTable[]>;
@OneToMany(type => PostNamedColumn, post => post.categoryNamedColumn)
onePostsNamedColumn: Promise<PostNamedColumn[]>;
@OneToMany(type => PostNamedAll, post => post.categoryNamedAll)
onePostsNamedAll: Promise<PostNamedAll[]>;
// OneToOne with named properties
@OneToOne(type => PostNamedTable, post => post.oneCategoryNamedTable)
onePostNamedTable: Promise<PostNamedTable>;
@OneToOne(type => PostNamedColumn, post => post.oneCategoryNamedColumn)
onePostNamedColumn: Promise<PostNamedColumn>;
@OneToOne(type => PostNamedAll, post => post.oneCategoryNamedAll)
onePostNamedAll: Promise<PostNamedAll>;
}

View File

@ -1,354 +0,0 @@
import { Entity } from "../../../../src/decorator/entity/Entity";
import { PrimaryGeneratedColumn } from "../../../../src/decorator/columns/PrimaryGeneratedColumn";
import { PrimaryColumn } from "../../../../src/decorator/columns/PrimaryColumn";
import { Column } from "../../../../src/decorator/columns/Column";
import { ManyToMany } from "../../../../src/decorator/relations/ManyToMany";
import { JoinTable } from "../../../../src/decorator/relations/JoinTable";
import { ManyToOne } from "../../../../src/decorator/relations/ManyToOne";
import { OneToOne } from "../../../../src/decorator/relations/OneToOne";
import { JoinColumn } from "../../../../src/decorator/relations/JoinColumn";
import {
Category,
CategoryNamedAll,
CategoryNamedTable,
CategoryNamedColumn,
} from "./Category";
@Entity()
export class Post {
@PrimaryGeneratedColumn()
id: number;
@Column()
title: string;
@Column()
text: string;
@ManyToMany(type => Category)
@JoinTable()
categories: Promise<Category[]>;
@ManyToMany(type => Category, category => category.twoSidePosts)
@JoinTable()
twoSideCategories: Promise<Category[]>;
@Column()
viewCount: number = 0;
@ManyToOne(type => Category)
category: Promise<Category>;
@OneToOne(type => Category, category => category.onePost)
@JoinColumn()
oneCategory: Promise<Category>;
@ManyToOne(type => Category, category => category.twoSidePosts2)
twoSideCategory: Promise<Category>;
// ManyToMany with named properties
@ManyToMany(type => CategoryNamedTable, category => category.postsNamedTable)
@JoinTable()
categoriesNamedTable: Promise<CategoryNamedTable[]>;
@ManyToMany(type => CategoryNamedColumn, category => category.postsNamedColumn)
@JoinTable()
categoriesNamedColumn: Promise<CategoryNamedColumn[]>;
@ManyToMany(type => CategoryNamedAll, category => category.postsNamedAll)
@JoinTable()
categoriesNamedAll: Promise<CategoryNamedAll[]>;
// ManyToOne with named properties
@ManyToOne(type => CategoryNamedTable, category => category.onePostsNamedTable)
@JoinColumn()
categoryNamedTable: Promise<CategoryNamedTable>;
@ManyToOne(type => CategoryNamedColumn, category => category.onePostsNamedColumn)
@JoinColumn({
name: "s_category_named_column_id"
})
categoryNamedColumn: Promise<CategoryNamedColumn>;
@ManyToOne(type => CategoryNamedAll, category => category.onePostsNamedAll)
@JoinColumn({
name: "s_category_named_all_id"
})
categoryNamedAll: Promise<CategoryNamedAll>;
// OneToOne with named properties
@OneToOne(type => CategoryNamedTable, category => category.onePostNamedTable)
@JoinColumn()
oneCategoryNamedTable: Promise<CategoryNamedTable>;
@OneToOne(type => CategoryNamedColumn, category => category.onePostNamedColumn)
@JoinColumn({
name: "s_one_category_named_column_id"
})
oneCategoryNamedColumn: Promise<CategoryNamedColumn>;
@OneToOne(type => CategoryNamedAll, category => category.onePostNamedAll)
@JoinColumn({
name: "s_one_category_named_all_id"
})
oneCategoryNamedAll: Promise<CategoryNamedAll>;
}
@Entity("s_post", {
orderBy: {
title: "ASC",
id: "DESC",
}
})
export class PostNamedTable {
@PrimaryColumn("int")
id: number;
@Column()
title: string;
@Column()
text: string;
@ManyToMany(type => Category)
@JoinTable()
categories: Promise<Category[]>;
@ManyToMany(type => Category, category => category.twoSidePosts)
@JoinTable()
twoSideCategories: Promise<Category[]>;
@Column()
viewCount: number = 0;
@ManyToOne(type => Category)
category: Promise<Category>;
@OneToOne(type => Category, category => category.onePost)
@JoinColumn()
oneCategory: Promise<Category>;
@ManyToOne(type => Category, category => category.twoSidePosts2)
twoSideCategory: Promise<Category>;
// ManyToMany with named properties
@ManyToMany(type => CategoryNamedTable, category => category.postsNamedTable)
@JoinTable()
categoriesNamedTable: Promise<CategoryNamedTable[]>;
@ManyToMany(type => CategoryNamedColumn, category => category.postsNamedColumn)
@JoinTable()
categoriesNamedColumn: Promise<CategoryNamedColumn[]>;
@ManyToMany(type => CategoryNamedAll, category => category.postsNamedAll)
@JoinTable()
categoriesNamedAll: Promise<CategoryNamedAll[]>;
// ManyToOne with named properties
@ManyToOne(type => CategoryNamedTable, category => category.onePostsNamedTable)
@JoinColumn()
categoryNamedTable: Promise<CategoryNamedTable>;
@ManyToOne(type => CategoryNamedColumn, category => category.onePostsNamedColumn)
@JoinColumn({
name: "s_category_named_column_id"
})
categoryNamedColumn: Promise<CategoryNamedColumn>;
@ManyToOne(type => CategoryNamedAll, category => category.onePostsNamedAll)
@JoinColumn({
name: "s_category_named_all_id"
})
categoryNamedAll: Promise<CategoryNamedAll>;
// OneToOne with named properties
@OneToOne(type => CategoryNamedTable, category => category.onePostNamedTable)
@JoinColumn()
oneCategoryNamedTable: Promise<CategoryNamedTable>;
@OneToOne(type => CategoryNamedColumn, category => category.onePostNamedColumn)
@JoinColumn({
name: "s_one_category_named_column_id"
})
oneCategoryNamedColumn: Promise<CategoryNamedColumn>;
@OneToOne(type => CategoryNamedAll, category => category.onePostNamedAll)
@JoinColumn({
name: "s_one_category_named_all_id"
})
oneCategoryNamedAll: Promise<CategoryNamedAll>;
}
@Entity()
export class PostNamedColumn {
@PrimaryColumn("int", {
name: "s_post_id"
})
id: number;
@Column()
title: string;
@Column()
text: string;
@ManyToMany(type => Category)
@JoinTable()
categories: Promise<Category[]>;
@ManyToMany(type => Category, category => category.twoSidePosts)
@JoinTable()
twoSideCategories: Promise<Category[]>;
@Column()
viewCount: number = 0;
@ManyToOne(type => Category)
category: Promise<Category>;
@OneToOne(type => Category, category => category.onePost)
@JoinColumn()
oneCategory: Promise<Category>;
@ManyToOne(type => Category, category => category.twoSidePosts2)
twoSideCategory: Promise<Category>;
// ManyToMany with named properties
@ManyToMany(type => CategoryNamedTable, category => category.postsNamedTable)
@JoinTable()
categoriesNamedTable: Promise<CategoryNamedTable[]>;
@ManyToMany(type => CategoryNamedColumn, category => category.postsNamedColumn)
@JoinTable()
categoriesNamedColumn: Promise<CategoryNamedColumn[]>;
@ManyToMany(type => CategoryNamedAll, category => category.postsNamedAll)
@JoinTable()
categoriesNamedAll: Promise<CategoryNamedAll[]>;
// ManyToOne with named properties
@ManyToOne(type => CategoryNamedTable, category => category.onePostsNamedTable)
@JoinColumn()
categoryNamedTable: Promise<CategoryNamedTable>;
@ManyToOne(type => CategoryNamedColumn, category => category.onePostsNamedColumn)
@JoinColumn({
name: "s_category_named_column_id"
})
categoryNamedColumn: Promise<CategoryNamedColumn>;
@ManyToOne(type => CategoryNamedAll, category => category.onePostsNamedAll)
@JoinColumn({
name: "s_category_named_all_id"
})
categoryNamedAll: Promise<CategoryNamedAll>;
// OneToOne with named properties
@OneToOne(type => CategoryNamedTable, category => category.onePostNamedTable)
@JoinColumn()
oneCategoryNamedTable: Promise<CategoryNamedTable>;
@OneToOne(type => CategoryNamedColumn, category => category.onePostNamedColumn)
@JoinColumn({
name: "s_one_category_named_column_id"
})
oneCategoryNamedColumn: Promise<CategoryNamedColumn>;
@OneToOne(type => CategoryNamedAll, category => category.onePostNamedAll)
@JoinColumn({
name: "s_one_category_named_all_id"
})
oneCategoryNamedAll: Promise<CategoryNamedAll>;
}
@Entity("s_post_named_all", {
orderBy: {
title: "ASC",
id: "DESC",
}
})
export class PostNamedAll {
@PrimaryColumn("int", {
name: "s_post_id"
})
id: number;
@Column()
title: string;
@Column()
text: string;
@ManyToMany(type => Category)
@JoinTable()
categories: Promise<Category[]>;
@ManyToMany(type => Category, category => category.twoSidePosts)
@JoinTable()
twoSideCategories: Promise<Category[]>;
@Column()
viewCount: number = 0;
@ManyToOne(type => Category)
category: Promise<Category>;
@OneToOne(type => Category, category => category.onePost)
@JoinColumn()
oneCategory: Promise<Category>;
@ManyToOne(type => Category, category => category.twoSidePosts2)
twoSideCategory: Promise<Category>;
// ManyToMany with named properties
@ManyToMany(type => CategoryNamedTable, category => category.postsNamedTable)
@JoinTable()
categoriesNamedTable: Promise<CategoryNamedTable[]>;
@ManyToMany(type => CategoryNamedColumn, category => category.postsNamedColumn)
@JoinTable()
categoriesNamedColumn: Promise<CategoryNamedColumn[]>;
@ManyToMany(type => CategoryNamedAll, category => category.postsNamedAll)
@JoinTable()
categoriesNamedAll: Promise<CategoryNamedAll[]>;
// ManyToOne with named properties
@ManyToOne(type => CategoryNamedTable, category => category.onePostsNamedTable)
@JoinColumn()
categoryNamedTable: Promise<CategoryNamedTable>;
@ManyToOne(type => CategoryNamedColumn, category => category.onePostsNamedColumn)
@JoinColumn({
name: "s_category_named_column_id"
})
categoryNamedColumn: Promise<CategoryNamedColumn>;
@ManyToOne(type => CategoryNamedAll, category => category.onePostsNamedAll)
@JoinColumn({
name: "s_category_named_all_id"
})
categoryNamedAll: Promise<CategoryNamedAll>;
// OneToOne with named properties
@OneToOne(type => CategoryNamedTable, category => category.onePostNamedTable)
@JoinColumn()
oneCategoryNamedTable: Promise<CategoryNamedTable>;
@OneToOne(type => CategoryNamedColumn, category => category.onePostNamedColumn)
@JoinColumn({
name: "s_one_category_named_column_id"
})
oneCategoryNamedColumn: Promise<CategoryNamedColumn>;
@OneToOne(type => CategoryNamedAll, category => category.onePostNamedAll)
@JoinColumn({
name: "s_one_category_named_all_id"
})
oneCategoryNamedAll: Promise<CategoryNamedAll>;
}

View File

@ -0,0 +1,42 @@
import { Entity } from "../../../../../src/decorator/entity/Entity";
import { PrimaryGeneratedColumn } from "../../../../../src/decorator/columns/PrimaryGeneratedColumn";
import { Column } from "../../../../../src/decorator/columns/Column";
import { ManyToMany } from "../../../../../src/decorator/relations/ManyToMany";
import { OneToMany } from "../../../../../src/decorator/relations/OneToMany";
import { OneToOne } from "../../../../../src/decorator/relations/OneToOne";
import {
Post,
} from "./Post";
@Entity()
export class Category {
@PrimaryGeneratedColumn({
name: "s_category_id",
})
id: number;
@Column()
name: string;
@OneToOne(type => Post, post => post.oneCategory)
onePost: Promise<Post>;
@ManyToMany(type => Post, post => post.twoSideCategories)
twoSidePosts: Promise<Post[]>;
@OneToMany(type => Post, post => post.twoSideCategory)
twoSidePosts2: Promise<Post[]>;
// ManyToMany with named properties
@ManyToMany(type => Post, post => post.categoriesNamedColumn)
postsNamedColumn: Promise<Post[]>;
// OneToMany with named properties
@OneToMany(type => Post, post => post.categoryNamedColumn)
onePostsNamedColumn: Promise<Post[]>;
// OneToOne with named properties
@OneToOne(type => Post, post => post.oneCategoryNamedColumn)
onePostNamedColumn: Promise<Post>;
}

View File

@ -0,0 +1,66 @@
import { Entity } from "../../../../../src/decorator/entity/Entity";
import { PrimaryGeneratedColumn } from "../../../../../src/decorator/columns/PrimaryGeneratedColumn";
import { Column } from "../../../../../src/decorator/columns/Column";
import { ManyToMany } from "../../../../../src/decorator/relations/ManyToMany";
import { JoinTable } from "../../../../../src/decorator/relations/JoinTable";
import { ManyToOne } from "../../../../../src/decorator/relations/ManyToOne";
import { OneToOne } from "../../../../../src/decorator/relations/OneToOne";
import { JoinColumn } from "../../../../../src/decorator/relations/JoinColumn";
import {
Category,
} from "./Category";
@Entity()
export class Post {
@PrimaryGeneratedColumn({
name: "s_post_id"
})
id: number;
@Column()
title: string;
@Column()
text: string;
@ManyToMany(type => Category)
@JoinTable()
categories: Promise<Category[]>;
@ManyToMany(type => Category, category => category.twoSidePosts)
@JoinTable()
twoSideCategories: Promise<Category[]>;
@Column()
viewCount: number = 0;
@ManyToOne(type => Category)
category: Promise<Category>;
@OneToOne(type => Category, category => category.onePost)
@JoinColumn()
oneCategory: Promise<Category>;
@ManyToOne(type => Category, category => category.twoSidePosts2)
twoSideCategory: Promise<Category>;
// ManyToMany with named properties
@ManyToMany(type => Category, category => category.postsNamedColumn)
@JoinTable()
categoriesNamedColumn: Promise<Category[]>;
// ManyToOne with named properties
@ManyToOne(type => Category, category => category.onePostsNamedColumn)
@JoinColumn({
name: "s_category_named_column_id"
})
categoryNamedColumn: Promise<Category>;
// OneToOne with named properties
@OneToOne(type => Category, category => category.onePostNamedColumn)
@JoinColumn({
name: "s_one_category_named_column_id"
})
oneCategoryNamedColumn: Promise<Category>;
}

View File

@ -0,0 +1,301 @@
import "reflect-metadata";
import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../../utils/test-utils";
import {Connection} from "../../../../src/connection/Connection";
import {
Post,
} from "./entity/Post";
import {
Category,
} from "./entity/Category";
/**
* Because lazy relations are overriding prototype is impossible to run these tests on multiple connections.
* So we run tests only for mysql.
*/
describe("named-columns-lazy-relations", () => {
let connections: Connection[];
before(async () => connections = await createTestingConnections({
entities: [
Post,
Category,
],
schemaCreate: true,
dropSchema: true,
enabledDrivers: ["mysql"] // we can properly test lazy-relations only on one platform
}));
beforeEach(() => reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));
it("should persist and hydrate successfully on a relation without inverse side", () => Promise.all(connections.map(async connection => {
const postRepository = connection.getRepository(Post);
const categoryRepository = connection.getRepository(Category);
const savedCategory1 = new Category();
savedCategory1.name = "kids";
const savedCategory2 = new Category();
savedCategory2.name = "people";
const savedCategory3 = new Category();
savedCategory3.name = "animals";
await categoryRepository.save(savedCategory1);
await categoryRepository.save(savedCategory2);
await categoryRepository.save(savedCategory3);
const savedPost = new Post();
savedPost.title = "Hello post";
savedPost.text = "This is post about post";
savedPost.categories = Promise.resolve([
savedCategory1, savedCategory2, savedCategory3
]);
await postRepository.save(savedPost);
savedPost.categories.should.eventually.be.eql([savedCategory1, savedCategory2, savedCategory3]);
const post = (await postRepository.findOneById(1))!;
post.title.should.be.equal("Hello post");
post.text.should.be.equal("This is post about post");
post.categories.should.be.instanceOf(Promise);
const categories = await post.categories;
categories.length.should.be.equal(3);
categories.should.contain(savedCategory1);
categories.should.contain(savedCategory2);
categories.should.contain(savedCategory3);
})));
it("should persist and hydrate successfully on a relation with inverse side", () => Promise.all(connections.map(async connection => {
const postRepository = connection.getRepository(Post);
const categoryRepository = connection.getRepository(Category);
const savedCategory1 = new Category();
savedCategory1.name = "kids";
const savedCategory2 = new Category();
savedCategory2.name = "people";
const savedCategory3 = new Category();
savedCategory3.name = "animals";
await categoryRepository.save(savedCategory1);
await categoryRepository.save(savedCategory2);
await categoryRepository.save(savedCategory3);
const savedPost = new Post();
savedPost.title = "Hello post";
savedPost.text = "This is post about post";
savedPost.twoSideCategories = Promise.resolve([
savedCategory1, savedCategory2, savedCategory3
]);
await postRepository.save(savedPost);
savedPost.twoSideCategories.should.eventually.be.eql([savedCategory1, savedCategory2, savedCategory3]);
const post = (await postRepository.findOneById(1))!;
post.title.should.be.equal("Hello post");
post.text.should.be.equal("This is post about post");
post.twoSideCategories.should.be.instanceOf(Promise);
const categories = await post.twoSideCategories;
categories.length.should.be.equal(3);
categories.should.contain(savedCategory1);
categories.should.contain(savedCategory2);
categories.should.contain(savedCategory3);
const category = (await categoryRepository.findOneById(1))!;
category.name.should.be.equal("kids");
const twoSidePosts = await category.twoSidePosts;
const likePost = new Post();
likePost.id = 1;
likePost.title = "Hello post";
likePost.text = "This is post about post";
twoSidePosts.should.contain(likePost);
})));
it("should persist and hydrate successfully on a many-to-one relation without inverse side", () => Promise.all(connections.map(async connection => {
// create some fake posts and categories to make sure that there are several post ids in the db
const fakePosts: Post[] = [];
for (let i = 0; i < 30; i++) {
const fakePost = new Post();
fakePost.title = "post #" + i;
fakePost.text = "post #" + i;
fakePosts.push(fakePost);
}
await connection.manager.save(fakePosts);
const fakeCategories: Category[] = [];
for (let i = 0; i < 8; i++) {
const fakeCategory = new Category();
fakeCategory.name = "category #" + i;
fakeCategories.push(fakeCategory);
}
await connection.manager.save(fakeCategories);
const category = new Category();
category.name = "category of great post";
const post = new Post();
post.title = "post with great category";
post.text = "post with great category and great text";
post.category = Promise.resolve(category);
await connection.manager.save(category);
await connection.manager.save(post);
const loadedPost = await connection.manager.findOne(Post, { where: { title: "post with great category" } });
const loadedCategory = await loadedPost!.category;
loadedCategory.name.should.be.equal("category of great post");
})));
it("should persist and hydrate successfully on a many-to-one relation with inverse side", () => Promise.all(connections.map(async connection => {
// create some fake posts and categories to make sure that there are several post ids in the db
const fakePosts: Post[] = [];
for (let i = 0; i < 8; i++) {
const fakePost = new Post();
fakePost.title = "post #" + i;
fakePost.text = "post #" + i;
fakePosts.push(fakePost);
}
await connection.manager.save(fakePosts);
const fakeCategories: Category[] = [];
for (let i = 0; i < 30; i++) {
const fakeCategory = new Category();
fakeCategory.name = "category #" + i;
fakeCategories.push(fakeCategory);
}
await connection.manager.save(fakeCategories);
const category = new Category();
category.name = "category of great post";
const post = new Post();
post.title = "post with great category";
post.text = "post with great category and great text";
post.twoSideCategory = Promise.resolve(category);
await connection.manager.save(category);
await connection.manager.save(post);
const loadedPost = await connection.manager.findOne(Post, { where: { title: "post with great category" } });
const loadedCategory = await loadedPost!.twoSideCategory;
loadedCategory.name.should.be.equal("category of great post");
})));
it("should persist and hydrate successfully on a one-to-many relation", () => Promise.all(connections.map(async connection => {
// create some fake posts and categories to make sure that there are several post ids in the db
const fakePosts: Post[] = [];
for (let i = 0; i < 8; i++) {
const fakePost = new Post();
fakePost.title = "post #" + i;
fakePost.text = "post #" + i;
fakePosts.push(fakePost);
}
await connection.manager.save(fakePosts);
const fakeCategories: Category[] = [];
for (let i = 0; i < 30; i++) {
const fakeCategory = new Category();
fakeCategory.name = "category #" + i;
fakeCategories.push(fakeCategory);
}
await connection.manager.save(fakeCategories);
const category = new Category();
category.name = "category of great post";
await connection.manager.save(category);
const post = new Post();
post.title = "post with great category";
post.text = "post with great category and great text";
post.twoSideCategory = Promise.resolve(category);
await connection.manager.save(post);
const loadedCategory = await connection.manager.findOne(Category, { where: { name: "category of great post" } });
const loadedPost = await loadedCategory!.twoSidePosts2;
loadedPost[0].title.should.be.equal("post with great category");
})));
it("should persist and hydrate successfully on a one-to-one relation owner side", () => Promise.all(connections.map(async connection => {
// create some fake posts and categories to make sure that there are several post ids in the db
const fakePosts: Post[] = [];
for (let i = 0; i < 8; i++) {
const fakePost = new Post();
fakePost.title = "post #" + i;
fakePost.text = "post #" + i;
fakePosts.push(fakePost);
}
await connection.manager.save(fakePosts);
const fakeCategories: Category[] = [];
for (let i = 0; i < 30; i++) {
const fakeCategory = new Category();
fakeCategory.name = "category #" + i;
fakeCategories.push(fakeCategory);
}
await connection.manager.save(fakeCategories);
const category = new Category();
category.name = "category of great post";
await connection.manager.save(category);
const post = new Post();
post.title = "post with great category";
post.text = "post with great category and great text";
post.oneCategory = Promise.resolve(category);
await connection.manager.save(post);
const loadedPost = await connection.manager.findOne(Post, { where: { title: "post with great category" } });
const loadedCategory = await loadedPost!.oneCategory;
loadedCategory.name.should.be.equal("category of great post");
})));
it("should persist and hydrate successfully on a one-to-one relation inverse side", () => Promise.all(connections.map(async connection => {
// create some fake posts and categories to make sure that there are several post ids in the db
const fakePosts: Post[] = [];
for (let i = 0; i < 8; i++) {
const fakePost = new Post();
fakePost.title = "post #" + i;
fakePost.text = "post #" + i;
fakePosts.push(fakePost);
}
await connection.manager.save(fakePosts);
const fakeCategories: Category[] = [];
for (let i = 0; i < 30; i++) {
const fakeCategory = new Category();
fakeCategory.name = "category #" + i;
fakeCategories.push(fakeCategory);
}
await connection.manager.save(fakeCategories);
const category = new Category();
category.name = "category of great post";
await connection.manager.save(category);
const post = new Post();
post.title = "post with great category";
post.text = "post with great category and great text";
post.oneCategory = Promise.resolve(category);
await connection.manager.save(post);
const loadedCategory = await connection.manager.findOne(Category, { where: { name: "category of great post" } });
const loadedPost = await loadedCategory!.onePost;
loadedPost.title.should.be.equal("post with great category");
})));
});

View File

@ -0,0 +1,46 @@
import { Entity } from "../../../../../src/decorator/entity/Entity";
import { PrimaryGeneratedColumn } from "../../../../../src/decorator/columns/PrimaryGeneratedColumn";
import { Column } from "../../../../../src/decorator/columns/Column";
import { ManyToMany } from "../../../../../src/decorator/relations/ManyToMany";
import { OneToMany } from "../../../../../src/decorator/relations/OneToMany";
import { OneToOne } from "../../../../../src/decorator/relations/OneToOne";
import {
Post,
} from "./Post";
@Entity("s_category_named_all", {
orderBy: {
id: "ASC",
}
})
export class Category {
@PrimaryGeneratedColumn({
name: "s_category_id",
})
id: number;
@Column()
name: string;
@OneToOne(type => Post, post => post.oneCategory)
onePost: Promise<Post>;
@ManyToMany(type => Post, post => post.twoSideCategories)
twoSidePosts: Promise<Post[]>;
@OneToMany(type => Post, post => post.twoSideCategory)
twoSidePosts2: Promise<Post[]>;
// ManyToMany with named properties
@ManyToMany(type => Post, post => post.categoriesNamedAll)
postsNamedAll: Promise<Post[]>;
// OneToMany with named properties
@OneToMany(type => Post, post => post.categoryNamedAll)
onePostsNamedAll: Promise<Post[]>;
// OneToOne with named properties
@OneToOne(type => Post, post => post.oneCategoryNamedAll)
onePostNamedAll: Promise<Post>;
}

View File

@ -0,0 +1,71 @@
import { Entity } from "../../../../../src/decorator/entity/Entity";
import { PrimaryGeneratedColumn } from "../../../../../src/decorator/columns/PrimaryGeneratedColumn";
import { Column } from "../../../../../src/decorator/columns/Column";
import { ManyToMany } from "../../../../../src/decorator/relations/ManyToMany";
import { JoinTable } from "../../../../../src/decorator/relations/JoinTable";
import { ManyToOne } from "../../../../../src/decorator/relations/ManyToOne";
import { OneToOne } from "../../../../../src/decorator/relations/OneToOne";
import { JoinColumn } from "../../../../../src/decorator/relations/JoinColumn";
import {
Category,
} from "./Category";
@Entity("s_post_named_all", {
orderBy: {
title: "ASC",
id: "DESC",
}
})
export class Post {
@PrimaryGeneratedColumn({
name: "s_post_id"
})
id: number;
@Column()
title: string;
@Column()
text: string;
@ManyToMany(type => Category)
@JoinTable()
categories: Promise<Category[]>;
@ManyToMany(type => Category, category => category.twoSidePosts)
@JoinTable()
twoSideCategories: Promise<Category[]>;
@Column()
viewCount: number = 0;
@ManyToOne(type => Category)
category: Promise<Category>;
@OneToOne(type => Category, category => category.onePost)
@JoinColumn()
oneCategory: Promise<Category>;
@ManyToOne(type => Category, category => category.twoSidePosts2)
twoSideCategory: Promise<Category>;
// ManyToMany with named properties
@ManyToMany(type => Category, category => category.postsNamedAll)
@JoinTable()
categoriesNamedAll: Promise<Category[]>;
// ManyToOne with named properties
@ManyToOne(type => Category, category => category.onePostsNamedAll)
@JoinColumn({
name: "s_category_named_all_id"
})
categoryNamedAll: Promise<Category>;
// OneToOne with named properties
@OneToOne(type => Category, category => category.onePostNamedAll)
@JoinColumn({
name: "s_one_category_named_all_id"
})
oneCategoryNamedAll: Promise<Category>;
}

View File

@ -0,0 +1,301 @@
import "reflect-metadata";
import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../../utils/test-utils";
import {Connection} from "../../../../src/connection/Connection";
import {
Post,
} from "./entity/Post";
import {
Category,
} from "./entity/Category";
/**
* Because lazy relations are overriding prototype is impossible to run these tests on multiple connections.
* So we run tests only for mysql.
*/
describe("named-tables-and-columns-lazy-relations", () => {
let connections: Connection[];
before(async () => connections = await createTestingConnections({
entities: [
Post,
Category,
],
schemaCreate: true,
dropSchema: true,
enabledDrivers: ["mysql"] // we can properly test lazy-relations only on one platform
}));
beforeEach(() => reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));
it("should persist and hydrate successfully on a relation without inverse side", () => Promise.all(connections.map(async connection => {
const postRepository = connection.getRepository(Post);
const categoryRepository = connection.getRepository(Category);
const savedCategory1 = new Category();
savedCategory1.name = "kids";
const savedCategory2 = new Category();
savedCategory2.name = "people";
const savedCategory3 = new Category();
savedCategory3.name = "animals";
await categoryRepository.save(savedCategory1);
await categoryRepository.save(savedCategory2);
await categoryRepository.save(savedCategory3);
const savedPost = new Post();
savedPost.title = "Hello post";
savedPost.text = "This is post about post";
savedPost.categories = Promise.resolve([
savedCategory1, savedCategory2, savedCategory3
]);
await postRepository.save(savedPost);
savedPost.categories.should.eventually.be.eql([savedCategory1, savedCategory2, savedCategory3]);
const post = (await postRepository.findOneById(1))!;
post.title.should.be.equal("Hello post");
post.text.should.be.equal("This is post about post");
post.categories.should.be.instanceOf(Promise);
const categories = await post.categories;
categories.length.should.be.equal(3);
categories.should.contain(savedCategory1);
categories.should.contain(savedCategory2);
categories.should.contain(savedCategory3);
})));
it("should persist and hydrate successfully on a relation with inverse side", () => Promise.all(connections.map(async connection => {
const postRepository = connection.getRepository(Post);
const categoryRepository = connection.getRepository(Category);
const savedCategory1 = new Category();
savedCategory1.name = "kids";
const savedCategory2 = new Category();
savedCategory2.name = "people";
const savedCategory3 = new Category();
savedCategory3.name = "animals";
await categoryRepository.save(savedCategory1);
await categoryRepository.save(savedCategory2);
await categoryRepository.save(savedCategory3);
const savedPost = new Post();
savedPost.title = "Hello post";
savedPost.text = "This is post about post";
savedPost.twoSideCategories = Promise.resolve([
savedCategory1, savedCategory2, savedCategory3
]);
await postRepository.save(savedPost);
savedPost.twoSideCategories.should.eventually.be.eql([savedCategory1, savedCategory2, savedCategory3]);
const post = (await postRepository.findOneById(1))!;
post.title.should.be.equal("Hello post");
post.text.should.be.equal("This is post about post");
post.twoSideCategories.should.be.instanceOf(Promise);
const categories = await post.twoSideCategories;
categories.length.should.be.equal(3);
categories.should.contain(savedCategory1);
categories.should.contain(savedCategory2);
categories.should.contain(savedCategory3);
const category = (await categoryRepository.findOneById(1))!;
category.name.should.be.equal("kids");
const twoSidePosts = await category.twoSidePosts;
const likePost = new Post();
likePost.id = 1;
likePost.title = "Hello post";
likePost.text = "This is post about post";
twoSidePosts.should.contain(likePost);
})));
it("should persist and hydrate successfully on a many-to-one relation without inverse side", () => Promise.all(connections.map(async connection => {
// create some fake posts and categories to make sure that there are several post ids in the db
const fakePosts: Post[] = [];
for (let i = 0; i < 30; i++) {
const fakePost = new Post();
fakePost.title = "post #" + i;
fakePost.text = "post #" + i;
fakePosts.push(fakePost);
}
await connection.manager.save(fakePosts);
const fakeCategories: Category[] = [];
for (let i = 0; i < 8; i++) {
const fakeCategory = new Category();
fakeCategory.name = "category #" + i;
fakeCategories.push(fakeCategory);
}
await connection.manager.save(fakeCategories);
const category = new Category();
category.name = "category of great post";
const post = new Post();
post.title = "post with great category";
post.text = "post with great category and great text";
post.category = Promise.resolve(category);
await connection.manager.save(category);
await connection.manager.save(post);
const loadedPost = await connection.manager.findOne(Post, { where: { title: "post with great category" } });
const loadedCategory = await loadedPost!.category;
loadedCategory.name.should.be.equal("category of great post");
})));
it("should persist and hydrate successfully on a many-to-one relation with inverse side", () => Promise.all(connections.map(async connection => {
// create some fake posts and categories to make sure that there are several post ids in the db
const fakePosts: Post[] = [];
for (let i = 0; i < 8; i++) {
const fakePost = new Post();
fakePost.title = "post #" + i;
fakePost.text = "post #" + i;
fakePosts.push(fakePost);
}
await connection.manager.save(fakePosts);
const fakeCategories: Category[] = [];
for (let i = 0; i < 30; i++) {
const fakeCategory = new Category();
fakeCategory.name = "category #" + i;
fakeCategories.push(fakeCategory);
}
await connection.manager.save(fakeCategories);
const category = new Category();
category.name = "category of great post";
const post = new Post();
post.title = "post with great category";
post.text = "post with great category and great text";
post.twoSideCategory = Promise.resolve(category);
await connection.manager.save(category);
await connection.manager.save(post);
const loadedPost = await connection.manager.findOne(Post, { where: { title: "post with great category" } });
const loadedCategory = await loadedPost!.twoSideCategory;
loadedCategory.name.should.be.equal("category of great post");
})));
it("should persist and hydrate successfully on a one-to-many relation", () => Promise.all(connections.map(async connection => {
// create some fake posts and categories to make sure that there are several post ids in the db
const fakePosts: Post[] = [];
for (let i = 0; i < 8; i++) {
const fakePost = new Post();
fakePost.title = "post #" + i;
fakePost.text = "post #" + i;
fakePosts.push(fakePost);
}
await connection.manager.save(fakePosts);
const fakeCategories: Category[] = [];
for (let i = 0; i < 30; i++) {
const fakeCategory = new Category();
fakeCategory.name = "category #" + i;
fakeCategories.push(fakeCategory);
}
await connection.manager.save(fakeCategories);
const category = new Category();
category.name = "category of great post";
await connection.manager.save(category);
const post = new Post();
post.title = "post with great category";
post.text = "post with great category and great text";
post.twoSideCategory = Promise.resolve(category);
await connection.manager.save(post);
const loadedCategory = await connection.manager.findOne(Category, { where: { name: "category of great post" } });
const loadedPost = await loadedCategory!.twoSidePosts2;
loadedPost[0].title.should.be.equal("post with great category");
})));
it("should persist and hydrate successfully on a one-to-one relation owner side", () => Promise.all(connections.map(async connection => {
// create some fake posts and categories to make sure that there are several post ids in the db
const fakePosts: Post[] = [];
for (let i = 0; i < 8; i++) {
const fakePost = new Post();
fakePost.title = "post #" + i;
fakePost.text = "post #" + i;
fakePosts.push(fakePost);
}
await connection.manager.save(fakePosts);
const fakeCategories: Category[] = [];
for (let i = 0; i < 30; i++) {
const fakeCategory = new Category();
fakeCategory.name = "category #" + i;
fakeCategories.push(fakeCategory);
}
await connection.manager.save(fakeCategories);
const category = new Category();
category.name = "category of great post";
await connection.manager.save(category);
const post = new Post();
post.title = "post with great category";
post.text = "post with great category and great text";
post.oneCategory = Promise.resolve(category);
await connection.manager.save(post);
const loadedPost = await connection.manager.findOne(Post, { where: { title: "post with great category" } });
const loadedCategory = await loadedPost!.oneCategory;
loadedCategory.name.should.be.equal("category of great post");
})));
it("should persist and hydrate successfully on a one-to-one relation inverse side", () => Promise.all(connections.map(async connection => {
// create some fake posts and categories to make sure that there are several post ids in the db
const fakePosts: Post[] = [];
for (let i = 0; i < 8; i++) {
const fakePost = new Post();
fakePost.title = "post #" + i;
fakePost.text = "post #" + i;
fakePosts.push(fakePost);
}
await connection.manager.save(fakePosts);
const fakeCategories: Category[] = [];
for (let i = 0; i < 30; i++) {
const fakeCategory = new Category();
fakeCategory.name = "category #" + i;
fakeCategories.push(fakeCategory);
}
await connection.manager.save(fakeCategories);
const category = new Category();
category.name = "category of great post";
await connection.manager.save(category);
const post = new Post();
post.title = "post with great category";
post.text = "post with great category and great text";
post.oneCategory = Promise.resolve(category);
await connection.manager.save(post);
const loadedCategory = await connection.manager.findOne(Category, { where: { name: "category of great post" } });
const loadedPost = await loadedCategory!.onePost;
loadedPost.title.should.be.equal("post with great category");
})));
});

View File

@ -0,0 +1,44 @@
import { Entity } from "../../../../../src/decorator/entity/Entity";
import { PrimaryGeneratedColumn } from "../../../../../src/decorator/columns/PrimaryGeneratedColumn";
import { Column } from "../../../../../src/decorator/columns/Column";
import { ManyToMany } from "../../../../../src/decorator/relations/ManyToMany";
import { OneToMany } from "../../../../../src/decorator/relations/OneToMany";
import { OneToOne } from "../../../../../src/decorator/relations/OneToOne";
import {
Post,
} from "./Post";
@Entity("s_category", {
orderBy: {
id: "ASC",
}
})
export class Category {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@OneToOne(type => Post, post => post.oneCategory)
onePost: Promise<Post>;
@ManyToMany(type => Post, post => post.twoSideCategories)
twoSidePosts: Promise<Post[]>;
@OneToMany(type => Post, post => post.twoSideCategory)
twoSidePosts2: Promise<Post[]>;
// ManyToMany with named properties
@ManyToMany(type => Post, post => post.categoriesNamedTable)
postsNamedTable: Promise<Post[]>;
// OneToMany with named properties
@OneToMany(type => Post, post => post.categoryNamedTable)
onePostsNamedTable: Promise<Post[]>;
// OneToOne with named properties
@OneToOne(type => Post, post => post.oneCategoryNamedTable)
onePostNamedTable: Promise<Post>;
}

View File

@ -0,0 +1,65 @@
import { Entity } from "../../../../../src/decorator/entity/Entity";
import { PrimaryGeneratedColumn } from "../../../../../src/decorator/columns/PrimaryGeneratedColumn";
import { Column } from "../../../../../src/decorator/columns/Column";
import { ManyToMany } from "../../../../../src/decorator/relations/ManyToMany";
import { JoinTable } from "../../../../../src/decorator/relations/JoinTable";
import { ManyToOne } from "../../../../../src/decorator/relations/ManyToOne";
import { OneToOne } from "../../../../../src/decorator/relations/OneToOne";
import { JoinColumn } from "../../../../../src/decorator/relations/JoinColumn";
import {
Category,
} from "./Category";
@Entity("s_post", {
orderBy: {
title: "ASC",
id: "DESC",
}
})
export class Post {
@PrimaryGeneratedColumn()
id: number;
@Column()
title: string;
@Column()
text: string;
@ManyToMany(type => Category)
@JoinTable()
categories: Promise<Category[]>;
@ManyToMany(type => Category, category => category.twoSidePosts)
@JoinTable()
twoSideCategories: Promise<Category[]>;
@Column()
viewCount: number = 0;
@ManyToOne(type => Category)
category: Promise<Category>;
@OneToOne(type => Category, category => category.onePost)
@JoinColumn()
oneCategory: Promise<Category>;
@ManyToOne(type => Category, category => category.twoSidePosts2)
twoSideCategory: Promise<Category>;
// ManyToMany with named properties
@ManyToMany(type => Category, category => category.postsNamedTable)
@JoinTable()
categoriesNamedTable: Promise<Category[]>;
// ManyToOne with named properties
@ManyToOne(type => Category, category => category.onePostsNamedTable)
@JoinColumn()
categoryNamedTable: Promise<Category>;
// OneToOne with named properties
@OneToOne(type => Category, category => category.onePostNamedTable)
@JoinColumn()
oneCategoryNamedTable: Promise<Category>;
}

View File

@ -0,0 +1,301 @@
import "reflect-metadata";
import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../../utils/test-utils";
import {Connection} from "../../../../src/connection/Connection";
import {
Post,
} from "./entity/Post";
import {
Category,
} from "./entity/Category";
/**
* Because lazy relations are overriding prototype is impossible to run these tests on multiple connections.
* So we run tests only for mysql.
*/
describe("named-tables-lazy-relations", () => {
let connections: Connection[];
before(async () => connections = await createTestingConnections({
entities: [
Post,
Category,
],
schemaCreate: true,
dropSchema: true,
enabledDrivers: ["mysql"] // we can properly test lazy-relations only on one platform
}));
beforeEach(() => reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));
it("should persist and hydrate successfully on a relation without inverse side", () => Promise.all(connections.map(async connection => {
const postRepository = connection.getRepository(Post);
const categoryRepository = connection.getRepository(Category);
const savedCategory1 = new Category();
savedCategory1.name = "kids";
const savedCategory2 = new Category();
savedCategory2.name = "people";
const savedCategory3 = new Category();
savedCategory3.name = "animals";
await categoryRepository.save(savedCategory1);
await categoryRepository.save(savedCategory2);
await categoryRepository.save(savedCategory3);
const savedPost = new Post();
savedPost.title = "Hello post";
savedPost.text = "This is post about post";
savedPost.categories = Promise.resolve([
savedCategory1, savedCategory2, savedCategory3
]);
await postRepository.save(savedPost);
savedPost.categories.should.eventually.be.eql([savedCategory1, savedCategory2, savedCategory3]);
const post = (await postRepository.findOneById(1))!;
post.title.should.be.equal("Hello post");
post.text.should.be.equal("This is post about post");
post.categories.should.be.instanceOf(Promise);
const categories = await post.categories;
categories.length.should.be.equal(3);
categories.should.contain(savedCategory1);
categories.should.contain(savedCategory2);
categories.should.contain(savedCategory3);
})));
it("should persist and hydrate successfully on a relation with inverse side", () => Promise.all(connections.map(async connection => {
const postRepository = connection.getRepository(Post);
const categoryRepository = connection.getRepository(Category);
const savedCategory1 = new Category();
savedCategory1.name = "kids";
const savedCategory2 = new Category();
savedCategory2.name = "people";
const savedCategory3 = new Category();
savedCategory3.name = "animals";
await categoryRepository.save(savedCategory1);
await categoryRepository.save(savedCategory2);
await categoryRepository.save(savedCategory3);
const savedPost = new Post();
savedPost.title = "Hello post";
savedPost.text = "This is post about post";
savedPost.twoSideCategories = Promise.resolve([
savedCategory1, savedCategory2, savedCategory3
]);
await postRepository.save(savedPost);
savedPost.twoSideCategories.should.eventually.be.eql([savedCategory1, savedCategory2, savedCategory3]);
const post = (await postRepository.findOneById(1))!;
post.title.should.be.equal("Hello post");
post.text.should.be.equal("This is post about post");
post.twoSideCategories.should.be.instanceOf(Promise);
const categories = await post.twoSideCategories;
categories.length.should.be.equal(3);
categories.should.contain(savedCategory1);
categories.should.contain(savedCategory2);
categories.should.contain(savedCategory3);
const category = (await categoryRepository.findOneById(1))!;
category.name.should.be.equal("kids");
const twoSidePosts = await category.twoSidePosts;
const likePost = new Post();
likePost.id = 1;
likePost.title = "Hello post";
likePost.text = "This is post about post";
twoSidePosts.should.contain(likePost);
})));
it("should persist and hydrate successfully on a many-to-one relation without inverse side", () => Promise.all(connections.map(async connection => {
// create some fake posts and categories to make sure that there are several post ids in the db
const fakePosts: Post[] = [];
for (let i = 0; i < 30; i++) {
const fakePost = new Post();
fakePost.title = "post #" + i;
fakePost.text = "post #" + i;
fakePosts.push(fakePost);
}
await connection.manager.save(fakePosts);
const fakeCategories: Category[] = [];
for (let i = 0; i < 8; i++) {
const fakeCategory = new Category();
fakeCategory.name = "category #" + i;
fakeCategories.push(fakeCategory);
}
await connection.manager.save(fakeCategories);
const category = new Category();
category.name = "category of great post";
const post = new Post();
post.title = "post with great category";
post.text = "post with great category and great text";
post.category = Promise.resolve(category);
await connection.manager.save(category);
await connection.manager.save(post);
const loadedPost = await connection.manager.findOne(Post, { where: { title: "post with great category" } });
const loadedCategory = await loadedPost!.category;
loadedCategory.name.should.be.equal("category of great post");
})));
it("should persist and hydrate successfully on a many-to-one relation with inverse side", () => Promise.all(connections.map(async connection => {
// create some fake posts and categories to make sure that there are several post ids in the db
const fakePosts: Post[] = [];
for (let i = 0; i < 8; i++) {
const fakePost = new Post();
fakePost.title = "post #" + i;
fakePost.text = "post #" + i;
fakePosts.push(fakePost);
}
await connection.manager.save(fakePosts);
const fakeCategories: Category[] = [];
for (let i = 0; i < 30; i++) {
const fakeCategory = new Category();
fakeCategory.name = "category #" + i;
fakeCategories.push(fakeCategory);
}
await connection.manager.save(fakeCategories);
const category = new Category();
category.name = "category of great post";
const post = new Post();
post.title = "post with great category";
post.text = "post with great category and great text";
post.twoSideCategory = Promise.resolve(category);
await connection.manager.save(category);
await connection.manager.save(post);
const loadedPost = await connection.manager.findOne(Post, { where: { title: "post with great category" } });
const loadedCategory = await loadedPost!.twoSideCategory;
loadedCategory.name.should.be.equal("category of great post");
})));
it("should persist and hydrate successfully on a one-to-many relation", () => Promise.all(connections.map(async connection => {
// create some fake posts and categories to make sure that there are several post ids in the db
const fakePosts: Post[] = [];
for (let i = 0; i < 8; i++) {
const fakePost = new Post();
fakePost.title = "post #" + i;
fakePost.text = "post #" + i;
fakePosts.push(fakePost);
}
await connection.manager.save(fakePosts);
const fakeCategories: Category[] = [];
for (let i = 0; i < 30; i++) {
const fakeCategory = new Category();
fakeCategory.name = "category #" + i;
fakeCategories.push(fakeCategory);
}
await connection.manager.save(fakeCategories);
const category = new Category();
category.name = "category of great post";
await connection.manager.save(category);
const post = new Post();
post.title = "post with great category";
post.text = "post with great category and great text";
post.twoSideCategory = Promise.resolve(category);
await connection.manager.save(post);
const loadedCategory = await connection.manager.findOne(Category, { where: { name: "category of great post" } });
const loadedPost = await loadedCategory!.twoSidePosts2;
loadedPost[0].title.should.be.equal("post with great category");
})));
it("should persist and hydrate successfully on a one-to-one relation owner side", () => Promise.all(connections.map(async connection => {
// create some fake posts and categories to make sure that there are several post ids in the db
const fakePosts: Post[] = [];
for (let i = 0; i < 8; i++) {
const fakePost = new Post();
fakePost.title = "post #" + i;
fakePost.text = "post #" + i;
fakePosts.push(fakePost);
}
await connection.manager.save(fakePosts);
const fakeCategories: Category[] = [];
for (let i = 0; i < 30; i++) {
const fakeCategory = new Category();
fakeCategory.name = "category #" + i;
fakeCategories.push(fakeCategory);
}
await connection.manager.save(fakeCategories);
const category = new Category();
category.name = "category of great post";
await connection.manager.save(category);
const post = new Post();
post.title = "post with great category";
post.text = "post with great category and great text";
post.oneCategory = Promise.resolve(category);
await connection.manager.save(post);
const loadedPost = await connection.manager.findOne(Post, { where: { title: "post with great category" } });
const loadedCategory = await loadedPost!.oneCategory;
loadedCategory.name.should.be.equal("category of great post");
})));
it("should persist and hydrate successfully on a one-to-one relation inverse side", () => Promise.all(connections.map(async connection => {
// create some fake posts and categories to make sure that there are several post ids in the db
const fakePosts: Post[] = [];
for (let i = 0; i < 8; i++) {
const fakePost = new Post();
fakePost.title = "post #" + i;
fakePost.text = "post #" + i;
fakePosts.push(fakePost);
}
await connection.manager.save(fakePosts);
const fakeCategories: Category[] = [];
for (let i = 0; i < 30; i++) {
const fakeCategory = new Category();
fakeCategory.name = "category #" + i;
fakeCategories.push(fakeCategory);
}
await connection.manager.save(fakeCategories);
const category = new Category();
category.name = "category of great post";
await connection.manager.save(category);
const post = new Post();
post.title = "post with great category";
post.text = "post with great category and great text";
post.oneCategory = Promise.resolve(category);
await connection.manager.save(post);
const loadedCategory = await connection.manager.findOne(Category, { where: { name: "category of great post" } });
const loadedPost = await loadedCategory!.onePost;
loadedPost.title.should.be.equal("post with great category");
})));
});