-
-
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: don't transform json(b) column value when computing update changes
- Loading branch information
dimitryvolkov
committed
Oct 17, 2020
1 parent
dee1758
commit 16c962b
Showing
5 changed files
with
104 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
17 changes: 17 additions & 0 deletions
17
test/other-issues/transformed-json-column-update-compute/entity/json-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,17 @@ | ||
import { Entity } from "../../../../src/decorator/entity/Entity"; | ||
import { Column } from "../../../../src/decorator/columns/Column"; | ||
import { PrimaryGeneratedColumn } from "../../../../src/decorator/columns/PrimaryGeneratedColumn"; | ||
import { VersionColumn } from "../../../../src"; | ||
import { testTransformer } from "../test-transformer"; | ||
|
||
@Entity() | ||
export class DummyJSONEntity { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@VersionColumn() | ||
version: number; | ||
|
||
@Column({ type: "json", transformer: testTransformer }) | ||
value: Record<string, any>; | ||
} |
17 changes: 17 additions & 0 deletions
17
test/other-issues/transformed-json-column-update-compute/entity/jsonb-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,17 @@ | ||
import { Entity } from "../../../../src/decorator/entity/Entity"; | ||
import { Column } from "../../../../src/decorator/columns/Column"; | ||
import { PrimaryGeneratedColumn } from "../../../../src/decorator/columns/PrimaryGeneratedColumn"; | ||
import { VersionColumn } from "../../../../src"; | ||
import { testTransformer } from "../test-transformer"; | ||
|
||
@Entity() | ||
export class DummyJSONBEntity { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@VersionColumn() | ||
version: number; | ||
|
||
@Column({ type: "jsonb", transformer: testTransformer }) | ||
value: Record<string, any>; | ||
} |
14 changes: 14 additions & 0 deletions
14
test/other-issues/transformed-json-column-update-compute/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,14 @@ | ||
export const testTransformer = { | ||
to(data: any) { | ||
if ("secretProperty" in data) { | ||
data.secretProperty = `secret-${data.secretProperty}`; | ||
} | ||
return data; | ||
}, | ||
from(data: any) { | ||
if ("secretProperty" in data) { | ||
data.secretProperty = data.secretProperty.split("-")[1]; | ||
} | ||
return data; | ||
} | ||
}; |
53 changes: 53 additions & 0 deletions
53
...r-issues/transformed-json-column-update-compute/transformed-json-column-update-compute.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,53 @@ | ||
import "reflect-metadata"; | ||
import { createTestingConnections, closeTestingConnections, reloadTestingDatabases } from "../../utils/test-utils"; | ||
import { Connection } from "../../../src/connection/Connection"; | ||
import { expect } from "chai"; | ||
import { DummyJSONEntity } from "./entity/json-entity"; | ||
import { DummyJSONBEntity } from "./entity/jsonb-entity"; | ||
|
||
describe("other issues > correctly compute change for transformed json / jsonb columns", () => { | ||
|
||
let connections: Connection[]; | ||
before(async () => connections = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
schemaCreate: true, | ||
dropSchema: true, | ||
enabledDrivers: ["postgres"] | ||
})); | ||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should not update entity if transformed JSON column did not change", () => Promise.all(connections.map(async connection => { | ||
const repository = connection.getRepository(DummyJSONEntity); | ||
|
||
const dummy = repository.create({ | ||
value: { | ||
secretProperty: "hello" | ||
}, | ||
}); | ||
|
||
await repository.save(dummy); | ||
|
||
await repository.save(dummy); | ||
|
||
const dummyEntity = await repository.findOneOrFail(dummy.id); | ||
expect(dummyEntity.version).to.equal(1); | ||
}))); | ||
|
||
it("should not update entity if transformed JSONB column did not change", () => Promise.all(connections.map(async connection => { | ||
const repository = connection.getRepository(DummyJSONBEntity); | ||
|
||
const dummy = repository.create({ | ||
value: { | ||
secretProperty: "hello" | ||
}, | ||
}); | ||
|
||
await repository.save(dummy); | ||
|
||
await repository.save(dummy); | ||
|
||
const dummyEntity = await repository.findOneOrFail(dummy.id); | ||
expect(dummyEntity.version).to.equal(1); | ||
}))); | ||
}); |