-
-
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.
Browse files
Browse the repository at this point in the history
* fix: transform values for FindOperators Closes: #9381 * refactor: simplify correction do not transform value, when it is a FindOperator Closes: #9381 * fix: transform value of FindOperator when it is not a FindOperator Closes: #9381 #9816 * fix: perform transformation on the array instead of each value in case of JsonContains * fix: remove anti pattern, correct functionality for operators without multiple parameter * fix: correct linting * fix: resolve issue when parameterValue is not an instanceof FindOperator --------- Co-authored-by: ke <ke@sbs.co.at>
- Loading branch information
Showing
5 changed files
with
202 additions
and
5 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
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, Generated, PrimaryColumn } from "../../../../src" | ||
|
||
const ID_TRANSFORMER = { | ||
from: (dbValue: number) => dbValue?.toString(), | ||
to: (entityValue: string) => | ||
entityValue ? Number(entityValue) : entityValue, | ||
} | ||
|
||
@Entity() | ||
export class ExampleEntity { | ||
@Generated("increment") | ||
@PrimaryColumn({ | ||
type: "integer", | ||
transformer: ID_TRANSFORMER, | ||
}) | ||
id: string | ||
} |
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,29 @@ | ||
import { Column, Entity, Generated, PrimaryColumn } from "../../../../src" | ||
|
||
const ID_TRANSFORMER = { | ||
from: (dbValue: number) => dbValue?.toString(), | ||
to: (entityValue: string) => | ||
entityValue ? Number(entityValue) : entityValue, | ||
} | ||
|
||
const JSON_TRANSFORMER = { | ||
to: (value: any) => ({ wrap: value }), | ||
from: (value: { wrap: any }) => value.wrap, | ||
} | ||
|
||
@Entity() | ||
export class JsonExampleEntity { | ||
constructor(value: any) { | ||
this.jsonvalue = value | ||
} | ||
|
||
@Generated("increment") | ||
@PrimaryColumn({ | ||
type: "integer", | ||
transformer: ID_TRANSFORMER, | ||
}) | ||
id: string | ||
|
||
@Column({ type: "jsonb", transformer: JSON_TRANSFORMER }) | ||
jsonvalue: any | ||
} |
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,124 @@ | ||
import "reflect-metadata" | ||
import { | ||
closeTestingConnections, | ||
createTestingConnections, | ||
} from "../../utils/test-utils" | ||
import { In, JsonContains, Not } from "../../../src" | ||
import { ExampleEntity } from "./entity/ExampleEntity" | ||
import { expect } from "chai" | ||
import { JsonExampleEntity } from "./entity/JsonExampleEntity" | ||
|
||
describe("github issues > #9381 The column option 《transformer》 affects the result of the query condition generation", () => { | ||
it("transform and find values", async () => { | ||
const dataSources = await createTestingConnections({ | ||
entities: [__dirname + "/entity/ExampleEntity{.js,.ts}"], | ||
schemaCreate: true, | ||
dropSchema: true, | ||
}) | ||
|
||
await Promise.all( | ||
dataSources.map(async (dataSource) => { | ||
let repository = dataSource.getRepository(ExampleEntity) | ||
await repository.save(new ExampleEntity()) | ||
await repository.save(new ExampleEntity()) | ||
await repository.save(new ExampleEntity()) | ||
await repository.save(new ExampleEntity()) | ||
await repository.save(new ExampleEntity()) | ||
const resultFindAll = await repository.find() | ||
expect(resultFindAll.length).to.be.eql(5) | ||
|
||
const resultTransformer = await repository.findBy({ | ||
id: Not(In(["1", "3", "5"])), | ||
}) | ||
expect(resultTransformer).to.be.eql([ | ||
{ | ||
id: "2", | ||
}, | ||
{ | ||
id: "4", | ||
}, | ||
]) | ||
const findEqualsTransformer = await repository.findOne({ | ||
where: { | ||
id: "1", | ||
}, | ||
}) | ||
expect(findEqualsTransformer).to.be.eql({ id: "1" }) | ||
}), | ||
) | ||
|
||
await closeTestingConnections(dataSources) | ||
}) | ||
|
||
it("transform json values and find values", async () => { | ||
const dataSources = await createTestingConnections({ | ||
entities: [__dirname + "/entity/JsonExampleEntity{.js,.ts}"], | ||
schemaCreate: true, | ||
dropSchema: true, | ||
enabledDrivers: ["postgres"], | ||
}) | ||
|
||
await Promise.all( | ||
dataSources.map(async (dataSource) => { | ||
let repository = dataSource.getRepository(JsonExampleEntity) | ||
await repository.save(new JsonExampleEntity({ foo: "bar1" })) | ||
await repository.save(new JsonExampleEntity({ foo: "bar2" })) | ||
await repository.save(new JsonExampleEntity({ foo: "bar3" })) | ||
await repository.save(new JsonExampleEntity({ foo: "bar4" })) | ||
await repository.save(new JsonExampleEntity({ foo: "bar5" })) | ||
await repository.save( | ||
new JsonExampleEntity({ foo: { bar: [5, 6, 7, 8] } }), | ||
) | ||
await repository.save(new JsonExampleEntity([5, 6, 7, 8])) | ||
|
||
const resultTransformer = await repository.findBy({ | ||
id: Not(In(["1", "3", "5", "6", "7"])), | ||
}) | ||
expect(resultTransformer).to.be.eql([ | ||
{ | ||
id: "2", | ||
jsonvalue: { | ||
foo: "bar2", | ||
}, | ||
}, | ||
{ | ||
id: "4", | ||
jsonvalue: { | ||
foo: "bar4", | ||
}, | ||
}, | ||
]) | ||
|
||
let result = await repository.findOneBy({ | ||
jsonvalue: JsonContains({ foo: "bar1" }), | ||
}) | ||
expect(result).to.be.eql({ | ||
id: "1", | ||
jsonvalue: { | ||
foo: "bar1", | ||
}, | ||
}) | ||
|
||
result = await repository.findOneBy({ | ||
jsonvalue: JsonContains({ foo: {} }), | ||
}) | ||
expect(result).to.be.eql({ | ||
id: "6", | ||
jsonvalue: { | ||
foo: { bar: [5, 6, 7, 8] }, | ||
}, | ||
}) | ||
|
||
result = await repository.findOneBy({ | ||
jsonvalue: JsonContains([5, 6, 7, 8] as any), | ||
}) | ||
expect(result).to.be.eql({ | ||
id: "7", | ||
jsonvalue: [5, 6, 7, 8], | ||
}) | ||
}), | ||
) | ||
await closeTestingConnections(dataSources) | ||
}) | ||
// you can add additional tests if needed | ||
}) |