Skip to content

Commit

Permalink
fix: find select object api should support false values #8796 (#8807)
Browse files Browse the repository at this point in the history
* fix: select supports false value instead of only undefined

* test: issue-8796

* refactor: add test description

* refactor: format code
  • Loading branch information
chfleury committed Mar 26, 2022
1 parent 002274c commit 9ac8e9e
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/query-builder/SelectQueryBuilder.ts
Expand Up @@ -3582,7 +3582,7 @@ export class SelectQueryBuilder<Entity>
embedPrefix?: string,
) {
for (let key in select) {
if (select[key] === undefined) continue
if (select[key] === undefined || select[key] === false) continue

const propertyPath = embedPrefix ? embedPrefix + "." + key : key
const column =
Expand Down
16 changes: 16 additions & 0 deletions test/github-issues/8796/entity/User.ts
@@ -0,0 +1,16 @@
import { Column, Entity, PrimaryColumn } from "../../../../src"

@Entity()
export class User {
@PrimaryColumn({ type: "int", nullable: false })
id: number

@Column()
firstName: string

@Column()
lastName: string

@Column()
github: string
}
57 changes: 57 additions & 0 deletions test/github-issues/8796/issue-8796.ts
@@ -0,0 +1,57 @@
import "reflect-metadata"
import {
createTestingConnections,
closeTestingConnections,
reloadTestingDatabases,
} from "../../utils/test-utils"
import { Connection } from "../../../src/connection/Connection"
import { expect } from "chai"
import { User } from "../8796/entity/User"

describe("github issues > #8796 New find select object api should support false values as expected", () => {
let connections: Connection[]

const user: User = {
id: 1,
firstName: "Christian",
lastName: "Fleury",
github: "chfleury",
}

const expectedUser: Partial<User> = {
id: 1,
firstName: "Christian",
}

before(
async () =>
(connections = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
schemaCreate: true,
dropSchema: true,
})),
)
beforeEach(() => reloadTestingDatabases(connections))
after(() => closeTestingConnections(connections))

it("should suport false value when selecting fields", () =>
Promise.all(
connections.map(async (connection) => {
const userRepository = connection.getRepository(User)

await userRepository.save(user)

const foundUser = await userRepository.find({
where: { id: 1 },
select: {
id: true,
firstName: true,
lastName: undefined,
github: false,
},
})

expect(foundUser[0]).to.deep.equal(expectedUser)
}),
))
})

0 comments on commit 9ac8e9e

Please sign in to comment.