3

How to user case insensitive where filter in loopback with postgresql. I have trued using

pattern = new RegExp('.*'+data+'.*', "i")

but not working. My code is

searchUsersAppointment(data): void {
    let cpr = /^\d+$/.test(data);
    let pattern = new RegExp('.*'+data+'.*', "i");
    let query = {};
    if(cpr){
        query = {where: {CPR: {like: data} } };
    }else{
        query = {where: {firstName: {like: pattern} } };
    }
    this.patientDetailsApi.find(query).subscribe(searchDetails => {
        this.searchDetails = searchDetails;
    })
}

Can any one help me to fix this issue.

Nijesh W
  • 39
  • 1
  • 6
  • 1
    @RahulSingh I got solution changed **query = {where: {firstName: {like: pattern} } };** to **query = {where: {firstName: {regexp: data+"/i"} } };** – Nijesh W Jan 31 '18 at 10:42

3 Answers3

4

There is no need to use Regex for case insensitive search in Loopback.

If you are using database that support ANSI SQL formats like MySql, PostgreSql you can use the following code snippet:

filter.where = {
  name: { 
    ilike: '%'+ search_string +'%'
  }
};

If you are using noSQL database like MongoDb use the following code snippet:

where = {
  name: {
    like: search_string,
    options: "i" 
  }
};
Nahid Ali
  • 636
  • 3
  • 11
1

Took me a day fiddling around with it since most answers seem to work for other specific connections.

Here's what worked for me:

Datasource: loopback-connector-oracle

where: { 
   property: { regexp: `/^${query}/i` }
}

REST API:

?filter={"where":{"userName":{"regexp":"/^query/i"}}}

This does the following validation

let str = 'john_smith'
console.log(/^john_smith/i.test(str)) //true
console.log(/^joHN_Smith/i.test(str)) //true
console.log(/^ohn_Smith/i.test(str2)) //false
console.log(/^John_mith/i.test(str2)) //false

RoboKy
  • 141
  • 1
  • 10
0

If you are using noSQL database like MongoDb with loopback4 use the following code snippet:

const pattern = new RegExp('^' + request.search + '.*', "i");
where: { 
   name: { regexp: pattern }
}