Search code examples
postgresqlnestjstypeorm

Not able to search through character varying[] array column


My postgres database table has a column location which is character varying[]. In my nestjs entity of the table I have following for the location column-

 @Column("character varying",{array:true})
    location: string[];

What I am trying to do is search the rows having passed parameter as locations. This is the raw query which is giving me appropriate results-

select * from blogs where language @> '{"Spanish","English"}'

In my nestjs service, how can I achieve the above query? I tried doing this-

return await this.blogsRepo.find({
  where: [
    {
      location: Any(body.locations)
    }
  ]
})

body.locations is an array like this-

body.locations = ["Spanish","English"]

The above typeorm solution gives me following error-

'could not find array type for data type character varying[]'

What could be the possible solution for this? I will love a typeorm solution as I have kept raw query execution as my last option.

Thanks in advance,


Solution

  • As per the typeorm documentations, there is no way/ built in typeorm operator equivalent to @> operator.

    So the only way to achieve what I wanted is to generate an entire query and then execute it using Repository.query() method.

    Seems to work so far!