@nathapp/nestjs-prisma 3.3.0 → 4.0.1

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 (43) hide show
  1. package/package.json +10 -7
  2. package/.eslintrc.js +0 -8
  3. package/.turbo/turbo-build.log +0 -4
  4. package/CHANGELOG.md +0 -279
  5. package/docs/20260215-fix-plan-prisma.md +0 -37
  6. package/docs/20260215-review-prisma.md +0 -598
  7. package/docs/20260216-plan-prisma-a-minus.md +0 -70
  8. package/lib/errors/prisma-error.mapper.ts +0 -164
  9. package/lib/filter/prisma-exception.filter.ts +0 -67
  10. package/lib/index.ts +0 -12
  11. package/lib/pagination/index.ts +0 -1
  12. package/lib/pagination/paginate.ts +0 -44
  13. package/lib/prisma.constants.ts +0 -40
  14. package/lib/prisma.decorator.ts +0 -8
  15. package/lib/prisma.health.ts +0 -40
  16. package/lib/prisma.interfaces.ts +0 -51
  17. package/lib/prisma.module.ts +0 -281
  18. package/lib/prisma.service.ts +0 -179
  19. package/lib/repository/abstract-prisma.repository.ts +0 -138
  20. package/lib/repository/index.ts +0 -1
  21. package/lib/testing/index.ts +0 -1
  22. package/lib/testing/prisma-testing.utils.ts +0 -29
  23. package/lib/transaction/index.ts +0 -2
  24. package/lib/transaction/prisma-transaction-manager.ts +0 -77
  25. package/lib/transaction/prisma-transaction.constants.ts +0 -29
  26. package/test/error-mapping.spec.ts +0 -231
  27. package/test/jest-nax.json +0 -27
  28. package/test/jest-test.json +0 -29
  29. package/test/lifecycle.spec.ts +0 -241
  30. package/test/multi-db-als.spec.ts +0 -142
  31. package/test/multi-db-transaction.spec.ts +0 -75
  32. package/test/pagination/paginate.spec.ts +0 -359
  33. package/test/prisma-exception-filter.spec.ts +0 -213
  34. package/test/prisma-transaction-manager.spec.ts +0 -285
  35. package/test/prisma.decorator.spec.ts +0 -257
  36. package/test/prisma.health.spec.ts +0 -226
  37. package/test/prisma.module.spec.ts +0 -506
  38. package/test/prisma.service.spec.ts +0 -359
  39. package/test/repository/abstract-prisma.repository.spec.ts +0 -157
  40. package/test/transaction-manager-e2e.spec.ts +0 -496
  41. package/test/transaction.spec.ts +0 -371
  42. package/test/transparent-transaction.spec.ts +0 -500
  43. package/tsconfig.json +0 -9
