nestjs-prisma-base

中文文档
1.1.5发布于 2025/06/15MITTypeScriptCommonJS零依赖

A comprehensive NestJS package providing base classes, utilities, and decorators for building CRUD APIs with Prisma ORM integration, featuring pagination, search, filtering, relation loading, configurable DTOs, and modular composition capabilities.

此软件包已被标记为弃用

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

原始使用文档
文档目录20 节
  1. Features
  2. 🚀 Core Features
  3. 🛠 DTO System
  4. ⚡ Developer Experience
  5. Quick Start
  6. Installation
  7. Configuration (Required for Advanced Features)
  8. 1. Setup PrismaModule
  9. 2. Create Service & Controller
  10. 3. Define DTOs
  11. Pagination Response
  12. Advanced Features
  13. 📚 Swagger Integration with Message Fields
  14. 🏭 Module Factory
  15. 🎨 DTO Configuration
  16. Documentation
  17. 📖 Available Documentation
  18. 🚀 Get More Help
  19. Version History
  20. License

NestJS Prisma Base

A comprehensive NestJS package providing base classes, utilities, and decorators for building CRUD APIs with Prisma ORM integration.

npm version License: MIT

Features

🚀 Core Features

  • Base CRUD Operations: Ready-to-use controllers and services
  • Enhanced Pagination: Metadata-rich pagination with configurable limits
  • Advanced Search & Filtering: Multi-field search with operators and sorting
  • Relation Loading: Configurable nested relation includes with validation
  • Endpoint Control: Enable/disable specific endpoints with decorators

🛠 DTO System

  • Configurable DTOs: Global and per-class configuration
  • Swagger Integration: Automatic API documentation
  • Minimal DTOs: Maximum control with empty base classes
  • Modular Composition: Mix-and-match DTO features with mixins
  • Response Messages: Optional message fields for consistent API feedback NEW in v1.1.0

⚡ Developer Experience

  • Module Factories: Auto-generate complete modules
  • Multiple Database Support: Work with multiple Prisma clients
  • TypeScript: Full type safety and IntelliSense support
  • Validation: Built-in request validation and error handling

Quick Start

Installation

npm install nestjs-prisma-base

Configuration (Required for Advanced Features)

⚠️ CRITICAL: Configuration must be imported BEFORE your application starts!

Create a configuration file and import it first in main.ts:

1. Create 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. Import in src/main.ts FIRST:

// ⚠️ 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. Setup PrismaModule

import { Module } from '@nestjs/common';
import { PrismaModule } from 'nestjs-prisma-base';

@Module({
  imports: [
    PrismaModule.forRoot({
      prismaClient: new PrismaClient(),
    }),
  ],
})
export class AppModule {}

2. Create Service & Controller

// 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. Define DTOs

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
}

That's it! You now have a fully functional CRUD API with pagination, search, and filtering.

Pagination Response

All findAll methods return rich pagination metadata:

{
  "data": [{ "id": 1, "name": "John", "email": "[email protected]" }],
  "meta": {
    "total": 150,
    "page": 1,
    "limit": 10,
    "totalPages": 15,
    "hasNext": true,
    "hasPrev": false
  }
}

Advanced Features

📚 Swagger Integration with Message Fields

For automatic message field documentation in 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
}

🏭 Module Factory

Generate complete modules with one function:

import { createModelModule, EndpointType } from 'nestjs-prisma-base';

export const UserModule = createModelModule({
  modelName: 'user',
  routePath: 'users',
  enableAllEndpoints: true,
});

🎨 DTO Configuration

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
}

Documentation

📖 Available Documentation

🚀 Get More Help

Version History

  • v1.1.4 - Fixed missing EnableSwaggerBaseFields export
  • v1.1.3 - Fixed SwaggerBaseResponseDto inheritance with @EnableSwaggerBaseFields decorator
  • v1.1.2 - Fixed SwaggerBaseResponseDto message field documentation issue
  • v1.1.1 - Fixed Swagger circular dependency issues with audit field mixins
  • v1.1.0 - Response message fields for consistent API feedback
  • v1.0.0 - Modular DTO composition with mixins
  • v0.9.0 - Configurable DTOs and Swagger integration
  • v0.8.0 - Relation loading and module factories
  • v0.7.0 - Advanced search with operators
  • v0.6.0 - Search and filtering capabilities
  • v0.5.0 - Enhanced pagination with metadata

License

MIT

版本与发布记录

nestjs-prisma-base 共发布 30 个版本,首次发布于 2025/04/05;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.5latest2025/06/151.4 年前
1.1.42025/06/151.4 年前
1.1.32025/06/151.4 年前
1.1.22025/06/151.4 年前
1.1.12025/06/141.4 年前
1.1.02025/06/141.4 年前

关于 nestjs-prisma-base 的常见问题

nestjs-prisma-base 是什么?

A comprehensive NestJS package providing base classes, utilities, and decorators for building CRUD APIs with Prisma ORM integration, featuring pagination, search, filtering, relation loading, configurable DTOs, and modular composition capabilities.注意:该包已被作者标记为弃用——Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

如何安装 nestjs-prisma-base?

在项目目录运行 npm install nestjs-prisma-base,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:rxjs(^7.0.0)、@nestjs/core(^8.0.0 || ^9.0.0 || ^10.0.0)、@nestjs/common(^8.0.0 || ^9.0.0 || ^10.0.0)、@prisma/client(^4.0.0 || ^5.0.0)、@nestjs/swagger(^6.0.0 || ^7.0.0)、class-validator(^0.13.0 || ^0.14.0)、reflect-metadata(^0.1.13)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

nestjs-prisma-base 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

nestjs-prisma-base 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

nestjs-prisma-base 可以商用吗?

nestjs-prisma-base 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

nestjs-prisma-base 还在维护吗?

最新版本 1.1.5 发布于 2025/06/15(1.4 年前);至今共发布 30 个版本,其中过去 12 个月发布了 0 个;首次发布于 2025/04/05。作者已将其标记为弃用,新项目建议寻找替代方案。