nicot 1.1.9 → 1.1.10

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.
Files changed (2) hide show
  1. package/README.md +167 -8
  2. package/package.json +8 -7
package/README.md CHANGED
@@ -13,8 +13,10 @@
13
13
 
14
14
  ## 📦 安装
15
15
 
16
+ 在你的 Nest.js 项目中:
17
+
16
18
  ```bash
17
- npm install nicot typeorm @nestjs/typeorm class-validator class-transformer reflect-metadata @nestjs/swagger
19
+ npm install nicot @nestjs/config typeorm @nestjs/typeorm class-validator class-transformer reflect-metadata @nestjs/swagger
18
20
  ```
19
21
 
20
22
  ---
@@ -478,8 +480,133 @@ export class UserController {
478
480
  - 所有的接口都是返回状态码 200。
479
481
  - OpenAPI 文档会自动生成,包含所有 DTO 类型与查询参数。
480
482
  - Service 需要使用 `CrudService(Entity, options)` 进行标准化实现。
481
- - `RestfulFactory` 的选项 `options` 支持传入 `relations`,形式和 `CrudService` 一致,用于自动裁剪结果 DTO 字段。
482
- - 如果本内容的 `CrudService` 不查询任何关系字段,那么请设置 `{ relations: [] }` 以裁剪所有关系字段。
483
+
484
+ ---
485
+
486
+ ### 导出 DTO 类
487
+
488
+ `RestfulFactory` 会自动生成以下 DTO 类:供你导出并在其他的 OpenAPI 装饰器中使用。
489
+
490
+ ```ts
491
+ const factory = new RestfulFactory(User, {
492
+ relations: ['articles'],
493
+ });
494
+
495
+ class CreateUserDto extends factory.createDto {} // 创建用 DTO,在 POST /user 中使用
496
+ class UpdateUserDto extends factory.updateDto {} // 更新用 DTO,在 PATCH /user/:id 中使用
497
+ class FindAllUserDto extends factory.findAllDto {} // 查询用 DTO,在 GET /user 中使用
498
+ class UserResultDto extends factory.entityResultDto {} // 查询结果 DTO,在 GET /user/:id 和 GET /user 中返回
499
+ class UserCreateResultDto extends factory.entityCreateResultDto {} // 创建结果 DTO,在 POST /user 中返回。相比 entityResultDto 省略了间接字段和关系字段
500
+ class UserReturnMessageDto extends factory.entityReturnMessageDto {} // 相当于 ReturnMessageDto(UserResultDto),在 GET /user 中返回
501
+ class UserCreateReturnMessageDto extends factory.entityCreateReturnMessageDto {} // 相当于 ReturnMessageDto(UserCreateResultDto),在 POST /user 中返回
502
+ class UserArrayResultDto extends factory.entityArrayResultDto {} // 相当于 PaginatedReturnMessageDto(UserResultDto),在 GET /user 中返回
503
+ ```
504
+
505
+ ---
506
+
507
+ ### 关系定义
508
+
509
+ 类似于 `CrudService`,`RestfulFactory` 也需要在配置中定义关系字段。语法和 `CrudService` 的 `relations` 参数完全一致。
510
+
511
+ ```ts
512
+ class User extends IdBase() {
513
+ @OneToMany(() => Article, article => article.user)
514
+ articles: Article[];
515
+
516
+ @OneToMany(() => Comment, comment => comment.user)
517
+ comments: Comment[];
518
+
519
+ @OneToMany(() => Like, like => like.users)
520
+ likes: Like[];
521
+ }
522
+
523
+ class Article extends IdBase() {
524
+ @ManyToOne(() => User, user => user.articles)
525
+ user: User;
526
+
527
+ @OneToMany(() => Comment, comment => comment.article)
528
+ comments: Comment[];
529
+
530
+ @OneToMany(() => Like, like => like.article)
531
+ likes: Like[];
532
+ }
533
+
534
+ class Like extends IdBase() {
535
+ @ManyToOne(() => User, user => user.likes)
536
+ user: User;
537
+
538
+ @ManyToOne(() => Article, article => article.likes)
539
+ article: Article;
540
+ }
541
+
542
+ class Comment extends IdBase() {
543
+ @ManyToOne(() => Article, article => article.comments)
544
+ article: Article;
545
+
546
+ @ManyToOne(() => User, user => user.articles)
547
+ user: User;
548
+ }
549
+
550
+ const factory = new RestfulFactory(User, {
551
+ relations: ['comments', 'articles', 'articles.comments'], // 生成的 DTO 类中,只含有标明的关系字段,而 articles.user 不会被包含
552
+ });
553
+
554
+ class UserResultDto extends factory.entityResultDto {
555
+ // 生成的 DTO 类中包含 comments, articles, articles.comments 字段
556
+ // 但是不包含 likes, articles.user, articles.likes 等未声明关系字段
557
+ }
558
+ ```
559
+
560
+ 如果你的配套 `CrudService` 不准备加载任何关系,那么可以传入空数组:
561
+
562
+ ```ts
563
+ const factory = new RestfulFactory(User, {
564
+ relations: [], // DTO 不包含任何关系字段
565
+ });
566
+ ```
567
+
568
+ 如果不写 `relations`,则默认会尽可能加载所有非 `@NotInResult()` 的关系字段。但现在推荐显式声明需要加载的关系,以避免不必要的 OpenAPI 文档杂乱。
569
+
570
+ > 这是曾经版本的 nicot (<1.1.9) 的做法。
571
+
572
+ ---
573
+
574
+ ### 依赖关系的间接字段
575
+
576
+ 如果你有实体类,某一间接字段(`@NotColumn()`),依赖某个关系字段,那么需要显示声明这个字段。
577
+
578
+ ```ts
579
+ export class Participant extends IdBase() {
580
+ @OneToMany(() => Match, match => match.player1)
581
+ matches1: Match[];
582
+
583
+ @OneToMany(() => Match, match => match.player2)
584
+ matches2: Match[];
585
+ }
586
+
587
+ export class Match extends IdBase() {
588
+ @ManyToOne(() => Participant, participant => participant.matches1)
589
+ player1: Participant;
590
+ @ManyToOne(() => Participant, participant => participant.matches2)
591
+ player2: Participant;
592
+
593
+ @NotColumn()
594
+ @RelationComputed(() => Participant) // 声明这个字段依赖于 player1 和 player2 生成,当作关系参与裁剪,避免被拖入 Participant 属性黑洞
595
+ players: Participant[];
596
+
597
+ async afterGet() {
598
+ this.players = [this.player1, this.player2].filter(s => s);
599
+ }
600
+ }
601
+
602
+ const factory = new RestfulFactory(Match, {
603
+ relations: ['player1', 'player2', 'players'],
604
+ });
605
+
606
+ class MatchResultDto extends factory.entityResultDto {
607
+ // 包含 player1, player2, players 字段,但是不包含 player1.matches1, player1.matches2 等间接关系字段
608
+ }
609
+ ```
483
610
 
484
611
  ---
485
612
 
@@ -546,10 +673,9 @@ NICOT 默认提供统一的接口返回格式与 Swagger 自动注解能力,
546
673
 
547
674
  ### ✅ 返回结构 DTO 类型(用于 Swagger 类型标注)
548
675
 
549
- #### `ReturnMessageDto(EntityClass)`
550
- 用于生成带数据的标准返回结构类型(**不是直接返回值**,用于 `@nestjs/swagger`)。
676
+ #### `ReturnMessageDto(EntityClass)`
551
677
 
552
- 返回结构样式:
678
+ 用于生成带数据的标准返回结构类型(**不是直接返回值**,用于 `@nestjs/swagger`)。
553
679
 
554
680
  ```json
555
681
  {
@@ -557,13 +683,42 @@ NICOT 默认提供统一的接口返回格式与 Swagger 自动注解能力,
557
683
  "success": true,
558
684
  "message": "success",
559
685
  "timestamp": "2025-04-25T12:00:00.000Z",
560
- "data": { ... }
686
+ "data": {}
561
687
  }
562
688
  ```
563
689
 
564
- #### `BlankReturnMessageDto`
690
+ #### `BlankReturnMessageDto`
691
+
565
692
  无数据返回结构的类型(用于 DELETE、UPDATE 等空响应)。
566
693
 
694
+ ```json
695
+ {
696
+ "statusCode": 200,
697
+ "success": true,
698
+ "message": "success"
699
+ }
700
+ ```
701
+
702
+ #### `PaginatedReturnMessageDto(EntityClass)`
703
+
704
+ 带有分页信息的返回结构类型。
705
+
706
+ > EntityClass 会自动变成数组类型。
707
+
708
+ ```json
709
+ {
710
+ "statusCode": 200,
711
+ "success": true,
712
+ "message": "success",
713
+ "timestamp": "2025-04-25T12:00:00.000Z",
714
+ "data": [{}],
715
+ "total": 100,
716
+ "totalPages": 4,
717
+ "pageCount": 1,
718
+ "recordsPerPage": 25
719
+ }
720
+ ```
721
+
567
722
  ---
568
723
 
569
724
  ### 📊 实际返回结构
@@ -815,3 +970,7 @@ NICOT 作为一个 “Entity 驱动” 的框架,在开发体验、安全性
815
970
  - 内建返回结构、Swagger 注解、守卫装饰器等功能
816
971
 
817
972
  是构建 NestJS 标准化、低重复、文档完善的后端服务的理想选择。
973
+
974
+ ## LICENSE
975
+
976
+ MIT
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nicot",
3
3
  "description": "Nest.js interacting with class-validator + OpenAPI + TypeORM for Nest.js Restful API development.",
4
- "version": "1.1.9",
4
+ "version": "1.1.10",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "scripts": {
@@ -12,15 +12,15 @@
12
12
  },
13
13
  "repository": {
14
14
  "type": "git",
15
- "url": "https://code.mycard.moe/3rdeye/nicot.git"
15
+ "url": "https://github.com/purerosefallen/nicot.git"
16
16
  },
17
17
  "author": "Nanahira <nanahira@momobako.com>",
18
18
  "license": "MIT",
19
19
  "keywords": [],
20
20
  "bugs": {
21
- "url": "https://code.mycard.moe/3rdeye/nicot/issues"
21
+ "url": "https://github.com/purerosefallen/nicot/issues"
22
22
  },
23
- "homepage": "https://code.mycard.moe/3rdeye/nicot",
23
+ "homepage": "https://github.com/purerosefallen/nicot",
24
24
  "jest": {
25
25
  "moduleFileExtensions": [
26
26
  "js",
@@ -61,15 +61,16 @@
61
61
  "typescript": "^4.7.4"
62
62
  },
63
63
  "peerDependencies": {
64
- "@nestjs/common": "^9.4.0 || ^10.0.0 || ^11.0.14",
65
- "@nestjs/swagger": "^7.1.8 || ^6.3.0 || ^11.1.1",
64
+ "@nestjs/common": "^11.0.0 || ^9.4.0 || ^10.0.0",
65
+ "@nestjs/config": "^4.0.0 || ^3.0.0",
66
+ "@nestjs/swagger": "^11.0.0 || ^7.1.8 || ^6.3.0",
66
67
  "class-transformer": "^0.5.1",
67
68
  "class-validator": "^0.14.0",
68
69
  "typeorm": "^0.3.16"
69
70
  },
70
71
  "dependencies": {
71
72
  "lodash": "^4.17.21",
72
- "nesties": "^1.1.1",
73
+ "nesties": "^1.1.2",
73
74
  "typed-reflector": "^1.0.11"
74
75
  }
75
76
  }