@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,506 +0,0 @@
1
- import { Test } from '@nestjs/testing';
2
- import { Injectable } from '@nestjs/common';
3
- import { PrismaModule } from '../lib/prisma.module';
4
- import { PrismaService } from '../lib/prisma.service';
5
- import { getPrismaToken, getPrismaTransactionManagerToken } from '../lib/prisma.constants';
6
- import { getPrismaTransactionAlsToken } from '../lib/transaction/prisma-transaction.constants';
7
- import { PrismaTransactionManager } from '../lib/transaction/prisma-transaction-manager';
8
- import type { PrismaModuleOptions, PrismaOptionsFactory, PrismaClientLike } from '../lib/prisma.interfaces';
9
-
10
- describe('PrismaModule', () => {
11
- let mockClient: jest.Mocked<PrismaClientLike>;
12
-
13
- beforeEach(() => {
14
- mockClient = {
15
- $connect: jest.fn().mockResolvedValue(undefined),
16
- $disconnect: jest.fn().mockResolvedValue(undefined),
17
- $on: jest.fn(),
18
- };
19
- });
20
-
21
- describe('forRoot()', () => {
22
- it('should create a global module by default', async () => {
23
- const ClientClass = jest.fn(() => mockClient);
24
- const module = PrismaModule.forRoot({
25
- client: ClientClass as any,
26
- });
27
-
28
- expect(module.global).toBe(true);
29
- });
30
-
31
- it('should respect isGlobal=false', async () => {
32
- const ClientClass = jest.fn(() => mockClient);
33
- const module = PrismaModule.forRoot({
34
- client: ClientClass as any,
35
- isGlobal: false,
36
- });
37
-
38
- expect(module.global).toBe(false);
39
- });
40
-
41
- it('should provide PrismaService under default token', async () => {
42
- const ClientClass = jest.fn(() => mockClient);
43
- const module = await Test.createTestingModule({
44
- imports: [
45
- PrismaModule.forRoot({
46
- client: ClientClass as any,
47
- }),
48
- ],
49
- }).compile();
50
-
51
- const defaultService = module.get(getPrismaToken());
52
- expect(defaultService).toBeInstanceOf(PrismaService);
53
- });
54
-
55
- it('should provide PrismaService as class token when name is default', async () => {
56
- const ClientClass = jest.fn(() => mockClient);
57
- const module = await Test.createTestingModule({
58
- imports: [
59
- PrismaModule.forRoot({
60
- client: ClientClass as any,
61
- name: 'default',
62
- }),
63
- ],
64
- }).compile();
65
-
66
- const service = module.get(PrismaService);
67
- expect(service).toBeInstanceOf(PrismaService);
68
- });
69
-
70
- it('should provide PrismaService under named token when name is provided', async () => {
71
- const ClientClass = jest.fn(() => mockClient);
72
- const module = await Test.createTestingModule({
73
- imports: [
74
- PrismaModule.forRoot({
75
- client: ClientClass as any,
76
- name: 'analytics',
77
- }),
78
- ],
79
- }).compile();
80
-
81
- const namedService = module.get(getPrismaToken('analytics'));
82
- expect(namedService).toBeInstanceOf(PrismaService);
83
- });
84
-
85
- it('should NOT provide PrismaService class token when name is not default', async () => {
86
- const ClientClass = jest.fn(() => mockClient);
87
- const module = await Test.createTestingModule({
88
- imports: [
89
- PrismaModule.forRoot({
90
- client: ClientClass as any,
91
- name: 'analytics',
92
- }),
93
- ],
94
- }).compile();
95
-
96
- // Should throw because PrismaService class token is only provided for default connection
97
- expect(() => module.get(PrismaService, { strict: false })).toThrow();
98
- });
99
-
100
- it('should support multiple named connections (multi-db)', async () => {
101
- const ClientClass1 = jest.fn(() => mockClient);
102
- const ClientClass2 = jest.fn(() => mockClient);
103
-
104
- const module = await Test.createTestingModule({
105
- imports: [
106
- PrismaModule.forRoot({
107
- client: ClientClass1 as any,
108
- name: 'default',
109
- }),
110
- PrismaModule.forRoot({
111
- client: ClientClass2 as any,
112
- name: 'analytics',
113
- }),
114
- ],
115
- }).compile();
116
-
117
- const defaultService = module.get(getPrismaToken('default'));
118
- const analyticsService = module.get(getPrismaToken('analytics'));
119
-
120
- expect(defaultService).toBeInstanceOf(PrismaService);
121
- expect(analyticsService).toBeInstanceOf(PrismaService);
122
- expect(defaultService).not.toBe(analyticsService);
123
- });
124
-
125
- it('should pass options to PrismaService', async () => {
126
- const ClientClass = jest.fn(() => mockClient);
127
- const options: PrismaModuleOptions = {
128
- client: ClientClass as any,
129
- clientOptions: { log: ['query'] },
130
- logging: true,
131
- name: 'test',
132
- };
133
-
134
- const module = await Test.createTestingModule({
135
- imports: [PrismaModule.forRoot(options)],
136
- }).compile();
137
-
138
- const service = module.get<PrismaService>(getPrismaToken('test'));
139
-
140
- // Verify the client was created with correct options
141
- expect(ClientClass).toHaveBeenCalledWith({ log: ['query'] });
142
- expect(service).toBeInstanceOf(PrismaService);
143
- });
144
- });
145
-
146
- describe('forRootAsync()', () => {
147
- it('should provide a disabled transaction manager when transaction is not enabled', async () => {
148
- const ClientClass = jest.fn(() => mockClient);
149
-
150
- const module = await Test.createTestingModule({
151
- imports: [
152
- PrismaModule.forRootAsync({
153
- useFactory: () => ({
154
- client: ClientClass as any,
155
- transaction: false,
156
- }),
157
- }),
158
- ],
159
- }).compile();
160
-
161
- const tx = module.get<any>(getPrismaTransactionManagerToken());
162
- expect(module.get(PrismaTransactionManager)).toBeInstanceOf(PrismaTransactionManager);
163
- expect(tx.isInTransaction()).toBe(false);
164
- await expect(tx.run(async () => undefined)).rejects.toThrow('Prisma transaction support for connection "default" was not enabled');
165
- });
166
-
167
- it('should work with useFactory', async () => {
168
- const ClientClass = jest.fn(() => mockClient);
169
-
170
- const module = await Test.createTestingModule({
171
- imports: [
172
- PrismaModule.forRootAsync({
173
- useFactory: () => ({
174
- client: ClientClass as any,
175
- }),
176
- }),
177
- ],
178
- }).compile();
179
-
180
- const service = module.get(getPrismaToken());
181
- expect(service).toBeInstanceOf(PrismaService);
182
- });
183
-
184
- it('should work with useFactory returning Promise', async () => {
185
- const ClientClass = jest.fn(() => mockClient);
186
-
187
- const module = await Test.createTestingModule({
188
- imports: [
189
- PrismaModule.forRootAsync({
190
- useFactory: async () => ({
191
- client: ClientClass as any,
192
- }),
193
- }),
194
- ],
195
- }).compile();
196
-
197
- const service = module.get(getPrismaToken());
198
- expect(service).toBeInstanceOf(PrismaService);
199
- });
200
-
201
- it('should inject dependencies into useFactory', async () => {
202
- const ClientClass = jest.fn(() => mockClient);
203
-
204
- @Injectable()
205
- class ConfigService {
206
- getDatabaseUrl() {
207
- return 'postgresql://localhost:5432/test';
208
- }
209
- }
210
-
211
- const module = await Test.createTestingModule({
212
- imports: [
213
- PrismaModule.forRootAsync({
214
- imports: [
215
- {
216
- module: class ConfigModule {},
217
- providers: [ConfigService],
218
- exports: [ConfigService],
219
- global: true,
220
- },
221
- ],
222
- inject: [ConfigService],
223
- useFactory: (config: ConfigService) => ({
224
- client: ClientClass as any,
225
- clientOptions: { datasources: { db: { url: config.getDatabaseUrl() } } },
226
- }),
227
- }),
228
- ],
229
- }).compile();
230
-
231
- const service = module.get(getPrismaToken());
232
- expect(service).toBeInstanceOf(PrismaService);
233
- expect(ClientClass).toHaveBeenCalledWith({
234
- datasources: { db: { url: 'postgresql://localhost:5432/test' } },
235
- });
236
- });
237
-
238
- it('should work with useClass', async () => {
239
- const ClientClass = jest.fn(() => mockClient);
240
-
241
- @Injectable()
242
- class PrismaConfigService implements PrismaOptionsFactory {
243
- createPrismaOptions(): PrismaModuleOptions {
244
- return {
245
- client: ClientClass as any,
246
- };
247
- }
248
- }
249
-
250
- const module = await Test.createTestingModule({
251
- imports: [
252
- PrismaModule.forRootAsync({
253
- useClass: PrismaConfigService,
254
- }),
255
- ],
256
- }).compile();
257
-
258
- const service = module.get(getPrismaToken());
259
- expect(service).toBeInstanceOf(PrismaService);
260
- });
261
-
262
- it('should work with useExisting', async () => {
263
- const ClientClass = jest.fn(() => mockClient);
264
-
265
- @Injectable()
266
- class PrismaConfigService implements PrismaOptionsFactory {
267
- createPrismaOptions(): PrismaModuleOptions {
268
- return {
269
- client: ClientClass as any,
270
- };
271
- }
272
- }
273
-
274
- const module = await Test.createTestingModule({
275
- imports: [
276
- PrismaModule.forRootAsync({
277
- imports: [
278
- {
279
- module: class ConfigModule {},
280
- providers: [PrismaConfigService],
281
- exports: [PrismaConfigService],
282
- global: true,
283
- },
284
- ],
285
- useExisting: PrismaConfigService,
286
- }),
287
- ],
288
- }).compile();
289
-
290
- const service = module.get(getPrismaToken());
291
- expect(service).toBeInstanceOf(PrismaService);
292
- });
293
-
294
- it('should work with useClass + transaction:true', async () => {
295
- const ClientClass = jest.fn(() => mockClient);
296
-
297
- @Injectable()
298
- class PrismaConfigService implements PrismaOptionsFactory {
299
- createPrismaOptions(): PrismaModuleOptions {
300
- return {
301
- client: ClientClass as any,
302
- transaction: true,
303
- };
304
- }
305
- }
306
-
307
- const module = await Test.createTestingModule({
308
- imports: [
309
- PrismaModule.forRootAsync({
310
- useClass: PrismaConfigService,
311
- }),
312
- ],
313
- }).compile();
314
-
315
- const service = module.get(getPrismaToken());
316
- expect(service).toBeInstanceOf(PrismaService);
317
-
318
- const tx = module.get(getPrismaTransactionManagerToken());
319
- expect(tx).toBeDefined();
320
- expect(typeof (tx as any).run).toBe('function');
321
- });
322
-
323
- it('should work with useExisting + transaction:true', async () => {
324
- const ClientClass = jest.fn(() => mockClient);
325
-
326
- @Injectable()
327
- class PrismaConfigService implements PrismaOptionsFactory {
328
- createPrismaOptions(): PrismaModuleOptions {
329
- return {
330
- client: ClientClass as any,
331
- transaction: true,
332
- };
333
- }
334
- }
335
-
336
- const module = await Test.createTestingModule({
337
- imports: [
338
- PrismaModule.forRootAsync({
339
- imports: [
340
- {
341
- module: class ConfigModule {},
342
- providers: [PrismaConfigService],
343
- exports: [PrismaConfigService],
344
- global: true,
345
- },
346
- ],
347
- useExisting: PrismaConfigService,
348
- }),
349
- ],
350
- }).compile();
351
-
352
- const service = module.get(getPrismaToken());
353
- expect(service).toBeInstanceOf(PrismaService);
354
-
355
- const tx = module.get(getPrismaTransactionManagerToken());
356
- expect(tx).toBeDefined();
357
- expect(typeof (tx as any).run).toBe('function');
358
- });
359
-
360
- it('ALS token resolves to undefined when transaction support is disabled', async () => {
361
- const ClientClass = jest.fn(() => mockClient);
362
-
363
- const moduleRef = await Test.createTestingModule({
364
- imports: [
365
- PrismaModule.forRootAsync({
366
- useFactory: () => ({
367
- client: ClientClass as any,
368
- transaction: false,
369
- }),
370
- }),
371
- ],
372
- }).compile();
373
-
374
- // The ALS provider is always registered (transaction flag is only known at
375
- // factory-resolution time with async options), so the token is resolvable but
376
- // returns undefined when transactions are disabled. This test pins that behavior.
377
- const als = moduleRef.get(getPrismaTransactionAlsToken(), { strict: false });
378
- expect(als).toBeUndefined();
379
- });
380
-
381
- it('should respect isGlobal option', async () => {
382
- const ClientClass = jest.fn(() => mockClient);
383
-
384
- const module = PrismaModule.forRootAsync({
385
- isGlobal: false,
386
- useFactory: () => ({
387
- client: ClientClass as any,
388
- }),
389
- });
390
-
391
- expect(module.global).toBe(false);
392
- });
393
-
394
- it('should default to global module', async () => {
395
- const ClientClass = jest.fn(() => mockClient);
396
-
397
- const module = PrismaModule.forRootAsync({
398
- useFactory: () => ({
399
- client: ClientClass as any,
400
- }),
401
- });
402
-
403
- expect(module.global).toBe(true);
404
- });
405
-
406
- it('should support named connections with useFactory', async () => {
407
- const ClientClass = jest.fn(() => mockClient);
408
-
409
- const module = await Test.createTestingModule({
410
- imports: [
411
- PrismaModule.forRootAsync({
412
- name: 'analytics',
413
- useFactory: () => ({
414
- client: ClientClass as any,
415
- name: 'analytics',
416
- }),
417
- }),
418
- ],
419
- }).compile();
420
-
421
- const service = module.get(getPrismaToken('analytics'));
422
- expect(service).toBeInstanceOf(PrismaService);
423
- });
424
-
425
- it('should support multiple async connections', async () => {
426
- const ClientClass1 = jest.fn(() => mockClient);
427
- const ClientClass2 = jest.fn(() => mockClient);
428
-
429
- const module = await Test.createTestingModule({
430
- imports: [
431
- PrismaModule.forRootAsync({
432
- name: 'default',
433
- useFactory: () => ({
434
- client: ClientClass1 as any,
435
- }),
436
- }),
437
- PrismaModule.forRootAsync({
438
- name: 'analytics',
439
- useFactory: () => ({
440
- client: ClientClass2 as any,
441
- name: 'analytics',
442
- }),
443
- }),
444
- ],
445
- }).compile();
446
-
447
- const defaultService = module.get(getPrismaToken('default'));
448
- const analyticsService = module.get(getPrismaToken('analytics'));
449
-
450
- expect(defaultService).toBeInstanceOf(PrismaService);
451
- expect(analyticsService).toBeInstanceOf(PrismaService);
452
- expect(defaultService).not.toBe(analyticsService);
453
- });
454
- });
455
-
456
- describe('module metadata', () => {
457
- it('should export PrismaService provider', async () => {
458
- const ClientClass = jest.fn(() => mockClient);
459
- const module = PrismaModule.forRoot({
460
- client: ClientClass as any,
461
- });
462
-
463
- expect(module.exports).toBeDefined();
464
- expect(Array.isArray(module.exports)).toBe(true);
465
- });
466
-
467
- it('should have module definition', () => {
468
- const ClientClass = jest.fn(() => mockClient);
469
- const module = PrismaModule.forRoot({
470
- client: ClientClass as any,
471
- });
472
-
473
- expect(module.module).toBe(PrismaModule);
474
- });
475
- });
476
-
477
- describe('error cases', () => {
478
- it('should throw error when no client provided in forRoot', async () => {
479
- await expect(
480
- Test.createTestingModule({
481
- imports: [
482
- PrismaModule.forRoot({
483
- name: 'test',
484
- // client is missing
485
- }),
486
- ],
487
- }).compile(),
488
- ).rejects.toThrow(/No PrismaClient class provided/);
489
- });
490
-
491
- it('should throw error when no client provided in forRootAsync', async () => {
492
- await expect(
493
- Test.createTestingModule({
494
- imports: [
495
- PrismaModule.forRootAsync({
496
- useFactory: () => ({
497
- name: 'test',
498
- // client is missing
499
- }),
500
- }),
501
- ],
502
- }).compile(),
503
- ).rejects.toThrow(/No PrismaClient class provided/);
504
- });
505
- });
506
- });