From b1a01074c99b1fee9b2da2c056f5f773367d391b Mon Sep 17 00:00:00 2001 From: Samuel <52704828+devx-opensource@users.noreply.github.com> Date: Wed, 30 Mar 2022 17:28:31 +0200 Subject: [PATCH] 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 --- src/driver/postgres/PostgresDriver.ts | 4 +- .../entity/hstore-entity.ts | 13 +++++ .../hstore-allow-transformer.ts | 47 +++++++++++++++++++ .../test-transformer.ts | 8 ++++ 4 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 test/other-issues/hstore-allow-transformer/entity/hstore-entity.ts create mode 100644 test/other-issues/hstore-allow-transformer/hstore-allow-transformer.ts create mode 100644 test/other-issues/hstore-allow-transformer/test-transformer.ts diff --git a/src/driver/postgres/PostgresDriver.ts b/src/driver/postgres/PostgresDriver.ts index 81ce4c40cd..9c26dc4a45 100644 --- a/src/driver/postgres/PostgresDriver.ts +++ b/src/driver/postgres/PostgresDriver.ts @@ -723,9 +723,7 @@ export class PostgresDriver implements Driver { return "" }, ) - return object - } else { - return value + value = object } } else if (columnMetadata.type === "simple-array") { value = DateUtils.stringToSimpleArray(value) diff --git a/test/other-issues/hstore-allow-transformer/entity/hstore-entity.ts b/test/other-issues/hstore-allow-transformer/entity/hstore-entity.ts new file mode 100644 index 0000000000..1c17c9bed6 --- /dev/null +++ b/test/other-issues/hstore-allow-transformer/entity/hstore-entity.ts @@ -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 +} diff --git a/test/other-issues/hstore-allow-transformer/hstore-allow-transformer.ts b/test/other-issues/hstore-allow-transformer/hstore-allow-transformer.ts new file mode 100644 index 0000000000..2df4f64b61 --- /dev/null +++ b/test/other-issues/hstore-allow-transformer/hstore-allow-transformer.ts @@ -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") + }), + )) +}) diff --git a/test/other-issues/hstore-allow-transformer/test-transformer.ts b/test/other-issues/hstore-allow-transformer/test-transformer.ts new file mode 100644 index 0000000000..474440c16c --- /dev/null +++ b/test/other-issues/hstore-allow-transformer/test-transformer.ts @@ -0,0 +1,8 @@ +export const testTransformer = { + to(data: any) { + return data + }, + from(data: any) { + return data.en_US + }, +}