@nathapp/nestjs-prisma 4.0.0 → 4.0.2

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 (45) hide show
  1. package/dist/prisma.module.js +8 -0
  2. package/dist/prisma.module.js.map +1 -1
  3. package/package.json +4 -4
  4. package/.eslintrc.js +0 -8
  5. package/.turbo/turbo-build.log +0 -4
  6. package/CHANGELOG.md +0 -314
  7. package/docs/20260215-fix-plan-prisma.md +0 -37
  8. package/docs/20260215-review-prisma.md +0 -598
  9. package/docs/20260216-plan-prisma-a-minus.md +0 -70
  10. package/lib/errors/prisma-error.mapper.ts +0 -164
  11. package/lib/filter/prisma-exception.filter.ts +0 -67
  12. package/lib/index.ts +0 -12
  13. package/lib/pagination/index.ts +0 -1
  14. package/lib/pagination/paginate.ts +0 -44
  15. package/lib/prisma.constants.ts +0 -40
  16. package/lib/prisma.decorator.ts +0 -8
  17. package/lib/prisma.health.ts +0 -40
  18. package/lib/prisma.interfaces.ts +0 -51
  19. package/lib/prisma.module.ts +0 -281
  20. package/lib/prisma.service.ts +0 -179
  21. package/lib/repository/abstract-prisma.repository.ts +0 -138
  22. package/lib/repository/index.ts +0 -1
  23. package/lib/testing/index.ts +0 -1
  24. package/lib/testing/prisma-testing.utils.ts +0 -29
  25. package/lib/transaction/index.ts +0 -2
  26. package/lib/transaction/prisma-transaction-manager.ts +0 -77
  27. package/lib/transaction/prisma-transaction.constants.ts +0 -29
  28. package/test/error-mapping.spec.ts +0 -231
  29. package/test/jest-nax.json +0 -27
  30. package/test/jest-test.json +0 -29
  31. package/test/lifecycle.spec.ts +0 -241
  32. package/test/multi-db-als.spec.ts +0 -142
  33. package/test/multi-db-transaction.spec.ts +0 -75
  34. package/test/pagination/paginate.spec.ts +0 -359
  35. package/test/prisma-exception-filter.spec.ts +0 -213
  36. package/test/prisma-transaction-manager.spec.ts +0 -285
  37. package/test/prisma.decorator.spec.ts +0 -257
  38. package/test/prisma.health.spec.ts +0 -226
  39. package/test/prisma.module.spec.ts +0 -506
  40. package/test/prisma.service.spec.ts +0 -359
  41. package/test/repository/abstract-prisma.repository.spec.ts +0 -157
  42. package/test/transaction-manager-e2e.spec.ts +0 -496
  43. package/test/transaction.spec.ts +0 -371
  44. package/test/transparent-transaction.spec.ts +0 -500
  45. package/tsconfig.json +0 -9
