387 lines
7.0 KiB
Markdown
Raw Normal View History

2019-05-16 21:37:45 +02:00
# Queries
2019-08-12 16:58:18 +02:00
Strapi provides a utility function `strapi.query` to make database queries.
2019-05-16 21:37:45 +02:00
2019-09-24 17:27:02 +02:00
You can just call `strapi.query('modelName', 'pluginName')` to access the query API for any model.
2019-05-16 21:37:45 +02:00
These queries handle for you specific Strapi features like `components`, `dynamic zones`, `filters` and `search`.
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
## API Reference
2019-05-16 21:37:45 +02:00
2019-11-07 12:05:39 +01:00
:::: tabs
2019-05-16 21:37:45 +02:00
2019-11-07 12:05:39 +01:00
::: tab find
### `find`
This method returns a list of entries matching Strapi filters.
2019-08-12 16:58:18 +02:00
You can also pass a populate option to specify which relations you want to be populated.
#### Examples
**Find by id**:
2019-05-16 21:37:45 +02:00
```js
strapi.query('restaurant').find({ id: 1 });
2019-08-12 16:58:18 +02:00
```
2019-05-16 21:37:45 +02:00
**Find by in IN, with a limit**:
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
```js
strapi.query('restaurant').find({ _limit: 10, id_in: [1, 2] });
2019-08-12 16:58:18 +02:00
```
2019-05-16 21:37:45 +02:00
**Find by date orderBy name**:
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
```js
strapi
2019-09-24 17:27:02 +02:00
.query('restaurant')
.find({ date_gt: '2019-01-01T00:00:00Z', _sort: 'name:desc' });
2019-08-12 16:58:18 +02:00
```
2019-05-16 21:37:45 +02:00
**Find by id not in and populate a relation. Skip the first ten results**
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
```js
strapi
.query('restaurant')
.find({ id_nin: [1], _start: 10 }, ['category', 'category.name']);
2019-08-12 16:58:18 +02:00
```
2019-05-16 21:37:45 +02:00
:::
2019-05-16 21:37:45 +02:00
2019-11-07 12:05:39 +01:00
::: tab findOne
### `findOne`
This method returns the first entry matching some basic params.
2019-08-12 16:58:18 +02:00
You can also pass a populate option to specify which relations you want to be populated.
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
#### Examples
2019-05-16 21:37:45 +02:00
**Find one by id**:
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
```js
strapi.query('restaurant').findOne({ id: 1 });
2019-08-12 16:58:18 +02:00
```
2019-05-16 21:37:45 +02:00
**Find one by name**:
2019-08-12 16:58:18 +02:00
```js
strapi.query('restaurant').findOne({ name: 'restaurant name' });
2019-05-16 21:37:45 +02:00
```
**Find one by name and creation_date**:
2019-05-16 21:37:45 +02:00
```js
2019-08-12 16:58:18 +02:00
strapi
2019-09-24 17:27:02 +02:00
.query('restaurant')
.findOne({ name: 'restaurant name', date: '2019-01-01T00:00:00Z' });
2019-08-12 16:58:18 +02:00
```
2019-05-16 21:37:45 +02:00
**Find one by id and populate a relation**
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
```js
strapi.query('restaurant').findOne({ id: 1 }, ['category', 'category.name']);
2019-08-12 16:58:18 +02:00
```
2019-05-16 21:37:45 +02:00
:::
2019-11-07 12:05:39 +01:00
::: tab create
2019-08-12 16:58:18 +02:00
### `create`
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
Creates an entry in the database and returns the entry.
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
#### Example
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
```js
2019-09-24 17:27:02 +02:00
strapi.query('restaurant').create({
name: 'restaurant name',
// this is a dynamiczone field. the order is persisted in db.
content: [
{
__component: 'blog.rich-text',
2019-12-09 16:38:41 +01:00
title: 'Some title',
subTitle: 'Some sub title',
},
{
__component: 'blog.quote',
quote: 'Some interesting quote',
author: 1,
},
],
// this is a component field. the order is persisted in db.
2019-09-26 12:21:56 +02:00
opening_hours: [
2019-08-12 16:58:18 +02:00
{
2019-09-24 17:27:02 +02:00
day_interval: 'Mon',
opening_hour: '7:00 PM',
closing_hour: '11:00 PM',
2019-08-12 16:58:18 +02:00
},
{
2019-09-24 17:27:02 +02:00
day_interval: 'Tue',
opening_hour: '7:00 PM',
closing_hour: '11:00 PM',
2019-08-12 16:58:18 +02:00
},
],
// pass the id of a media to link it to the entry
2019-09-24 17:27:02 +02:00
cover: 1,
2019-08-12 16:58:18 +02:00
// automatically creates the relations when passing the ids in the field
2019-09-24 17:27:02 +02:00
reviews: [1, 2, 3],
2019-08-12 16:58:18 +02:00
});
```
2019-05-16 21:37:45 +02:00
:::
2019-11-07 12:05:39 +01:00
::: tab update
2019-08-12 16:58:18 +02:00
### `update`
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
Updates an entry in the database and returns the entry.
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
#### Examples
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
**Update by id**
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
```js
2019-09-24 17:27:02 +02:00
strapi.query('restaurant').update(
2019-08-12 16:58:18 +02:00
{ id: 1 },
{
2019-09-24 17:27:02 +02:00
name: 'restaurant name',
content: [
{
__component: 'blog.rich-text',
2019-12-09 16:38:41 +01:00
title: 'Some title',
subTitle: 'Some sub title',
},
{
__component: 'blog.quote',
quote: 'Some interesting quote',
author: 1,
},
],
2019-09-26 12:21:56 +02:00
opening_hours: [
2019-08-12 16:58:18 +02:00
{
2019-09-24 17:27:02 +02:00
day_interval: 'Mon',
opening_hour: '7:00 PM',
closing_hour: '11:00 PM',
2019-08-12 16:58:18 +02:00
},
{
2019-09-24 17:27:02 +02:00
day_interval: 'Tue',
opening_hour: '7:00 PM',
closing_hour: '11:00 PM',
2019-08-12 16:58:18 +02:00
},
],
// pass the id of a media to link it to the entry
2019-09-24 17:27:02 +02:00
cover: 1,
2019-08-12 16:58:18 +02:00
// automatically creates the relations when passing the ids in the field
2019-09-24 17:27:02 +02:00
reviews: [1, 2, 3],
2019-08-12 16:58:18 +02:00
}
);
```
2019-05-16 21:37:45 +02:00
When updating an entry with its components or dynamiczones beware that if you send the components without any `id` the previous components will be deleted and replaced. You can update the components by sending their `id` with the rest of the fields:
2019-05-16 21:37:45 +02:00
**Update by id and update previous components**
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
```js
2019-09-24 17:27:02 +02:00
strapi.query('restaurant').update(
2019-08-12 16:58:18 +02:00
{ id: 1 },
{
2019-09-24 17:27:02 +02:00
name: 'Mytitle',
content: [
{
__component: 'blog.rich-text',
id: 1,
2019-12-09 16:38:41 +01:00
title: 'Some title',
subTitle: 'Some sub title',
},
{
__component: 'blog.quote',
id: 1,
quote: 'Some interesting quote',
author: 1,
},
],
2019-09-26 12:21:56 +02:00
opening_hours: [
2019-08-12 16:58:18 +02:00
{
2019-09-24 17:27:02 +02:00
id: 2,
day_interval: 'Mon',
opening_hour: '7:00 PM',
closing_hour: '11:00 PM',
2019-08-12 16:58:18 +02:00
},
{
2019-09-24 17:27:02 +02:00
id: 1,
day_interval: 'Tue',
opening_hour: '7:00 PM',
closing_hour: '11:00 PM',
},
2019-08-12 16:58:18 +02:00
],
// pass the id of a media to link it to the entry
2019-09-24 17:27:02 +02:00
cover: 1,
2019-08-12 16:58:18 +02:00
// automatically creates the relations when passing the ids in the field
2019-09-24 17:27:02 +02:00
reviews: [1, 2, 3],
2019-08-12 16:58:18 +02:00
}
);
```
2019-05-16 21:37:45 +02:00
2019-09-24 17:27:02 +02:00
**Partial update by name**
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
```js
2019-09-24 17:27:02 +02:00
strapi.query('restaurant').update(
{ title: 'specific name' },
2019-08-12 16:58:18 +02:00
{
2019-09-24 17:27:02 +02:00
title: 'restaurant name',
2019-08-12 16:58:18 +02:00
}
);
```
2019-05-16 21:37:45 +02:00
:::
2019-11-07 12:05:39 +01:00
::: tab delete
2019-08-12 16:58:18 +02:00
### `delete`
2019-05-16 21:37:45 +02:00
2019-08-13 10:41:11 +02:00
Deletes and entry and return its value before deletion.
2019-08-12 16:58:18 +02:00
You can delete multiple entries at once with the passed params.
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
#### Examples
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
**Delete one by id**
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
```js
2019-09-24 17:27:02 +02:00
strapi.query('restaurant').delete({ id: 1 });
2019-08-12 16:58:18 +02:00
```
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
**Delete multiple by field**
```js
2019-09-24 17:27:02 +02:00
strapi.query('restaurant').delete({ district: '_18th' });
2019-05-16 21:37:45 +02:00
```
:::
2019-11-07 12:05:39 +01:00
::: tab count
2019-08-12 16:58:18 +02:00
### `count`
2019-05-16 21:37:45 +02:00
Returns the count of entries matching Strapi filters.
#### Examples
2019-09-24 17:27:02 +02:00
**Count by district**
```js
2019-09-24 17:27:02 +02:00
strapi.query('restaurant').count({ district: '_1st' });
```
2019-09-24 17:27:02 +02:00
**Count by name contains**
```js
2019-09-24 17:27:02 +02:00
strapi.query('restaurant').count({ name_contains: 'food' });
```
**Count by date less than**
```js
2019-09-24 17:27:02 +02:00
strapi.query('restaurant').count({ date_lt: '2019-08-01T00:00:00Z' });
```
:::
2019-11-07 12:05:39 +01:00
::: tab search
2019-08-12 16:58:18 +02:00
### `search`
2019-05-16 21:37:45 +02:00
2019-08-13 10:41:11 +02:00
Returns entries based on a search on all fields allowing it. (this feature will return all entries on sqlite).
#### Examples
**Search first ten starting at 20**
```js
2019-09-24 17:27:02 +02:00
strapi
.query('restaurant')
.search({ _q: 'my search query', _limit: 10, _start: 20 });
```
**Search and sort**
```js
strapi
2019-09-24 17:27:02 +02:00
.query('restaurant')
.search({ _q: 'my search query', _limit: 100, _sort: 'date:desc' });
```
:::
2019-11-07 12:05:39 +01:00
::: tab countSearch
2019-08-12 16:58:18 +02:00
### `countSearch`
2019-05-16 21:37:45 +02:00
2019-08-13 10:41:11 +02:00
Returns the total count of entries based on a search. (this feature will return all entries on sqlite).
#### Example
```js
2019-09-24 17:27:02 +02:00
strapi.query('restaurant').countSearch({ _q: 'my search query' });
```
:::
::::
2019-08-12 16:58:18 +02:00
## Custom Queries
2019-05-16 21:37:45 +02:00
When you want to customize your services or create new ones you will have to build your queries with the underlying ORM models.
2019-05-16 21:37:45 +02:00
To access the underlying model:
```js
strapi.query(modelName, plugin).model;
```
2019-05-16 21:37:45 +02:00
Then you can run any queries available on the model. You should refer to the specific ORM documentation for more details:
2019-05-16 21:37:45 +02:00
2019-11-07 12:05:39 +01:00
:::: tabs
2019-11-07 12:05:39 +01:00
::: tab Bookshelf
2019-08-12 16:58:18 +02:00
### Bookshelf
Documentation: [https://bookshelfjs.org/](https://bookshelfjs.org/)
2019-08-12 16:58:18 +02:00
**Example**
2019-08-12 16:58:18 +02:00
```js
2019-09-06 11:36:46 +02:00
const result = await strapi
2019-09-24 17:27:02 +02:00
.query('restaurant')
.model.query(qb => {
qb.where('id', 1);
})
.fetch();
2019-09-06 11:36:46 +02:00
const fields = result.toJSON();
```
:::
2019-11-07 12:05:39 +01:00
::: tab Mongoose
### Mongoose
Documentation: [https://mongoosejs.com/](https://mongoosejs.com/)
**Example**
```js
2019-09-24 17:27:02 +02:00
const result = strapi.query('restaurant').model.find({
2019-09-06 11:36:46 +02:00
date: { $gte: '2019-01-01T00.00.00Z' },
});
const fields = result.toObject();
```
:::
::::