|
@@ -1,78 +1,117 @@
|
|
# Custom Module
|
|
# Custom Module
|
|
|
|
|
|
-A module is a package of reusable functionalities. It can be integrated into your Medusa application without affecting the overall system.
|
|
|
|
|
|
+A module is a package of reusable functionalities. It can be integrated into your Medusa application without affecting the overall system. You can create a module as part of a plugin.
|
|
|
|
+
|
|
|
|
+> Learn more about modules in [this documentation](https://docs.medusajs.com/learn/fundamentals/modules).
|
|
|
|
|
|
To create a module:
|
|
To create a module:
|
|
|
|
|
|
-## 1. Create a Service
|
|
|
|
|
|
+## 1. Create a Data Model
|
|
|
|
+
|
|
|
|
+A data model represents a table in the database. You create a data model in a TypeScript or JavaScript file under the `models` directory of a module.
|
|
|
|
+
|
|
|
|
+For example, create the file `src/modules/blog/models/post.ts` with the following content:
|
|
|
|
+
|
|
|
|
+```ts
|
|
|
|
+import { model } from "@medusajs/framework/utils"
|
|
|
|
+
|
|
|
|
+const Post = model.define("post", {
|
|
|
|
+ id: model.id().primaryKey(),
|
|
|
|
+ title: model.text(),
|
|
|
|
+})
|
|
|
|
+
|
|
|
|
+export default Post
|
|
|
|
+```
|
|
|
|
+
|
|
|
|
+## 2. Create a Service
|
|
|
|
|
|
A module must define a service. A service is a TypeScript or JavaScript class holding methods related to a business logic or commerce functionality.
|
|
A module must define a service. A service is a TypeScript or JavaScript class holding methods related to a business logic or commerce functionality.
|
|
|
|
|
|
-For example, create the file `src/modules/hello/service.ts` with the following content:
|
|
|
|
|
|
+For example, create the file `src/modules/blog/service.ts` with the following content:
|
|
|
|
|
|
-```ts title="src/modules/hello/service.ts"
|
|
|
|
-export default class HelloModuleService {
|
|
|
|
- getMessage() {
|
|
|
|
- return "Hello, world!"
|
|
|
|
- }
|
|
|
|
|
|
+```ts
|
|
|
|
+import { MedusaService } from "@medusajs/framework/utils"
|
|
|
|
+import Post from "./models/post"
|
|
|
|
+
|
|
|
|
+class BlogModuleService extends MedusaService({
|
|
|
|
+ Post,
|
|
|
|
+}){
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+export default BlogModuleService
|
|
```
|
|
```
|
|
|
|
|
|
-## 2. Export Module Definition
|
|
|
|
|
|
+## 3. Export Module Definition
|
|
|
|
|
|
A module must have an `index.ts` file in its root directory that exports its definition. The definition specifies the main service of the module.
|
|
A module must have an `index.ts` file in its root directory that exports its definition. The definition specifies the main service of the module.
|
|
|
|
|
|
-For example, create the file `src/modules/hello/index.ts` with the following content:
|
|
|
|
|
|
+For example, create the file `src/modules/blog/index.ts` with the following content:
|
|
|
|
|
|
-```ts title="src/modules/hello.index.ts" highlights={[["4", "", "The main service of the module."]]}
|
|
|
|
-import HelloModuleService from "./service"
|
|
|
|
|
|
+```ts
|
|
|
|
+import BlogModuleService from "./service"
|
|
import { Module } from "@medusajs/framework/utils"
|
|
import { Module } from "@medusajs/framework/utils"
|
|
|
|
|
|
-export const HELLO_MODULE = "hello"
|
|
|
|
|
|
+export const BLOG_MODULE = "blog"
|
|
|
|
|
|
-export default Module(HELLO_MODULE, {
|
|
|
|
- service: HelloModuleService,
|
|
|
|
|
|
+export default Module(BLOG_MODULE, {
|
|
|
|
+ service: BlogModuleService,
|
|
})
|
|
})
|
|
```
|
|
```
|
|
|
|
|
|
-## 3. Add Module to Configurations
|
|
|
|
-
|
|
|
|
-The last step is to add the module in Medusa’s configurations.
|
|
|
|
|
|
+## 4. Add Module to Medusa's Configurations
|
|
|
|
|
|
-In `medusa-config.js`, add the module to the `modules` object:
|
|
|
|
-
|
|
|
|
-```js title="medusa-config.js"
|
|
|
|
-import { HELLO_MODULE } from "./src/modules/hello"
|
|
|
|
|
|
+To start using the module, add it to `medusa-config.ts`:
|
|
|
|
|
|
|
|
+```ts
|
|
module.exports = defineConfig({
|
|
module.exports = defineConfig({
|
|
- // ...
|
|
|
|
|
|
+ projectConfig: {
|
|
|
|
+ // ...
|
|
|
|
+ },
|
|
modules: [
|
|
modules: [
|
|
{
|
|
{
|
|
- resolve: "./modules/hello",
|
|
|
|
- }
|
|
|
|
- ]
|
|
|
|
|
|
+ resolve: "./src/modules/blog",
|
|
|
|
+ },
|
|
|
|
+ ],
|
|
})
|
|
})
|
|
```
|
|
```
|
|
|
|
|
|
|
|
+## 5. Generate and Run Migrations
|
|
|
|
+
|
|
|
|
+To generate migrations for your module, run the following command:
|
|
|
|
+
|
|
|
|
+```bash
|
|
|
|
+npx medusa db:generate blog
|
|
|
|
+```
|
|
|
|
+
|
|
|
|
+Then, to run migrations, run the following command:
|
|
|
|
+
|
|
|
|
+```bash
|
|
|
|
+npx medusa db:migrate
|
|
|
|
+```
|
|
|
|
+
|
|
## Use Module
|
|
## Use Module
|
|
|
|
|
|
-You can resolve the main service of the module in other resources, such as an API route:
|
|
|
|
|
|
+You can use the module in customizations within the Medusa application, such as workflows and API routes.
|
|
|
|
+
|
|
|
|
+For example, to use the module in an API route:
|
|
|
|
|
|
```ts
|
|
```ts
|
|
import { MedusaRequest, MedusaResponse } from "@medusajs/framework"
|
|
import { MedusaRequest, MedusaResponse } from "@medusajs/framework"
|
|
-import HelloModuleService from "../../../modules/hello/service"
|
|
|
|
-import { HELLO_MODULE } from "../../../modules/hello"
|
|
|
|
|
|
+import BlogModuleService from "../../../modules/blog/service"
|
|
|
|
+import { BLOG_MODULE } from "../../../modules/blog"
|
|
|
|
|
|
export async function GET(
|
|
export async function GET(
|
|
req: MedusaRequest,
|
|
req: MedusaRequest,
|
|
res: MedusaResponse
|
|
res: MedusaResponse
|
|
): Promise<void> {
|
|
): Promise<void> {
|
|
- const helloModuleService: HelloModuleService = req.scope.resolve(
|
|
|
|
- HELLO_MODULE
|
|
|
|
|
|
+ const blogModuleService: BlogModuleService = req.scope.resolve(
|
|
|
|
+ BLOG_MODULE
|
|
)
|
|
)
|
|
|
|
|
|
|
|
+ const posts = await blogModuleService.listPosts()
|
|
|
|
+
|
|
res.json({
|
|
res.json({
|
|
- message: helloModuleService.getMessage(),
|
|
|
|
|
|
+ posts
|
|
})
|
|
})
|
|
}
|
|
}
|
|
```
|
|
```
|