chore: reorganize docs apps (#7228)
* reorganize docs apps * add README * fix directory * add condition for old docs
This commit is contained in:
1 parent
224ebb2154
commit
4fe28f5a95
6187 files changed
+601447
-598226
No files matched your search
@@ -0,0 +1,102 @@
|
||||
export const metadata = {
|
||||
title: `${pageNumber} Common Data Model Definitions`,
|
||||
}
|
||||
|
||||
# {metadata.title}
|
||||
|
||||
In this chapter, you'll learn where to find resources on common data model definitions related to field's column types, relations, and indices.
|
||||
|
||||
## MikroORM Learning Resources
|
||||
|
||||
Refer to the following resources to learn about common definitions:
|
||||
|
||||
1. [Field column types](https://mikro-orm.io/docs/defining-entities).
|
||||
2. [Creating indices](https://mikro-orm.io/docs/defining-entities#indexes).
|
||||
|
||||
Refer to MikroORM's documentation for more details related to your use case.
|
||||
|
||||
---
|
||||
|
||||
## Relations Between Data Models
|
||||
|
||||
You can build relations between data models in the same module.
|
||||
|
||||
Refer to [MikroORM's relations definition](https://mikro-orm.io/docs/relationships)
|
||||
|
||||
<Note>
|
||||
|
||||
For building relationships between data models in different module, refer to the [Module Relationships chapter](../../modules/module-relationships/page.mdx) instead.
|
||||
|
||||
</Note>
|
||||
|
||||
---
|
||||
|
||||
## Example Data Model
|
||||
|
||||
The following example showcase a data model with common definitions:
|
||||
|
||||
```ts
|
||||
import {
|
||||
BeforeCreate,
|
||||
Entity,
|
||||
Enum,
|
||||
OneToOne,
|
||||
PrimaryKey,
|
||||
Property,
|
||||
} from "@mikro-orm/core"
|
||||
import {
|
||||
createPsqlIndexStatementHelper,
|
||||
generateEntityId,
|
||||
} from "@medusajs/utils"
|
||||
import ProductVariant from "./product-variant"
|
||||
|
||||
export enum MediaType {
|
||||
MAIN = "main",
|
||||
PREVIEW = "preview"
|
||||
}
|
||||
|
||||
const VariantIdIndex = createPsqlIndexStatementHelper({
|
||||
name: "IDX_product_media_variant_id",
|
||||
tableName: "product_media",
|
||||
columns: "variant_id",
|
||||
}).MikroORMIndex
|
||||
|
||||
@Entity({ tableName: "product_media" })
|
||||
export default class ProductMedia {
|
||||
@PrimaryKey({ columnType: "text" })
|
||||
id: string
|
||||
|
||||
@Property({ columnType: "text" })
|
||||
name: string
|
||||
|
||||
@Enum({ items: ["main", "preview"] })
|
||||
type: MediaType
|
||||
|
||||
@Property({ columnType: "text" })
|
||||
file_key: string
|
||||
|
||||
@Property({ columnType: "text" })
|
||||
mime_type: string
|
||||
|
||||
@VariantIdIndex()
|
||||
@Property({ columnType: "text" })
|
||||
variant_id: string
|
||||
|
||||
@OneToOne({
|
||||
entity: ProductVariant,
|
||||
onDelete: "cascade",
|
||||
})
|
||||
variant: ProductVariant
|
||||
|
||||
@BeforeCreate()
|
||||
onCreate() {
|
||||
this.id = generateEntityId(this.id, "promed")
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
In the example above:
|
||||
|
||||
- The `ProductMedia` data model has the columns `id`, `name`, `type`, `file_key`, `mime_type`, and `variant_id`.
|
||||
- The data model has an index on the `variant_id` column.
|
||||
- The data model has a one-to-one relation to the `ProductVariant` data model.
|
||||
@@ -0,0 +1,75 @@
|
||||
export const metadata = {
|
||||
title: `${pageNumber} Soft-Deletable Models`,
|
||||
}
|
||||
|
||||
# {metadata.title}
|
||||
|
||||
In this document, you'll learn how to create soft-deletable data models.
|
||||
|
||||
## What is a Soft-Deletable Model?
|
||||
|
||||
A soft-deletable data model is a model whose records aren't actually removed from the database when they're deleted.
|
||||
|
||||
Instead, their `deleted_at` field is set to the date the record was deleted.
|
||||
|
||||
When retrieving or listing records of that data model, records having their `deleted_at` field set aren't retrieved unless the `withDeleted` filter is provided.
|
||||
|
||||
---
|
||||
|
||||
## How to Create a Soft-Deletable Model?
|
||||
|
||||
To create a soft-deletable model, first, add the following filter decorator to the data model class:
|
||||
|
||||
```ts title="src/module/hello/models/my-soft-deletable.ts" highlights={[["6"]]}
|
||||
// other imports...
|
||||
import { Entity, Filter } from "@mikro-orm/core"
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
|
||||
@Entity()
|
||||
@Filter(DALUtils.mikroOrmSoftDeletableFilterOptions)
|
||||
export default class MySoftDeletable {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
Then, add a `deleted_at` field to the data model:
|
||||
|
||||
```ts highlights={[["4"], ["5"]]}
|
||||
// other imports...
|
||||
import { Property } from "@mikro-orm/core"
|
||||
|
||||
export default class MySoftDeletable {
|
||||
// ...
|
||||
|
||||
@Property({ columnType: "timestamptz", nullable: true })
|
||||
deleted_at: Date | null = null
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Managing Soft-Deletable Models
|
||||
|
||||
Services extending the service factory have methods to soft delete and restore records for all models specified during its creation.
|
||||
|
||||
For example, to soft delete a `MySoftDeletable` record:
|
||||
|
||||
```ts
|
||||
await helloModuleService.softDelete([
|
||||
"id_123", "id_321",
|
||||
])
|
||||
```
|
||||
|
||||
The method receives an array of IDs of records to delete.
|
||||
|
||||
To restore a `MySoftDeletable` record:
|
||||
|
||||
```ts
|
||||
await helloModuleService.restore([
|
||||
"id_123", "id_321",
|
||||
])
|
||||
```
|
||||
|
||||
The method also receives an array of IDs of records to restore.
|
||||
|
||||
If the data model isn't the main data model, its method names are `softDelete` and `restore` suffixed with the plural name of the model. For example, `softDeleteMySoftDeletable`.
|
||||
Reference in new issue
Block a user