Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: allow hstore type to use transformers in driver postgres (#8823)
The HSTORE type is the only type bypassing the transformer column options. This fix makes the option available to this column type as well. Co-authored-by: Samuel Roy <sam@pricemetry.com>
- Loading branch information
1 parent
ed06f4c
commit b1a0107
Showing
4 changed files
with
69 additions
and
3 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
13 changes: 13 additions & 0 deletions
13
test/other-issues/hstore-allow-transformer/entity/hstore-entity.ts
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,13 @@ | ||
import { Entity } from "../../../../src/decorator/entity/Entity" | ||
import { Column } from "../../../../src/decorator/columns/Column" | ||
import { PrimaryGeneratedColumn } from "../../../../src/decorator/columns/PrimaryGeneratedColumn" | ||
import { testTransformer } from "../test-transformer" | ||
|
||
@Entity() | ||
export class DummyHSTOREEntity { | ||
@PrimaryGeneratedColumn() | ||
id: number | ||
|
||
@Column({ type: "hstore", transformer: testTransformer }) | ||
translation: object | ||
} |
47 changes: 47 additions & 0 deletions
47
test/other-issues/hstore-allow-transformer/hstore-allow-transformer.ts
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,47 @@ | ||
import "../../utils/test-setup" | ||
import { | ||
createTestingConnections, | ||
closeTestingConnections, | ||
reloadTestingDatabases, | ||
} from "../../utils/test-utils" | ||
import { DataSource } from "../../../src" | ||
import { expect } from "chai" | ||
import { DummyHSTOREEntity } from "./entity/hstore-entity" | ||
|
||
describe("other issues > allow HSTORE column type to use transformers", () => { | ||
let connections: DataSource[] | ||
before( | ||
async () => | ||
(connections = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
schemaCreate: true, | ||
dropSchema: true, | ||
enabledDrivers: ["postgres"], | ||
})), | ||
) | ||
beforeEach(() => reloadTestingDatabases(connections)) | ||
after(() => closeTestingConnections(connections)) | ||
|
||
it("should use the transformer set in the column options", () => | ||
Promise.all( | ||
connections.map(async (connection) => { | ||
const repository = connection.getRepository(DummyHSTOREEntity) | ||
|
||
const translation = { | ||
en_US: "hello", | ||
fr_FR: "salut", | ||
} | ||
|
||
const dummy = repository.create({ | ||
translation, | ||
}) | ||
|
||
await repository.save(dummy) | ||
|
||
const dummyEntity = await repository.findOneByOrFail({ | ||
id: dummy.id, | ||
}) | ||
expect(dummyEntity.translation).to.equal("hello") | ||
}), | ||
)) | ||
}) |
8 changes: 8 additions & 0 deletions
8
test/other-issues/hstore-allow-transformer/test-transformer.ts
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,8 @@ | ||
export const testTransformer = { | ||
to(data: any) { | ||
return data | ||
}, | ||
from(data: any) { | ||
return data.en_US | ||
}, | ||
} |