2019-10-18 18:20:43 +08:00
# Parameters
2019-05-06 16:17:16 +02:00
2019-10-09 12:32:58 +02:00
## Concept
2019-05-06 16:17:16 +02:00
2020-02-08 18:23:47 +01:00
You can use `strapi-utils` to parse the query params to Strapi's standard filters programmatically if you need it.
2019-05-06 16:17:16 +02:00
2019-10-09 12:32:58 +02:00
## Extracting requests filters
2019-05-06 16:17:16 +02:00
2020-02-08 18:23:47 +01:00
To transform the query params to Strapi's standard filters, you can use the `convertRestQueryParams` function from [strapi-utils ](../global-strapi/api-reference.md#strapiutils ).
2019-05-06 16:17:16 +02:00
```js
const { convertRestQueryParams } = require('strapi-utils');
module.exports = {
// when sending a request like GET /products?_sort:id& id=1
fetchExpensiveProducts: (params, populate) => {
const filters = convertRestQueryParams(params);
/**
* filters = {
* start: 0,
* limit: 10,
* sort: [{ field: 'id', order: 'desc' }],
* where: [
* { field: 'id', operator: 'eq', value: 1 },
* ]
* }
*/
// do sth with them
},
};
```
2019-10-09 12:32:58 +02:00
## Querying data
2019-05-06 16:17:16 +02:00
2020-03-22 13:28:03 -04:00
We added a new filters API to query the database.
2019-05-06 16:17:16 +02:00
```js
const { convertRestQueryParams, buildQuery } = require('strapi-utils');
module.exports = {
find: async (ctx) => {
// Convert params.
const filter = convertRestQueryParams(ctx.request.query);
return buildQuery({
model: Article
filters,
populate: []
});
};
};
```
2019-09-20 12:44:24 +02:00
### SQL databases (bookshelf)
2019-05-06 16:17:16 +02:00
2020-02-08 18:23:47 +01:00
If you are using a SQL database, calling `buildQuery` will return a [`Bookshelf Query` ](https://bookshelfjs.org/api.html ) on which you can call other functions (e.g. `count` ).
2019-05-06 16:17:16 +02:00
2019-10-09 12:32:58 +02:00
### Mongo database
2019-05-06 16:17:16 +02:00
2020-02-08 18:23:47 +01:00
If you are using a mongo database, calling `buildQuery` returns either a [`Mongoose Query` ](https://mongoosejs.com/docs/api.html#Query ) or a custom query when used with deep filtering.
2019-05-06 16:17:16 +02:00
2019-10-09 12:32:58 +02:00
#### Custom Query
2019-05-06 16:17:16 +02:00
2020-02-08 18:23:47 +01:00
When using the deep filtering feature with mongo, we build an aggregation query to avoid too many round-trips to the Mongo DB.
2020-03-22 13:28:03 -04:00
Doing that means we don't get a Mongoose object as a response but instead a plain JS Object. This brings some issues, like no virtual fields available and no Mongoose lifecycles.
2019-05-06 16:17:16 +02:00
To deliver the best possible experience, we decided to rehydrate the Mongoose models, forcing us to override the Mongoose query
```js
const query = buildQuery({
model: Product, // you can use any models from strapi.models or strapi.plugins[pluginName].models
filters: { limit: 10 },
populate: [],
});
```
returns a query with the following functions
2020-02-08 18:23:47 +01:00
- `count` => Returns an integer equal to the number of matching entities.
- `lean` => Returns the matching elements as Objects.
2020-03-22 13:28:03 -04:00
- `then(onSuccess, onFailure)` => Calls the onSuccess with an array of Mongoose objects.
2020-02-08 18:23:47 +01:00
- `catch(onError)` => Promise catch.
- `group(options)` => Calls the aggregation group function of Mongoose.