docs: add documentation for v1.8 (#3669)

This commit is contained in:
Shahed Nasser
2023-04-03 13:50:59 +02:00
committed by GitHub
parent 0cca13779d
commit c6bfad14d8
123 changed files with 7610 additions and 2697 deletions
@@ -3,13 +3,13 @@ description: 'Learn how to create a service in Medusa. This guide also includes
addHowToData: true
---
# Create a Service
# How to Create a Service
In this document, youll learn how you can create a [Service](./overview.mdx) and use it across your Medusa backend just like any of the core services.
## Implementation
## Service Implementation
To create a service, create a TypeScript or JavaScript file in `src/services` to hold the service. The name of the file should be the registration name of the service without `Service` as it will be appended to it by default.
To create a service, create a TypeScript or JavaScript file in `src/services` to hold the service. The name of the file should be the name of the service without `Service`. This is essential as the file name is used when registering the service in the [dependency container](../fundamentals/dependency-injection.md), and `Service` is appended to the camel-case version of the file name automatically.
For example, if you want to create a service `helloService`, create the file `hello.ts` in `src/services` with the following content:
@@ -28,6 +28,33 @@ class HelloService extends TransactionBaseService {
export default HelloService
```
This service will be registered in the dependency container as `helloService`.
---
## Service Life Time
As the dependency container in Medusa is built on top of [awilix](https://github.com/jeffijoe/awilix), you can specify the [Lifetime](https://github.com/jeffijoe/awilix#lifetime-management) of a service. The lifetime is added as a static property to the service.
There are three lifetime types:
1. `Lifetime.TRANSIENT`: (default for custom services) when used, a new instance of the service is created everytime it is resolved in other resources from the dependency container.
2. `Lifetime.SCOPED`: when used, an instance of the service is created and reused in the scope of the dependency container. So, when the service is resolved in other resources that share that dependency container, the same instance of the service will be returned.
3. `Lifetime.SINGLETON`: (default for core services) when used, the service is always reused, regardless of the scope. An instance of the service is cached in the root container.
You can set the lifetime of your service by setting the `LIFE_TIME` static property:
```ts title=/src/services/hello.ts
import { TransactionBaseService } from "@medusajs/medusa"
import { Lifetime } from "awilix"
class HelloService extends TransactionBaseService {
static LIFE_TIME = Lifetime.SCOPED
// ...
}
```
---
## Service Constructor
@@ -0,0 +1,86 @@
---
description: 'Learn how to create a service in Medusa. This guide also includes how to use services in other services, subscribers, and endpoints.'
addHowToData: true
---
# How to Extend a Service
In this document, youll learn how to extend a core service in Medusa.
## Overview
Medusas core services cover a wide range of functionalities related to each domain or entity. You can extend these services to add custom methods or override existing methods.
### Word of Caution about Overriding
Extending services to add new methods shouldn't cause any issues within your commerce application. However, if you extend them to override their existing methods, you should be aware that this could have negative implications, such as unanticipated bugs, especially when you try to upgrade the core Medusa package to a newer version.
---
## Step 1: Create the Service File
In your Medusa backend, create the file `src/services/product.ts`. This file will hold your extended service.
Note that the name of the file must be the same as the name of the original service in the core package. So, if youre extending the `ProductService`, the files name should be `product.ts`. On the other hand, if youre extending the `CustomerService`, the files name should be `customer.ts`.
---
## Step 2: Implementing the Service
In the file, you can import the original service from the Medusa core, then create your service that extends the core service.
For example, to extend the Product service:
```ts title=src/services/product.ts
import {
ProductService as MedusaProductService,
} from "@medusajs/medusa"
class ProductService extends MedusaProductService {
// TODO add customizations
}
export default ProductService
```
Notice that you alias the `ProductService` of the core to avoid naming conflicts.
Within the service, you can add new methods or extend existing ones.
You can also change the lifetime of the service:
```ts title=src/services/product.ts
import { Lifetime } from "awilix"
import {
ProductService as MedusaProductService,
} from "@medusajs/medusa"
class ProductService extends MedusaProductService {
// The default life time for a core service is SINGLETON
static LIFE_TIME = LifeTime.SCOPED
// ...
}
export default ProductService
```
You can learn more details about the service lifetime and other considerations when creating a service in the [Create Service documentation](./create-service.md).
---
## Step 3: Test it Out
To test out your customization, start by transpiling your files by running the following command in the root directory of the Medusa backend:
```bash npm2yarn
npm run build
```
Then, start the backend:
```bash npm2yarn
npm run start
```
You should see the customizations you made in effect.
+21 -7
View File
@@ -2,7 +2,7 @@
description: 'Learn what Services are in Medusa. Services represent bundled helper methods that you want to use across your commerce application.'
---
import DocCard from '@theme/DocCard';
import DocCardList from '@theme/DocCardList';
import Icons from '@theme/Icon';
# Services
@@ -17,11 +17,15 @@ For example, you can use Medusas `productService` to get the list of products
In the Medusa backend, custom services are TypeScript or JavaScript files located in the `src/services` directory. Each service should be a class that extends the `TransactionBaseService` class from the core Medusa package `@medusajs/medusa`. Each file you create in `src/services` should hold one service and export it.
The file name is important as it determines the name of the service when you need to use it elsewhere. The name of the service will be registered as the camel-case version of the file name + `Service` at the end of the name.
The file name is important as it determines the name of the service when you need to use it elsewhere. The name of the service will be registered in the dependency container as the camel-case version of the file name with `Service` appended to the end of the name. Other resources, such as other services or endpoints, will use that name when resolving the service from the dependency container.
For example, if the file name is `hello.ts`, the service will be registered as `helloService`. If the file name is `hello-world.ts`, the service name will be registered as `helloWorldService`.
For example, if the file name is `hello.ts`, the service will be registered as `helloService` in the dependency container. If the file name is `hello-world.ts`, the service name will be registered as `helloWorldService`.
The registration name of the service is important, as youll be referring to it when you want to get access to the service using dependency injection or in routes.
:::note
You can learn more about the dependency container and how it works in the [dependency injection](../fundamentals/dependency-injection.md) documentation.
:::
The service must then be transpiled using the `build` command, which moves them to the `dist` directory, to be used across your commerce application.
@@ -37,7 +41,8 @@ If you're creating a service in a plugin, learn more about the required structur
Developers can create custom services in the Medusa backend, a plugin, or in a Commerce Module.
<DocCard item={{
<DocCardList colSize={6} items={[
{
type: 'link',
href: '/development/services/create-service',
label: 'Create a Service',
@@ -45,5 +50,14 @@ Developers can create custom services in the Medusa backend, a plugin, or in a C
icon: Icons['academic-cap-solid'],
description: 'Learn how to create a service in Medusa.'
}
}}
/>
},
{
type: 'link',
href: '/development/services/extend-service',
label: 'Extend a Service',
customProps: {
icon: Icons['academic-cap-solid'],
description: 'Learn how to extend a core Medusa service.'
}
},
]} />