@@ -1,496 +0,0 @@
1
- import { Test } from '@nestjs/testing';
2
- import { Injectable, Inject, Module } from '@nestjs/common';
3
- import { ITransactionManager, TRANSACTION_MANAGER } from '@nathapp/nestjs-data';
4
- import { PrismaModule } from '../lib/prisma.module';
5
- import { PrismaService } from '../lib/prisma.service';
6
- import { getPrismaToken, getPrismaTransactionManagerToken } from '../lib/prisma.constants';
7
- import { PrismaModuleOptions, PrismaOptionsFactory } from '../lib/prisma.interfaces';
8
- import { AbstractPrismaRepository } from '../lib/repository/abstract-prisma.repository';
9
-
10
- describe('E2E: Prisma TransactionManager wiring', () => {
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
- // ============================================================================
39
- // Helper classes
40
- // ============================================================================
41
-
42
- @Injectable()
43
- class DefaultTxRepository {
44
- constructor(
45
- @Inject(TRANSACTION_MANAGER) private readonly txManager: ITransactionManager,
46
- @Inject(getPrismaToken()) private readonly prisma: PrismaService,
47
- ) {}
48
-
49
- async create(data: any) {
50
- return this.txManager.run(async () => {
51
- return (this.prisma.client as any).user.create({ data });
52
- });
53
- }
54
- }
55
-
56
- @Injectable()
57
- class NamedTxRepository {
58
- constructor(
59
- @Inject(getPrismaTransactionManagerToken('dbA')) private readonly txManager: ITransactionManager,
60
- @Inject(getPrismaToken('dbA')) private readonly prisma: PrismaService,
61
- ) {}
62
-
63
- async create(data: any) {
64
- return this.txManager.run(async () => {
65
- return (this.prisma.client as any).user.create({ data });
66
- });
67
- }
68
- }
69
-
70
- interface TestDomain { id: string; name: string; }
71
- interface TestModel { id: string; name: string; }
72
-
73
- @Injectable()
74
- class TestAbstractRepo extends AbstractPrismaRepository<TestDomain, TestModel, string> {
75
- protected modelDelegate(client: any) { return client.user; }
76
- protected toDomain(m: TestModel): TestDomain { return { id: m.id, name: m.name }; }
77
- protected toPersistenceCreate(d: TestDomain): any { return { id: d.id, name: d.name }; }
78
- protected toPersistenceUpdate(p: Partial<TestDomain>): any { return { ...p }; }
79
- }
80
-
81
- @Module({ providers: [DefaultTxRepository], exports: [DefaultTxRepository] })
82
- class DefaultFeatureModule {}
83
-
84
- @Module({ providers: [NamedTxRepository], exports: [NamedTxRepository] })
85
- class NamedFeatureModule {}
86
-
87
- @Module({ providers: [TestAbstractRepo], exports: [TestAbstractRepo] })
88
- class AbstractRepoFeatureModule {}
89
-
90
- // ============================================================================
91
- // forRoot + default connection
92
- // ============================================================================
93
-
94
- describe('forRoot({ transaction: true }) — default connection', () => {
95
- it('resolves TRANSACTION_MANAGER in a feature module', async () => {
96
- const ClientClass = jest.fn(() => mockClient);
97
- const module = await Test.createTestingModule({
98
- imports: [
99
- PrismaModule.forRoot({ client: ClientClass as any, transaction: true }),
100
- DefaultFeatureModule,
101
- ],
102
- }).compile();
103
-
104
- const repo = module.get(DefaultTxRepository);
105
- expect(repo).toBeDefined();
106
-
107
- const result = await repo.create({ name: 'test' });
108
- expect(result).toEqual({ id: 2, name: 'created-in-tx' });
109
- });
110
-
111
- it('resolves AbstractPrismaRepository with TRANSACTION_MANAGER', async () => {
112
- const ClientClass = jest.fn(() => mockClient);
113
- const module = await Test.createTestingModule({
114
- imports: [
115
- PrismaModule.forRoot({ client: ClientClass as any, transaction: true }),
116
- AbstractRepoFeatureModule,
117
- ],
118
- }).compile();
119
-
120
- const repo = module.get(TestAbstractRepo);
121
- expect(repo).toBeDefined();
122
- expect(typeof repo.findById).toBe('function');
123
- expect(typeof repo.create).toBe('function');
124
- });
125
- });
126
-
127
- // ============================================================================
128
- // forRootAsync + default connection
129
- // ============================================================================
130
-
131
- describe('forRootAsync({ transaction: true }) — default connection', () => {
132
- it('useFactory: resolves TRANSACTION_MANAGER in a feature module', async () => {
133
- const ClientClass = jest.fn(() => mockClient);
134
- const module = await Test.createTestingModule({
135
- imports: [
136
- PrismaModule.forRootAsync({
137
- useFactory: () => ({ client: ClientClass as any, transaction: true }),
138
- }),
139
- DefaultFeatureModule,
140
- ],
141
- }).compile();
142
-
143
- const repo = module.get(DefaultTxRepository);
144
- expect(repo).toBeDefined();
145
-
146
- const result = await repo.create({ name: 'test' });
147
- expect(result).toEqual({ id: 2, name: 'created-in-tx' });
148
- });
149
-
150
- it('useClass: resolves TRANSACTION_MANAGER in a feature module', async () => {
151
- const ClientClass = jest.fn(() => mockClient);
152
-
153
- @Injectable()
154
- class PrismaConfigService implements PrismaOptionsFactory {
155
- createPrismaOptions(): PrismaModuleOptions {
156
- return { client: ClientClass as any, transaction: true };
157
- }
158
- }
159
-
160
- const module = await Test.createTestingModule({
161
- imports: [
162
- PrismaModule.forRootAsync({ useClass: PrismaConfigService }),
163
- DefaultFeatureModule,
164
- ],
165
- }).compile();
166
-
167
- const repo = module.get(DefaultTxRepository);
168
- expect(repo).toBeDefined();
169
- expect(await repo.create({ name: 'test' })).toEqual({ id: 2, name: 'created-in-tx' });
170
- });
171
-
172
- it('useExisting: resolves TRANSACTION_MANAGER in a feature module', async () => {
173
- const ClientClass = jest.fn(() => mockClient);
174
-
175
- @Injectable()
176
- class PrismaConfigService implements PrismaOptionsFactory {
177
- createPrismaOptions(): PrismaModuleOptions {
178
- return { client: ClientClass as any, transaction: true };
179
- }
180
- }
181
-
182
- const module = await Test.createTestingModule({
183
- imports: [
184
- PrismaModule.forRootAsync({
185
- imports: [
186
- {
187
- module: class ConfigModule {},
188
- providers: [PrismaConfigService],
189
- exports: [PrismaConfigService],
190
- global: true,
191
- },
192
- ],
193
- useExisting: PrismaConfigService,
194
- }),
195
- DefaultFeatureModule,
196
- ],
197
- }).compile();
198
-
199
- const repo = module.get(DefaultTxRepository);
200
- expect(repo).toBeDefined();
201
- expect(await repo.create({ name: 'test' })).toEqual({ id: 2, name: 'created-in-tx' });
202
- });
203
- });
204
-
205
- // ============================================================================
206
- // Named connection (single named, no default)
207
- // ============================================================================
208
-
209
- describe('forRoot({ name, transaction: true }) — named connection', () => {
210
- it('resolves named tx manager in a feature module', async () => {
211
- const ClientClass = jest.fn(() => mockClient);
212
- const module = await Test.createTestingModule({
213
- imports: [
214
- PrismaModule.forRoot({
215
- name: 'dbA',
216
- client: ClientClass as any,
217
- transaction: true,
218
- }),
219
- NamedFeatureModule,
220
- ],
221
- }).compile();
222
-
223
- const repo = module.get(NamedTxRepository);
224
- expect(repo).toBeDefined();
225
- expect(await repo.create({ name: 'test' })).toEqual({ id: 2, name: 'created-in-tx' });
226
- });
227
-
228
- it('does NOT resolve global TRANSACTION_MANAGER for named connection', async () => {
229
- const ClientClass = jest.fn(() => mockClient);
230
- const module = await Test.createTestingModule({
231
- imports: [
232
- PrismaModule.forRoot({
233
- name: 'dbA',
234
- client: ClientClass as any,
235
- transaction: true,
236
- }),
237
- ],
238
- }).compile();
239
-
240
- expect(() => module.get(TRANSACTION_MANAGER)).toThrow();
241
- });
242
- });
243
-
244
- describe('forRootAsync({ name, transaction: true }) — named connection', () => {
245
- it('resolves named tx manager in a feature module', async () => {
246
- const ClientClass = jest.fn(() => mockClient);
247
- const module = await Test.createTestingModule({
248
- imports: [
249
- PrismaModule.forRootAsync({
250
- name: 'dbA',
251
- useFactory: () => ({
252
- name: 'dbA',
253
- client: ClientClass as any,
254
- transaction: true,
255
- }),
256
- }),
257
- NamedFeatureModule,
258
- ],
259
- }).compile();
260
-
261
- const repo = module.get(NamedTxRepository);
262
- expect(repo).toBeDefined();
263
- expect(await repo.create({ name: 'test' })).toEqual({ id: 2, name: 'created-in-tx' });
264
- });
265
- });
266
-
267
- // ============================================================================
268
- // Multi-DB: default + named coexistence
269
- // ============================================================================
270
-
271
- describe('Multi-DB: default + named connections', () => {
272
- @Injectable()
273
- class AnalyticsRepository {
274
- constructor(
275
- @Inject(getPrismaTransactionManagerToken('analytics')) private readonly txManager: ITransactionManager,
276
- @Inject(getPrismaToken('analytics')) private readonly prisma: PrismaService,
277
- ) {}
278
-
279
- async create(data: any) {
280
- return this.txManager.run(async () => {
281
- return (this.prisma.client as any).user.create({ data });
282
- });
283
- }
284
- }
285
-
286
- @Module({ providers: [AnalyticsRepository], exports: [AnalyticsRepository] })
287
- class AnalyticsFeatureModule {}
288
-
289
- it('default feature module gets default tx, named feature module gets named tx', async () => {
290
- const DefaultClient = jest.fn(() => ({ ...mockClient, $$db: 'default' }));
291
- const AnalyticsClient = jest.fn(() => ({ ...mockClient, $$db: 'analytics' }));
292
-
293
- const module = await Test.createTestingModule({
294
- imports: [
295
- PrismaModule.forRoot({ client: DefaultClient as any, transaction: true }),
296
- PrismaModule.forRoot({
297
- name: 'analytics',
298
- client: AnalyticsClient as any,
299
- transaction: true,
300
- }),
301
- DefaultFeatureModule,
302
- AnalyticsFeatureModule,
303
- ],
304
- }).compile();
305
-
306
- const defaultRepo = module.get(DefaultTxRepository);
307
- const analyticsRepo = module.get(AnalyticsRepository);
308
-
309
- expect(defaultRepo).toBeDefined();
310
- expect(analyticsRepo).toBeDefined();
311
-
312
- // Both can run transactions independently
313
- const defaultResult = await defaultRepo.create({ name: 'default' });
314
- const analyticsResult = await analyticsRepo.create({ name: 'analytics' });
315
-
316
- expect(defaultResult).toEqual({ id: 2, name: 'created-in-tx' });
317
- expect(analyticsResult).toEqual({ id: 2, name: 'created-in-tx' });
318
- });
319
-
320
- it('default + named forRootAsync: each feature module gets correct tx manager', async () => {
321
- const DefaultClient = jest.fn(() => mockClient);
322
- const AnalyticsClient = jest.fn(() => mockClient);
323
-
324
- const module = await Test.createTestingModule({
325
- imports: [
326
- PrismaModule.forRootAsync({
327
- useFactory: () => ({ client: DefaultClient as any, transaction: true }),
328
- }),
329
- PrismaModule.forRootAsync({
330
- name: 'analytics',
331
- useFactory: () => ({
332
- name: 'analytics',
333
- client: AnalyticsClient as any,
334
- transaction: true,
335
- }),
336
- }),
337
- DefaultFeatureModule,
338
- AnalyticsFeatureModule,
339
- ],
340
- }).compile();
341
-
342
- const defaultRepo = module.get(DefaultTxRepository);
343
- const analyticsRepo = module.get(AnalyticsRepository);
344
-
345
- expect(defaultRepo).toBeDefined();
346
- expect(analyticsRepo).toBeDefined();
347
- });
348
-
349
- it('global TRANSACTION_MANAGER equals default, not named', async () => {
350
- const module = await Test.createTestingModule({
351
- imports: [
352
- PrismaModule.forRoot({ client: jest.fn(() => mockClient) as any, transaction: true }),
353
- PrismaModule.forRoot({
354
- name: 'analytics',
355
- client: jest.fn(() => mockClient) as any,
356
- transaction: true,
357
- }),
358
- ],
359
- }).compile();
360
-
361
- const globalTx = module.get(TRANSACTION_MANAGER);
362
- const defaultTx = module.get(getPrismaTransactionManagerToken());
363
- const analyticsTx = module.get(getPrismaTransactionManagerToken('analytics'));
364
-
365
- expect(globalTx).toBe(defaultTx);
366
- expect(globalTx).not.toBe(analyticsTx);
367
- });
368
- });
369
-
370
- // ============================================================================
371
- // Multi-DB: two named connections (no default)
372
- // ============================================================================
373
-
374
- describe('Multi-DB: two named connections (no default)', () => {
375
- @Injectable()
376
- class DbARepository {
377
- constructor(
378
- @Inject(getPrismaTransactionManagerToken('dbA')) private readonly txManager: ITransactionManager,
379
- @Inject(getPrismaToken('dbA')) private readonly prisma: PrismaService,
380
- ) {}
381
-
382
- async create(data: any) {
383
- return this.txManager.run(async () => {
384
- return (this.prisma.client as any).user.create({ data });
385
- });
386
- }
387
- }
388
-
389
- @Injectable()
390
- class DbBRepository {
391
- constructor(
392
- @Inject(getPrismaTransactionManagerToken('dbB')) private readonly txManager: ITransactionManager,
393
- @Inject(getPrismaToken('dbB')) private readonly prisma: PrismaService,
394
- ) {}
395
-
396
- async create(data: any) {
397
- return this.txManager.run(async () => {
398
- return (this.prisma.client as any).user.create({ data });
399
- });
400
- }
401
- }
402
-
403
- @Module({ providers: [DbARepository], exports: [DbARepository] })
404
- class DbAFeatureModule {}
405
-
406
- @Module({ providers: [DbBRepository], exports: [DbBRepository] })
407
- class DbBFeatureModule {}
408
-
409
- it('each named connection resolves its own tx manager in feature modules', async () => {
410
- const module = await Test.createTestingModule({
411
- imports: [
412
- PrismaModule.forRoot({ name: 'dbA', client: jest.fn(() => mockClient) as any, transaction: true }),
413
- PrismaModule.forRoot({ name: 'dbB', client: jest.fn(() => mockClient) as any, transaction: true }),
414
- DbAFeatureModule,
415
- DbBFeatureModule,
416
- ],
417
- }).compile();
418
-
419
- const repoA = module.get(DbARepository);
420
- const repoB = module.get(DbBRepository);
421
-
422
- expect(repoA).toBeDefined();
423
- expect(repoB).toBeDefined();
424
-
425
- const resultA = await repoA.create({ name: 'a' });
426
- const resultB = await repoB.create({ name: 'b' });
427
-
428
- expect(resultA).toEqual({ id: 2, name: 'created-in-tx' });
429
- expect(resultB).toEqual({ id: 2, name: 'created-in-tx' });
430
- });
431
-
432
- it('global TRANSACTION_MANAGER does not exist when no default connection', async () => {
433
- const module = await Test.createTestingModule({
434
- imports: [
435
- PrismaModule.forRoot({ name: 'dbA', client: jest.fn(() => mockClient) as any, transaction: true }),
436
- PrismaModule.forRoot({ name: 'dbB', client: jest.fn(() => mockClient) as any, transaction: true }),
437
- ],
438
- }).compile();
439
-
440
- expect(() => module.get(TRANSACTION_MANAGER)).toThrow();
441
- });
442
-
443
- it('tx state is isolated between named connections', async () => {
444
- const module = await Test.createTestingModule({
445
- imports: [
446
- PrismaModule.forRoot({ name: 'dbA', client: jest.fn(() => mockClient) as any, transaction: true }),
447
- PrismaModule.forRoot({ name: 'dbB', client: jest.fn(() => mockClient) as any, transaction: true }),
448
- ],
449
- }).compile();
450
-
451
- const txA = module.get<ITransactionManager>(getPrismaTransactionManagerToken('dbA'));
452
- const txB = module.get<ITransactionManager>(getPrismaTransactionManagerToken('dbB'));
453
-
454
- expect(txA.isInTransaction()).toBe(false);
455
- expect(txB.isInTransaction()).toBe(false);
456
-
457
- await txA.run(async () => {
458
- expect(txA.isInTransaction()).toBe(true);
459
- expect(txB.isInTransaction()).toBe(false);
460
- });
461
-
462
- expect(txA.isInTransaction()).toBe(false);
463
- expect(txB.isInTransaction()).toBe(false);
464
- });
465
- });
466
-
467
- // ============================================================================
468
- // transaction: false — no tx manager registered
469
- // ============================================================================
470
-
471
- describe('transaction: false — no tx manager', () => {
472
- it('forRoot without transaction does not provide TRANSACTION_MANAGER', async () => {
473
- const ClientClass = jest.fn(() => mockClient);
474
- const module = await Test.createTestingModule({
475
- imports: [PrismaModule.forRoot({ client: ClientClass as any })],
476
- }).compile();
477
-
478
- expect(() => module.get(TRANSACTION_MANAGER)).toThrow();
479
- });
480
-
481
- it('forRootAsync without transaction does not provide TRANSACTION_MANAGER', async () => {
482
- const ClientClass = jest.fn(() => mockClient);
483
- const module = await Test.createTestingModule({
484
- imports: [
485
- PrismaModule.forRootAsync({
486
- useFactory: () => ({ client: ClientClass as any }),
487
- }),
488
- ],
489
- }).compile();
490
-
491
- const txManager = module.get<ITransactionManager>(TRANSACTION_MANAGER);
492
- expect(txManager.isInTransaction()).toBe(false);
493
- await expect(txManager.run(async () => undefined)).rejects.toThrow('Prisma transaction support for connection "default" was not enabled');
494
- });
495
- });
496
- });