-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: malformed query when selecting deeply nested embedded entities (#…
…9273) Update `SelectQueryBuilder.buildSelect` method to correctly build the property path for deeply nested embedded entities
- Loading branch information
1 parent
48976c2
commit 83f7b88
Showing
3 changed files
with
121 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}), | ||
)) | ||
}) |