Add core service docs

This commit is contained in:
Alexandre Bodin 2019-05-16 15:00:55 +02:00 committed by Jim LAURIE
parent b9a0344a29
commit 7d130cad3d
6 changed files with 172 additions and 22 deletions

View File

@ -149,7 +149,7 @@ The administration URL will be http://yourfrontend.com/dashboard and every reque
The generated `index.html` will look like this:
**Path —** `./admin/admin/build/index.html`.
**Path —** `./build/index.html`.
```html
<html lang="en">

View File

@ -73,7 +73,6 @@ The framework allows to load hooks from the project directly without having to i
│ - index.js
│ └─── strapi-server-side-rendering
│ - index.js
└─── plugins
└─── public
- favicon.ico
- package.json

View File

@ -94,7 +94,6 @@ The framework allows the application to override the default middlewares and add
│ - index.js
│ └─── views // It will be added into the stack of middleware
│ - index.js
└─── plugins
└─── public
- favicon.ico
- package.json

View File

@ -4,11 +4,11 @@ See the [controllers' concepts](../concepts/concepts.md#controllers) for a simpl
## Core controllers
When you create a new Content type or a new model. You will see a new empty controller as been created. It is because Strapi builds a generic api for your models by default and allows you to override and extend it in the generated files.
When you create a new Content type or a new model. You will see a new empty controller has been created. It is because Strapi builds a generic controller for your models by default and allows you to override and extend it in the generated files.
### Extending a Model Controller
Here are the core methods (and there current implementation).
Here are the core methods (and their current implementation).
You can simply copy and paste this code in your own controller file to customize the methods.
::: warning
@ -121,11 +121,11 @@ module.exports = {
You can also create custom controllers to build your own business logic and API endpoints.
### Creating a custom controller?
### How to create a custom controller
There are two ways to create a controller:
- Using the CLI `strapi generate:controller products`. Read the [CLI documentation](../cli/CLI.md#strapi-generatecontroller) for more information.
- Using the CLI `strapi generate:controller product`. Read the [CLI documentation](../cli/CLI.md#strapi-generatecontroller) for more information.
- Manually create a JavaScript file in `./api/**/controllers`.
### Adding Endpoints

View File

@ -1,18 +1,163 @@
# Services
See the [services concept](../concepts/concepts.md#services) for details.
See the [services concept](../concepts/concepts.md#services) for an overview.
## How to create a service?
## Core services
There is two ways to create a service.
- Using the CLI `strapi generate:service user`. Read the [CLI documentation](../cli/CLI.md) for more information.
- Manually create a JavaScript file named `User.js` in `./api/**/services/`.
When you create a new Content type or a new model. You will see a new empty service has been created. It is because Strapi builds a generic service for your models by default and allows you to override and extend it in the generated files.
### Extending a Model Service
Here are the core methods (and their current implementation).
You can simply copy and paste this code in your own service file to customize the methods.
::: warning
In the following example we will consider your controller, service and model is named `product`
:::
#### `find`
```js
module.exports = {
/**
* Promise to fetch all records
*
* @return {Promise}
*/
find(params, populate) {
return strapi.query(Product).find(params, populate);
},
};
```
#### `findOne`
```js
module.exports = {
/**
* Promise to fetch record
*
* @return {Promise}
*/
findOne(params, populate) {
return strapi.query(Product).findOne(params, populate);
},
};
```
#### `count`
```js
module.exports = {
/**
* Promise to count record
*
* @return {Promise}
*/
count(params) {
return strapi.query(Product).count(params);
},
};
```
#### `create`
```js
module.exports = {
/**
* Promise to add record
*
* @return {Promise}
*/
create(values) {
return strapi.query(Product).create(values);
},
};
```
#### `update`
```js
module.exports = {
/**
* Promise to edit record
*
* @return {Promise}
*/
update(params, values) {
return strapi.query(Product).update(params, values);
},
};
```
#### `delete`
```js
module.exports = {
/**
* Promise to delete a record
*
* @return {Promise}
*/
delete(params) {
return strapi.query(Product).delete(params);
},
};
```
#### `search`
```js
module.exports = {
/**
* Promise to search records
*
* @return {Promise}
*/
search(params) {
return strapi.query(Product).search(params);
},
};
```
#### `countSearch`
```js
module.exports = {
/**
* Promise to count searched records
*
* @return {Promise}
*/
countSearch(params) {
return strapi.query(Product).countSearch(params);
},
};
```
## Custom services
You can also create custom services to build your own business logic.
### How to create a custom service
There are two ways to create a service.
- Using the CLI `strapi generate:service product`. Read the [CLI documentation](../cli/CLI.md) for more information.
- Manually create a JavaScript file named in `./api/**/services/`.
#### Example
The goal of a service is to store reusable functions. An `email` service could be useful, if we plan to send emails from different functions in our codebase:
**Path —** `./api/email/services/Email.js`.
```js
const nodemailer = require('nodemailer');
@ -21,23 +166,23 @@ const transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'user@gmail.com',
pass: 'password'
}
pass: 'password',
},
});
module.exports = {
send: (from, to, subject, text) => {
send: (from, to, subject, text) => {
// Setup e-mail data.
const options = {
from,
to,
subject,
text
text,
};
// Return a promise of the function that sends the email.
return transporter.sendMail(options);
}
},
};
```
@ -48,20 +193,26 @@ please make sure you installed `nodemailer` (`npm install nodemailer`) for this
The service is now available through the `strapi.services` global variable. We can use it in another part of our codebase. For example a controller like below:
**Path —** `./api/user/controllers/User.js`.
```js
module.exports = {
// GET /hello
signup: async (ctx) => {
signup: async ctx => {
// Store the new user in database.
const user = await User.create(ctx.params);
// Send an email to validate his subscriptions.
strapi.services.email.send('welcome@mysite.com', user.email, 'Welcome', '...');
strapi.services.email.send(
'welcome@mysite.com',
user.email,
'Welcome',
'...'
);
// Send response to the server.
ctx.send({
ok: true
ok: true,
});
}
},
};
```

View File

@ -1,7 +1,8 @@
'use strict';
/**
* Read the documentation () to implement custom service functions
* Read the documentation (https://strapi.io/documentation/3.0.0-beta.x/guides/services.html#core-services)
* to customize this service
*/
module.exports = {};