@@ -1,500 +0,0 @@
1
- import { Test, TestingModule } from '@nestjs/testing';
2
- import { Injectable, Inject } from '@nestjs/common';
3
- import { TRANSACTION_MANAGER, ITransactionManager } from '@nathapp/nestjs-data';
4
- import { PrismaModule } from '../lib/prisma.module';
5
- import { PrismaService } from '../lib/prisma.service';
6
- import { PrismaTransactionManager } from '../lib/transaction/prisma-transaction-manager';
7
- import { getPrismaToken, getPrismaTransactionManagerToken } from '../lib/prisma.constants';
8
- import type { PrismaClientLike } from '../lib/prisma.interfaces';
9
-
10
- describe('Transparent Transaction Support', () => {
11
- let mockClient: any;
12
- let mockTxClient: any;
13
-
14
- beforeEach(() => {
15
- mockTxClient = {
16
- $connect: jest.fn().mockResolvedValue(undefined),
17
- $disconnect: jest.fn().mockResolvedValue(undefined),
18
- user: {
19
- findMany: jest.fn().mockResolvedValue([{ id: 1, name: 'tx-user' }]),
20
- create: jest.fn().mockResolvedValue({ id: 2, name: 'created-in-tx' }),
21
- },
22
- };
23
-
24
- mockClient = {
25
- $connect: jest.fn().mockResolvedValue(undefined),
26
- $disconnect: jest.fn().mockResolvedValue(undefined),
27
- $on: jest.fn(),
28
- $transaction: jest.fn().mockImplementation(async (callback: Function) => {
29
- return callback(mockTxClient);
30
- }),
31
- user: {
32
- findMany: jest.fn().mockResolvedValue([{ id: 1, name: 'regular-user' }]),
33
- create: jest.fn().mockResolvedValue({ id: 2, name: 'created-outside-tx' }),
34
- },
35
- };
36
- });
37
-
38
- describe('forRoot with transaction: true', () => {
39
- it('should provide TRANSACTION_MANAGER token', async () => {
40
- const ClientClass = jest.fn(() => mockClient);
41
- const module = await Test.createTestingModule({
42
- imports: [
43
- PrismaModule.forRoot({
44
- client: ClientClass as any,
45
- transaction: true,
46
- }),
47
- ],
48
- }).compile();
49
-
50
- const txManager = module.get(TRANSACTION_MANAGER);
51
- expect(txManager).toBeDefined();
52
- expect(txManager).toBeInstanceOf(PrismaTransactionManager);
53
- });
54
-
55
- it('should provide PrismaTransactionManager', async () => {
56
- const ClientClass = jest.fn(() => mockClient);
57
- const module = await Test.createTestingModule({
58
- imports: [
59
- PrismaModule.forRoot({
60
- client: ClientClass as any,
61
- transaction: true,
62
- }),
63
- ],
64
- }).compile();
65
-
66
- const txManager = module.get(PrismaTransactionManager);
67
- expect(txManager).toBeDefined();
68
- });
69
-
70
- it('should NOT provide TRANSACTION_MANAGER when transaction is false/undefined', async () => {
71
- const ClientClass = jest.fn(() => mockClient);
72
- const module = await Test.createTestingModule({
73
- imports: [
74
- PrismaModule.forRoot({
75
- client: ClientClass as any,
76
- }),
77
- ],
78
- }).compile();
79
-
80
- expect(() => module.get(TRANSACTION_MANAGER)).toThrow();
81
- });
82
-
83
- it('should use regular client outside of transaction', async () => {
84
- const ClientClass = jest.fn(() => mockClient);
85
- const module = await Test.createTestingModule({
86
- imports: [
87
- PrismaModule.forRoot({
88
- client: ClientClass as any,
89
- transaction: true,
90
- }),
91
- ],
92
- }).compile();
93
-
94
- const prismaService = module.get<PrismaService>(getPrismaToken());
95
- const result = await (prismaService.client as any).user.findMany();
96
-
97
- expect(result).toEqual([{ id: 1, name: 'regular-user' }]);
98
- expect(mockClient.user.findMany).toHaveBeenCalled();
99
- });
100
-
101
- it('should transparently use transaction client inside txManager.run()', async () => {
102
- const ClientClass = jest.fn(() => mockClient);
103
- const module = await Test.createTestingModule({
104
- imports: [
105
- PrismaModule.forRoot({
106
- client: ClientClass as any,
107
- transaction: true,
108
- }),
109
- ],
110
- }).compile();
111
-
112
- const prismaService = module.get<PrismaService>(getPrismaToken());
113
- const txManager = module.get<ITransactionManager>(TRANSACTION_MANAGER);
114
-
115
- let resultInTx: any;
116
- await txManager.run(async () => {
117
- resultInTx = await (prismaService.client as any).user.findMany();
118
- });
119
-
120
- expect(resultInTx).toEqual([{ id: 1, name: 'tx-user' }]);
121
- expect(mockTxClient.user.findMany).toHaveBeenCalled();
122
- expect(mockClient.user.findMany).not.toHaveBeenCalled();
123
- });
124
-
125
- it('should revert to regular client after transaction completes', async () => {
126
- const ClientClass = jest.fn(() => mockClient);
127
- const module = await Test.createTestingModule({
128
- imports: [
129
- PrismaModule.forRoot({
130
- client: ClientClass as any,
131
- transaction: true,
132
- }),
133
- ],
134
- }).compile();
135
-
136
- const prismaService = module.get<PrismaService>(getPrismaToken());
137
- const txManager = module.get<ITransactionManager>(TRANSACTION_MANAGER);
138
-
139
- await txManager.run(async () => {
140
- await (prismaService.client as any).user.findMany();
141
- });
142
-
143
- // After transaction, should use regular client
144
- const result = await (prismaService.client as any).user.findMany();
145
- expect(result).toEqual([{ id: 1, name: 'regular-user' }]);
146
- expect(mockClient.user.findMany).toHaveBeenCalled();
147
- });
148
-
149
- it('should handle nested transactions (reuse outer tx)', async () => {
150
- const ClientClass = jest.fn(() => mockClient);
151
- const module = await Test.createTestingModule({
152
- imports: [
153
- PrismaModule.forRoot({
154
- client: ClientClass as any,
155
- transaction: true,
156
- }),
157
- ],
158
- }).compile();
159
-
160
- const prismaService = module.get<PrismaService>(getPrismaToken());
161
- const txManager = module.get<ITransactionManager>(TRANSACTION_MANAGER);
162
-
163
- let innerResult: any;
164
- await txManager.run(async () => {
165
- await txManager.run(async () => {
166
- innerResult = await (prismaService.client as any).user.findMany();
167
- });
168
- });
169
-
170
- expect(innerResult).toEqual([{ id: 1, name: 'tx-user' }]);
171
- // Only one $transaction call (nested reuses outer)
172
- expect(mockClient.$transaction).toHaveBeenCalledTimes(1);
173
- });
174
-
175
- it('should handle concurrent transactions independently', async () => {
176
- const ClientClass = jest.fn(() => mockClient);
177
- const module = await Test.createTestingModule({
178
- imports: [
179
- PrismaModule.forRoot({
180
- client: ClientClass as any,
181
- transaction: true,
182
- }),
183
- ],
184
- }).compile();
185
-
186
- const prismaService = module.get<PrismaService>(getPrismaToken());
187
- const txManager = module.get<ITransactionManager>(TRANSACTION_MANAGER);
188
-
189
- const results: any[] = [];
190
- await Promise.all([
191
- txManager.run(async () => {
192
- results.push(await (prismaService.client as any).user.findMany());
193
- }),
194
- txManager.run(async () => {
195
- results.push(await (prismaService.client as any).user.create({ data: {} }));
196
- }),
197
- ]);
198
-
199
- expect(results).toHaveLength(2);
200
- expect(mockClient.$transaction).toHaveBeenCalledTimes(2);
201
- });
202
-
203
- it('should roll back and revert proxy on error', async () => {
204
- const ClientClass = jest.fn(() => mockClient);
205
- const module = await Test.createTestingModule({
206
- imports: [
207
- PrismaModule.forRoot({
208
- client: ClientClass as any,
209
- transaction: true,
210
- }),
211
- ],
212
- }).compile();
213
-
214
- const prismaService = module.get<PrismaService>(getPrismaToken());
215
- const txManager = module.get<ITransactionManager>(TRANSACTION_MANAGER);
216
-
217
- mockClient.$transaction.mockImplementation(async (cb: Function) => {
218
- try {
219
- return await cb(mockTxClient);
220
- } catch (e) {
221
- throw e; // simulates rollback
222
- }
223
- });
224
-
225
- await expect(
226
- txManager.run(async () => {
227
- throw new Error('boom');
228
- }),
229
- ).rejects.toThrow('boom');
230
-
231
- // After failed tx, proxy should resolve to regular client
232
- const result = await (prismaService.client as any).user.findMany();
233
- expect(result).toEqual([{ id: 1, name: 'regular-user' }]);
234
- });
235
- });
236
-
237
- describe('Integration: repo pattern', () => {
238
- @Injectable()
239
- class UserRepository {
240
- constructor(
241
- @Inject(getPrismaToken()) private readonly prisma: PrismaService,
242
- ) {}
243
-
244
- async findAll() {
245
- return (this.prisma.client as any).user.findMany();
246
- }
247
-
248
- async create(data: any) {
249
- return (this.prisma.client as any).user.create({ data });
250
- }
251
- }
252
-
253
- @Injectable()
254
- class UserService {
255
- constructor(
256
- @Inject(TRANSACTION_MANAGER) private readonly txManager: ITransactionManager,
257
- private readonly userRepo: UserRepository,
258
- ) {}
259
-
260
- async createWithTransaction(data: any) {
261
- return this.txManager.run(async () => {
262
- return this.userRepo.create(data);
263
- });
264
- }
265
- }
266
-
267
- it('repo is transaction-unaware but uses tx client transparently', async () => {
268
- const ClientClass = jest.fn(() => mockClient);
269
- const module = await Test.createTestingModule({
270
- imports: [
271
- PrismaModule.forRoot({
272
- client: ClientClass as any,
273
- transaction: true,
274
- }),
275
- ],
276
- providers: [UserRepository, UserService],
277
- }).compile();
278
-
279
- const userService = module.get(UserService);
280
- const result = await userService.createWithTransaction({ name: 'test' });
281
-
282
- expect(result).toEqual({ id: 2, name: 'created-in-tx' });
283
- expect(mockTxClient.user.create).toHaveBeenCalledWith({ data: { name: 'test' } });
284
- // The regular client's create should NOT have been called
285
- expect(mockClient.user.create).not.toHaveBeenCalled();
286
- });
287
-
288
- it('repo uses regular client when not in transaction', async () => {
289
- const ClientClass = jest.fn(() => mockClient);
290
- const module = await Test.createTestingModule({
291
- imports: [
292
- PrismaModule.forRoot({
293
- client: ClientClass as any,
294
- transaction: true,
295
- }),
296
- ],
297
- providers: [UserRepository],
298
- }).compile();
299
-
300
- const userRepo = module.get(UserRepository);
301
- const result = await userRepo.findAll();
302
-
303
- expect(result).toEqual([{ id: 1, name: 'regular-user' }]);
304
- expect(mockClient.user.findMany).toHaveBeenCalled();
305
- });
306
- });
307
-
308
- describe('Named connections with transaction: true', () => {
309
- it('should support transaction on named connections', async () => {
310
- const ClientClass = jest.fn(() => mockClient);
311
- const module = await Test.createTestingModule({
312
- imports: [
313
- PrismaModule.forRoot({
314
- client: ClientClass as any,
315
- name: 'analytics',
316
- transaction: true,
317
- }),
318
- ],
319
- }).compile();
320
-
321
- const prismaService = module.get<PrismaService>(getPrismaToken('analytics'));
322
- const txManager = module.get<ITransactionManager>(getPrismaTransactionManagerToken('analytics'));
323
-
324
- let resultInTx: any;
325
- await txManager.run(async () => {
326
- resultInTx = await (prismaService.client as any).user.findMany();
327
- });
328
-
329
- expect(resultInTx).toEqual([{ id: 1, name: 'tx-user' }]);
330
- });
331
- });
332
- });
333
-
334
- describe('getPrismaTransactionManagerToken', () => {
335
- it('returns the default token when name is undefined', () => {
336
- expect(getPrismaTransactionManagerToken()).toBe('PRISMA_TRANSACTION_MANAGER_DEFAULT');
337
- });
338
-
339
- it('returns the default token when name is "default"', () => {
340
- expect(getPrismaTransactionManagerToken('default')).toBe('PRISMA_TRANSACTION_MANAGER_DEFAULT');
341
- });
342
-
343
- it('returns a distinct token per name', () => {
344
- expect(getPrismaTransactionManagerToken('dbA')).toBe('PRISMA_TRANSACTION_MANAGER_dbA');
345
- expect(getPrismaTransactionManagerToken('dbB')).toBe('PRISMA_TRANSACTION_MANAGER_dbB');
346
- expect(getPrismaTransactionManagerToken('dbA')).not.toBe(getPrismaTransactionManagerToken('dbB'));
347
- });
348
-
349
- it('does not collide with getPrismaToken for the same name', () => {
350
- const { getPrismaToken } = require('../lib/prisma.constants');
351
- expect(getPrismaTransactionManagerToken('dbA')).not.toBe(getPrismaToken('dbA'));
352
- });
353
- });
354
-
355
- describe('forRoot — multi-DB (named connection)', () => {
356
- let mockClient: any;
357
-
358
- beforeEach(() => {
359
- mockClient = {
360
- $connect: jest.fn().mockResolvedValue(undefined),
361
- $disconnect: jest.fn().mockResolvedValue(undefined),
362
- $on: jest.fn(),
363
- $transaction: jest.fn().mockImplementation(async (callback: Function) => {
364
- return callback({});
365
- }),
366
- };
367
- });
368
-
369
- it('registers named tx manager token but NOT global TRANSACTION_MANAGER', async () => {
370
- const module = await Test.createTestingModule({
371
- imports: [
372
- PrismaModule.forRoot({
373
- name: 'dbA',
374
- client: jest.fn(() => mockClient) as any,
375
- transaction: true,
376
- }),
377
- ],
378
- }).compile();
379
-
380
- const named = module.get(getPrismaTransactionManagerToken('dbA'));
381
- expect(named).toBeDefined();
382
- expect(typeof (named as any).run).toBe('function');
383
-
384
- // Global token is NOT registered for non-default connections.
385
- expect(() => module.get(TRANSACTION_MANAGER)).toThrow();
386
- });
387
-
388
- it('two named connections in the same module do not collide', async () => {
389
- const module = await Test.createTestingModule({
390
- imports: [
391
- PrismaModule.forRoot({ name: 'dbA', client: jest.fn(() => mockClient) as any, transaction: true }),
392
- PrismaModule.forRoot({ name: 'dbB', client: jest.fn(() => mockClient) as any, transaction: true }),
393
- ],
394
- }).compile();
395
-
396
- const txA = module.get(getPrismaTransactionManagerToken('dbA'));
397
- const txB = module.get(getPrismaTransactionManagerToken('dbB'));
398
-
399
- expect(txA).toBeDefined();
400
- expect(txB).toBeDefined();
401
- expect(txA).not.toBe(txB);
402
-
403
- // Global token is NOT registered when neither connection is the default.
404
- expect(() => module.get(TRANSACTION_MANAGER)).toThrow();
405
- });
406
-
407
- it('default connection still aliases the global TRANSACTION_MANAGER', async () => {
408
- const module = await Test.createTestingModule({
409
- imports: [
410
- PrismaModule.forRoot({ client: jest.fn(() => mockClient) as any, transaction: true }),
411
- ],
412
- }).compile();
413
-
414
- const named = module.get(getPrismaTransactionManagerToken());
415
- const global = module.get(TRANSACTION_MANAGER);
416
- expect(named).toBe(global); // useExisting alias
417
- });
418
-
419
- it('default + named connections coexist; only default aliases the global token', async () => {
420
- const module = await Test.createTestingModule({
421
- imports: [
422
- PrismaModule.forRoot({ client: jest.fn(() => mockClient) as any, transaction: true }),
423
- PrismaModule.forRoot({ name: 'analytics', client: jest.fn(() => mockClient) as any, transaction: true }),
424
- ],
425
- }).compile();
426
-
427
- const txDefault = module.get(getPrismaTransactionManagerToken());
428
- const txAnalytics = module.get(getPrismaTransactionManagerToken('analytics'));
429
- const global = module.get(TRANSACTION_MANAGER);
430
-
431
- expect(txDefault).toBe(global);
432
- expect(txAnalytics).not.toBe(global);
433
- });
434
- });
435
-
436
- describe('forRootAsync — tx manager registration', () => {
437
- let mockClient: any;
438
-
439
- beforeEach(() => {
440
- mockClient = {
441
- $connect: jest.fn().mockResolvedValue(undefined),
442
- $disconnect: jest.fn().mockResolvedValue(undefined),
443
- $on: jest.fn(),
444
- $transaction: jest.fn().mockImplementation(async (callback: Function) => {
445
- return callback({});
446
- }),
447
- };
448
- });
449
-
450
- it('default connection: registers global TRANSACTION_MANAGER alias', async () => {
451
- const module = await Test.createTestingModule({
452
- imports: [
453
- PrismaModule.forRootAsync({
454
- useFactory: () => ({
455
- client: jest.fn(() => mockClient) as any,
456
- transaction: true,
457
- }),
458
- }),
459
- ],
460
- }).compile();
461
-
462
- const named = module.get(getPrismaTransactionManagerToken());
463
- const global = module.get(TRANSACTION_MANAGER);
464
- expect(named).toBe(global);
465
- expect(typeof (named as any).run).toBe('function');
466
- });
467
-
468
- it('named connection: registers named token but NOT global', async () => {
469
- const module = await Test.createTestingModule({
470
- imports: [
471
- PrismaModule.forRootAsync({
472
- name: 'dbA',
473
- useFactory: () => ({
474
- name: 'dbA',
475
- client: jest.fn(() => mockClient) as any,
476
- transaction: true,
477
- }),
478
- }),
479
- ],
480
- }).compile();
481
-
482
- const named = module.get(getPrismaTransactionManagerToken('dbA'));
483
- expect(typeof (named as any).run).toBe('function');
484
- expect(() => module.get(TRANSACTION_MANAGER)).toThrow();
485
- });
486
-
487
- it('async useFactory + transaction:false → tx manager throws a clear error on use', async () => {
488
- const module = await Test.createTestingModule({
489
- imports: [
490
- PrismaModule.forRootAsync({
491
- useFactory: () => ({ client: jest.fn(() => mockClient) as any }),
492
- }),
493
- ],
494
- }).compile();
495
-
496
- const txManager = module.get<ITransactionManager>(TRANSACTION_MANAGER);
497
- expect(txManager.isInTransaction()).toBe(false);
498
- await expect(txManager.run(async () => undefined)).rejects.toThrow('Prisma transaction support for connection "default" was not enabled');
499
- });
500
- });
package/tsconfig.json DELETED
@@ -1,9 +0,0 @@
1
- {
2
- "extends": "@nathapp/typescript-config/base.json",
3
- "compilerOptions": {
4
- "outDir": "./dist",
5
- "rootDir": "./lib",
6
- },
7
- "include": ["lib/**/*"],
8
- "exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
9
- }