fixed issue with joined empty relations inside empty objects

This commit is contained in:
Umed Khudoiberdiev 2017-01-20 18:17:49 +05:00
parent bcef8953cf
commit 14b67da103
6 changed files with 123 additions and 2 deletions

View File

@ -1,7 +1,7 @@
{
"name": "typeorm",
"private": true,
"version": "0.0.7-alpha.22",
"version": "0.0.7-alpha.23",
"description": "Data-Mapper ORM for TypeScript, ES7, ES6, ES5. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, WebSQL databases.",
"license": "MIT",
"readmeFilename": "README.md",

View File

@ -156,7 +156,9 @@ export class RawSqlResultsToEntityTransformer {
} else {
entity[propertyName] = result;
}
hasData = true;
if (!isResultArray || result.length > 0)
hasData = true;
}
}

View File

@ -0,0 +1,14 @@
import {Entity} from "../../../../src/decorator/entity/Entity";
import {PrimaryGeneratedColumn} from "../../../../src/decorator/columns/PrimaryGeneratedColumn";
import {Column} from "../../../../src/decorator/columns/Column";
@Entity()
export class Author {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
}

View File

@ -0,0 +1,21 @@
import {Entity} from "../../../../src/decorator/entity/Entity";
import {PrimaryGeneratedColumn} from "../../../../src/decorator/columns/PrimaryGeneratedColumn";
import {Column} from "../../../../src/decorator/columns/Column";
import {JoinTable} from "../../../../src/decorator/relations/JoinTable";
import {ManyToMany} from "../../../../src/decorator/relations/ManyToMany";
import {Author} from "./Author";
@Entity()
export class Category {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@ManyToMany(type => Author)
@JoinTable()
authors: Author[];
}

View File

@ -0,0 +1,21 @@
import {Entity} from "../../../../src/decorator/entity/Entity";
import {PrimaryGeneratedColumn} from "../../../../src/decorator/columns/PrimaryGeneratedColumn";
import {Column} from "../../../../src/decorator/columns/Column";
import {Category} from "./Category";
import {ManyToMany} from "../../../../src/decorator/relations/ManyToMany";
import {JoinTable} from "../../../../src/decorator/relations/JoinTable";
@Entity()
export class Post {
@PrimaryGeneratedColumn()
id: number;
@Column()
title: string;
@ManyToMany(type => Category)
@JoinTable()
categories: Category[];
}

View File

@ -0,0 +1,63 @@
import "reflect-metadata";
import {createTestingConnections, closeTestingConnections, reloadTestingDatabases} from "../../utils/test-utils";
import {Connection} from "../../../src/connection/Connection";
import {Post} from "./entity/Post";
import {expect} from "chai";
describe.only("other issues > joining empty relations", () => {
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 return empty array if its joined and nothing was found", () => Promise.all(connections.map(async function(connection) {
const post = new Post();
post.title = "Hello Post";
await connection.entityManager.persist(post);
// check if ordering by main object works correctly
const loadedPosts1 = await connection.entityManager
.createQueryBuilder(Post, "post")
.leftJoinAndSelect("post.categories", "categories")
.getMany();
expect(loadedPosts1).not.to.be.empty;
loadedPosts1.should.be.eql([{
id: 1,
title: "Hello Post",
categories: []
}]);
})));
it("should return empty array if its joined and nothing was found, but relations in empty results should be skipped", () => Promise.all(connections.map(async function(connection) {
const post = new Post();
post.title = "Hello Post";
await connection.entityManager.persist(post);
// check if ordering by main object works correctly
const loadedPosts1 = await connection.entityManager
.createQueryBuilder(Post, "post")
.leftJoinAndSelect("post.categories", "categories")
.leftJoinAndSelect("categories.authors", "authors")
.getMany();
expect(loadedPosts1).not.to.be.empty;
loadedPosts1.should.be.eql([{
id: 1,
title: "Hello Post",
categories: []
}]);
})));
});