|
1
|
--- |
|
2
|
generate_toc: true |
|
3
|
--- |
|
4
|
|
|
5
|
import { CodeTabs, CodeTab } from "docs-ui" |
|
6
|
|
|
7
|
export const metadata = { |
|
8
|
title: `Store Module`, |
|
9
|
} |
|
10
|
|
|
11
|
# {metadata.title} |
|
12
|
|
|
13
|
In this section of the documentation, you will find resources to learn more about the Store 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!/settings/store) to learn how to manage your store using the dashboard. |
|
18
|
|
|
19
|
</Note> |
|
20
|
|
|
21
|
Medusa has store related features available out-of-the-box through the Store Module. A [module](!docs!/learn/fundamentals/modules) is a standalone package that provides features for a single domain. Each of Medusa's commerce features are placed in Commerce Modules, such as this Store Module. |
|
22
|
|
|
23
|
<Note> |
|
24
|
|
|
25
|
Learn more about why modules are isolated in [this documentation](!docs!/learn/fundamentals/modules/isolation). |
|
26
|
|
|
27
|
</Note> |
|
28
|
|
|
29
|
## Store Features |
|
30
|
|
|
31
|
- [Store Management](/references/store/models/Store): Create and manage stores in your application. |
|
32
|
- [Multi-Tenancy Support](/references/store/models/Store): While Medusa doesn't natively support multi-tenancy, the Store Module allows you to create and manage multiple stores within a single Medusa instance. You can then build customizations to link products, customers, orders, and other resources to specific stores. |
|
33
|
|
|
34
|
--- |
|
35
|
|
|
36
|
## How to Use Store Module's Service |
|
37
|
|
|
38
|
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 reliable roll-back mechanism. |
|
39
|
|
|
40
|
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. |
|
41
|
|
|
42
|
For example: |
|
43
|
|
|
44
|
export const highlights = [ |
|
45
|
["12", "Modules.STORE", "Resolve the module in a step."] |
|
46
|
] |
|
47
|
|
|
48
|
```ts title="src/workflows/create-store.ts" highlights={highlights} |
|
49
|
import { |
|
50
|
createWorkflow, |
|
51
|
WorkflowResponse, |
|
52
|
createStep, |
|
53
|
StepResponse, |
|
54
|
} from "@medusajs/framework/workflows-sdk" |
|
55
|
import { Modules } from "@medusajs/framework/utils" |
|
56
|
|
|
57
|
const createStoreStep = createStep( |
|
58
|
"create-store", |
|
59
|
async ({}, { container }) => { |
|
60
|
const storeModuleService = container.resolve(Modules.STORE) |
|
61
|
|
|
62
|
const store = await storeModuleService.createStores({ |
|
63
|
name: "My Store", |
|
64
|
supported_currencies: [{ |
|
65
|
currency_code: "usd", |
|
66
|
is_default: true, |
|
67
|
}], |
|
68
|
}) |
|
69
|
|
|
70
|
return new StepResponse({ store }, store.id) |
|
71
|
}, |
|
72
|
async (storeId, { container }) => { |
|
73
|
if(!storeId) { |
|
74
|
return |
|
75
|
} |
|
76
|
const storeModuleService = container.resolve(Modules.STORE) |
|
77
|
|
|
78
|
await storeModuleService.deleteStores([storeId]) |
|
79
|
} |
|
80
|
) |
|
81
|
|
|
82
|
export const createStoreWorkflow = createWorkflow( |
|
83
|
"create-store", |
|
84
|
() => { |
|
85
|
const { store } = createStoreStep() |
|
86
|
|
|
87
|
return new WorkflowResponse({ store }) |
|
88
|
} |
|
89
|
) |
|
90
|
``` |
|
91
|
|
|
92
|
You can then execute the workflow in your custom API routes, scheduled jobs, or subscribers: |
|
93
|
|
|
94
|
<CodeTabs group="resource-types"> |
|
95
|
<CodeTab label="API Route" value="api-route"> |
|
96
|
|
|
97
|
```ts title="src/api/workflow/route.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports" |
|
98
|
import type { |
|
99
|
MedusaRequest, |
|
100
|
MedusaResponse, |
|
101
|
} from "@medusajs/framework/http" |
|
102
|
import { createStoreWorkflow } from "../../workflows/create-store" |
|
103
|
|
|
104
|
export async function GET( |
|
105
|
req: MedusaRequest, |
|
106
|
res: MedusaResponse |
|
107
|
) { |
|
108
|
const { result } = await createStoreWorkflow(req.scope) |
|
109
|
.run() |
|
110
|
|
|
111
|
res.send(result) |
|
112
|
} |
|
113
|
``` |
|
114
|
|
|
115
|
</CodeTab> |
|
116
|
<CodeTab label="Subscriber" value="subscriber"> |
|
117
|
|
|
118
|
```ts title="src/subscribers/user-created.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports" |
|
119
|
import { |
|
120
|
type SubscriberConfig, |
|
121
|
type SubscriberArgs, |
|
122
|
} from "@medusajs/framework" |
|
123
|
import { createStoreWorkflow } from "../workflows/create-store" |
|
124
|
|
|
125
|
export default async function handleUserCreated({ |
|
126
|
event: { data }, |
|
127
|
container, |
|
128
|
}: SubscriberArgs<{ id: string }>) { |
|
129
|
const { result } = await createStoreWorkflow(container) |
|
130
|
.run() |
|
131
|
|
|
132
|
console.log(result) |
|
133
|
} |
|
134
|
|
|
135
|
export const config: SubscriberConfig = { |
|
136
|
event: "user.created", |
|
137
|
} |
|
138
|
``` |
|
139
|
|
|
140
|
</CodeTab> |
|
141
|
<CodeTab label="Scheduled Job" value="scheduled-job"> |
|
142
|
|
|
143
|
```ts title="src/jobs/run-daily.ts" highlights={[["7"], ["8"]]} |
|
144
|
import { MedusaContainer } from "@medusajs/framework/types" |
|
145
|
import { createStoreWorkflow } from "../workflows/create-store" |
|
146
|
|
|
147
|
export default async function myCustomJob( |
|
148
|
container: MedusaContainer |
|
149
|
) { |
|
150
|
const { result } = await createStoreWorkflow(container) |
|
151
|
.run() |
|
152
|
|
|
153
|
console.log(result) |
|
154
|
} |
|
155
|
|
|
156
|
export const config = { |
|
157
|
name: "run-once-a-day", |
|
158
|
schedule: `0 0 * * *`, |
|
159
|
} |
|
160
|
``` |
|
161
|
|
|
162
|
</CodeTab> |
|
163
|
</CodeTabs> |
|
164
|
|
|
165
|
Learn more about workflows in [this documentation](!docs!/learn/fundamentals/workflows). |
|
166
|
|
|
167
|
--- |
|
168
|
|
|
169
|
<CommerceModuleSections /> |