I am using loopback
as a backend now I wish to be able to search across all fields in the table
For instance, take the following table fields:
id, name, title, created, updated
Now say I want to search across the table using the following string "nomad
"
However, I am unsure how to structure the query
I have attempted:
{"where": {"keywords": {"inq": ["nomad"]}}}
However, this just returns all results
So how do I do this? :)
If it matters my database is a postgresql
Loopback is not where you need to look at. It's just a framework with universal ORM / ODM to connect to your DB and expose is with a query language over rest. You probably need in your case to add a text
index in your postgresql database. By indexing all your desired properties into a text index, you'll be able to perform search in your DB.
Here is the documentation. https://www.postgresql.org/docs/9.5/static/textsearch.html
You can still achieve your goal using loopback ORM with something like
{"where": {"prop1": {"regex": "nomad"}, "prop2": {"regex": "nomad"}}}
but your DB will die in few queries ...
You can use loopback's case insensitive regex to search the database for the value.
Example:
Fields: [id, name, title, created, updated]
let whereCondition = {id: {regexp: '/nomad/i'}, name: {regexp: '/nomad/i'}}
app.models.ModelName.find({
where: whereCondition
});
Refer the loopback documentation url below: https://loopback.io/doc/en/lb2/Where-filter.html#regular-expressions
You will need to get a querying function that will transverse through your DB and return back your data from the table.
Eg:
Account.find({where: {name: 'John'}, limit: 3}, function(err, accounts) { /* ... */ });
Or
{where: {property: value}}
You can get to discover more from Loopback Querying data, Loopback Where filter and postgresql RETURN QUERY.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With