Skip to content

Commit

Permalink
feat(plugin/model): support sort (#12838)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc authored Dec 11, 2024
1 parent b7659b1 commit 13588c7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
27 changes: 27 additions & 0 deletions docs/docs/docs/max/data-flow.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,33 @@ translated_at: '2024-03-18T00:49:20.502Z'

`@umi/max` has a built-in **data flow management** [plugin](https://github.com/umijs/umi/blob/master/packages/plugins/src/model.ts), which is a lightweight data management solution based on the `hooks` paradigm. It can be used to manage global shared data in Umi projects.

## Configuration

e.g.

```ts
export default {
model: {
extraModels: ['src/models/userModel.ts'],
sort: (a, b) => a.namespace.localeCompare(b.namespace),
},
};
```

### extraModels

- Type: `string[]`
- Default: `[]`

Configure `extraModels` to automatically add these Model files to the data stream management.

### sort

- Type: `(a: Model, b: Model) => number`
- Default: `(a, b) => a.namespace.localeCompare(b.namespace)`

Configure `sort` to sort the Model based on the return value of the `sort` function.

## Getting Started

### Creating a Model
Expand Down
27 changes: 27 additions & 0 deletions docs/docs/docs/max/data-flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,33 @@ toc: content

`@umi/max` 内置了**数据流管理**[插件](https://github.com/umijs/umi/blob/master/packages/plugins/src/model.ts),它是一种基于 `hooks` 范式的轻量级数据管理方案,可以在 Umi 项目中管理全局的共享数据。

## 配置

e.g.

```ts
export default {
model: {
extraModels: ['src/models/userModel.ts'],
sort: (a, b) => a.namespace.localeCompare(b.namespace),
},
};
```

### extraModels

- Type: `string[]`
- Default: `[]`

配置 `extraModels` 后,插件会自动将这些 Model 文件添加到数据流管理中。

### sort

- Type: `(a: Model, b: Model) => number`
- Default: `(a, b) => a.namespace.localeCompare(b.namespace)`

配置 `sort` 后,插件会根据 `sort` 函数返回的值对 Model 进行排序。

## 开始使用

### 创建 Model
Expand Down
4 changes: 4 additions & 0 deletions packages/plugins/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default (api: IApi) => {
return zod
.object({
extraModels: zod.array(zod.string()),
sort: zod.function().optional(),
})
.partial();
},
Expand All @@ -22,6 +23,9 @@ export default (api: IApi) => {

api.onGenerateFiles(async () => {
const models = await getAllModels(api);
if (api.userConfig.model.sort) {
models.sort(api.userConfig.model.sort);
}

// model.ts
api.writeTmpFile({
Expand Down

0 comments on commit 13588c7

Please sign in to comment.