|
1
|
--- |
|
2
|
generate_toc: true |
|
3
|
--- |
|
4
|
|
|
5
|
import { CodeTabs, CodeTab } from "docs-ui" |
|
6
|
|
|
7
|
export const metadata = { |
|
8
|
title: `Settings Module`, |
|
9
|
} |
|
10
|
|
|
11
|
# {metadata.title} |
|
12
|
|
|
13
|
In this section of the documentation, you will find resources to learn more about the Settings Module and how to use it in your application. |
|
14
|
|
|
15
|
<Note title="Looking for no-code docs?"> |
|
16
|
|
|
17
|
Refer to the [Medusa Admin User Guide](!user-guide!/tips/table-views) to learn how to customize data tables and manage views in the dashboard. |
|
18
|
|
|
19
|
</Note> |
|
20
|
|
|
21
|
Medusa has admin personalization and configuration features available out-of-the-box through the Settings Module. A [module](!docs!/learn/fundamentals/modules) is a standalone package that provides features for a single domain. Each of Medusa's commerce features is provided in Commerce Modules, such as the Settings Module. |
|
22
|
|
|
23
|
The Settings Module powers dashboard personalization features, such as configuring data tables, customizing page layouts, and storing user preferences. |
|
24
|
|
|
25
|
<Note> |
|
26
|
|
|
27
|
Refer to the [Module Isolation](!docs!/learn/fundamentals/modules/isolation) guide to learn more about why modules are isolated. |
|
28
|
|
|
29
|
</Note> |
|
30
|
|
|
31
|
## Settings Features |
|
32
|
|
|
33
|
- [View Configurations](./view-configurations/page.mdx): Store data-table views for an entity, including visible columns, column order and widths, filters, sorting, and search. Views can be personal to a user or a shared system default. |
|
34
|
- [Layout Configurations](./layout-configurations/page.mdx): Store per-zone layout preferences, such as the order and visibility of sections on a page, for a user or as a shared system default. |
|
35
|
- [Property Labels](./property-labels/page.mdx): Set custom, translatable labels and descriptions for an entity's properties. |
|
36
|
- [User Preferences](./user-preferences/page.mdx): Store arbitrary preferences for a user as key-value pairs. |
|
37
|
|
|
38
|
--- |
|
39
|
|
|
40
|
## How to Use the Settings Module |
|
41
|
|
|
42
|
In your Medusa application, you build flows around Commerce Modules. A flow is built as a [Workflow](!docs!/learn/fundamentals/workflows), which is a special function composed of a series of steps that guarantees data consistency and a reliable rollback mechanism. |
|
43
|
|
|
44
|
You can build custom workflows and steps. You can also re-use Medusa's workflows and steps, which are provided by the `@medusajs/medusa/core-flows` package. |
|
45
|
|
|
46
|
For example: |
|
47
|
|
|
48
|
export const highlights = [ |
|
49
|
["12", "Modules.SETTINGS", "Resolve the module in a step."] |
|
50
|
] |
|
51
|
|
|
52
|
```ts title="src/workflows/set-preference.ts" highlights={highlights} |
|
53
|
import { |
|
54
|
createWorkflow, |
|
55
|
WorkflowResponse, |
|
56
|
createStep, |
|
57
|
StepResponse, |
|
58
|
} from "@medusajs/framework/workflows-sdk" |
|
59
|
import { Modules } from "@medusajs/framework/utils" |
|
60
|
|
|
61
|
const setPreferenceStep = createStep( |
|
62
|
"set-preference", |
|
63
|
async ({}, { container }) => { |
|
64
|
const settingsModuleService = container.resolve(Modules.SETTINGS) |
|
65
|
|
|
66
|
const preference = await settingsModuleService |
|
67
|
.setUserPreference("user_123", "theme", { |
|
68
|
mode: "dark", |
|
69
|
}) |
|
70
|
|
|
71
|
return new StepResponse({ preference }) |
|
72
|
} |
|
73
|
) |
|
74
|
|
|
75
|
export const setPreferenceWorkflow = createWorkflow( |
|
76
|
"set-preference", |
|
77
|
() => { |
|
78
|
const { preference } = setPreferenceStep() |
|
79
|
|
|
80
|
return new WorkflowResponse({ |
|
81
|
preference, |
|
82
|
}) |
|
83
|
} |
|
84
|
) |
|
85
|
``` |
|
86
|
|
|
87
|
You can then execute the workflow in your custom API routes, scheduled jobs, or subscribers: |
|
88
|
|
|
89
|
<CodeTabs group="resource-types"> |
|
90
|
<CodeTab label="API Route" value="api-route"> |
|
91
|
|
|
92
|
```ts title="src/api/workflow/route.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports" |
|
93
|
import type { |
|
94
|
MedusaRequest, |
|
95
|
MedusaResponse, |
|
96
|
} from "@medusajs/framework/http" |
|
97
|
import { setPreferenceWorkflow } from "../../workflows/set-preference" |
|
98
|
|
|
99
|
export async function GET( |
|
100
|
req: MedusaRequest, |
|
101
|
res: MedusaResponse |
|
102
|
) { |
|
103
|
const { result } = await setPreferenceWorkflow(req.scope) |
|
104
|
.run() |
|
105
|
|
|
106
|
res.send(result) |
|
107
|
} |
|
108
|
``` |
|
109
|
|
|
110
|
</CodeTab> |
|
111
|
<CodeTab label="Subscriber" value="subscriber"> |
|
112
|
|
|
113
|
```ts title="src/subscribers/user-created.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports" |
|
114
|
import { |
|
115
|
type SubscriberConfig, |
|
116
|
type SubscriberArgs, |
|
117
|
} from "@medusajs/framework" |
|
118
|
import { setPreferenceWorkflow } from "../workflows/set-preference" |
|
119
|
|
|
120
|
export default async function handleUserCreated({ |
|
121
|
event: { data }, |
|
122
|
container, |
|
123
|
}: SubscriberArgs<{ id: string }>) { |
|
124
|
const { result } = await setPreferenceWorkflow(container) |
|
125
|
.run() |
|
126
|
|
|
127
|
console.log(result) |
|
128
|
} |
|
129
|
|
|
130
|
export const config: SubscriberConfig = { |
|
131
|
event: "user.created", |
|
132
|
} |
|
133
|
``` |
|
134
|
|
|
135
|
</CodeTab> |
|
136
|
<CodeTab label="Scheduled Job" value="scheduled-job"> |
|
137
|
|
|
138
|
```ts title="src/jobs/run-daily.ts" highlights={[["7"], ["8"]]} |
|
139
|
import { MedusaContainer } from "@medusajs/framework/types" |
|
140
|
import { setPreferenceWorkflow } from "../workflows/set-preference" |
|
141
|
|
|
142
|
export default async function myCustomJob( |
|
143
|
container: MedusaContainer |
|
144
|
) { |
|
145
|
const { result } = await setPreferenceWorkflow(container) |
|
146
|
.run() |
|
147
|
|
|
148
|
console.log(result) |
|
149
|
} |
|
150
|
|
|
151
|
export const config = { |
|
152
|
name: "run-once-a-day", |
|
153
|
schedule: `0 0 * * *`, |
|
154
|
} |
|
155
|
``` |
|
156
|
|
|
157
|
</CodeTab> |
|
158
|
</CodeTabs> |
|
159
|
|
|
160
|
Refer to the [Workflows](!docs!/learn/fundamentals/workflows) documentation to learn more. |
|
161
|
|
|
162
|
--- |
|
163
|
|
|
164
|
<CommerceModuleSections /> |