diff --git a/package.json b/package.json index 76ebee258..bd245e97e 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/query-builder/transformer/RawSqlResultsToEntityTransformer.ts b/src/query-builder/transformer/RawSqlResultsToEntityTransformer.ts index 3b7300206..e4e575ccf 100644 --- a/src/query-builder/transformer/RawSqlResultsToEntityTransformer.ts +++ b/src/query-builder/transformer/RawSqlResultsToEntityTransformer.ts @@ -156,7 +156,9 @@ export class RawSqlResultsToEntityTransformer { } else { entity[propertyName] = result; } - hasData = true; + + if (!isResultArray || result.length > 0) + hasData = true; } } diff --git a/test/other-issues/join-empty-relations/entity/Author.ts b/test/other-issues/join-empty-relations/entity/Author.ts new file mode 100644 index 000000000..1b619e667 --- /dev/null +++ b/test/other-issues/join-empty-relations/entity/Author.ts @@ -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; + +} \ No newline at end of file diff --git a/test/other-issues/join-empty-relations/entity/Category.ts b/test/other-issues/join-empty-relations/entity/Category.ts new file mode 100644 index 000000000..6470aa652 --- /dev/null +++ b/test/other-issues/join-empty-relations/entity/Category.ts @@ -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[]; + +} \ No newline at end of file diff --git a/test/other-issues/join-empty-relations/entity/Post.ts b/test/other-issues/join-empty-relations/entity/Post.ts new file mode 100644 index 000000000..5650dd4d5 --- /dev/null +++ b/test/other-issues/join-empty-relations/entity/Post.ts @@ -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[]; + +} \ No newline at end of file diff --git a/test/other-issues/join-empty-relations/join-empty-relations.ts b/test/other-issues/join-empty-relations/join-empty-relations.ts new file mode 100644 index 000000000..8fc0bc484 --- /dev/null +++ b/test/other-issues/join-empty-relations/join-empty-relations.ts @@ -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: [] + }]); + + }))); + +}); \ No newline at end of file