2021-10-28 22:55:51 +08:00
|
|
|
---
|
|
|
|
toc: menu
|
|
|
|
---
|
|
|
|
|
|
|
|
# Context
|
|
|
|
|
|
|
|
## ctx.db
|
|
|
|
|
2021-11-09 15:08:24 +08:00
|
|
|
##### Definition
|
|
|
|
|
|
|
|
```ts
|
|
|
|
interface Context {
|
|
|
|
db: Database;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
##### Examples
|
|
|
|
|
|
|
|
```ts
|
|
|
|
async (ctx, next) {
|
|
|
|
const User = ctx.db.getCollection('users');
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-10-28 22:55:51 +08:00
|
|
|
## ctx.resourcer
|
|
|
|
|
2021-11-17 18:54:32 +08:00
|
|
|
## ctx.action <Badge>待完善</Badge>
|
2021-10-28 22:55:51 +08:00
|
|
|
|
2021-12-05 18:43:26 +08:00
|
|
|
### action.params
|
|
|
|
|
|
|
|
资源标识定位
|
|
|
|
|
|
|
|
- `associatedName`
|
|
|
|
- `associatedIndex`
|
|
|
|
- `resourceName`
|
|
|
|
- `resourceIndex`
|
|
|
|
|
|
|
|
request body
|
|
|
|
|
|
|
|
- `values` bodyparser 之后的 body
|
|
|
|
|
|
|
|
request query
|
|
|
|
|
|
|
|
- `filter`
|
|
|
|
- `fields`
|
|
|
|
- `pageSize`
|
|
|
|
- `page`
|
|
|
|
- `sort`
|
|
|
|
- 其他参数
|
|
|
|
|
|
|
|
### action.mergeParams();
|
|
|
|
|
|
|
|
```ts
|
|
|
|
interface mergeParams {
|
|
|
|
(params: ActionParams, strategies?: MergeStrategies): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
type MergeStrategyType = 'merge' | 'deepMerge' | 'overwrite' | 'andMerge' | 'orMerge' | 'intersect' | 'union';
|
|
|
|
type MergeStrategyFunc = (x: any, y: any) => any;
|
|
|
|
type MergeStrategy = MergeStrategyType | MergeStrategyFunc;
|
|
|
|
|
|
|
|
interface MergeStrategies {
|
|
|
|
[key: string]: MergeStrategy;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
合并策略
|
|
|
|
|
|
|
|
- `merge` 浅合并
|
|
|
|
- `deepMerge` 深层合并(默认)
|
|
|
|
- `andMerge` and 合并,用于 filter 参数
|
|
|
|
- `orMerge` or 合并,用于 filter 参数
|
|
|
|
- `overwrite` 覆盖
|
|
|
|
- `intersect` 交集,用于 array 类型,也支持逗号间隔的字符串数组
|
|
|
|
- `union` 并集,去重,用于 array 类型,也支持逗号间隔的字符串数组
|
|
|
|
|
|
|
|
特定参数的默认合并策略
|
|
|
|
|
|
|
|
```ts
|
|
|
|
{
|
|
|
|
filter: 'andMerge',
|
|
|
|
fields: 'intersect',
|
|
|
|
appends: 'union',
|
|
|
|
except: 'union',
|
|
|
|
whitelist: 'intersect',
|
|
|
|
blacklist: 'union',
|
|
|
|
sort: 'overwrite',
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
示例
|
|
|
|
|
|
|
|
```ts
|
|
|
|
ctx.action.mergeParams({
|
|
|
|
filter: { b: 'b1' },
|
|
|
|
fields: 'a1,b1,c1',
|
|
|
|
key1: 'abcdef',
|
|
|
|
}, {
|
|
|
|
// filter 参数采用 orMerge 策略
|
|
|
|
filter: 'orMerge',
|
|
|
|
// fields 参数采用 intersect 策略
|
|
|
|
fields: 'intersect',
|
|
|
|
// key1 自定义函数
|
|
|
|
key1: (x, y) => y.split(''),
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2021-10-28 22:55:51 +08:00
|
|
|
## ctx.i18n
|
|
|
|
|
2021-11-09 15:08:24 +08:00
|
|
|
app.i18n 的 cloneInstance。详情见 [I18next API](https://www.i18next.com/overview/api)
|
|
|
|
|
|
|
|
##### Definition
|
|
|
|
|
|
|
|
```ts
|
|
|
|
interface Context {
|
|
|
|
i18n: I18next.I18n;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
##### Examples
|
|
|
|
|
|
|
|
```ts
|
|
|
|
async (ctx, next) {
|
|
|
|
await ctx.i18n.changeLanguage('zh-CN');
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-10-28 22:55:51 +08:00
|
|
|
## ctx.t()
|
|
|
|
|