Skip to content

Commit

Permalink
fix: malformed query when selecting deeply nested embedded entities (#…
Browse files Browse the repository at this point in the history
…9273)

Update `SelectQueryBuilder.buildSelect` method to correctly build the property path for deeply nested embedded entities
  • Loading branch information
gabrielkim13 committed Sep 19, 2022
1 parent 48976c2 commit 83f7b88
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/query-builder/SelectQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3683,7 +3683,7 @@ export class SelectQueryBuilder<Entity extends ObjectLiteral>
select[key] as FindOptionsSelect<any>,
metadata,
alias,
key,
propertyPath,
)

// } else if (relation) {
Expand Down
32 changes: 32 additions & 0 deletions test/github-issues/9272/entity/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Entity, PrimaryGeneratedColumn, Column } from "../../../../src"

export class LatLong {
@Column()
latitude: number

@Column()
longitude: number
}

export class Address {
@Column(() => LatLong)
latLong: LatLong
}

@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number

@Column()
firstName: string

@Column()
lastName: string

@Column(() => Address)
address: Address

@Column()
age: number
}
88 changes: 88 additions & 0 deletions test/github-issues/9272/issue-9272.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import "reflect-metadata"
import {
createTestingConnections,
closeTestingConnections,
reloadTestingDatabases,
} from "../../utils/test-utils"
import { DataSource } from "../../../src/data-source/DataSource"
import { expect } from "chai"

import { Address, LatLong, User } from "./entity/User"

describe("github issues > #9272 Fix select on deeply nested embedded entities, using Repository API", () => {
let dataSources: DataSource[]
before(
async () =>
(dataSources = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
schemaCreate: true,
dropSchema: true,
enabledDrivers: [
"better-sqlite3",
"cockroachdb",
"mariadb",
"mssql",
"mysql",
"oracle",
"postgres",
"spanner",
"sqlite",
],
})),
)
beforeEach(() => reloadTestingDatabases(dataSources))
after(() => closeTestingConnections(dataSources))

it("should be able to pass select options for deeply nested embedded entities", () =>
Promise.all(
dataSources.map(async (dataSource) => {
// Arrange
const testUser = new User()
testUser.firstName = "Timber"
testUser.lastName = "Saw"
testUser.age = 25

const latLong = new LatLong()
latLong.latitude = -23
latLong.longitude = -46

const address = new Address()
address.latLong = latLong

testUser.address = address

await dataSource.manager.save(User, testUser)

// Act
const usersWithLatitudeOnly = await dataSource.manager.find(
User,
{
select: {
id: true,
address: {
latLong: {
latitude: true,
},
},
},
},
)

// Assert
expect(usersWithLatitudeOnly).to.have.length(1)

const [user] = usersWithLatitudeOnly

user.should.haveOwnProperty("id")
user.should.haveOwnProperty("address")
user.should.not.haveOwnProperty("firstName")
user.should.not.haveOwnProperty("lastName")
user.should.not.haveOwnProperty("age")

user.address.latLong.should.haveOwnProperty("latitude")
user.address.latLong.should.not.haveOwnProperty("longitude")

user.address.latLong.latitude.should.equal(-23)
}),
))
})

0 comments on commit 83f7b88

Please sign in to comment.