mirror of
https://github.com/strapi/strapi.git
synced 2025-11-02 10:55:37 +00:00
Merge pull request #5181 from strapi/docs-plugin-queries
Remove queries section in local plugin docs
This commit is contained in:
commit
a91b268447
@ -104,122 +104,3 @@ A plugin can have its own policies, such as adding security rules. For instance,
|
||||
```
|
||||
|
||||
Please refer to the [Policies documentation](../concepts/policies.md) for more information.
|
||||
|
||||
## ORM queries
|
||||
|
||||
Strapi supports multiple ORMs in order to let the users choose the database management system that suits their needs. Hence, each plugin must be compatible with at least one ORM. Each plugin contains a folder named `queries` in `./plugins/**/api/queries`. A file must be created for each ORM (eg. `mongoose.js` which exports the Mongoose ORM related queries).
|
||||
|
||||
The queries of a plugin are accessible through the `strapi.plugins.['pluginName'].queries('modelName', 'pluginName')` method, which automatically contains the queries according to the ORM used by the model.
|
||||
|
||||
### Example
|
||||
|
||||
Mongoose ORM queries definition:
|
||||
|
||||
**Path —** `./plugins/my-plugin/api/config/queries/mongoose.js`.
|
||||
|
||||
```js
|
||||
module.exports = () => {
|
||||
return {
|
||||
getUsers: async params => {
|
||||
return User.find(params);
|
||||
},
|
||||
};
|
||||
};
|
||||
```
|
||||
|
||||
Bookshelf ORM queries definition:
|
||||
|
||||
**Path —** `./plugins/my-plugin/api/config/queries/bookshelf.js`.
|
||||
|
||||
```js
|
||||
module.exports = () => {
|
||||
return {
|
||||
getUsers: async params => {
|
||||
return User.fetchAll(params);
|
||||
},
|
||||
};
|
||||
};
|
||||
```
|
||||
|
||||
Usage from the plugin:
|
||||
|
||||
**Path —** `./plugins/my-plugin/api/controllers/index.js`.
|
||||
|
||||
```js
|
||||
module.exports = {
|
||||
getUsers: async () => {
|
||||
// Get parameters from the request
|
||||
const { limit, sort } = ctx.request.query;
|
||||
|
||||
// Get the list of users using the plugins queries
|
||||
const users = await strapi.plugins['my-plugin']
|
||||
.queries('User', 'my-plugin')
|
||||
.getUsers({ limit, sort });
|
||||
|
||||
// Send the list of users as response
|
||||
ctx.body = users;
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
### Advanced usage
|
||||
|
||||
Strapi injects the model in the queries when instantiating them. It means you can create reusable queries very easily.
|
||||
We use it for example in the [Content Manager plugin](https://github.com/strapi/strapi/tree/master/packages/strapi-plugin-content-manager/config/queries).
|
||||
|
||||
#### Example
|
||||
|
||||
Mongoose ORM generic queries:
|
||||
|
||||
**Path —** `./plugins/my-plugin/api/config/queries/mongoose.js`.
|
||||
|
||||
```js
|
||||
module.exports = ({ model }) => {
|
||||
return {
|
||||
getAll: async function(params) {
|
||||
// this refers to the Mongoose model called in the query
|
||||
// ex: strapi.plugins['my-plugin'].queries('Product').getAll(), this will be equal to the product Mongoose model.
|
||||
return model.find(params);
|
||||
},
|
||||
};
|
||||
};
|
||||
```
|
||||
|
||||
Bookshelf ORM generic queries:
|
||||
|
||||
**Path —** `./plugins/my-plugin/api/config/queries/bookshelf/index.js`.
|
||||
|
||||
```js
|
||||
module.exports = ({ model }) => {
|
||||
return {
|
||||
getAll: async function(params) {
|
||||
// this refers to the Bookshelf model called in the query
|
||||
// ex: strapi.plugins['my-plugin'].queries('Product').getAll(), this will be equal to the product Bookshelf model.
|
||||
return model.fetchAll(params);
|
||||
},
|
||||
};
|
||||
};
|
||||
```
|
||||
|
||||
Usage from the plugin:
|
||||
|
||||
**Path —** `./plugins/my-plugin/api/controllers/index.js`.
|
||||
|
||||
```js
|
||||
module.exports = {
|
||||
getUsers: async () => {
|
||||
// Get parameters from the request
|
||||
const { limit, sort } = ctx.request.query;
|
||||
|
||||
// Get the list of users using the plugin's queries
|
||||
const users = await strapi.plugins['my-plugin']
|
||||
.queries('User')
|
||||
.getAll({ limit, sort });
|
||||
|
||||
// Send the list of users as response
|
||||
ctx.body = users;
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user