typeorm: TypeOrm 3: search is broken in array of enums (postgres)

Issue Description

Search by array property is broken

Expected Behavior

Same behavior as in v2

Actual Behavior

throws type error

Steps to Reproduce

export enum AdminRole {
  ADMIN = "ADMIN",
  MODERATOR = "MODERATOR",
}

@Entity({ schema: ns, name: "admin" })
export class AdminEntity extends BaseEntity {
  @PrimaryGeneratedColumn()
  public id: number;

  @Column({ type: "varchar" })
  public displayName: string;

  @Column({
    type: "enum",
    enum: AdminRole,
    array: true,
  })
  public adminRoles: Array<AdminRole>;
}

v2 worked fine

  public async autocomplete(): Promise<Array<AdminEntity>> {
    return this.adminEntityRepository.find({
      where: { adminRoles: [AdminRole.ADMIN] },
      select: ["id", "displayName"],
    });
  }

v3 shows type error

  public async autocomplete(): Promise<Array<AdminEntity>> {
    return this.adminEntityRepository.find({
      where: { adminRoles: [AdminRole.ADMIN] },
      select: {
        id: true,
        displayName: true,
      },
    });
  }
TS2322: Type '{ adminRoles: AdminRole[]; }' is not assignable to type 'FindOptionsWhere<AdminEntity> | FindOptionsWhere<AdminEntity>[] | undefined'.   Types of property 'adminRoles' are incompatible.     Type 'AdminRole[]' is not assignable to type 'AdminRole | FindOperator<AdminRole.ADMIN> | FindOperator<AdminRole.MODERATOR> | undefined'.

I tried to fix with

  public async autocomplete(): Promise<Array<AdminEntity>> {
    return this.adminEntityRepository.find({
      where: {
        adminRoles: In( [AdminRole.ADMIN] ),
      },
      select: {
        id: true,
        displayName: true,
      },
    });
  }

but it throws Postgres error

SELECT "AdminEntity"."id" AS "AdminEntity_id", "AdminEntity"."display_name" AS "AdminEntity_display_name"
FROM "xxx"."admin" "AdminEntity"
WHERE ("AdminEntity"."admin_roles" IN ('ADMIN'))
[22P02] ERROR: malformed array literal: "ADMIN" Detail: Array value must start with "{" or dimension information. Position: 181

Have no idea how to fix this, please help

My Environment

Dependency Version
Operating System
Node.js version v16.7.0
Typescript version 4.6.2
TypeORM version 0.3.0
NestJS 8.4.1

Additional Context

Relevant Database Driver(s)

DB Type Reproducible
aurora-mysql no
aurora-postgres no
better-sqlite3 no
cockroachdb no
cordova no
expo no
mongodb no
mysql no
nativescript no
oracle no
postgres yes
react-native no
sap no
sqlite no
sqlite-abstract no
sqljs no
sqlserver no

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

  • ✖️ Yes, I have the time, and I know how to start.
  • ✖️ Yes, I have the time, but I don’t know how to start. I would need guidance.
  • ✖️ No, I don’t have the time, but I can support (using donations) development.
  • ✖️ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 15 (5 by maintainers)

Most upvoted comments

Variations of OR syntax

      where: [{ adminRoles: AdminRole.ADMIN }],
      where: { adminRoles: `{${adminRoles.join(",")}}` },

does not work