1

I have adonis back end (that run knex under the hood) that sending some data to front end according user filtering.

I need to implement one of the filtering parameters with something like startWith js method, to check if the text that user submitted is the beginning of the name of data at backend, and not appears in the middle of it

Currently I using

this.where('companies.name', 'like', params.name)

What checks also for middle appearance.

Ideally I want to do something like

this.where('companies.name', 'startWith', params.name)

But this syntax is invalid.

Are there is something similar to this use case? Couldn't find something in Knex doc that seems to do the trick for my issue

Just for clarify, I join few tables in this query and do some where filtering, so a solution follow this syntax will be the best, but open for more solutions

Thanks in advance!

Hagai Harari
  • 2,767
  • 3
  • 11
  • 23

1 Answers1

4

It depends on what kind of DB are you using, but must of the SQL db's supports wildcards.

You can read more info here.

this.where('companies.name', 'like', `${params.name}%`)
// -------------------------------------------------^

This % character means "A substitute for zero or more characters".

felixmosh
  • 32,615
  • 9
  • 69
  • 88