棱镜基地
一个全面的NestJS包,提供基类、实用程序和装饰器,用于与Prisma ORM集成构建CRUD api。
特性
🚀 核心功能
- 基础CRUD操作:现成的控制器和服务
- 增强的分页:具有可配置限制的元数据分页
- 高级搜索和过滤:带有操作符和排序的多字段搜索
- 加载的关系:可配置的嵌套关系包含验证
- 端点控制:启用/禁用带有装饰器的特定端点
🛠 DTO系统
- 可配置的dto:全局和类配置
- 昂首阔步的集成:自动API文档
- 最小的dto:对空基类的最大控制
- 模块化的组合:混合和匹配DTO功能与mixins
- 响应消息:可选消息字段,用于一致的API反馈 v1.1.0新增功能
⚡ 开发人员的经验
- 模块的工厂:自动生成完整模块
- 多数据库支持:使用多个Prisma客户端
- 打印稿完整的类型安全和智能感知支持
- 验证内置请求验证和错误处理
快速启动
安装
npm install nestjs-prisma-base
配置(高级特性必备)
⚠️关键:配置必须在应用程序启动之前导入!
创建一个配置文件并首先导入 main.ts:
1. 创建 src/config/dto-config.ts:
import { configureDTOs, configureSwaggerDTOs } from 'nestjs-prisma-base';
// Configure DTOs globally
configureDTOs({
includeTimestamps: true,
includeId: true,
includeMessage: true, // Enable response message fields
messageField: {
fieldName: 'message',
defaultValue: 'Operation completed successfully',
maxLength: 500,
},
});
// Configure Swagger integration
configureSwaggerDTOs({
enabled: true,
});
2. 进口的 src/main.ts 第一:
// ⚠️ CRITICAL: This import MUST be first!
import './config/dto-config';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap();
1. 设置PrismaModule
import { Module } from '@nestjs/common';
import { PrismaModule } from 'nestjs-prisma-base';
@Module({
imports: [
PrismaModule.forRoot({
prismaClient: new PrismaClient(),
}),
],
})
export class AppModule {}
2. 创建服务和控制器
// user.service.ts
import { Injectable } from '@nestjs/common';
import { BaseService, PrismaService } from 'nestjs-prisma-base';
@Injectable()
export class UserService extends BaseService<User, CreateUserDto, UpdateUserDto> {
protected readonly modelName = 'user';
constructor(protected readonly prisma: PrismaService) {
super(prisma);
}
}
// user.controller.ts
import { Controller } from '@nestjs/common';
import { BaseController, EnableAllEndpoints } from 'nestjs-prisma-base';
@Controller('users')
@EnableAllEndpoints() // Enables all CRUD endpoints
export class UserController extends BaseController<User, CreateUserDto, UpdateUserDto> {
constructor(private readonly userService: UserService) {
super(userService);
}
}
3. 定义dto
import { BaseCreateDto, BaseUpdateDto, BaseResponseDto } from 'nestjs-prisma-base';
import { IsEmail, IsString, IsOptional } from 'class-validator';
export class CreateUserDto extends BaseCreateDto {
@IsString() name: string;
@IsEmail() email: string;
}
export class UpdateUserDto extends BaseUpdateDto {
@IsOptional() @IsString() name?: string;
@IsOptional() @IsEmail() email?: string;
}
export class UserResponseDto extends BaseResponseDto {
name: string;
email: string;
// message field available for API feedback
}
这是它! 现在,您拥有了一个具有分页、搜索和过滤功能的全功能CRUD API。
分页响应
全部 findAll 方法返回富分页元数据:
{
"data": [{ "id": 1, "name": "John", "email": "[email protected]" }],
"meta": {
"total": 150,
"page": 1,
"limit": 10,
"totalPages": 15,
"hasNext": true,
"hasPrev": false
}
}
先进的功能
📚 Swagger与消息字段的集成
有关Swagger中的自动消息字段文档:
import { SwaggerBaseResponseDto, EnableSwaggerBaseFields } from 'nestjs-prisma-base';
import { ApiProperty } from '@nestjs/swagger';
@EnableSwaggerBaseFields // ← Required for automatic base field documentation
export class UserResponseDto extends SwaggerBaseResponseDto {
@ApiProperty() name: string;
@ApiProperty() email: string;
// message, id, createdAt, updatedAt fields automatically documented
}
🏭 模块的工厂
用一个功能生成完整的模块:
import { createModelModule, EndpointType } from 'nestjs-prisma-base';
export const UserModule = createModelModule({
modelName: 'user',
routePath: 'users',
enableAllEndpoints: true,
});
🎨 DTO配置
import { ConfigurableBaseCreateDto, configureDTOs } from 'nestjs-prisma-base';
// Global configuration (already done above)
export class CreateUserDto extends ConfigurableBaseCreateDto {
name: string;
email: string;
// Automatically includes configured fields
}
文档
📖 可用的文档
🚀 获得更多帮助
版本历史
- v1.1.4 修复了缺少EnableSwaggerBaseFields导出的问题
- v1.1.3 修复了SwaggerBaseResponseDto与@EnableSwaggerBaseFields装饰器的继承
- 也 修复了SwaggerBaseResponseDto消息字段文档的问题
- v1.1.1 修复了Swagger循环依赖审计字段混合的问题
- v1.1.0 -响应消息字段,用于一致的API反馈
- v1.0.0 -模块化DTO组合与混合
- v0.9.0 -可配置dto和Swagger集成
- v0.8.0 -关联加载和模块工厂
- v0.7.0 -带操作符的高级搜索
- v0.6.0 —搜索和过滤功能
- v0.5.0 -增强元数据分页
许可证
麻省理工学院