Search code examples
loopbackjsloopback4

How to filter the result whose type is array in loopback 4?


import {Entity, model, property} from '@loopback/repository';

@model()
export class Misc extends Entity {
  @property({
    type: 'number',
    id: true,
    generated: true,
  })
  id?: number;

  @property({
    type: 'array',
    itemType: 'number',
    required: true,
  })
  members: number[];


  constructor(data?: Partial<Misc>) {
    super(data);
  }
}

export interface MiscRelations {
  // describe navigational properties here
}

export type MiscWithRelations = Misc & MiscRelations;

Above is the model for misc API. I am using PostgreSQL.

I have inserted data in the table. Result of GET request from this table is as following -

[
   {
      "id":1,
      "members":[
         1,
         2,
         3
      ]
   },
   {
      "id":2,
      "members":[
         1,
         2,
         3,
         4,
         5
      ]
   },
   {
      "id":3,
      "members":[
         10,
         20,
         30,
         40,
         50
      ]
   },
   {
      "id":4,
      "members":[
         100,
         200,
         300,
         400,
         500
      ]
   },
   {
      "id":5,
      "members":[
         1,
         2,
         3,
         500,
         1000,
         5000
      ]
   }
]

I want to get the records who have members with value 1, so I applied a filter like this -

http://localhost:3000/miscs?filter[where][inq][members]=1

But this isn't working. If there is no way to execute such a query then can I do some change in the model to adjust its type such that it can accept CSV values and also can filter those data?

Please help. Thanks in advance!


Solution

  • Finally, I found an answer. Regex can be used to match the record here like this

    filter[where][members][regexp]=1,|1]