Skip to content

New JsonContains operator breaks, if a column has a transformer #9816

@vlad-ostrovski

Description

@vlad-ostrovski

Issue description

If a column of type json or jsonb has a transformer on it, a simple findOneBy query will throw an exception because SelectQueryBuilder will transform the JsonContains operator itself using the transformer, instead of it's value property.

Expected Behavior

The following snippet should work correctly if jsonColumn is a column with a transformer

const result = await this.myRepository.findOneBy({
  jsonColumn: JsonContains({
    foo: 'bar',
  }),
});

Actual Behavior

The following snippet throws column with a transformer

const result = await this.myRepository.findOneBy({
  jsonColumn: JsonContains({
    foo: 'bar,
  }),
});
Unsupported FindOperator Function
TypeError: Unsupported FindOperator Function

Steps to reproduce

class MyType {
  foo!: string;
}

@Entity()
class MyEntity {
  @Column('jsonb', {
    transformer: {
      to: (value) => ({ wrap: value }),
      from: (value) => value.wrap,
    },
  })
  jsonColumn!: MyType;
}

const result = await this.myEntityRepository.findOneBy({
  jsonColumn: JsonContains({
    foo: 'bar,
  }),
});

My Environment

Dependency Version
Operating System Ubuntu 22.04.1 LTS
Node.js version 16.13.0
Typescript version 4.9.5
TypeORM version 0.3.12

Additional Context

The issue is due to the following code in SelectQueryBuilder.ts (line 4179):

// todo: we need to handle other operators as well?
let parameterValue = where[key]
if (InstanceChecker.isEqualOperator(where[key])) {
    parameterValue = where[key].value
}
if (column.transformer) {
    parameterValue = ApplyValueTransformers.transformTo(
        column.transformer,
        parameterValue,
    )
}

This code only checks for EqualOperator, but JsonContains is a FindOperator, so instead of transforming where[key].value, the whole operator (where[key]) is transformed, which of course ruins the operator.

I guess the answer for that //todo at the top is yes ;)

Relevant Database Driver(s)

  • aurora-mysql
  • aurora-postgres
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • spanner
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

Yes, I have the time, and I know how to start.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions