@buka/nestjs-kit 4.3.1 → 4.3.3
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 +14 -0
- package/dist/index.d.mts +55 -33
- package/dist/index.d.ts +55 -33
- package/dist/index.js +129 -85
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +129 -85
- package/dist/index.mjs.map +1 -1
- package/docs/core/converters.md +4 -0
- package/docs/core/models.md +54 -0
- package/docs/mikro-orm.md +61 -0
- package/package.json +1 -1
package/docs/core/converters.md
CHANGED
|
@@ -197,6 +197,8 @@ async findOne(@Param('id') id: string): Promise<UserResponseBody> {
|
|
|
197
197
|
|
|
198
198
|
`ResponseBodyType` 生成的类内置了 `toJSON()` 方法,在 JSON 序列化时会根据 `@Model()` 元数据递归处理 `data` 属性。对于 MikroORM 实体,已 populate 的关联会完整序列化,未 populate 的关联仅返回主键,无需手动处理。
|
|
199
199
|
|
|
200
|
+
> `lazy: true` 的属性(如 `OneToMany`、`ManyToMany` 集合)默认不注册 Swagger schema,因此不会出现在 Swagger schema 中。这确保了 API 类型与 `findOne()` 不加 populate 的运行时结果一致。如需在响应中包含 lazy 属性,请使用 [EntityDto](/mikro-orm#entitydto) 派生 DTO 并手动声明。详见 [懒加载属性](/core/models#懒加载属性lazy)。
|
|
201
|
+
|
|
200
202
|
---
|
|
201
203
|
|
|
202
204
|
### ListResponseBodyType
|
|
@@ -252,6 +254,8 @@ async findAll(): Promise<UserListResponseBody> {
|
|
|
252
254
|
|
|
253
255
|
`ListResponseBodyType` 生成的类内置了 `toJSON()` 方法,在 JSON 序列化时会根据 `@Model()` 元数据递归处理 `data` 数组中的每个元素。对于 MikroORM 实体,已 populate 的关联会完整序列化,未 populate 的关联仅返回主键,无需手动处理。
|
|
254
256
|
|
|
257
|
+
> `lazy: true` 的属性(如 `OneToMany`、`ManyToMany` 集合)默认不注册 Swagger schema,因此不会出现在 Swagger schema 中。这确保了 API 类型与 `findOne()` 不加 populate 的运行时结果一致。如需在响应中包含 lazy 属性,请使用 [EntityDto](/mikro-orm#entitydto) 派生 DTO 并手动声明。详见 [懒加载属性](/core/models#懒加载属性lazy)。
|
|
258
|
+
|
|
255
259
|
---
|
|
256
260
|
|
|
257
261
|
## 查询转换器
|
package/docs/core/models.md
CHANGED
|
@@ -118,6 +118,7 @@ class UserProfile {
|
|
|
118
118
|
| --- | --- | --- | --- |
|
|
119
119
|
| `type` | `() => Class<object>` | 是 | 返回嵌套对象的类(使用惰性函数避免循环引用) |
|
|
120
120
|
| `optional` | `boolean` | 否 | 标记为可选,默认 `false` |
|
|
121
|
+
| `lazy` | `boolean` | 否 | 标记为懒加载属性,默认 `false`。`lazy: true` 时不注册 Swagger schema,详见[懒加载属性](#懒加载属性lazy) |
|
|
121
122
|
| `schema` | `ApiPropertyOptions` | 否 | Swagger 配置,默认使用 `{ type: () => options.type() }` |
|
|
122
123
|
| `association` | `AssociationMetadata` | 否 | 关联元数据,用于描述 ORM 关系 |
|
|
123
124
|
|
|
@@ -153,6 +154,7 @@ class Order {
|
|
|
153
154
|
| --- | --- | --- | --- |
|
|
154
155
|
| `type` | `(() => Class<object>)` \| `ScalarClass` | 是 | 数组元素类型。标量传 `String` / `Number` / `Boolean`;对象传惰性函数 |
|
|
155
156
|
| `optional` | `boolean` | 否 | 标记为可选,默认 `false` |
|
|
157
|
+
| `lazy` | `boolean` | 否 | 标记为懒加载属性,默认 `false`。`lazy: true` 时不注册 Swagger schema,详见[懒加载属性](#懒加载属性lazy) |
|
|
156
158
|
| `schema` | `ApiPropertyOptions` | 否 | Swagger 配置 |
|
|
157
159
|
| `association` | `AssociationMetadata` | 否 | 关联元数据 |
|
|
158
160
|
|
|
@@ -267,6 +269,58 @@ interface AssociationMetadata {
|
|
|
267
269
|
|
|
268
270
|
在业务层无需手动设置 `association`,它由 ORM 关系装饰器自动注入。查询关联信息时可通过 `ModelRegister.getProperty()` 获取。
|
|
269
271
|
|
|
272
|
+
## 懒加载属性(lazy)
|
|
273
|
+
|
|
274
|
+
`lazy` 选项用于标记那些**默认不会被加载**的属性。当 `lazy: true` 时,装饰器不会为该属性注册 Swagger schema(`@ApiProperty`),因此该属性**不会出现在 API 文档中**。
|
|
275
|
+
|
|
276
|
+
### 设计意图
|
|
277
|
+
|
|
278
|
+
Entity 的 Swagger schema 默认应与 `findOne()` **不加任何 populate** 的 `toJSON()` 结果保持一致。MikroORM 默认不加载 Collection 关系(OneToMany / ManyToMany),因此这些属性不应出现在默认的 API 类型中。
|
|
279
|
+
|
|
280
|
+
### 哪些属性默认是 lazy 的
|
|
281
|
+
|
|
282
|
+
`Cardinality.OneToMany` 和 `Cardinality.ManyToMany` 装饰器默认设置 `lazy: true`:
|
|
283
|
+
|
|
284
|
+
```typescript
|
|
285
|
+
@Entity()
|
|
286
|
+
export class AuthorEntity extends DiscreteEntity {
|
|
287
|
+
@Cardinality.OneToMany(() => BookEntity, (book) => book.author)
|
|
288
|
+
books = new Collection<BookEntity>(this) // lazy: true,不在 Swagger 中
|
|
289
|
+
}
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
`Cardinality.ManyToOne` 和 `Cardinality.OneToOne` **不设置** `lazy: true`,因为未 populate 时它们序列化为 `{ id: "xxx" }` 主键引用,与默认查询结果一致。
|
|
293
|
+
|
|
294
|
+
### 如何包含 lazy 属性
|
|
295
|
+
|
|
296
|
+
需要在 API 响应中返回 lazy 属性时,使用 `EntityDto` 派生基本 DTO 后手动声明:
|
|
297
|
+
|
|
298
|
+
```typescript
|
|
299
|
+
// 基本 DTO(lazy 属性自动排除,无 Collection/Ref 类型冲突)
|
|
300
|
+
export class AuthorBriefDto extends EntityDto(AuthorEntity) {}
|
|
301
|
+
|
|
302
|
+
// 详情 DTO(手动添加 lazy 属性)
|
|
303
|
+
export class AuthorDetailDto extends EntityDto(AuthorEntity) {
|
|
304
|
+
@List({ type: () => PrimaryKeyType(BookEntity) })
|
|
305
|
+
books!: PrimaryKeyTypeClass[]
|
|
306
|
+
}
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
> 直接 `extends AuthorEntity` 会导致 `Collection<Book>` 与 `Book[]` 类型冲突。`EntityDto` 返回不继承实体的独立类,避免了此问题。
|
|
310
|
+
|
|
311
|
+
同时在 Service 层显式 `populate: ['books']`,确保运行时数据与类型声明一致。
|
|
312
|
+
|
|
313
|
+
### eager 选项
|
|
314
|
+
|
|
315
|
+
在 `Cardinality.OneToMany` 或 `Cardinality.ManyToMany` 中设置 `eager: true` 会覆盖默认的 `lazy: true`,使该集合直接出现在 Swagger schema 中:
|
|
316
|
+
|
|
317
|
+
```typescript
|
|
318
|
+
@Cardinality.OneToMany({ entity: () => BookEntity, mappedBy: 'author', eager: true })
|
|
319
|
+
books = new Collection<BookEntity>(this) // 出现在 Swagger 中
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
> 不推荐使用 `eager: true`,因为每次查询都需要 populate 该集合。建议使用派生 DTO 方式按需包含。
|
|
323
|
+
|
|
270
324
|
## ModelRegister
|
|
271
325
|
|
|
272
326
|
`ModelRegister` 是元数据注册中心,基于 `reflect-metadata` 存储和检索模型与属性信息。
|
package/docs/mikro-orm.md
CHANGED
|
@@ -286,6 +286,8 @@ export class AuthorEntity extends DiscreteEntity {
|
|
|
286
286
|
}
|
|
287
287
|
```
|
|
288
288
|
|
|
289
|
+
> `OneToMany` 默认 `lazy: true`,该集合**不会**出现在 Swagger 文档中。这与 MikroORM 默认不 populate 集合的行为一致——`findOne()` 的 `toJSON()` 结果不含未填充的 Collection。如需在响应中包含此集合,请使用 `EntityDto` 派生 DTO 并手动声明,同时显式 `populate`。详见 [EntityDto](/core/converters#entitydto) 和 [懒加载属性(lazy)](/core/models#懒加载属性lazy)。
|
|
290
|
+
|
|
289
291
|
### Cardinality.OneToOne — 一对一关系
|
|
290
292
|
|
|
291
293
|
**场景示例**:一个用户有一份详细资料
|
|
@@ -314,11 +316,70 @@ export class BookEntity extends DiscreteEntity {
|
|
|
314
316
|
}
|
|
315
317
|
```
|
|
316
318
|
|
|
319
|
+
> `ManyToMany` 默认 `lazy: true`,该集合**不会**出现在 Swagger 文档中。这与 MikroORM 默认不 populate 集合的行为一致。如需包含,请使用 `EntityDto` 派生 DTO 并手动声明,或设置 `eager: true`。详见 [EntityDto](/core/converters#entitydto) 和 [懒加载属性(lazy)](/core/models#懒加载属性lazy)。
|
|
320
|
+
|
|
317
321
|
> 所有关系装饰器都会自动添加 Swagger 文档和验证规则,无需手动配置。
|
|
318
322
|
> 当关系设置了 `hidden: true` 时,仅应用 `@ApiHideProperty()` 而不会注册模型属性元数据。
|
|
319
323
|
|
|
320
324
|
---
|
|
321
325
|
|
|
326
|
+
## EntityDto
|
|
327
|
+
|
|
328
|
+
从实体生成**无继承关系**的纯 DTO 类,用于安全派生响应类型。
|
|
329
|
+
|
|
330
|
+
### 解决的问题
|
|
331
|
+
|
|
332
|
+
直接 `extends` MikroORM 实体会引入 `Collection<Entity>` 类型。当你在派生 DTO 中重新声明同名字段(如 `comments: PrimaryKeyTypeClass[]`)时,TypeScript 会报类型冲突——父类的 `Collection<CommentEntity>` 与子类的 `PrimaryKeyTypeClass[]` 不兼容。
|
|
333
|
+
|
|
334
|
+
`EntityDto` 返回的类**不继承**源实体,因此不存在此冲突。同时它自动排除 `lazy: true` 的属性,Swagger schema 与 `findOne()` 不加 populate 的结果一致。
|
|
335
|
+
|
|
336
|
+
### 类型转换
|
|
337
|
+
|
|
338
|
+
`EntityDtoShape<T>` 对实体类型做以下变换:
|
|
339
|
+
|
|
340
|
+
| 实体类型 | DTO 类型 | 原因 |
|
|
341
|
+
|---------|---------|------|
|
|
342
|
+
| `Collection<Entity>` | 键被排除 | 默认 `lazy: true`,不在 Swagger 中 |
|
|
343
|
+
| `Ref<Entity>` | `Ref<Entity>`(保留) | nestjs-kit 一等公民,序列化层自动处理 |
|
|
344
|
+
| `Xxx & Opt` | `Xxx \| undefined` | 去除 MikroORM 标记 |
|
|
345
|
+
|
|
346
|
+
### 示例
|
|
347
|
+
|
|
348
|
+
```typescript
|
|
349
|
+
import { EntityDto } from '@buka/nestjs-kit/mikro-orm'
|
|
350
|
+
|
|
351
|
+
@Entity()
|
|
352
|
+
class ArticleEntity extends DiscreteEntity {
|
|
353
|
+
@Column.Varchar({ length: 64, comment: '标题' })
|
|
354
|
+
title!: string
|
|
355
|
+
|
|
356
|
+
@Column.Text({ comment: '正文内容', lazy: true })
|
|
357
|
+
content?: string
|
|
358
|
+
|
|
359
|
+
@Cardinality.ManyToOne({ entity: () => AuthorEntity, ref: true })
|
|
360
|
+
author!: Ref<AuthorEntity>
|
|
361
|
+
|
|
362
|
+
@Cardinality.OneToMany({ entity: () => CommentEntity, mappedBy: 'article' })
|
|
363
|
+
comments = new Collection<CommentEntity>(this)
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// 基本 DTO — 排除 content(标量 lazy)和 comments(集合 lazy)
|
|
367
|
+
class ArticleBriefDto extends EntityDto(ArticleEntity) {}
|
|
368
|
+
|
|
369
|
+
// 详情 DTO — 将 lazy 属性显式加回
|
|
370
|
+
class ArticleDetailDto extends EntityDto(ArticleEntity) {
|
|
371
|
+
@Property({ schema: { description: '正文内容' } })
|
|
372
|
+
content?: string
|
|
373
|
+
|
|
374
|
+
@List({ type: () => PrimaryKeyType(CommentEntity) })
|
|
375
|
+
comments!: PrimaryKeyTypeClass[]
|
|
376
|
+
}
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
> `EntityDto` 是推荐的实体→响应 DTO 派生方式。对于需要选取特定字段的场景,使用 `PickType`。
|
|
380
|
+
|
|
381
|
+
---
|
|
382
|
+
|
|
322
383
|
## PrimaryKeyType
|
|
323
384
|
|
|
324
385
|
`PrimaryKeyType()` 工厂函数基于实体的主键字段自动生成只包含主键的 DTO 类。
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@buka/nestjs-kit",
|
|
3
|
-
"version": "4.3.
|
|
3
|
+
"version": "4.3.3",
|
|
4
4
|
"description": "Buka Nestjs Kit",
|
|
5
5
|
"packageManager": "pnpm@10.33.4+sha512.1c67b3b359b2d408119ba1ed289f34b8fc3c6873412bec6fd264fbdc82489e510fcbecb9ce9d22dae7f3b76269d8441046014bdca53b9979cd7a561ad631b800",
|
|
6
6
|
"types": "dist/index.d.ts",
|