docs: add documentation for migration generate cli tool (#8128)

* docs: add documentation for migration generate cli tool

* changed migrations details in marketplace recipe

* added generated oas files to action

* vale + lint fixes

* don't import from src in medusa-config.js

* fix generate command in recipe

* fix module name
This commit is contained in:
Shahed Nasser authored and GitHub committed 2024-07-15 17:46:10 +02:00
1 parent 43eb38c8cb
commit a74c900ab1
19 files changed
+270 -229

No files matched your search

@@ -22,17 +22,21 @@ Similarly to your custom module, a commerce module's main service is registered
For example, you saw this code snippet in the [Medusa container chapter](../medusa-container/page.mdx):
```ts highlights={[["13"]]}
```ts highlights={[["10"]]}
import type { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IProductModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/utils"
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
export const GET = async (
req: MedusaRequest,
res: MedusaResponse
) => {
const productModuleService: IProductModuleService = req.scope.resolve(
ModuleRegistrationName.PRODUCT
)
const [, count] = await productModuleService.listAndCount()
const [, count] = await productModuleService
.listAndCountProducts()
res.json({
count,
+11 -49
View File
@@ -17,7 +17,7 @@ A data model is a class that represents a table in the database. It's created in
<Note title="Steps Summary">
1. Create a data model in a module.
2. Create migration for the data model.
2. Generate migration for the data model.
4. Run migration to add the data model's table in the database.
</Note>
@@ -44,53 +44,21 @@ You define a data model using the `model`'s `define` method. It accepts two para
The example above defines the data model `MyCustom` with the properties `id` and `name`.
### Create a Migration
### Generate a Migration
A migration defines changes to be made in the database, such as create or update tables.
A migration is a class created in a TypeScript or JavaScript file under a module's `migrations` directory. It has two methods:
To generate a migration for the data models in your module, run the following command:
- The `up` method reflects changes on the database.
- The `down` method reverts the changes made in the `up` method.
```bash
npx medusa migrations generate helloModuleService
```
<Details summaryContent="Generate Migration">
To generate migrations:
The `migrations generate` command of the Medusa CLI accepts one or more module names (for example, `helloModuleService`) to generate the migration for.
1. Create the file `src/modules/hello/migrations-config.ts` with the following content:
The above command creates a migration file at the directory `src/modules/hello/migrations` similar to the following:
```ts
import { defineMikroOrmCliConfig } from "@medusajs/utils"
import path from "path"
import MyCustom from "./models/my-custom"
import { HELLO_MODULE } from "."
export default defineMikroOrmCliConfig(HELLO_MODULE, {
entities: [MyCustom] as any[],
migrations: {
path: path.join(__dirname, "migrations"),
},
})
```
2. Run the following command in the root directory of your Medusa application:
```bash
npx cross-env MIKRO_ORM_CLI=./src/modules/hello/migrations-config.ts mikro-orm migration:create
```
<Note title="Tip">
Add this command as a script in `package.json` for easy usage in the future. Use this command whenever you want to generate a new migration in your module.
</Note>
After running the command, a migration file is generated under the `src/modules/hello/migrations` directory.
</Details>
For example:
```ts title="src/modules/migrations/Migration20240429090012.ts"
```ts
import { Migration } from "@mikro-orm/migrations"
export class Migration20240702105919 extends Migration {
@@ -106,17 +74,11 @@ export class Migration20240702105919 extends Migration {
}
```
In the `up` method, you create the table `my_custom` and define its columns. In the `down` method, you drop the table.
<Note title="Tip">
The queries performed in each of the methods use PostgreSQL syntax.
</Note>
In the migration class, the `up` method creates the table `my_custom` and defines its columns. The `down` method drops the table.
### Run Migration
To reflect the changes in the migration, run the `migration` command:
To reflect the changes in the generated migration file, run the `migration` command:
```bash
npx medusa migrations run
@@ -81,9 +81,9 @@ The subscriber function accepts an object parameter with the property `container
For example:
export const highlights = [
["10", "container", "Recieve the Medusa Container in the object parameter."],
["13", "resolve", "Resolve the Product Module's main service."],
["13", "ModuleRegistrationName.PRODUCT", "The module's registration name imported from `@medusajs/modules-sdk`."]
["7", "container", "Recieve the Medusa Container in the object parameter."],
["10", "resolve", "Resolve the Product Module's main service."],
["10", "ModuleRegistrationName.PRODUCT", "The module's registration name imported from `@medusajs/modules-sdk`."]
]
```ts title="src/subscribers/product-created.ts" highlights={highlights}
@@ -15,9 +15,9 @@ You use the Medusa container to resolve resources, such as services.
For example, in a custom API route you can resolve any service registered in the Medusa application using the `scope.resolve` method of the `MedusaRequest` parameter:
export const highlights = [
["13", "resolve", "Resolve the Product Module's main service."],
["9", "resolve", "Resolve the Product Module's main service."],
[
"13",
"10",
"ModuleRegistrationName.PRODUCT",
"The resource registration name imported from `@medusajs/modules-sdk`.",
],
@@ -28,7 +28,10 @@ import type { MedusaRequest, MedusaResponse } from "@medusajs/medusa"
import { IProductModuleService } from "@medusajs/types"
import { ModuleRegistrationName } from "@medusajs/utils"
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
export const GET = async (
req: MedusaRequest,
res: MedusaResponse
) => {
const productModuleService: IProductModuleService = req.scope.resolve(
ModuleRegistrationName.PRODUCT
)
@@ -70,21 +70,18 @@ The last step is to add the module in Medusa’s configurations.
In `medusa-config.js`, add a `modules` property and pass to it your custom module:
```js title="medusa-config.js" highlights={[["7", "HELLO_MODULE", "The key of the main service to be registered in the Medusa container."]]}
import { HELLO_MODULE } from "./src/modules/hello"
// ...
```js title="medusa-config.js" highlights={[["4", "helloModuleService", "The key of the main service to be registered in the Medusa container."]]}
module.exports = defineConfig({
// ...
modules: {
[HELLO_MODULE]: {
"helloModuleService": {
resolve: "./modules/hello",
},
},
})
```
Its key (`helloModuleService` or `HELLO_MODULE`) is the name of the module’s main service. It will be registered in the Medusa container with that name. It should also be the same name passed as the first parameter to the `Module` function in the module's definition.
Its key (`helloModuleService`) is the name of the module’s main service. It will be registered in the Medusa container with that name. It should also be the same name passed as the first parameter to the `Module` function in the module's definition.
Its value is an object having the `resolve` property, whose value is either a path to module's directory relative to `src`(it shouldn't include `src` in the path), or an `npm` package’s name.