Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: alter relation loader to use transforms when present (#8691)
Use column transforms when rendering queries during relation loading. Previously, transforms were ignored by the relation loader, causing unexpected values to be put into sql queries. Closes: #8690
- Loading branch information
1 parent
948d328
commit 2c2fb29
Showing
3 changed files
with
77 additions
and
6 deletions.
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,40 @@ | ||
import { | ||
Column, | ||
Entity, | ||
ManyToOne, | ||
OneToMany, | ||
JoinColumn, | ||
PrimaryColumn, | ||
} from "../../../../src"; | ||
|
||
// This is a contrived and silly example of a primary | ||
// column transformer but it's enough to show the issue. | ||
const WrappedIntTransformer = { | ||
from: (value: number) => `"${value}"`, | ||
to: (value: string | undefined | null) => value ? parseInt(value.slice(1, value.length - 1)) : null, | ||
} | ||
|
||
@Entity() | ||
export class User { | ||
@PrimaryColumn({ type: "int", transformer: WrappedIntTransformer, nullable: false }) | ||
id: string; | ||
|
||
@OneToMany(() => Photo, (photo) => photo.user) | ||
photos: Promise<Photo[]>; | ||
} | ||
|
||
@Entity() | ||
export class Photo { | ||
@PrimaryColumn({ type: "int", transformer: WrappedIntTransformer, nullable: false }) | ||
id: string; | ||
|
||
@Column({ type: "text" }) | ||
url: string; | ||
|
||
@Column({ type: "int", transformer: WrappedIntTransformer, nullable: false }) | ||
userId: string; | ||
|
||
@ManyToOne(() => User, (user) => user.photos) | ||
@JoinColumn({ name: "userId", referencedColumnName: "id" }) | ||
user: Promise<User>; | ||
} |
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,31 @@ | ||
import "reflect-metadata"; | ||
import { createTestingConnections, closeTestingConnections, reloadTestingDatabases } from "../../utils/test-utils"; | ||
import { Connection } from "../../../src/connection/Connection"; | ||
import { expect } from "chai"; | ||
import { getRepository } from "../../../src"; | ||
import { User, Photo } from "./entity/entities"; | ||
|
||
describe("github issues > #8690 Relations do not render primary key column values correctly when transformers present", () => { | ||
|
||
let connections: Connection[]; | ||
before(async () => connections = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
schemaCreate: true, | ||
dropSchema: true, | ||
})); | ||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should load relations correctly when primary columns have transformers", () => Promise.all(connections.map(async connection => { | ||
const userRepository = getRepository(User); | ||
const photoRepository = getRepository(Photo); | ||
const user = userRepository.create({id: `"1"`}); | ||
await userRepository.save(user); | ||
const photo = photoRepository.create({id: `"42"`, url: "example.com/photo1", userId: user.id}) | ||
await photoRepository.save(photo); | ||
const userPhotos = await user.photos; | ||
expect(userPhotos.length === 1) | ||
expect(userPhotos[0].id === photo.id) | ||
expect(userPhotos[0].userId === user.id) | ||
}))); | ||
}); |