docs: updates to custom columns and build guides (#13624)

This commit is contained in:
Shahed Nasser
2025-09-30 10:01:58 +03:00
committed by GitHub
parent 9d3c71fefd
commit bd9ecd5e66
5 changed files with 196 additions and 58 deletions
+50 -9
View File
@@ -4,13 +4,13 @@ export const metadata = {
# {metadata.title}
In this chapter, you'll learn how to create a production build of your Medusa application to be deployed to a hosting provider.
In this chapter, you'll learn how to create a production build of your Medusa application for deployment to a hosting provider.
Next chapters explain how to deploy the Medusa application.
## build Command
The Medusa CLI tool has a [build](!resources!/medusa-cli/commands/build) command which creates a standalone build of the Medusa application that:
The Medusa CLI tool provides a [build](!resources!/medusa-cli/commands/build) command that creates a standalone build of the Medusa application that:
- Doesn't rely on the source TypeScript files.
- Can be copied to a production server reliably.
@@ -25,9 +25,9 @@ npx medusa build
## Build Output
The `build` command creates a `.medusa` directory in the root of your project that contains your build assets. Don't commit this directory to your repository.
The `build` command creates a `.medusa` directory in the root of your project that contains your build assets. Do not commit this directory to your repository.
The `.medusa` directory contains the following directories:
The `.medusa` directory contains the following subdirectories:
- `.medusa/server`: Contains the production build of your Medusa application.
- `.medusa/server/public/admin`: Contains the production build of the admin dashboard.
@@ -46,13 +46,19 @@ npx medusa build --admin-only
To start the Medusa application after running the `build` command:
- Change to the `.medusa/server` directory and install the dependencies:
<Note>
You need to run these steps every time you run the `build` command, as the `.medusa` directory is recreated each time.
</Note>
1. Change to the `.medusa/server` directory and install the dependencies:
```bash npm2yarn
cd .medusa/server && npm install
```
- When running the application locally, make sure to copy the `.env` file from the root project's directory. In production, use system environment variables instead.
2. When running the application locally, make sure to copy the `.env` file from the root project's directory. In production, use system environment variables instead.
```bash title=".medusa/server"
cp ../../.env .env.production
@@ -64,17 +70,52 @@ When `NODE_ENV=production`, the Medusa application loads the environment variabl
</Note>
- Set `NODE_ENV` to `production` in the system environment variable, then start the Medusa application from `.medusa/server`:
3. Set `NODE_ENV` to `production` in the system environment variable:
```bash npm2yarn title=".medusa/server"
export NODE_ENV=production
```
4. Start the Medusa application from `.medusa/server`:
```bash npm2yarn title=".medusa/server"
npm run start
```
### Authentication Locally in Production Build
When the Medusa application is started in production (`NODE_ENV=production`) or staging modes, cookie settings are more strict. You can only use cookie authentication if the client application is served from the same domain as the Medusa server, and the domain is not `localhost` or part of the [public suffix list](https://publicsuffix.org/).
So, if you try to access the Medusa Admin locally in production mode, you won't be able to log in.
To access the Medusa Admin locally in production mode, set the `projectConfig.cookieOptions` in your `medusa-config.ts` file to be less strict. For example:
```ts title="medusa-config.ts"
module.exports = defineConfig({
projectConfig: {
// ...
cookieOptions: {
sameSite: "lax",
secure: false,
}
}
})
```
In this example, you set `sameSite` to `lax` and `secure` to `false`, which allows cookies to be sent over non-secure connections and from different domains.
Then, rebuild the Medusa application and start it again, as described in the [steps above](#start-built-medusa-application). You can now access the Medusa Admin locally in production mode.
<Note type="warning">
Make sure to remove the `projectConfig.cookieOptions` configuration once you're done testing locally, as it's not secure for production environments.
</Note>
---
## Deploying Production Build
The next chapter covers how you generally deploy the production build.
The next chapter covers how to deploy the production build.
You can also refer to the [deployment how-to guides](!resources!/deployment) for platform-specific how-to guides.
You can also refer to the [deployment how-to guides](!resources!/deployment) for platform-specific deployment instructions.
@@ -79,7 +79,7 @@ This option is available since Medusa [v2.8.5](https://github.com/medusajs/medus
</Note>
The `projectConfig.cookieOptions` configuration defines cookie options to be passed to `express-session` when creating the session cookie. This configuration is useful when simulating a production environment locally, where you may need to set options like `secure` or `sameSite`.
The `projectConfig.cookieOptions` configuration defines cookie options to be passed to `express-session` when creating the session cookie. This configuration is useful when simulating a production environment locally, where you may need to set options like `secure` or `sameSite`. Learn more in the [Build chapter](../../build/page.mdx#authentication-locally-in-production-build).
#### Example
@@ -121,7 +121,35 @@ await link.create({
## Retrieve Custom Column with Link
To retrieve linked records with their custom columns, use [Query](../query/page.mdx). A module link's definition, exported by a file under `src/links`, has a special `entryPoint` property. Use this property when specifying the `entity` property in Query's `graph` method.
To retrieve linked records with their custom columns, use [Query](../query/page.mdx). This section explores two methods to retrieve a link's custom columns.
### Method 1: Using Special Link Field
A data model will have a special field for all its link tables. The field's name is in the `{camel_case_data_model_name}_link` format, where `{camel_case_data_model_name}` is the name of the linked data model in camel case.
For example:
export const method1Highlights = [
["3", `"post_link.metadata"`, "Retrieve the `metadata` custom column."]
]
```ts highlights={method1Highlights}
const { data } = await query.graph({
entity: "product",
fields: ["id", "title", "post_link.metadata", "post.*"],
filters: {
id: "prod_123",
},
})
```
In this example, you retrieve a product and the `metadata` custom column in the link table between `product` and `post` using the `post_link.metadata` field.
You can also retrieve all custom columns in the link table using the `post_link.*` field.
### Method 2: Using Entry Point
A module link's definition, exported by a file under `src/links`, has a special `entryPoint` property. Use this property when specifying the `entity` property in Query's `graph` method.
For example: