mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
working on RelationId tests;
This commit is contained in:
parent
83b8f0e418
commit
74ed97b399
@ -10,6 +10,8 @@ import {Connection} from "../../../../../../src/connection/Connection";
|
||||
import {Post} from "./entity/Post";
|
||||
import {Category} from "./entity/Category";
|
||||
import {Counters} from "./entity/Counters";
|
||||
import {User} from "./entity/User";
|
||||
import {Subcounters} from "./entity/Subcounters";
|
||||
|
||||
const should = chai.should();
|
||||
|
||||
@ -24,7 +26,19 @@ describe("query builder > relation-id > many-to-many > embedded", () => {
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
it("should load ids when RelationId decorator used in embedded table", () => Promise.all(connections.map(async connection => {
|
||||
it("should load ids when loadRelationIdAndMap used on embedded and nested embedded tables", () => Promise.all(connections.map(async connection => {
|
||||
|
||||
const user1 = new User();
|
||||
user1.name = "Alice";
|
||||
await connection.manager.persist(user1);
|
||||
|
||||
const user2 = new User();
|
||||
user2.name = "Bob";
|
||||
await connection.manager.persist(user2);
|
||||
|
||||
const user3 = new User();
|
||||
user3.name = "Clara";
|
||||
await connection.manager.persist(user3);
|
||||
|
||||
const category1 = new Category();
|
||||
category1.name = "cars";
|
||||
@ -49,6 +63,10 @@ describe("query builder > relation-id > many-to-many > embedded", () => {
|
||||
post1.counters.comments = 2;
|
||||
post1.counters.favorites = 3;
|
||||
post1.counters.categories = [category1, category2];
|
||||
post1.counters.subcounters = new Subcounters();
|
||||
post1.counters.subcounters.version = 1;
|
||||
post1.counters.subcounters.watches = 2;
|
||||
post1.counters.subcounters.watchedUsers = [user1, user2];
|
||||
await connection.manager.persist(post1);
|
||||
|
||||
const post2 = new Post();
|
||||
@ -58,11 +76,16 @@ describe("query builder > relation-id > many-to-many > embedded", () => {
|
||||
post2.counters.comments = 4;
|
||||
post2.counters.favorites = 5;
|
||||
post2.counters.categories = [category3, category4];
|
||||
post2.counters.subcounters = new Subcounters();
|
||||
post2.counters.subcounters.version = 1;
|
||||
post2.counters.subcounters.watches = 1;
|
||||
post2.counters.subcounters.watchedUsers = [user3];
|
||||
await connection.manager.persist(post2);
|
||||
|
||||
const loadedPosts = await connection.manager
|
||||
.createQueryBuilder(Post, "post")
|
||||
.loadRelationIdAndMap("post.counters.categoryIds", "post.counters.categories")
|
||||
.loadRelationIdAndMap("post.counters.subcounters.watchedUserIds", "post.counters.subcounters.watchedUsers")
|
||||
.orderBy("post.id")
|
||||
.getMany();
|
||||
|
||||
@ -74,7 +97,12 @@ describe("query builder > relation-id > many-to-many > embedded", () => {
|
||||
likes: 1,
|
||||
comments: 2,
|
||||
favorites: 3,
|
||||
categoryIds: [1, 2]
|
||||
categoryIds: [1, 2],
|
||||
subcounters: {
|
||||
version: 1,
|
||||
watches: 2,
|
||||
watchedUserIds: [1, 2]
|
||||
}
|
||||
}
|
||||
}
|
||||
));
|
||||
@ -86,7 +114,12 @@ describe("query builder > relation-id > many-to-many > embedded", () => {
|
||||
likes: 3,
|
||||
comments: 4,
|
||||
favorites: 5,
|
||||
categoryIds: [3, 4]
|
||||
categoryIds: [3, 4],
|
||||
subcounters: {
|
||||
version: 1,
|
||||
watches: 1,
|
||||
watchedUserIds: [3]
|
||||
}
|
||||
}
|
||||
}
|
||||
));
|
||||
|
||||
@ -1,7 +1,9 @@
|
||||
import {Column} from "../../../../../../../src/decorator/columns/Column";
|
||||
import {ManyToMany} from "../../../../../../../src/decorator/relations/ManyToMany";
|
||||
import {JoinTable} from "../../../../../../../src/decorator/relations/JoinTable";
|
||||
import {Embedded} from "../../../../../../../src/decorator/Embedded";
|
||||
import {Category} from "./Category";
|
||||
import {Subcounters} from "./Subcounters";
|
||||
|
||||
export class Counters {
|
||||
|
||||
@ -18,6 +20,9 @@ export class Counters {
|
||||
@JoinTable()
|
||||
categories: Category[];
|
||||
|
||||
@Embedded(() => Subcounters)
|
||||
subcounters: Subcounters;
|
||||
|
||||
categoryIds: number[];
|
||||
|
||||
}
|
||||
@ -0,0 +1,20 @@
|
||||
import {Column} from "../../../../../../../src/decorator/columns/Column";
|
||||
import {ManyToMany} from "../../../../../../../src/decorator/relations/ManyToMany";
|
||||
import {User} from "./User";
|
||||
import {JoinTable} from "../../../../../../../src/decorator/relations/JoinTable";
|
||||
|
||||
export class Subcounters {
|
||||
|
||||
@Column()
|
||||
version: number;
|
||||
|
||||
@Column()
|
||||
watches: number;
|
||||
|
||||
@ManyToMany(type => User)
|
||||
@JoinTable()
|
||||
watchedUsers: User[];
|
||||
|
||||
watchedUserIds: number[];
|
||||
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
import {Column} from "../../../../../../../src/decorator/columns/Column";
|
||||
import {Entity} from "../../../../../../../src/decorator/entity/Entity";
|
||||
import {PrimaryGeneratedColumn} from "../../../../../../../src/decorator/columns/PrimaryGeneratedColumn";
|
||||
|
||||
@Entity()
|
||||
export class User {
|
||||
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
name: string;
|
||||
|
||||
}
|
||||
@ -0,0 +1,117 @@
|
||||
import "reflect-metadata";
|
||||
import * as chai from "chai";
|
||||
import {expect} from "chai";
|
||||
import {
|
||||
closeTestingConnections,
|
||||
createTestingConnections,
|
||||
reloadTestingDatabases
|
||||
} from "../../../../../utils/test-utils";
|
||||
import {Connection} from "../../../../../../src/connection/Connection";
|
||||
import {Post} from "./entity/Post";
|
||||
import {Category} from "./entity/Category";
|
||||
import {Counters} from "./entity/Counters";
|
||||
import {User} from "./entity/User";
|
||||
import {Subcounters} from "./entity/Subcounters";
|
||||
|
||||
const should = chai.should();
|
||||
|
||||
describe("query builder > relation-id > many-to-one > embedded", () => {
|
||||
|
||||
let connections: Connection[];
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
it("should load ids when loadRelationIdAndMap used on embedded and nested embedded tables", () => Promise.all(connections.map(async connection => {
|
||||
|
||||
const user1 = new User();
|
||||
user1.name = "Alice";
|
||||
await connection.manager.persist(user1);
|
||||
|
||||
const user2 = new User();
|
||||
user2.name = "Bob";
|
||||
await connection.manager.persist(user2);
|
||||
|
||||
const category1 = new Category();
|
||||
category1.name = "cars";
|
||||
await connection.manager.persist(category1);
|
||||
|
||||
const category2 = new Category();
|
||||
category2.name = "airplanes";
|
||||
await connection.manager.persist(category2);
|
||||
|
||||
const post1 = new Post();
|
||||
post1.title = "About BMW";
|
||||
post1.counters = new Counters();
|
||||
post1.counters.likes = 1;
|
||||
post1.counters.comments = 2;
|
||||
post1.counters.favorites = 3;
|
||||
post1.counters.category = category1;
|
||||
post1.counters.subcounters = new Subcounters();
|
||||
post1.counters.subcounters.version = 1;
|
||||
post1.counters.subcounters.watches = 2;
|
||||
post1.counters.subcounters.watchedUser = user1;
|
||||
await connection.manager.persist(post1);
|
||||
|
||||
const post2 = new Post();
|
||||
post2.title = "About Boeing";
|
||||
post2.counters = new Counters();
|
||||
post2.counters.likes = 3;
|
||||
post2.counters.comments = 4;
|
||||
post2.counters.favorites = 5;
|
||||
post2.counters.category = category2;
|
||||
post2.counters.subcounters = new Subcounters();
|
||||
post2.counters.subcounters.version = 1;
|
||||
post2.counters.subcounters.watches = 1;
|
||||
post2.counters.subcounters.watchedUser = user2;
|
||||
await connection.manager.persist(post2);
|
||||
|
||||
const loadedPosts = await connection.manager
|
||||
.createQueryBuilder(Post, "post")
|
||||
.loadRelationIdAndMap("post.counters.categoryId", "post.counters.category")
|
||||
.loadRelationIdAndMap("post.counters.subcounters.watchedUserId", "post.counters.subcounters.watchedUser")
|
||||
.orderBy("post.id")
|
||||
.getMany();
|
||||
|
||||
expect(loadedPosts[0].should.be.eql(
|
||||
{
|
||||
id: 1,
|
||||
title: "About BMW",
|
||||
counters: {
|
||||
likes: 1,
|
||||
comments: 2,
|
||||
favorites: 3,
|
||||
categoryId: 1,
|
||||
subcounters: {
|
||||
version: 1,
|
||||
watches: 2,
|
||||
watchedUserId: 1
|
||||
}
|
||||
}
|
||||
}
|
||||
));
|
||||
expect(loadedPosts[1].should.be.eql(
|
||||
{
|
||||
id: 2,
|
||||
title: "About Boeing",
|
||||
counters: {
|
||||
likes: 3,
|
||||
comments: 4,
|
||||
favorites: 5,
|
||||
categoryId: 2,
|
||||
subcounters: {
|
||||
version: 1,
|
||||
watches: 1,
|
||||
watchedUserId: 2
|
||||
}
|
||||
}
|
||||
}
|
||||
));
|
||||
|
||||
})));
|
||||
|
||||
});
|
||||
@ -0,0 +1,21 @@
|
||||
import {Entity} from "../../../../../../../src/decorator/entity/Entity";
|
||||
import {Column} from "../../../../../../../src/decorator/columns/Column";
|
||||
import {PrimaryGeneratedColumn} from "../../../../../../../src/decorator/columns/PrimaryGeneratedColumn";
|
||||
import {Post} from "./Post";
|
||||
import {OneToMany} from "../../../../../../../src/decorator/relations/OneToMany";
|
||||
|
||||
@Entity()
|
||||
export class Category {
|
||||
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
name: string;
|
||||
|
||||
@OneToMany(type => Post, post => post.counters.category)
|
||||
posts: Post[];
|
||||
|
||||
postIds: number[];
|
||||
|
||||
}
|
||||
@ -0,0 +1,28 @@
|
||||
import {Column} from "../../../../../../../src/decorator/columns/Column";
|
||||
import {JoinTable} from "../../../../../../../src/decorator/relations/JoinTable";
|
||||
import {Embedded} from "../../../../../../../src/decorator/Embedded";
|
||||
import {ManyToOne} from "../../../../../../../src/decorator/relations/ManyToOne";
|
||||
import {Category} from "./Category";
|
||||
import {Subcounters} from "./Subcounters";
|
||||
|
||||
export class Counters {
|
||||
|
||||
@Column()
|
||||
likes: number;
|
||||
|
||||
@Column()
|
||||
comments: number;
|
||||
|
||||
@Column()
|
||||
favorites: number;
|
||||
|
||||
@ManyToOne(type => Category, category => category.posts)
|
||||
@JoinTable()
|
||||
category: Category;
|
||||
|
||||
@Embedded(() => Subcounters)
|
||||
subcounters: Subcounters;
|
||||
|
||||
categoryId: number;
|
||||
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
import {Entity} from "../../../../../../../src/decorator/entity/Entity";
|
||||
import {Column} from "../../../../../../../src/decorator/columns/Column";
|
||||
import {PrimaryGeneratedColumn} from "../../../../../../../src/decorator/columns/PrimaryGeneratedColumn";
|
||||
import {Embedded} from "../../../../../../../src/decorator/Embedded";
|
||||
import {Counters} from "./Counters";
|
||||
|
||||
@Entity()
|
||||
export class Post {
|
||||
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
title: string;
|
||||
|
||||
@Embedded(() => Counters)
|
||||
counters: Counters;
|
||||
|
||||
}
|
||||
@ -0,0 +1,18 @@
|
||||
import {Column} from "../../../../../../../src/decorator/columns/Column";
|
||||
import {ManyToOne} from "../../../../../../../src/decorator/relations/ManyToOne";
|
||||
import {User} from "./User";
|
||||
|
||||
export class Subcounters {
|
||||
|
||||
@Column()
|
||||
version: number;
|
||||
|
||||
@Column()
|
||||
watches: number;
|
||||
|
||||
@ManyToOne(type => User)
|
||||
watchedUser: User;
|
||||
|
||||
watchedUserId: number;
|
||||
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
import {Column} from "../../../../../../../src/decorator/columns/Column";
|
||||
import {Entity} from "../../../../../../../src/decorator/entity/Entity";
|
||||
import {PrimaryGeneratedColumn} from "../../../../../../../src/decorator/columns/PrimaryGeneratedColumn";
|
||||
|
||||
@Entity()
|
||||
export class User {
|
||||
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
name: string;
|
||||
|
||||
}
|
||||
@ -0,0 +1,129 @@
|
||||
import "reflect-metadata";
|
||||
import * as chai from "chai";
|
||||
import {expect} from "chai";
|
||||
import {
|
||||
closeTestingConnections,
|
||||
createTestingConnections,
|
||||
reloadTestingDatabases
|
||||
} from "../../../../../utils/test-utils";
|
||||
import {Connection} from "../../../../../../src/connection/Connection";
|
||||
import {Post} from "./entity/Post";
|
||||
import {Category} from "./entity/Category";
|
||||
import {Counters} from "./entity/Counters";
|
||||
import {User} from "./entity/User";
|
||||
import {Subcounters} from "./entity/Subcounters";
|
||||
|
||||
const should = chai.should();
|
||||
|
||||
describe("query builder > relation-id > many-to-many > embedded", () => {
|
||||
|
||||
let connections: Connection[];
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
it("should load ids when loadRelationIdAndMap used on embedded and nested embedded tables", () => Promise.all(connections.map(async connection => {
|
||||
|
||||
const user1 = new User();
|
||||
user1.name = "Alice";
|
||||
await connection.manager.persist(user1);
|
||||
|
||||
const user2 = new User();
|
||||
user2.name = "Bob";
|
||||
await connection.manager.persist(user2);
|
||||
|
||||
const user3 = new User();
|
||||
user3.name = "Clara";
|
||||
await connection.manager.persist(user3);
|
||||
|
||||
const category1 = new Category();
|
||||
category1.name = "cars";
|
||||
await connection.manager.persist(category1);
|
||||
|
||||
const category2 = new Category();
|
||||
category2.name = "BMW";
|
||||
await connection.manager.persist(category2);
|
||||
|
||||
const category3 = new Category();
|
||||
category3.name = "airplanes";
|
||||
await connection.manager.persist(category3);
|
||||
|
||||
const category4 = new Category();
|
||||
category4.name = "Boeing";
|
||||
await connection.manager.persist(category4);
|
||||
|
||||
const post1 = new Post();
|
||||
post1.title = "About BMW";
|
||||
post1.counters = new Counters();
|
||||
post1.counters.likes = 1;
|
||||
post1.counters.comments = 2;
|
||||
post1.counters.favorites = 3;
|
||||
post1.counters.categories = [category1, category2];
|
||||
post1.counters.subcounters = new Subcounters();
|
||||
post1.counters.subcounters.version = 1;
|
||||
post1.counters.subcounters.watches = 2;
|
||||
post1.counters.subcounters.watchedUsers = [user1, user2];
|
||||
await connection.manager.persist(post1);
|
||||
|
||||
const post2 = new Post();
|
||||
post2.title = "About Boeing";
|
||||
post2.counters = new Counters();
|
||||
post2.counters.likes = 3;
|
||||
post2.counters.comments = 4;
|
||||
post2.counters.favorites = 5;
|
||||
post2.counters.categories = [category3, category4];
|
||||
post2.counters.subcounters = new Subcounters();
|
||||
post2.counters.subcounters.version = 1;
|
||||
post2.counters.subcounters.watches = 1;
|
||||
post2.counters.subcounters.watchedUsers = [user3];
|
||||
await connection.manager.persist(post2);
|
||||
|
||||
const loadedPosts = await connection.manager
|
||||
.createQueryBuilder(Post, "post")
|
||||
.loadRelationIdAndMap("post.counters.categoryIds", "post.counters.categories")
|
||||
.loadRelationIdAndMap("post.counters.subcounters.watchedUserIds", "post.counters.subcounters.watchedUsers")
|
||||
.orderBy("post.id")
|
||||
.getMany();
|
||||
|
||||
expect(loadedPosts[0].should.be.eql(
|
||||
{
|
||||
id: 1,
|
||||
title: "About BMW",
|
||||
counters: {
|
||||
likes: 1,
|
||||
comments: 2,
|
||||
favorites: 3,
|
||||
categoryIds: [1, 2],
|
||||
subcounters: {
|
||||
version: 1,
|
||||
watches: 2,
|
||||
watchedUserIds: [1, 2]
|
||||
}
|
||||
}
|
||||
}
|
||||
));
|
||||
expect(loadedPosts[1].should.be.eql(
|
||||
{
|
||||
id: 2,
|
||||
title: "About Boeing",
|
||||
counters: {
|
||||
likes: 3,
|
||||
comments: 4,
|
||||
favorites: 5,
|
||||
categoryIds: [3, 4],
|
||||
subcounters: {
|
||||
version: 1,
|
||||
watches: 1,
|
||||
watchedUserIds: [3]
|
||||
}
|
||||
}
|
||||
}
|
||||
));
|
||||
|
||||
})));
|
||||
|
||||
});
|
||||
@ -0,0 +1,23 @@
|
||||
import {Entity} from "../../../../../../../src/decorator/entity/Entity";
|
||||
import {Column} from "../../../../../../../src/decorator/columns/Column";
|
||||
import {PrimaryGeneratedColumn} from "../../../../../../../src/decorator/columns/PrimaryGeneratedColumn";
|
||||
import {Post} from "./Post";
|
||||
import {ManyToOne} from "../../../../../../../src/decorator/relations/ManyToOne";
|
||||
import {JoinColumn} from "../../../../../../../src/decorator/relations/JoinColumn";
|
||||
|
||||
@Entity()
|
||||
export class Category {
|
||||
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
name: string;
|
||||
|
||||
@ManyToOne(type => Post, post => post.counters.categories)
|
||||
@JoinColumn()
|
||||
posts: Post[];
|
||||
|
||||
postIds: number[];
|
||||
|
||||
}
|
||||
@ -0,0 +1,26 @@
|
||||
import {Column} from "../../../../../../../src/decorator/columns/Column";
|
||||
import {Embedded} from "../../../../../../../src/decorator/Embedded";
|
||||
import {Category} from "./Category";
|
||||
import {Subcounters} from "./Subcounters";
|
||||
import {OneToMany} from "../../../../../../../src/decorator/relations/OneToMany";
|
||||
|
||||
export class Counters {
|
||||
|
||||
@Column()
|
||||
likes: number;
|
||||
|
||||
@Column()
|
||||
comments: number;
|
||||
|
||||
@Column()
|
||||
favorites: number;
|
||||
|
||||
@OneToMany(type => Category, category => category.posts)
|
||||
categories: Category[];
|
||||
|
||||
@Embedded(() => Subcounters)
|
||||
subcounters: Subcounters;
|
||||
|
||||
categoryIds: number[];
|
||||
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
import {Entity} from "../../../../../../../src/decorator/entity/Entity";
|
||||
import {Column} from "../../../../../../../src/decorator/columns/Column";
|
||||
import {PrimaryGeneratedColumn} from "../../../../../../../src/decorator/columns/PrimaryGeneratedColumn";
|
||||
import {Embedded} from "../../../../../../../src/decorator/Embedded";
|
||||
import {Counters} from "./Counters";
|
||||
|
||||
@Entity()
|
||||
export class Post {
|
||||
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
title: string;
|
||||
|
||||
@Embedded(() => Counters)
|
||||
counters: Counters;
|
||||
|
||||
}
|
||||
@ -0,0 +1,18 @@
|
||||
import {Column} from "../../../../../../../src/decorator/columns/Column";
|
||||
import {OneToMany} from "../../../../../../../src/decorator/relations/OneToMany";
|
||||
import {User} from "./User";
|
||||
|
||||
export class Subcounters {
|
||||
|
||||
@Column()
|
||||
version: number;
|
||||
|
||||
@Column()
|
||||
watches: number;
|
||||
|
||||
@OneToMany(type => User, user => user.posts)
|
||||
watchedUsers: User[];
|
||||
|
||||
watchedUserIds: number[];
|
||||
|
||||
}
|
||||
@ -0,0 +1,21 @@
|
||||
import {Column} from "../../../../../../../src/decorator/columns/Column";
|
||||
import {Entity} from "../../../../../../../src/decorator/entity/Entity";
|
||||
import {PrimaryGeneratedColumn} from "../../../../../../../src/decorator/columns/PrimaryGeneratedColumn";
|
||||
import {ManyToOne} from "../../../../../../../src/decorator/relations/ManyToOne";
|
||||
import {JoinColumn} from "../../../../../../../src/decorator/relations/JoinColumn";
|
||||
import {Post} from "./Post";
|
||||
|
||||
@Entity()
|
||||
export class User {
|
||||
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
name: string;
|
||||
|
||||
@ManyToOne(type => Post, post => post.counters.subcounters.watchedUsers)
|
||||
@JoinColumn()
|
||||
posts: Post[];
|
||||
|
||||
}
|
||||
@ -0,0 +1,117 @@
|
||||
import "reflect-metadata";
|
||||
import * as chai from "chai";
|
||||
import {expect} from "chai";
|
||||
import {
|
||||
closeTestingConnections,
|
||||
createTestingConnections,
|
||||
reloadTestingDatabases
|
||||
} from "../../../../../utils/test-utils";
|
||||
import {Connection} from "../../../../../../src/connection/Connection";
|
||||
import {Post} from "./entity/Post";
|
||||
import {Category} from "./entity/Category";
|
||||
import {Counters} from "./entity/Counters";
|
||||
import {User} from "./entity/User";
|
||||
import {Subcounters} from "./entity/Subcounters";
|
||||
|
||||
const should = chai.should();
|
||||
|
||||
describe("query builder > relation-id > one-to-one > embedded", () => {
|
||||
|
||||
let connections: Connection[];
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true,
|
||||
}));
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
it("should load ids when loadRelationIdAndMap used on embedded and nested embedded tables", () => Promise.all(connections.map(async connection => {
|
||||
|
||||
const user1 = new User();
|
||||
user1.name = "Alice";
|
||||
await connection.manager.persist(user1);
|
||||
|
||||
const user2 = new User();
|
||||
user2.name = "Bob";
|
||||
await connection.manager.persist(user2);
|
||||
|
||||
const category1 = new Category();
|
||||
category1.name = "cars";
|
||||
await connection.manager.persist(category1);
|
||||
|
||||
const category2 = new Category();
|
||||
category2.name = "airplanes";
|
||||
await connection.manager.persist(category2);
|
||||
|
||||
const post1 = new Post();
|
||||
post1.title = "About BMW";
|
||||
post1.counters = new Counters();
|
||||
post1.counters.likes = 1;
|
||||
post1.counters.comments = 2;
|
||||
post1.counters.favorites = 3;
|
||||
post1.counters.category = category1;
|
||||
post1.counters.subcounters = new Subcounters();
|
||||
post1.counters.subcounters.version = 1;
|
||||
post1.counters.subcounters.watches = 2;
|
||||
post1.counters.subcounters.watchedUser = user1;
|
||||
await connection.manager.persist(post1);
|
||||
|
||||
const post2 = new Post();
|
||||
post2.title = "About Boeing";
|
||||
post2.counters = new Counters();
|
||||
post2.counters.likes = 3;
|
||||
post2.counters.comments = 4;
|
||||
post2.counters.favorites = 5;
|
||||
post2.counters.category = category2;
|
||||
post2.counters.subcounters = new Subcounters();
|
||||
post2.counters.subcounters.version = 1;
|
||||
post2.counters.subcounters.watches = 1;
|
||||
post2.counters.subcounters.watchedUser = user2;
|
||||
await connection.manager.persist(post2);
|
||||
|
||||
const loadedPosts = await connection.manager
|
||||
.createQueryBuilder(Post, "post")
|
||||
.loadRelationIdAndMap("post.counters.categoryId", "post.counters.category")
|
||||
.loadRelationIdAndMap("post.counters.subcounters.watchedUserId", "post.counters.subcounters.watchedUser")
|
||||
.orderBy("post.id")
|
||||
.getMany();
|
||||
|
||||
expect(loadedPosts[0].should.be.eql(
|
||||
{
|
||||
id: 1,
|
||||
title: "About BMW",
|
||||
counters: {
|
||||
likes: 1,
|
||||
comments: 2,
|
||||
favorites: 3,
|
||||
categoryId: 1,
|
||||
subcounters: {
|
||||
version: 1,
|
||||
watches: 2,
|
||||
watchedUserId: 1
|
||||
}
|
||||
}
|
||||
}
|
||||
));
|
||||
expect(loadedPosts[1].should.be.eql(
|
||||
{
|
||||
id: 2,
|
||||
title: "About Boeing",
|
||||
counters: {
|
||||
likes: 3,
|
||||
comments: 4,
|
||||
favorites: 5,
|
||||
categoryId: 2,
|
||||
subcounters: {
|
||||
version: 1,
|
||||
watches: 1,
|
||||
watchedUserId: 2
|
||||
}
|
||||
}
|
||||
}
|
||||
));
|
||||
|
||||
})));
|
||||
|
||||
});
|
||||
@ -0,0 +1,21 @@
|
||||
import {Entity} from "../../../../../../../src/decorator/entity/Entity";
|
||||
import {Column} from "../../../../../../../src/decorator/columns/Column";
|
||||
import {PrimaryGeneratedColumn} from "../../../../../../../src/decorator/columns/PrimaryGeneratedColumn";
|
||||
import {Post} from "./Post";
|
||||
import {OneToOne} from "../../../../../../../src/decorator/relations/OneToOne";
|
||||
|
||||
@Entity()
|
||||
export class Category {
|
||||
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
name: string;
|
||||
|
||||
@OneToOne(type => Post, post => post.counters.category)
|
||||
post: Post;
|
||||
|
||||
postId: number;
|
||||
|
||||
}
|
||||
@ -0,0 +1,28 @@
|
||||
import {Column} from "../../../../../../../src/decorator/columns/Column";
|
||||
import {Embedded} from "../../../../../../../src/decorator/Embedded";
|
||||
import {Category} from "./Category";
|
||||
import {Subcounters} from "./Subcounters";
|
||||
import {OneToOne} from "../../../../../../../src/decorator/relations/OneToOne";
|
||||
import {JoinColumn} from "../../../../../../../src/decorator/relations/JoinColumn";
|
||||
|
||||
export class Counters {
|
||||
|
||||
@Column()
|
||||
likes: number;
|
||||
|
||||
@Column()
|
||||
comments: number;
|
||||
|
||||
@Column()
|
||||
favorites: number;
|
||||
|
||||
@OneToOne(type => Category, category => category.post)
|
||||
@JoinColumn()
|
||||
category: Category;
|
||||
|
||||
@Embedded(() => Subcounters)
|
||||
subcounters: Subcounters;
|
||||
|
||||
categoryId: number;
|
||||
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
import {Entity} from "../../../../../../../src/decorator/entity/Entity";
|
||||
import {Column} from "../../../../../../../src/decorator/columns/Column";
|
||||
import {PrimaryGeneratedColumn} from "../../../../../../../src/decorator/columns/PrimaryGeneratedColumn";
|
||||
import {Embedded} from "../../../../../../../src/decorator/Embedded";
|
||||
import {Counters} from "./Counters";
|
||||
|
||||
@Entity()
|
||||
export class Post {
|
||||
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
title: string;
|
||||
|
||||
@Embedded(() => Counters)
|
||||
counters: Counters;
|
||||
|
||||
}
|
||||
@ -0,0 +1,20 @@
|
||||
import {Column} from "../../../../../../../src/decorator/columns/Column";
|
||||
import {User} from "./User";
|
||||
import {OneToOne} from "../../../../../../../src/decorator/relations/OneToOne";
|
||||
import {JoinColumn} from "../../../../../../../src/decorator/relations/JoinColumn";
|
||||
|
||||
export class Subcounters {
|
||||
|
||||
@Column()
|
||||
version: number;
|
||||
|
||||
@Column()
|
||||
watches: number;
|
||||
|
||||
@OneToOne(type => User)
|
||||
@JoinColumn()
|
||||
watchedUser: User;
|
||||
|
||||
watchedUserId: number;
|
||||
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
import {Column} from "../../../../../../../src/decorator/columns/Column";
|
||||
import {Entity} from "../../../../../../../src/decorator/entity/Entity";
|
||||
import {PrimaryGeneratedColumn} from "../../../../../../../src/decorator/columns/PrimaryGeneratedColumn";
|
||||
|
||||
@Entity()
|
||||
export class User {
|
||||
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
name: string;
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user