@nathapp/nestjs-common 2.1.5 → 3.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/CLAUDE.md +29 -0
- package/README.md +134 -0
- package/dist/config/index.d.ts +4 -4
- package/dist/config/index.js.map +1 -1
- package/dist/config/server-security.config.d.ts +1 -0
- package/dist/config/server-security.config.js +19 -2
- package/dist/config/server-security.config.js.map +1 -1
- package/dist/decorator/index.d.ts +2 -2
- package/dist/decorator/index.js.map +1 -1
- package/dist/entities/base-entity.d.ts +3 -0
- package/dist/entities/base-entity.js +8 -0
- package/dist/entities/base-entity.js.map +1 -0
- package/dist/entities/index.d.ts +1 -0
- package/dist/entities/index.js +18 -0
- package/dist/entities/index.js.map +1 -0
- package/dist/exception/app.exception.d.ts +4 -3
- package/dist/exception/app.exception.js +1 -1
- package/dist/exception/app.exception.js.map +1 -1
- package/dist/exception/default-i18n-validation-exception.filter.js +1 -1
- package/dist/exception/default-i18n-validation-exception.filter.js.map +1 -1
- package/dist/exception/exception.constants.d.ts +1 -0
- package/dist/exception/exception.constants.js +5 -0
- package/dist/exception/exception.constants.js.map +1 -0
- package/dist/exception/exception.module.d.ts +7 -0
- package/dist/exception/exception.module.js +41 -0
- package/dist/exception/exception.module.js.map +1 -0
- package/dist/exception/exceptions/auth.exception.d.ts +4 -0
- package/dist/exception/exceptions/auth.exception.js +12 -0
- package/dist/exception/exceptions/auth.exception.js.map +1 -0
- package/dist/exception/exceptions/forbidden-app.exception.d.ts +4 -0
- package/dist/exception/exceptions/forbidden-app.exception.js +12 -0
- package/dist/exception/exceptions/forbidden-app.exception.js.map +1 -0
- package/dist/exception/exceptions/index.d.ts +6 -0
- package/dist/exception/exceptions/index.js +16 -0
- package/dist/exception/exceptions/index.js.map +1 -0
- package/dist/exception/exceptions/internal-app.exception.d.ts +4 -0
- package/dist/exception/exceptions/internal-app.exception.js +12 -0
- package/dist/exception/exceptions/internal-app.exception.js.map +1 -0
- package/dist/exception/exceptions/not-found-app.exception.d.ts +4 -0
- package/dist/exception/exceptions/not-found-app.exception.js +12 -0
- package/dist/exception/exceptions/not-found-app.exception.js.map +1 -0
- package/dist/exception/exceptions/throttle-app.exception.d.ts +4 -0
- package/dist/exception/exceptions/throttle-app.exception.js +12 -0
- package/dist/exception/exceptions/throttle-app.exception.js.map +1 -0
- package/dist/exception/exceptions/validation-app.exception.d.ts +4 -0
- package/dist/exception/exceptions/validation-app.exception.js +12 -0
- package/dist/exception/exceptions/validation-app.exception.js.map +1 -0
- package/dist/exception/global-exception.filter.d.ts +8 -54
- package/dist/exception/global-exception.filter.js +45 -163
- package/dist/exception/global-exception.filter.js.map +1 -1
- package/dist/exception/handler/app-exception.handler.d.ts +7 -0
- package/dist/exception/handler/app-exception.handler.js +44 -0
- package/dist/exception/handler/app-exception.handler.js.map +1 -0
- package/dist/exception/handler/bad-request.handler.d.ts +6 -0
- package/dist/exception/handler/bad-request.handler.js +30 -0
- package/dist/exception/handler/bad-request.handler.js.map +1 -0
- package/dist/exception/handler/exception-handler.interface.d.ts +14 -0
- package/dist/exception/handler/exception-handler.interface.js +3 -0
- package/dist/exception/handler/exception-handler.interface.js.map +1 -0
- package/dist/exception/handler/forbidden.handler.d.ts +6 -0
- package/dist/exception/handler/forbidden.handler.js +27 -0
- package/dist/exception/handler/forbidden.handler.js.map +1 -0
- package/dist/exception/handler/index.d.ts +11 -0
- package/dist/exception/handler/index.js +38 -0
- package/dist/exception/handler/index.js.map +1 -0
- package/dist/exception/handler/microservice.handler.d.ts +6 -0
- package/dist/exception/handler/microservice.handler.js +27 -0
- package/dist/exception/handler/microservice.handler.js.map +1 -0
- package/dist/exception/handler/not-found.handler.d.ts +6 -0
- package/dist/exception/handler/not-found.handler.js +27 -0
- package/dist/exception/handler/not-found.handler.js.map +1 -0
- package/dist/exception/handler/throttler.handler.d.ts +6 -0
- package/dist/exception/handler/throttler.handler.js +33 -0
- package/dist/exception/handler/throttler.handler.js.map +1 -0
- package/dist/exception/handler/unknown.handler.d.ts +6 -0
- package/dist/exception/handler/unknown.handler.js +35 -0
- package/dist/exception/handler/unknown.handler.js.map +1 -0
- package/dist/exception/handler/username-not-found.handler.d.ts +6 -0
- package/dist/exception/handler/username-not-found.handler.js +28 -0
- package/dist/exception/handler/username-not-found.handler.js.map +1 -0
- package/dist/exception/index.d.ts +7 -4
- package/dist/exception/index.js +12 -3
- package/dist/exception/index.js.map +1 -1
- package/dist/exception/micro-service.exception.js +1 -1
- package/dist/exception/micro-service.exception.js.map +1 -1
- package/dist/exception/user-not-found.exception.js +1 -1
- package/dist/http/index.d.ts +3 -3
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/interceptor/serializable.interceptor.d.ts +4 -2
- package/dist/interceptor/serializable.interceptor.js +75 -18
- package/dist/interceptor/serializable.interceptor.js.map +1 -1
- package/dist/ip/index.d.ts +3 -2
- package/dist/ip/index.js +4 -1
- package/dist/ip/index.js.map +1 -1
- package/dist/ip/request-ip.d.ts +4 -2
- package/dist/ip/request-ip.js +92 -30
- package/dist/ip/request-ip.js.map +1 -1
- package/dist/ip/trusted-proxy-presets.d.ts +3 -0
- package/dist/ip/trusted-proxy-presets.js +114 -0
- package/dist/ip/trusted-proxy-presets.js.map +1 -0
- package/dist/module/configurable-module.builder.js +3 -1
- package/dist/module/configurable-module.builder.js.map +1 -1
- package/dist/module/index.d.ts +5 -5
- package/dist/module/index.js.map +1 -1
- package/dist/pagination/index.d.ts +1 -0
- package/dist/pagination/index.js +18 -0
- package/dist/pagination/index.js.map +1 -0
- package/dist/pagination/page.d.ts +39 -0
- package/dist/pagination/page.js +87 -0
- package/dist/pagination/page.js.map +1 -0
- package/dist/serialize/localize.string.js.map +1 -1
- package/dist/utils/package-utils.js +3 -0
- package/dist/utils/package-utils.js.map +1 -1
- package/dist/validation/index.d.ts +1 -0
- package/dist/validation/index.js +1 -0
- package/dist/validation/index.js.map +1 -1
- package/dist/validation/validators.d.ts +3 -0
- package/dist/validation/validators.js +29 -0
- package/dist/validation/validators.js.map +1 -0
- package/docs/20260214-coverage-plan.md +52 -0
- package/docs/20260214-fix-plan-common-bugs.md +90 -0
- package/docs/20260214-fix-plan-common-cleanup.md +119 -0
- package/docs/20260214-fix-plan-exception-redesign.md +274 -0
- package/docs/20260214-fix-plan-nestjs-prisma.md +454 -0
- package/docs/20260214-fix-plan-pagination-move-and-rename.md +148 -0
- package/docs/20260214-review-common.md +426 -0
- package/docs/20260215-fix-plan-review-enhancements.md +110 -0
- package/docs/20260215-fix-plan-trust-proxy-presets.md +227 -0
- package/docs/20260215-review-nestjs-common.md +1015 -0
- package/docs/trust-proxy-security.md +475 -0
- package/package.json +1 -1
- package/dist/exception/app-exception.filter.d.ts +0 -9
- package/dist/exception/app-exception.filter.js +0 -52
- package/dist/exception/app-exception.filter.js.map +0 -1
- package/dist/i18n/i18n.constants.d.ts +0 -0
- package/dist/i18n/i18n.constants.js +0 -1
- package/dist/i18n/i18n.constants.js.map +0 -1
- package/dist/middleware/index.d.ts +0 -4
- package/dist/middleware/index.js +0 -14
- package/dist/middleware/index.js.map +0 -1
- package/dist/middleware/log.provider.d.ts +0 -38
- package/dist/middleware/log.provider.js +0 -147
- package/dist/middleware/log.provider.js.map +0 -1
- package/dist/middleware/logger-option.factory.d.ts +0 -17
- package/dist/middleware/logger-option.factory.js +0 -73
- package/dist/middleware/logger-option.factory.js.map +0 -1
- package/dist/middleware/logger.middleware.d.ts +0 -8
- package/dist/middleware/logger.middleware.js +0 -97
- package/dist/middleware/logger.middleware.js.map +0 -1
|
@@ -0,0 +1,454 @@
|
|
|
1
|
+
# Fix Plan: Create @nathapp/nestjs-prisma Package
|
|
2
|
+
|
|
3
|
+
**Date:** 2026-02-14
|
|
4
|
+
**Branch:** `feat/monorepo-enterprise-upgrade`
|
|
5
|
+
|
|
6
|
+
## Overview
|
|
7
|
+
|
|
8
|
+
New package providing PrismaModule with lifecycle management, multi-database support, pagination, and testing utilities. Follows the same patterns as `@nathapp/nestjs-typeorm`.
|
|
9
|
+
|
|
10
|
+
## Package Structure
|
|
11
|
+
|
|
12
|
+
```
|
|
13
|
+
packages/nestjs-prisma/
|
|
14
|
+
├── lib/
|
|
15
|
+
│ ├── index.ts # Barrel exports
|
|
16
|
+
│ ├── prisma.constants.ts # DI tokens
|
|
17
|
+
│ ├── prisma.interfaces.ts # Options interfaces
|
|
18
|
+
│ ├── prisma.module.ts # PrismaModule with forRoot/forRootAsync
|
|
19
|
+
│ ├── prisma.service.ts # PrismaService wrapper
|
|
20
|
+
│ ├── prisma.decorator.ts # @InjectPrisma() decorator
|
|
21
|
+
│ ├── prisma.health.ts # Health check indicator
|
|
22
|
+
│ ├── pagination/
|
|
23
|
+
│ │ ├── index.ts
|
|
24
|
+
│ │ └── paginate.ts # Prisma-specific Paginate()
|
|
25
|
+
│ └── testing/
|
|
26
|
+
│ ├── index.ts
|
|
27
|
+
│ └── prisma-testing.utils.ts # Mock helpers
|
|
28
|
+
├── test/
|
|
29
|
+
│ ├── jest-test.json
|
|
30
|
+
│ ├── prisma.module.spec.ts # Module integration tests
|
|
31
|
+
│ ├── prisma.service.spec.ts # Service unit tests
|
|
32
|
+
│ ├── prisma.decorator.spec.ts # Decorator tests
|
|
33
|
+
│ ├── prisma.health.spec.ts # Health check tests
|
|
34
|
+
│ └── pagination/
|
|
35
|
+
│ └── paginate.spec.ts # Pagination tests
|
|
36
|
+
├── package.json
|
|
37
|
+
├── tsconfig.json
|
|
38
|
+
└── .eslintrc.js
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Phase 1: Package Scaffolding
|
|
42
|
+
|
|
43
|
+
### 1.1: Create package.json
|
|
44
|
+
**File:** `packages/nestjs-prisma/package.json`
|
|
45
|
+
```json
|
|
46
|
+
{
|
|
47
|
+
"name": "@nathapp/nestjs-prisma",
|
|
48
|
+
"version": "2.1.5",
|
|
49
|
+
"description": "NestJS Prisma integration with multi-database support",
|
|
50
|
+
"scripts": {
|
|
51
|
+
"clean": "rimraf dist/ .turbo/",
|
|
52
|
+
"build": "rimraf dist && tsc -p tsconfig.json",
|
|
53
|
+
"test": "jest --config ./test/jest-test.json",
|
|
54
|
+
"test:cov": "jest --config ./test/jest-test.json --coverage"
|
|
55
|
+
},
|
|
56
|
+
"author": "William Khoo",
|
|
57
|
+
"license": "ISC",
|
|
58
|
+
"types": "dist/index.d.ts",
|
|
59
|
+
"main": "dist/index.js",
|
|
60
|
+
"dependencies": {
|
|
61
|
+
"@nathapp/nestjs-common": "^2.1.5"
|
|
62
|
+
},
|
|
63
|
+
"devDependencies": {
|
|
64
|
+
"@nathapp/eslint-config": "1.0.1",
|
|
65
|
+
"@nathapp/typescript-config": "1.0.1",
|
|
66
|
+
"@golevelup/ts-jest": "^0.6.1",
|
|
67
|
+
"eslint": "^8.57.0",
|
|
68
|
+
"typescript": "^5"
|
|
69
|
+
},
|
|
70
|
+
"peerDependencies": {
|
|
71
|
+
"@nathapp/nestjs-common": "^2.1.5",
|
|
72
|
+
"@nestjs/common": "^10.0.0 || ^11.0.0",
|
|
73
|
+
"@nestjs/core": "^10.0.0 || ^11.0.0",
|
|
74
|
+
"@prisma/client": "^6.0.0"
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### 1.2: Create tsconfig.json
|
|
80
|
+
Copy from `packages/nestjs-typeorm/tsconfig.json`, adjust paths.
|
|
81
|
+
|
|
82
|
+
### 1.3: Create jest config
|
|
83
|
+
**File:** `packages/nestjs-prisma/test/jest-test.json`
|
|
84
|
+
Copy pattern from nestjs-typeorm or nestjs-common.
|
|
85
|
+
|
|
86
|
+
### 1.4: Create .eslintrc.js
|
|
87
|
+
Copy from another package.
|
|
88
|
+
|
|
89
|
+
**Commit:** `chore(nestjs-prisma): scaffold package structure`
|
|
90
|
+
|
|
91
|
+
## Phase 2: Core — Constants, Interfaces, Decorator
|
|
92
|
+
|
|
93
|
+
### 2.1: Constants
|
|
94
|
+
**File:** `packages/nestjs-prisma/lib/prisma.constants.ts`
|
|
95
|
+
```typescript
|
|
96
|
+
export const DEFAULT_PRISMA_CONNECTION = 'default';
|
|
97
|
+
export const PRISMA_MODULE_OPTIONS = Symbol('PRISMA_MODULE_OPTIONS');
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Get the DI token for a named Prisma connection.
|
|
101
|
+
*/
|
|
102
|
+
export const getPrismaToken = (name?: string): string =>
|
|
103
|
+
name && name !== DEFAULT_PRISMA_CONNECTION
|
|
104
|
+
? `PRISMA_SERVICE_${name}`
|
|
105
|
+
: 'PRISMA_SERVICE_DEFAULT';
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### 2.2: Interfaces
|
|
109
|
+
**File:** `packages/nestjs-prisma/lib/prisma.interfaces.ts`
|
|
110
|
+
```typescript
|
|
111
|
+
import { ModuleMetadata, Type } from '@nestjs/common';
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Minimal interface that any PrismaClient must satisfy.
|
|
115
|
+
* Works with Prisma 6+ generated clients.
|
|
116
|
+
*/
|
|
117
|
+
export interface PrismaClientLike {
|
|
118
|
+
$connect(): Promise<void>;
|
|
119
|
+
$disconnect(): Promise<void>;
|
|
120
|
+
$on?(event: string, listener: (...args: any[]) => void): void;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export interface PrismaModuleOptions {
|
|
124
|
+
/** Connection name for multi-database. Default: 'default' */
|
|
125
|
+
name?: string;
|
|
126
|
+
|
|
127
|
+
/** The PrismaClient class (generated by Prisma). */
|
|
128
|
+
client?: new (options?: any) => PrismaClientLike;
|
|
129
|
+
|
|
130
|
+
/** Options passed to the PrismaClient constructor. */
|
|
131
|
+
clientOptions?: Record<string, any>;
|
|
132
|
+
|
|
133
|
+
/** If true, module is registered globally. Default: true */
|
|
134
|
+
isGlobal?: boolean;
|
|
135
|
+
|
|
136
|
+
/** Enable logging of Prisma queries. Default: false */
|
|
137
|
+
logging?: boolean;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export interface PrismaModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
|
|
141
|
+
name?: string;
|
|
142
|
+
isGlobal?: boolean;
|
|
143
|
+
useFactory?: (...args: any[]) => PrismaModuleOptions | Promise<PrismaModuleOptions>;
|
|
144
|
+
inject?: any[];
|
|
145
|
+
useClass?: Type<PrismaOptionsFactory>;
|
|
146
|
+
useExisting?: Type<PrismaOptionsFactory>;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export interface PrismaOptionsFactory {
|
|
150
|
+
createPrismaOptions(): PrismaModuleOptions | Promise<PrismaModuleOptions>;
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### 2.3: Decorator
|
|
155
|
+
**File:** `packages/nestjs-prisma/lib/prisma.decorator.ts`
|
|
156
|
+
```typescript
|
|
157
|
+
import { Inject } from '@nestjs/common';
|
|
158
|
+
import { getPrismaToken } from './prisma.constants';
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Inject a named PrismaService.
|
|
162
|
+
* @param name - Connection name. Default: 'default'
|
|
163
|
+
*/
|
|
164
|
+
export const InjectPrisma = (name?: string) => Inject(getPrismaToken(name));
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
**Commit:** `feat(nestjs-prisma): add constants, interfaces, and InjectPrisma decorator`
|
|
168
|
+
|
|
169
|
+
## Phase 3: PrismaService
|
|
170
|
+
|
|
171
|
+
### 3.1: PrismaService
|
|
172
|
+
**File:** `packages/nestjs-prisma/lib/prisma.service.ts`
|
|
173
|
+
```typescript
|
|
174
|
+
import { Injectable, Logger, OnModuleInit, OnModuleDestroy } from '@nestjs/common';
|
|
175
|
+
import { PrismaClientLike, PrismaModuleOptions } from './prisma.interfaces';
|
|
176
|
+
|
|
177
|
+
@Injectable()
|
|
178
|
+
export class PrismaService<T extends PrismaClientLike = PrismaClientLike>
|
|
179
|
+
implements OnModuleInit, OnModuleDestroy
|
|
180
|
+
{
|
|
181
|
+
private readonly logger = new Logger(PrismaService.name);
|
|
182
|
+
private _client: T;
|
|
183
|
+
private _isConnected = false;
|
|
184
|
+
private readonly connectionName: string;
|
|
185
|
+
|
|
186
|
+
constructor(private readonly options: PrismaModuleOptions) {
|
|
187
|
+
this.connectionName = options.name || 'default';
|
|
188
|
+
|
|
189
|
+
const ClientClass = options.client;
|
|
190
|
+
if (!ClientClass) {
|
|
191
|
+
throw new Error(
|
|
192
|
+
`PrismaService [${this.connectionName}]: No PrismaClient class provided. ` +
|
|
193
|
+
`Pass the generated PrismaClient class in the 'client' option.`
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
this._client = new ClientClass(options.clientOptions) as T;
|
|
198
|
+
|
|
199
|
+
// Set up logging if enabled
|
|
200
|
+
if (options.logging && this._client.$on) {
|
|
201
|
+
this._client.$on('query', (event: any) => {
|
|
202
|
+
this.logger.debug(`[${this.connectionName}] Query: ${event.query}`, 'PrismaQuery');
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** The underlying PrismaClient instance, fully typed. */
|
|
208
|
+
get client(): T {
|
|
209
|
+
return this._client;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** Whether the client is currently connected. */
|
|
213
|
+
get isConnected(): boolean {
|
|
214
|
+
return this._isConnected;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
async onModuleInit(): Promise<void> {
|
|
218
|
+
try {
|
|
219
|
+
await this._client.$connect();
|
|
220
|
+
this._isConnected = true;
|
|
221
|
+
this.logger.log(`Prisma [${this.connectionName}] connected`);
|
|
222
|
+
} catch (error) {
|
|
223
|
+
this.logger.error(
|
|
224
|
+
`Prisma [${this.connectionName}] failed to connect: ${error.message}`,
|
|
225
|
+
error.stack,
|
|
226
|
+
);
|
|
227
|
+
throw error;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
async onModuleDestroy(): Promise<void> {
|
|
232
|
+
try {
|
|
233
|
+
await this._client.$disconnect();
|
|
234
|
+
this._isConnected = false;
|
|
235
|
+
this.logger.log(`Prisma [${this.connectionName}] disconnected`);
|
|
236
|
+
} catch (error) {
|
|
237
|
+
this.logger.error(
|
|
238
|
+
`Prisma [${this.connectionName}] failed to disconnect: ${error.message}`,
|
|
239
|
+
error.stack,
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
**Commit:** `feat(nestjs-prisma): add PrismaService with lifecycle management`
|
|
247
|
+
|
|
248
|
+
## Phase 4: PrismaModule
|
|
249
|
+
|
|
250
|
+
### 4.1: PrismaModule
|
|
251
|
+
**File:** `packages/nestjs-prisma/lib/prisma.module.ts`
|
|
252
|
+
|
|
253
|
+
Provides `forRoot()` and `forRootAsync()` with multi-database support via named connections.
|
|
254
|
+
|
|
255
|
+
Key behavior:
|
|
256
|
+
- `forRoot(options)` — creates a `PrismaService` provider under `getPrismaToken(name)`
|
|
257
|
+
- If `name` is 'default' or undefined, ALSO provides as `PrismaService` directly (for simple single-db case)
|
|
258
|
+
- `forRootAsync(options)` — same but with async factory/class
|
|
259
|
+
- Default `isGlobal: true`
|
|
260
|
+
|
|
261
|
+
**Commit:** `feat(nestjs-prisma): add PrismaModule with forRoot/forRootAsync and multi-database support`
|
|
262
|
+
|
|
263
|
+
## Phase 5: Pagination
|
|
264
|
+
|
|
265
|
+
### 5.1: Prisma-specific Paginate
|
|
266
|
+
**File:** `packages/nestjs-prisma/lib/pagination/paginate.ts`
|
|
267
|
+
|
|
268
|
+
```typescript
|
|
269
|
+
import { Page, resolvePageOption } from '@nathapp/nestjs-common';
|
|
270
|
+
import type { IPageOption } from '@nathapp/nestjs-common';
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Paginate a Prisma model query.
|
|
274
|
+
*
|
|
275
|
+
* @param model - The Prisma model delegate (e.g., prisma.user)
|
|
276
|
+
* @param pageOption - Pagination options (current page, page size)
|
|
277
|
+
* @param args - Prisma findMany args (where, orderBy, include, select, etc.)
|
|
278
|
+
*/
|
|
279
|
+
export async function Paginate<T>(
|
|
280
|
+
model: {
|
|
281
|
+
findMany(args?: any): Promise<T[]>;
|
|
282
|
+
count(args?: any): Promise<number>;
|
|
283
|
+
},
|
|
284
|
+
pageOption: IPageOption,
|
|
285
|
+
args?: {
|
|
286
|
+
where?: any;
|
|
287
|
+
orderBy?: any;
|
|
288
|
+
include?: any;
|
|
289
|
+
select?: any;
|
|
290
|
+
},
|
|
291
|
+
): Promise<Page<T>> {
|
|
292
|
+
const { skip, take } = resolvePageOption(pageOption);
|
|
293
|
+
|
|
294
|
+
const [records, total] = await Promise.all([
|
|
295
|
+
model.findMany({
|
|
296
|
+
...args,
|
|
297
|
+
skip,
|
|
298
|
+
take,
|
|
299
|
+
}),
|
|
300
|
+
model.count({ where: args?.where }),
|
|
301
|
+
]);
|
|
302
|
+
|
|
303
|
+
return new Page<T>(pageOption, total, records);
|
|
304
|
+
}
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
**Commit:** `feat(nestjs-prisma): add Prisma pagination using shared Page from nestjs-common`
|
|
308
|
+
|
|
309
|
+
## Phase 6: Health Check
|
|
310
|
+
|
|
311
|
+
### 6.1: PrismaHealthIndicator
|
|
312
|
+
**File:** `packages/nestjs-prisma/lib/prisma.health.ts`
|
|
313
|
+
|
|
314
|
+
```typescript
|
|
315
|
+
import { Injectable } from '@nestjs/common';
|
|
316
|
+
import { PrismaService } from './prisma.service';
|
|
317
|
+
|
|
318
|
+
export interface HealthCheckResult {
|
|
319
|
+
status: 'up' | 'down';
|
|
320
|
+
message?: string;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
@Injectable()
|
|
324
|
+
export class PrismaHealthIndicator {
|
|
325
|
+
constructor(private readonly prismaService: PrismaService) {}
|
|
326
|
+
|
|
327
|
+
async isHealthy(): Promise<HealthCheckResult> {
|
|
328
|
+
try {
|
|
329
|
+
// Try a simple query to check connectivity
|
|
330
|
+
await this.prismaService.client.$queryRaw`SELECT 1`;
|
|
331
|
+
return { status: 'up' };
|
|
332
|
+
} catch (error) {
|
|
333
|
+
return { status: 'down', message: error.message };
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
Note: `$queryRaw` may not exist on the `PrismaClientLike` interface. Use `$connect` as a fallback check if `$queryRaw` is not available. The health indicator should check `isConnected` first, then try a simple query if possible.
|
|
340
|
+
|
|
341
|
+
**Commit:** `feat(nestjs-prisma): add PrismaHealthIndicator`
|
|
342
|
+
|
|
343
|
+
## Phase 7: Testing Utilities
|
|
344
|
+
|
|
345
|
+
### 7.1: Mock helpers
|
|
346
|
+
**File:** `packages/nestjs-prisma/lib/testing/prisma-testing.utils.ts`
|
|
347
|
+
|
|
348
|
+
```typescript
|
|
349
|
+
import { PrismaClientLike } from '../prisma.interfaces';
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Create a mock PrismaClient for testing.
|
|
353
|
+
* All methods return jest mock functions.
|
|
354
|
+
*/
|
|
355
|
+
export function createMockPrismaClient(): PrismaClientLike & Record<string, any> {
|
|
356
|
+
return {
|
|
357
|
+
$connect: jest.fn().mockResolvedValue(undefined),
|
|
358
|
+
$disconnect: jest.fn().mockResolvedValue(undefined),
|
|
359
|
+
$on: jest.fn(),
|
|
360
|
+
$queryRaw: jest.fn(),
|
|
361
|
+
$executeRaw: jest.fn(),
|
|
362
|
+
$transaction: jest.fn(),
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Create a mock PrismaService for testing.
|
|
368
|
+
*/
|
|
369
|
+
export function createMockPrismaService() {
|
|
370
|
+
const mockClient = createMockPrismaClient();
|
|
371
|
+
return {
|
|
372
|
+
client: mockClient,
|
|
373
|
+
isConnected: true,
|
|
374
|
+
onModuleInit: jest.fn().mockResolvedValue(undefined),
|
|
375
|
+
onModuleDestroy: jest.fn().mockResolvedValue(undefined),
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
**Commit:** `feat(nestjs-prisma): add testing utilities`
|
|
381
|
+
|
|
382
|
+
## Phase 8: Barrel Exports and Build
|
|
383
|
+
|
|
384
|
+
### 8.1: Main barrel
|
|
385
|
+
**File:** `packages/nestjs-prisma/lib/index.ts`
|
|
386
|
+
```typescript
|
|
387
|
+
export * from './prisma.constants';
|
|
388
|
+
export * from './prisma.interfaces';
|
|
389
|
+
export * from './prisma.module';
|
|
390
|
+
export * from './prisma.service';
|
|
391
|
+
export * from './prisma.decorator';
|
|
392
|
+
export * from './prisma.health';
|
|
393
|
+
export * from './pagination';
|
|
394
|
+
export * from './testing';
|
|
395
|
+
```
|
|
396
|
+
|
|
397
|
+
### 8.2: Build and verify
|
|
398
|
+
Run: `bun install`
|
|
399
|
+
Run: `bun run build --filter=@nathapp/nestjs-prisma`
|
|
400
|
+
Verify no compilation errors.
|
|
401
|
+
|
|
402
|
+
**Commit:** `chore(nestjs-prisma): finalize barrel exports and verify build`
|
|
403
|
+
|
|
404
|
+
## Test Strategy
|
|
405
|
+
- **Mode:** two-session-tdd
|
|
406
|
+
- **Session 1 (tests):** Write all test files based on the contracts defined above. Tests MUST fail (no production code exists yet except scaffolding).
|
|
407
|
+
- **Session 2 (implementation):** Implement all production code to make tests pass.
|
|
408
|
+
|
|
409
|
+
### Test Cases Required
|
|
410
|
+
|
|
411
|
+
#### prisma.service.spec.ts
|
|
412
|
+
- Creates client from provided PrismaClient class
|
|
413
|
+
- Throws error if no client class provided
|
|
414
|
+
- Calls $connect on module init
|
|
415
|
+
- Calls $disconnect on module destroy
|
|
416
|
+
- Sets isConnected correctly
|
|
417
|
+
- Logs connection success/failure
|
|
418
|
+
- Sets up query logging when logging=true
|
|
419
|
+
- Does not set up logging when logging=false
|
|
420
|
+
- Uses connection name in log messages
|
|
421
|
+
|
|
422
|
+
#### prisma.module.spec.ts
|
|
423
|
+
- forRoot() creates a global module
|
|
424
|
+
- forRoot() provides PrismaService under default token
|
|
425
|
+
- forRoot() with name provides PrismaService under named token
|
|
426
|
+
- forRoot() without name also provides as PrismaService class
|
|
427
|
+
- forRootAsync() with useFactory works
|
|
428
|
+
- forRootAsync() with useClass works
|
|
429
|
+
- Multiple forRoot() calls create separate named services (multi-db)
|
|
430
|
+
|
|
431
|
+
#### prisma.decorator.spec.ts
|
|
432
|
+
- InjectPrisma() without name injects default token
|
|
433
|
+
- InjectPrisma('analytics') injects named token
|
|
434
|
+
|
|
435
|
+
#### paginate.spec.ts
|
|
436
|
+
- Paginates with correct skip/take
|
|
437
|
+
- Returns correct total and page info
|
|
438
|
+
- hasNext/hasPrev calculated correctly
|
|
439
|
+
- Passes where/orderBy/include to findMany
|
|
440
|
+
- Passes where to count
|
|
441
|
+
- Handles empty results
|
|
442
|
+
- Handles first page / last page
|
|
443
|
+
|
|
444
|
+
#### prisma.health.spec.ts
|
|
445
|
+
- Returns 'up' when connected and query succeeds
|
|
446
|
+
- Returns 'down' with message when query fails
|
|
447
|
+
|
|
448
|
+
## Important Notes
|
|
449
|
+
- Do NOT push to remote
|
|
450
|
+
- Prisma 6+ — PrismaClient is NOT extended (wrapper pattern)
|
|
451
|
+
- `PrismaClientLike` is the minimal interface — don't import from `@prisma/client` in our source (keep it as peer dep only)
|
|
452
|
+
- Use `@golevelup/ts-jest` for NestJS testing utilities (already in nathapp patterns)
|
|
453
|
+
- Use `import type` for type-only imports to avoid runtime resolution issues (lesson from nestjs-typeorm)
|
|
454
|
+
- The `$queryRaw` template literal syntax won't work without a real Prisma client — health check should gracefully handle this
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# Fix Plan: Move Pagination to Common + Rename nestjs-data → nestjs-typeorm
|
|
2
|
+
|
|
3
|
+
**Date:** 2026-02-14
|
|
4
|
+
**Branch:** `feat/monorepo-enterprise-upgrade`
|
|
5
|
+
|
|
6
|
+
## Phase 1: Move ORM-Agnostic Types to nestjs-common
|
|
7
|
+
|
|
8
|
+
### 1.1: Move pagination types to nestjs-common
|
|
9
|
+
**Source:** `packages/nestjs-data/lib/pagination/page.ts`
|
|
10
|
+
**Target:** Create `packages/nestjs-common/lib/pagination/page.ts`
|
|
11
|
+
|
|
12
|
+
Move ONLY the ORM-agnostic parts:
|
|
13
|
+
- `IPageOption` interface
|
|
14
|
+
- `PageOption` class
|
|
15
|
+
- `Page<T, S>` class (including `TransformOptions`, `mapFunction` type)
|
|
16
|
+
- `resolvePageOption` helper function (used by Paginate functions — keep it exported so typeorm/prisma packages can use it)
|
|
17
|
+
|
|
18
|
+
**Do NOT move:** `Paginate()` function and its helpers (`paginateRepository`, `paginateQueryBuilder`, `paginateMongoRepository`) — these are TypeORM-specific.
|
|
19
|
+
|
|
20
|
+
**Remove from Page class:** The `import { FindManyOptions, MongoRepository, Repository, SelectQueryBuilder } from "typeorm"` — these should not exist in the moved file. The `Page` class itself does NOT use these imports directly (only `Paginate()` does).
|
|
21
|
+
|
|
22
|
+
Create barrel: `packages/nestjs-common/lib/pagination/index.ts`
|
|
23
|
+
Update main barrel: `packages/nestjs-common/lib/index.ts` — add `export * from "./pagination"`
|
|
24
|
+
|
|
25
|
+
### 1.2: Move BaseEntity to nestjs-common
|
|
26
|
+
**Source:** `packages/nestjs-data/lib/entities/base-entity.ts`
|
|
27
|
+
**Target:** `packages/nestjs-common/lib/entities/base-entity.ts`
|
|
28
|
+
|
|
29
|
+
This is trivial — it just `extends Serializable` which is already in nestjs-common.
|
|
30
|
+
|
|
31
|
+
Create barrel: `packages/nestjs-common/lib/entities/index.ts`
|
|
32
|
+
Update main barrel: `packages/nestjs-common/lib/index.ts` — add `export * from "./entities"`
|
|
33
|
+
|
|
34
|
+
### 1.3: Build and test nestjs-common
|
|
35
|
+
Run: `bun run build --filter=@nathapp/nestjs-common`
|
|
36
|
+
Run: `bun test packages/nestjs-common`
|
|
37
|
+
|
|
38
|
+
**Commit:** `feat(nestjs-common): move Page, PageOption, IPageOption, BaseEntity from nestjs-data`
|
|
39
|
+
|
|
40
|
+
## Phase 2: Rename nestjs-data → nestjs-typeorm
|
|
41
|
+
|
|
42
|
+
### 2.1: Rename the package directory
|
|
43
|
+
```bash
|
|
44
|
+
mv packages/nestjs-data packages/nestjs-typeorm
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### 2.2: Update package.json
|
|
48
|
+
**File:** `packages/nestjs-typeorm/package.json`
|
|
49
|
+
- Change `"name"` from `"@nathapp/nestjs-data"` to `"@nathapp/nestjs-typeorm"`
|
|
50
|
+
- Keep version as-is (will bump to 3.0.0 later)
|
|
51
|
+
- Update dependency: `"@nathapp/nestjs-common": "^2.1.5"` (keep as-is for now)
|
|
52
|
+
|
|
53
|
+
### 2.3: Update internal imports in nestjs-typeorm
|
|
54
|
+
**File:** `packages/nestjs-typeorm/lib/pagination/page.ts`
|
|
55
|
+
- Remove `Page`, `PageOption`, `IPageOption`, `TransformOptions`, `mapFunction`, `resolvePageOption` from this file
|
|
56
|
+
- Keep ONLY the TypeORM-specific `Paginate()` function and its helpers
|
|
57
|
+
- Import `Page`, `PageOption`, `IPageOption`, `resolvePageOption`, `TransformOptions` from `@nathapp/nestjs-common`
|
|
58
|
+
- Rename file to `packages/nestjs-typeorm/lib/pagination/paginate.ts` for clarity
|
|
59
|
+
|
|
60
|
+
**File:** `packages/nestjs-typeorm/lib/entities/base-entity.ts`
|
|
61
|
+
- Delete this file — `BaseEntity` now lives in nestjs-common
|
|
62
|
+
- Update barrel `packages/nestjs-typeorm/lib/entities/index.ts` — re-export from `@nathapp/nestjs-common` for backward compat:
|
|
63
|
+
```typescript
|
|
64
|
+
// Re-export from nestjs-common for backward compatibility
|
|
65
|
+
export { BaseEntity } from '@nathapp/nestjs-common';
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
**File:** `packages/nestjs-typeorm/lib/index.ts`
|
|
69
|
+
- Keep all existing exports
|
|
70
|
+
- The re-exports from entities/ will provide backward compat
|
|
71
|
+
|
|
72
|
+
### 2.4: Fix typos while we're here
|
|
73
|
+
- `sanatizeTypeOrmOptions` → `sanitizeTypeOrmOptions` (in typeorm-utils.ts, typeorm-ex.module.ts, base-typeorm-options-factory.ts)
|
|
74
|
+
- `formattedSP` → keep (it's a stored procedure abbreviation, not a typo)
|
|
75
|
+
- `payload.inteface.ts` — already fixed in notification, check if similar issues here
|
|
76
|
+
|
|
77
|
+
### 2.5: Fix Page.remap() constructor bug
|
|
78
|
+
**File:** `packages/nestjs-common/lib/pagination/page.ts` (moved file)
|
|
79
|
+
The `remap()` method calls `new Page<E>()` with 4 args but constructor expects 5 (page, total, records, transformOptions, extractDefault). The `extra` param is being passed as the 4th arg (transformOptions position).
|
|
80
|
+
|
|
81
|
+
Fix:
|
|
82
|
+
```typescript
|
|
83
|
+
// Before
|
|
84
|
+
return new Page<E>(
|
|
85
|
+
{ current: this.current, size: this.size },
|
|
86
|
+
this.total,
|
|
87
|
+
cls ? plainToInstance(cls, records) : records,
|
|
88
|
+
extra || this.extra,
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
// After
|
|
92
|
+
return new Page<E, S>(
|
|
93
|
+
{ current: this.current, size: this.size },
|
|
94
|
+
this.total,
|
|
95
|
+
cls ? plainToInstance(cls, records) : records,
|
|
96
|
+
undefined, // no transformOptions for remapped page
|
|
97
|
+
extra || this.extra as any,
|
|
98
|
+
);
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### 2.6: Remove deprecated MongoDB options
|
|
102
|
+
**File:** `packages/nestjs-typeorm/lib/typeorm/constants.ts`
|
|
103
|
+
Remove `useNewUrlParser` and `useUnifiedTopology` from `defaultMongoExtraOptions` — these are deprecated in modern MongoDB drivers and ignored.
|
|
104
|
+
|
|
105
|
+
### 2.7: Update turbo.json / workspace config if needed
|
|
106
|
+
Check if `turbo.json` or root `package.json` workspaces array references `nestjs-data` — update to `nestjs-typeorm`.
|
|
107
|
+
|
|
108
|
+
### 2.8: Build and test
|
|
109
|
+
Run: `bun install` (to update workspace resolution)
|
|
110
|
+
Run: `bun run build --filter=@nathapp/nestjs-typeorm`
|
|
111
|
+
Run: `bun test packages/nestjs-typeorm`
|
|
112
|
+
|
|
113
|
+
**Commit:** `refactor!: rename @nathapp/nestjs-data to @nathapp/nestjs-typeorm, fix pagination bugs`
|
|
114
|
+
|
|
115
|
+
## Phase 3: Add Pagination Tests to nestjs-common
|
|
116
|
+
|
|
117
|
+
### 3.1: Unit tests for Page class
|
|
118
|
+
**File:** Create `packages/nestjs-common/test/pagination/page.spec.ts`
|
|
119
|
+
**Tests:**
|
|
120
|
+
- `Page` constructor sets all fields correctly
|
|
121
|
+
- `hasNext` is true when more pages exist
|
|
122
|
+
- `hasPrev` is true when not on first page
|
|
123
|
+
- `toInstance()` transforms records using class-transformer
|
|
124
|
+
- `toPlain()` converts to plain objects
|
|
125
|
+
- `mapTo()` maps records with a function
|
|
126
|
+
- `remap()` creates new Page with mapped records and correct pagination
|
|
127
|
+
- `PageOption.from()` static factory
|
|
128
|
+
- `PageOption` defaults (current=1, size=10)
|
|
129
|
+
- `resolvePageOption` calculates correct skip/take
|
|
130
|
+
- Edge cases: page 0, negative page, size 0
|
|
131
|
+
|
|
132
|
+
**Commit:** `test(nestjs-common): add unit tests for Page, PageOption, and resolvePageOption`
|
|
133
|
+
|
|
134
|
+
## Test Strategy
|
|
135
|
+
- Mode: hybrid
|
|
136
|
+
- TDD targets: none (this is refactoring, not new behavior)
|
|
137
|
+
- Test-after targets: Page class tests in nestjs-common
|
|
138
|
+
- After Phase 1: build nestjs-common, verify compilation
|
|
139
|
+
- After Phase 2: build nestjs-typeorm, verify it imports from nestjs-common correctly
|
|
140
|
+
- After Phase 3: full test suite for both packages
|
|
141
|
+
|
|
142
|
+
## Important Notes
|
|
143
|
+
- Do NOT push to remote — local commits only
|
|
144
|
+
- No other package in the monorepo imports from `@nathapp/nestjs-data` (verified by grep)
|
|
145
|
+
- The `nestjs-auth-api` in nestjs-infra repo DOES depend on it — that's a separate repo update
|
|
146
|
+
- `na-cli` template also depends on it — separate update (Phase 4 of the broader plan)
|
|
147
|
+
- `class-transformer` is already a peer dep of nestjs-common ✅
|
|
148
|
+
- Keep backward compat re-exports in nestjs-typeorm for `BaseEntity`
|