@buka/nestjs-kit 3.0.1 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/README.md +2 -0
- package/dist/index.d.mts +18530 -259
- package/dist/index.d.ts +18530 -259
- package/dist/index.js +9965 -2497
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +9665 -2429
- package/dist/index.mjs.map +1 -1
- package/docs/blind-index.md +107 -0
- package/docs/core/converters.md +409 -0
- package/docs/core/index.md +331 -0
- package/docs/core/models.md +303 -0
- package/docs/core/urn.md +214 -0
- package/docs/envelope-encryption.md +197 -0
- package/docs/exception.md +388 -0
- package/docs/index.md +83 -0
- package/docs/mikro-orm.md +471 -0
- package/docs/object-storage.md +193 -0
- package/docs/open-bao.md +168 -0
- package/docs/salted-hash.md +130 -0
- package/docs/swagger-patcher.md +60 -0
- package/package.json +50 -39
- package/.czrc +0 -3
- package/.vscode/tasks.json +0 -21
- package/coverage/cobertura-coverage.xml +0 -1173
- package/eslint.config.mjs +0 -18
- package/jest.config.cts +0 -23
- package/pnpm-workspace.yaml +0 -4
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# BlindIndexModule — 盲索引模块
|
|
2
|
+
|
|
3
|
+
## 概述
|
|
4
|
+
|
|
5
|
+
`BlindIndexModule` 提供盲索引(Blind Index)能力,用于对敏感数据生成不可逆的哈希值,以便在加密存储场景下仍可进行等值查询,而无需暴露原始明文。
|
|
6
|
+
|
|
7
|
+
典型场景:用户邮箱已通过信封加密存储,无法直接查询。此时可对邮箱生成盲索引,将索引值存入数据库,查询时对输入邮箱计算同样的盲索引进行匹配。
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
import { BlindIndexModule, BlindIndexService, BlindIndex } from '@buka/nestjs-kit'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## 模块配置
|
|
14
|
+
|
|
15
|
+
`BlindIndexModule` 无需任何配置,直接导入即可:
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { Module } from '@nestjs/common'
|
|
19
|
+
import { BlindIndexModule } from '@buka/nestjs-kit'
|
|
20
|
+
|
|
21
|
+
@Module({
|
|
22
|
+
imports: [BlindIndexModule],
|
|
23
|
+
})
|
|
24
|
+
export class AppModule {}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## BlindIndexService
|
|
28
|
+
|
|
29
|
+
### generate
|
|
30
|
+
|
|
31
|
+
对数据生成盲索引,始终使用最新版本的哈希算法。
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
import { Injectable } from '@nestjs/common'
|
|
35
|
+
import { BlindIndexService, BlindIndex } from '@buka/nestjs-kit'
|
|
36
|
+
|
|
37
|
+
@Injectable()
|
|
38
|
+
export class UserService {
|
|
39
|
+
constructor(private readonly blindIndexService: BlindIndexService) {}
|
|
40
|
+
|
|
41
|
+
async createUser(email: string) {
|
|
42
|
+
const index: BlindIndex = await this.blindIndexService.generate(email)
|
|
43
|
+
// index.value → SHA-256 哈希值(64 字符 hex 字符串)
|
|
44
|
+
// index.version → 1
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async findByEmail(email: string) {
|
|
48
|
+
const index = await this.blindIndexService.generate(email)
|
|
49
|
+
return this.userRepo.findOne({ emailIndex: { value: index.value } })
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
**方法签名**
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
async generate(data: JsonValue): Promise<BlindIndex>
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
| 参数 | 类型 | 说明 |
|
|
61
|
+
|------|------|------|
|
|
62
|
+
| `data` | `JsonValue` | 待哈希的 JSON 数据(`string`、`number`、`boolean`、`object`、`array`) |
|
|
63
|
+
|
|
64
|
+
> `data` 不能为 `null` 或 `undefined`,否则抛出 `TypeError`。
|
|
65
|
+
|
|
66
|
+
## BlindIndex 可嵌入实体
|
|
67
|
+
|
|
68
|
+
MikroORM 嵌入式实体,用于在数据库中存储盲索引。所有字段标记为 `hidden`,不会被序列化到 API 响应。
|
|
69
|
+
|
|
70
|
+
| 字段 | 类型 | 说明 |
|
|
71
|
+
|------|------|------|
|
|
72
|
+
| `value` | `char(64)` | 哈希值 |
|
|
73
|
+
| `version` | `smallint` | 哈希算法版本号 |
|
|
74
|
+
|
|
75
|
+
### 在实体中使用
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
import { Entity } from '@mikro-orm/core'
|
|
79
|
+
import { DiscreteEntity, Column } from '@buka/nestjs-kit/mikro-orm'
|
|
80
|
+
import { BlindIndex } from '@buka/nestjs-kit'
|
|
81
|
+
|
|
82
|
+
@Entity()
|
|
83
|
+
export class UserEntity extends DiscreteEntity {
|
|
84
|
+
@Column.Embedded(() => BlindIndex)
|
|
85
|
+
emailIndex!: BlindIndex
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## V1 实现
|
|
90
|
+
|
|
91
|
+
- **算法**:SHA-256
|
|
92
|
+
- **输入处理**:使用 stable JSON 序列化(`stableStringify`)确保相同数据始终生成相同的哈希值,不受对象属性顺序影响
|
|
93
|
+
- **输出**:64 字符的 hex 字符串
|
|
94
|
+
|
|
95
|
+
```
|
|
96
|
+
输入: "user@example.com"
|
|
97
|
+
→ stableStringify → '"user@example.com"'
|
|
98
|
+
→ SHA-256 → "a1b2c3d4..."(64 字符 hex)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## 注意事项
|
|
102
|
+
|
|
103
|
+
> 盲索引是确定性的:相同的输入始终产生相同的哈希值。这是等值查询的基础,但也意味着攻击者可以通过构建彩虹表来反向查找。对于高敏感数据,建议配合信封加密使用。
|
|
104
|
+
|
|
105
|
+
> 对象类型的数据会先通过 stable JSON 序列化,确保 `{ a: 1, b: 2 }` 和 `{ b: 2, a: 1 }` 生成相同的索引。
|
|
106
|
+
|
|
107
|
+
> `version` 字段用于支持算法升级。当引入新版本哈希算法时,旧数据的索引仍保持有效,新数据使用新算法。
|
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
# Converters — 类型转换工具
|
|
2
|
+
|
|
3
|
+
## 概述
|
|
4
|
+
|
|
5
|
+
`@buka/nestjs-kit` 提供了一组基于 Model 元数据的类型转换工具(Converters),用于从已有模型类派生出新的类(DTO、查询对象、响应体等)。
|
|
6
|
+
|
|
7
|
+
核心能力:
|
|
8
|
+
|
|
9
|
+
- **派生新类**:从 `@Model()` 注册的模型中选取、排除、合并属性,快速生成 DTO
|
|
10
|
+
- **自动复制元数据**:派生类自动继承原始类的 Swagger (`@ApiProperty`)、class-validator、class-transformer 装饰器元数据
|
|
11
|
+
- **类型安全**:所有转换器均保留完整的 TypeScript 类型推导
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import {
|
|
15
|
+
PickType,
|
|
16
|
+
OmitType,
|
|
17
|
+
PartialType,
|
|
18
|
+
ResponseBodyType,
|
|
19
|
+
ListResponseBodyType,
|
|
20
|
+
FilterQuery,
|
|
21
|
+
IFilter,
|
|
22
|
+
OrderQueryType,
|
|
23
|
+
} from '@buka/nestjs-kit'
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
> `IntersectionType` 同样从 `@buka/nestjs-kit` 导入。
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## 基础转换器
|
|
31
|
+
|
|
32
|
+
### PickType
|
|
33
|
+
|
|
34
|
+
从模型类中**选取指定属性**,生成仅包含这些属性的新类。
|
|
35
|
+
|
|
36
|
+
**函数签名**
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
function PickType<T, K extends keyof T>(
|
|
40
|
+
classRef: Class<T>,
|
|
41
|
+
keys: K[]
|
|
42
|
+
): Class<Pick<T, K>>
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
**示例**
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import { PickType } from '@buka/nestjs-kit'
|
|
49
|
+
|
|
50
|
+
@Model()
|
|
51
|
+
class User {
|
|
52
|
+
@Property({ schema: { type: 'string' } })
|
|
53
|
+
name: string
|
|
54
|
+
|
|
55
|
+
@Property({ schema: { type: 'string' } })
|
|
56
|
+
email: string
|
|
57
|
+
|
|
58
|
+
@Property({ schema: { type: 'number' } })
|
|
59
|
+
age: number
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// 仅包含 name 和 email
|
|
63
|
+
class UserContactDto extends PickType(User, ['name', 'email']) {}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
> 源类必须使用 `@Model()` 装饰器注册,否则会抛出 `TypeError`。
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
### OmitType
|
|
71
|
+
|
|
72
|
+
从模型类中**排除指定属性**,生成不包含这些属性的新类。
|
|
73
|
+
|
|
74
|
+
**函数签名**
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
function OmitType<T, K extends keyof T>(
|
|
78
|
+
classRef: Class<T>,
|
|
79
|
+
keys: K[]
|
|
80
|
+
): Class<Omit<T, K>>
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
**示例**
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
import { OmitType } from '@buka/nestjs-kit'
|
|
87
|
+
|
|
88
|
+
// 排除 age,保留 name 和 email
|
|
89
|
+
class CreateUserDto extends OmitType(User, ['age']) {}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
> 源类必须使用 `@Model()` 装饰器注册,否则会抛出 `TypeError`。
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
### PartialType
|
|
97
|
+
|
|
98
|
+
将模型类的**所有属性变为可选**,生成新类。
|
|
99
|
+
|
|
100
|
+
**函数签名**
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
function PartialType<T>(classRef: Class<T>): Class<Partial<T>>
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
**示例**
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
import { PartialType } from '@buka/nestjs-kit'
|
|
110
|
+
|
|
111
|
+
// 所有属性均为可选,适合用于更新接口的 DTO
|
|
112
|
+
class UpdateUserDto extends PartialType(User) {}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
> `PartialType` 不要求源类必须使用 `@Model()`,但如果源类未注册,则不会复制 Model 属性元数据。
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
|
|
119
|
+
### IntersectionType
|
|
120
|
+
|
|
121
|
+
**合并多个类**的属性,生成包含所有类属性的新类。
|
|
122
|
+
|
|
123
|
+
**函数签名**
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
function IntersectionType<T extends Type[]>(
|
|
127
|
+
...classRefs: T
|
|
128
|
+
): Type<Intersection of all T>
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
**示例**
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
import { IntersectionType } from '@buka/nestjs-kit'
|
|
135
|
+
|
|
136
|
+
@Model()
|
|
137
|
+
class UserBase {
|
|
138
|
+
@Property({ schema: { type: 'string' } })
|
|
139
|
+
name: string
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
@Model()
|
|
143
|
+
class UserExtra {
|
|
144
|
+
@Property({ schema: { type: 'number' } })
|
|
145
|
+
score: number
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// 同时包含 name 和 score
|
|
149
|
+
class FullUserDto extends IntersectionType(UserBase, UserExtra) {}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
> 支持传入两个或多个类,属性名冲突时以先传入的类为准。
|
|
153
|
+
|
|
154
|
+
---
|
|
155
|
+
|
|
156
|
+
## 响应体转换器
|
|
157
|
+
|
|
158
|
+
### ResponseBodyType
|
|
159
|
+
|
|
160
|
+
生成**单对象响应包装类**,结构为 `{ data }` 形式。
|
|
161
|
+
|
|
162
|
+
**函数签名**
|
|
163
|
+
|
|
164
|
+
```typescript
|
|
165
|
+
function ResponseBodyType<T extends object>(
|
|
166
|
+
classRef: Class<T>
|
|
167
|
+
): Class<IResponseBody<T>> & { from(data: T): IResponseBody<T> }
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
**响应体结构**
|
|
171
|
+
|
|
172
|
+
```typescript
|
|
173
|
+
interface IResponseBody<DATA> {
|
|
174
|
+
data: DATA
|
|
175
|
+
meta: { [key: string]: any }
|
|
176
|
+
}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
**示例**
|
|
180
|
+
|
|
181
|
+
```typescript
|
|
182
|
+
import { ResponseBodyType } from '@buka/nestjs-kit'
|
|
183
|
+
|
|
184
|
+
class UserResponseBody extends ResponseBodyType(User) {}
|
|
185
|
+
|
|
186
|
+
// 在 Controller 中使用
|
|
187
|
+
@Get(':id')
|
|
188
|
+
async findOne(@Param('id') id: string): Promise<UserResponseBody> {
|
|
189
|
+
const user = await this.userService.findOne(id)
|
|
190
|
+
return UserResponseBody.from(user)
|
|
191
|
+
}
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
> `ResponseBodyType` 内部使用 `@Composite()` 装饰 `data` 属性,自动生成 Swagger 文档。
|
|
195
|
+
|
|
196
|
+
**自动序列化**
|
|
197
|
+
|
|
198
|
+
`ResponseBodyType` 生成的类内置了 `toJSON()` 方法,在 JSON 序列化时会根据 `@Model()` 元数据递归处理 `data` 属性。对于 MikroORM 实体,已 populate 的关联会完整序列化,未 populate 的关联仅返回主键,无需手动处理。
|
|
199
|
+
|
|
200
|
+
---
|
|
201
|
+
|
|
202
|
+
### ListResponseBodyType
|
|
203
|
+
|
|
204
|
+
生成**列表响应包装类**,结构为 `{ data[], meta: { pagination } }` 形式,支持偏移分页和游标分页。
|
|
205
|
+
|
|
206
|
+
**函数签名**
|
|
207
|
+
|
|
208
|
+
```typescript
|
|
209
|
+
function ListResponseBodyType<T extends object, MODE extends 'offset' | 'cursor'>(
|
|
210
|
+
classRef: Type<T>,
|
|
211
|
+
mode?: MODE
|
|
212
|
+
): Class<IListResponseBody<T, MODE>> & { fromSlice(slice: Slice<T>): IListResponseBody<T, MODE> }
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
**参数**
|
|
216
|
+
|
|
217
|
+
| 参数 | 类型 | 说明 |
|
|
218
|
+
| --- | --- | --- |
|
|
219
|
+
| `classRef` | `Type<T>` | 列表元素的模型类 |
|
|
220
|
+
| `mode` | `'offset' \| 'cursor'` | 分页模式。`'offset'` 使用 `OffsetPagination`,`'cursor'` 使用 `CursorPagination` |
|
|
221
|
+
|
|
222
|
+
**响应体结构**
|
|
223
|
+
|
|
224
|
+
```typescript
|
|
225
|
+
interface IListResponseBody<DATA, MODE> {
|
|
226
|
+
data: DATA[]
|
|
227
|
+
meta: {
|
|
228
|
+
pagination: OffsetPagination | CursorPagination // 取决于 MODE
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
**示例**
|
|
234
|
+
|
|
235
|
+
```typescript
|
|
236
|
+
import { ListResponseBodyType } from '@buka/nestjs-kit'
|
|
237
|
+
|
|
238
|
+
// 偏移分页
|
|
239
|
+
class UserListResponseBody extends ListResponseBodyType(User, 'offset') {}
|
|
240
|
+
|
|
241
|
+
// 在 Controller 中使用
|
|
242
|
+
@Get()
|
|
243
|
+
async findAll(): Promise<UserListResponseBody> {
|
|
244
|
+
const slice = await this.userService.findAll()
|
|
245
|
+
return UserListResponseBody.fromSlice(slice)
|
|
246
|
+
}
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
> `fromSlice` 方法接受 `Slice<T>` 对象,自动提取 `data` 和 `pagination` 信息。
|
|
250
|
+
|
|
251
|
+
**自动序列化**
|
|
252
|
+
|
|
253
|
+
`ListResponseBodyType` 生成的类内置了 `toJSON()` 方法,在 JSON 序列化时会根据 `@Model()` 元数据递归处理 `data` 数组中的每个元素。对于 MikroORM 实体,已 populate 的关联会完整序列化,未 populate 的关联仅返回主键,无需手动处理。
|
|
254
|
+
|
|
255
|
+
---
|
|
256
|
+
|
|
257
|
+
## 查询转换器
|
|
258
|
+
|
|
259
|
+
### FilterQuery
|
|
260
|
+
|
|
261
|
+
`@FilterQuery()` 参数装饰器,从 query string 中提取 `filter` 参数并自动验证。HTTP 请求使用无前缀的操作符格式(`eq`、`ne`、`gte`),NestJS Controller 接收到的数据自动转换为 `$eq`、`$ne`、`$gte` 格式。
|
|
262
|
+
|
|
263
|
+
**查询结构**
|
|
264
|
+
|
|
265
|
+
```typescript
|
|
266
|
+
// 完整查询类型(用于 Controller 参数)
|
|
267
|
+
interface IFilterQuery<T> {
|
|
268
|
+
filter?: IFilterQueryObject<T>
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// 快捷类型(用于 Service 层,等价于 IFilterQuery<T>['filter'])
|
|
272
|
+
type IFilter<T> = IFilterQueryObject<T> | undefined
|
|
273
|
+
|
|
274
|
+
// HTTP 请求中使用无前缀操作符
|
|
275
|
+
// GET /users?filter={"name":{"eq":"Alice"},"age":{"gte":18}}
|
|
276
|
+
|
|
277
|
+
// Controller 接收到的类型(自动添加 $ 前缀)
|
|
278
|
+
interface IFilterQueryCondition<T> {
|
|
279
|
+
$eq?: T
|
|
280
|
+
$ne?: T
|
|
281
|
+
$lt?: T
|
|
282
|
+
$gt?: T
|
|
283
|
+
$lte?: T
|
|
284
|
+
$gte?: T
|
|
285
|
+
$in?: T[]
|
|
286
|
+
$nin?: T[]
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// 集合关系属性支持以下操作符(HTTP 用 some/every/none,Controller 收到 $some/$every/$none)
|
|
290
|
+
interface IFilterQueryCollectionCondition<T> {
|
|
291
|
+
$some?: IFilterQueryObject<T>
|
|
292
|
+
$every?: IFilterQueryObject<T>
|
|
293
|
+
$none?: IFilterQueryObject<T>
|
|
294
|
+
}
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
**示例**
|
|
298
|
+
|
|
299
|
+
```typescript
|
|
300
|
+
import { FilterQuery, IFilterQuery, IFilter } from '@buka/nestjs-kit'
|
|
301
|
+
|
|
302
|
+
// Controller 中使用 IFilterQuery<T>
|
|
303
|
+
@Get()
|
|
304
|
+
async findAll(@FilterQuery(User) query: IFilterQuery<User>) {
|
|
305
|
+
// HTTP: ?filter={"name":{"eq":"Alice"},"age":{"gte":18}}
|
|
306
|
+
// query.filter?.name?.$eq === 'Alice'
|
|
307
|
+
// query.filter?.age?.$gte === 18
|
|
308
|
+
return this.userService.findAll(query.filter)
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// Service 中使用 IFilter<T>
|
|
312
|
+
class UserService {
|
|
313
|
+
findAll(filter: IFilter<User>) {
|
|
314
|
+
// filter?.name?.$eq === 'Alice'
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
**自定义操作符**
|
|
320
|
+
|
|
321
|
+
通过 `@FilterQueryOperators` 装饰器可以限制某个属性允许使用的操作符:
|
|
322
|
+
|
|
323
|
+
```typescript
|
|
324
|
+
import { FilterQueryOperators } from '@buka/nestjs-kit'
|
|
325
|
+
|
|
326
|
+
@Model()
|
|
327
|
+
class User {
|
|
328
|
+
@Property({ schema: { type: 'string' } })
|
|
329
|
+
@FilterQueryOperators(['eq', 'ne', 'in'])
|
|
330
|
+
name: string
|
|
331
|
+
}
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
> 源类必须使用 `@Model()` 装饰器注册,否则会抛出 `TypeError`。关联属性会自动生成嵌套查询结构。
|
|
335
|
+
|
|
336
|
+
---
|
|
337
|
+
|
|
338
|
+
### OrderQueryType
|
|
339
|
+
|
|
340
|
+
生成**排序查询类**,基于模型属性自动构建排序 JSON Schema。
|
|
341
|
+
|
|
342
|
+
**函数签名**
|
|
343
|
+
|
|
344
|
+
```typescript
|
|
345
|
+
function OrderQueryType<T>(classRef: Class<T>): Class<IOrderQuery<T>>
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
**查询结构**
|
|
349
|
+
|
|
350
|
+
```typescript
|
|
351
|
+
interface IOrderQuery<T> {
|
|
352
|
+
orderBy?: QueryOrderMap<T> | QueryOrderMap<T>[]
|
|
353
|
+
}
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
**示例**
|
|
357
|
+
|
|
358
|
+
```typescript
|
|
359
|
+
import { OrderQueryType } from '@buka/nestjs-kit'
|
|
360
|
+
|
|
361
|
+
class UserOrderQuery extends OrderQueryType(User) {}
|
|
362
|
+
|
|
363
|
+
// 在 Controller 中使用
|
|
364
|
+
@Get()
|
|
365
|
+
async findAll(@Query() query: UserOrderQuery) {
|
|
366
|
+
// query.orderBy === { name: 'asc' }
|
|
367
|
+
// 或数组形式: [{ name: 'asc' }, { age: 'desc' }]
|
|
368
|
+
}
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
> `orderBy` 属性为可选字段,通过 JSON Schema 进行校验,支持 `QueryOrderMap` 单个或数组形式。
|
|
372
|
+
|
|
373
|
+
---
|
|
374
|
+
|
|
375
|
+
## 组合使用
|
|
376
|
+
|
|
377
|
+
转换器可以自由组合,快速构建复杂的 DTO 和查询对象:
|
|
378
|
+
|
|
379
|
+
```typescript
|
|
380
|
+
import {
|
|
381
|
+
PickType,
|
|
382
|
+
PartialType,
|
|
383
|
+
FilterQuery,
|
|
384
|
+
OrderQueryType,
|
|
385
|
+
ListResponseBodyType,
|
|
386
|
+
} from '@buka/nestjs-kit'
|
|
387
|
+
|
|
388
|
+
// 创建 DTO
|
|
389
|
+
class CreateUserDto extends PickType(User, ['name', 'email', 'age']) {}
|
|
390
|
+
|
|
391
|
+
// 更新 DTO(所有字段可选)
|
|
392
|
+
class UpdateUserDto extends PartialType(CreateUserDto) {}
|
|
393
|
+
|
|
394
|
+
// 排序查询参数
|
|
395
|
+
class UserOrderQuery extends OrderQueryType(User) {}
|
|
396
|
+
|
|
397
|
+
// 列表响应
|
|
398
|
+
class UserListResponse extends ListResponseBodyType(User, 'offset') {}
|
|
399
|
+
|
|
400
|
+
// 在 Controller 中组合使用
|
|
401
|
+
@Get()
|
|
402
|
+
async findAll(
|
|
403
|
+
@FilterQuery(User) query: IFilterQuery<User>,
|
|
404
|
+
@Query() order: UserOrderQuery,
|
|
405
|
+
) {
|
|
406
|
+
// query.filter?.name?.$eq === 'Alice'
|
|
407
|
+
// order.orderBy === { name: 'asc' }
|
|
408
|
+
}
|
|
409
|
+
```
|