@nathapp/nestjs-prisma 3.1.0 → 3.1.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.
@@ -4,7 +4,7 @@ import { TRANSACTION_MANAGER, ITransactionManager } from '@nathapp/nestjs-data';
4
4
  import { PrismaModule } from '../lib/prisma.module';
5
5
  import { PrismaService } from '../lib/prisma.service';
6
6
  import { PrismaTransactionManager } from '../lib/transaction/prisma-transaction-manager';
7
- import { getPrismaToken } from '../lib/prisma.constants';
7
+ import { getPrismaToken, getPrismaTransactionManagerToken } from '../lib/prisma.constants';
8
8
  import type { PrismaClientLike } from '../lib/prisma.interfaces';
9
9
 
10
10
  describe('Transparent Transaction Support', () => {
@@ -319,7 +319,7 @@ describe('Transparent Transaction Support', () => {
319
319
  }).compile();
320
320
 
321
321
  const prismaService = module.get<PrismaService>(getPrismaToken('analytics'));
322
- const txManager = module.get<ITransactionManager>(TRANSACTION_MANAGER);
322
+ const txManager = module.get<ITransactionManager>(getPrismaTransactionManagerToken('analytics'));
323
323
 
324
324
  let resultInTx: any;
325
325
  await txManager.run(async () => {
@@ -330,3 +330,170 @@ describe('Transparent Transaction Support', () => {
330
330
  });
331
331
  });
332
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 resolves to null', 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
+ expect(module.get(TRANSACTION_MANAGER)).toBeNull();
497
+ expect(module.get(getPrismaTransactionManagerToken())).toBeNull();
498
+ });
499
+ });
@@ -1,201 +0,0 @@
1
-
2
- > @nathapp/nestjs-prisma@3.0.1 test
3
- > jest --config ./test/jest-test.json
4
-
5
- FAIL test/transaction.spec.ts
6
- ● Test suite failed to run
7
-
8
- Cannot find module './dist' from '../nestjs-common/index.js'
9
-
10
- Require stack:
11
- /home/ubuntu/subrina-coder/projects/nathapp-nestjs-core/repos/nathapp-nestjs/packages/nestjs-common/index.js
12
- lib/errors/prisma-error.mapper.ts
13
- lib/prisma.service.ts
14
- test/transaction.spec.ts
15
-
16
- 15 | };
17
- 16 | Object.defineProperty(exports, "__esModule", { value: true });
18
- > 17 | __exportStar(require("./dist"), exports);
19
- | ^
20
- 18 |
21
-
22
- at Resolver._throwModNotFoundError (../../node_modules/jest-resolve/build/resolver.js:427:11)
23
- at Object.<anonymous> (../nestjs-common/index.js:17:14)
24
- at Object.<anonymous> (lib/errors/prisma-error.mapper.ts:1:1)
25
- at Object.<anonymous> (lib/prisma.service.ts:3:1)
26
- at Object.<anonymous> (test/transaction.spec.ts:2:1)
27
-
28
- FAIL test/prisma.module.spec.ts
29
- ● Test suite failed to run
30
-
31
- Cannot find module './dist' from '../nestjs-common/index.js'
32
-
33
- Require stack:
34
- /home/ubuntu/subrina-coder/projects/nathapp-nestjs-core/repos/nathapp-nestjs/packages/nestjs-common/index.js
35
- lib/errors/prisma-error.mapper.ts
36
- lib/prisma.service.ts
37
- lib/prisma.module.ts
38
- test/prisma.module.spec.ts
39
-
40
- 15 | };
41
- 16 | Object.defineProperty(exports, "__esModule", { value: true });
42
- > 17 | __exportStar(require("./dist"), exports);
43
- | ^
44
- 18 |
45
-
46
- at Resolver._throwModNotFoundError (../../node_modules/jest-resolve/build/resolver.js:427:11)
47
- at Object.<anonymous> (../nestjs-common/index.js:17:14)
48
- at Object.<anonymous> (lib/errors/prisma-error.mapper.ts:1:1)
49
- at Object.<anonymous> (lib/prisma.service.ts:3:1)
50
- at Object.<anonymous> (lib/prisma.module.ts:3:1)
51
- at Object.<anonymous> (test/prisma.module.spec.ts:3:1)
52
-
53
- FAIL test/prisma.service.spec.ts
54
- ● Test suite failed to run
55
-
56
- Cannot find module './dist' from '../nestjs-common/index.js'
57
-
58
- Require stack:
59
- /home/ubuntu/subrina-coder/projects/nathapp-nestjs-core/repos/nathapp-nestjs/packages/nestjs-common/index.js
60
- lib/errors/prisma-error.mapper.ts
61
- lib/prisma.service.ts
62
- test/prisma.service.spec.ts
63
-
64
- 15 | };
65
- 16 | Object.defineProperty(exports, "__esModule", { value: true });
66
- > 17 | __exportStar(require("./dist"), exports);
67
- | ^
68
- 18 |
69
-
70
- at Resolver._throwModNotFoundError (../../node_modules/jest-resolve/build/resolver.js:427:11)
71
- at Object.<anonymous> (../nestjs-common/index.js:17:14)
72
- at Object.<anonymous> (lib/errors/prisma-error.mapper.ts:1:1)
73
- at Object.<anonymous> (lib/prisma.service.ts:3:1)
74
- at Object.<anonymous> (test/prisma.service.spec.ts:2:1)
75
-
76
- FAIL test/lifecycle.spec.ts
77
- ● Test suite failed to run
78
-
79
- Cannot find module './dist' from '../nestjs-common/index.js'
80
-
81
- Require stack:
82
- /home/ubuntu/subrina-coder/projects/nathapp-nestjs-core/repos/nathapp-nestjs/packages/nestjs-common/index.js
83
- lib/errors/prisma-error.mapper.ts
84
- lib/prisma.service.ts
85
- test/lifecycle.spec.ts
86
-
87
- 15 | };
88
- 16 | Object.defineProperty(exports, "__esModule", { value: true });
89
- > 17 | __exportStar(require("./dist"), exports);
90
- | ^
91
- 18 |
92
-
93
- at Resolver._throwModNotFoundError (../../node_modules/jest-resolve/build/resolver.js:427:11)
94
- at Object.<anonymous> (../nestjs-common/index.js:17:14)
95
- at Object.<anonymous> (lib/errors/prisma-error.mapper.ts:1:1)
96
- at Object.<anonymous> (lib/prisma.service.ts:3:1)
97
- at Object.<anonymous> (test/lifecycle.spec.ts:2:1)
98
-
99
- FAIL test/pagination/paginate.spec.ts
100
- ● Test suite failed to run
101
-
102
- Cannot find module './dist' from '../nestjs-common/index.js'
103
-
104
- Require stack:
105
- /home/ubuntu/subrina-coder/projects/nathapp-nestjs-core/repos/nathapp-nestjs/packages/nestjs-common/index.js
106
- lib/pagination/paginate.ts
107
- test/pagination/paginate.spec.ts
108
-
109
- 15 | };
110
- 16 | Object.defineProperty(exports, "__esModule", { value: true });
111
- > 17 | __exportStar(require("./dist"), exports);
112
- | ^
113
- 18 |
114
-
115
- at Resolver._throwModNotFoundError (../../node_modules/jest-resolve/build/resolver.js:427:11)
116
- at Object.<anonymous> (../nestjs-common/index.js:17:14)
117
- at Object.<anonymous> (lib/pagination/paginate.ts:1:1)
118
- at Object.<anonymous> (test/pagination/paginate.spec.ts:1:1)
119
-
120
- FAIL test/prisma.decorator.spec.ts
121
- ● Test suite failed to run
122
-
123
- Cannot find module './dist' from '../nestjs-common/index.js'
124
-
125
- Require stack:
126
- /home/ubuntu/subrina-coder/projects/nathapp-nestjs-core/repos/nathapp-nestjs/packages/nestjs-common/index.js
127
- lib/errors/prisma-error.mapper.ts
128
- lib/prisma.service.ts
129
- lib/prisma.module.ts
130
- test/prisma.decorator.spec.ts
131
-
132
- 15 | };
133
- 16 | Object.defineProperty(exports, "__esModule", { value: true });
134
- > 17 | __exportStar(require("./dist"), exports);
135
- | ^
136
- 18 |
137
-
138
- at Resolver._throwModNotFoundError (../../node_modules/jest-resolve/build/resolver.js:427:11)
139
- at Object.<anonymous> (../nestjs-common/index.js:17:14)
140
- at Object.<anonymous> (lib/errors/prisma-error.mapper.ts:1:1)
141
- at Object.<anonymous> (lib/prisma.service.ts:3:1)
142
- at Object.<anonymous> (lib/prisma.module.ts:3:1)
143
- at Object.<anonymous> (test/prisma.decorator.spec.ts:4:1)
144
-
145
- FAIL test/prisma-exception-filter.spec.ts
146
- ● Test suite failed to run
147
-
148
- Cannot find module './dist' from '../nestjs-common/index.js'
149
-
150
- Require stack:
151
- /home/ubuntu/subrina-coder/projects/nathapp-nestjs-core/repos/nathapp-nestjs/packages/nestjs-common/index.js
152
- lib/errors/prisma-error.mapper.ts
153
- lib/filter/prisma-exception.filter.ts
154
- test/prisma-exception-filter.spec.ts
155
-
156
- 15 | };
157
- 16 | Object.defineProperty(exports, "__esModule", { value: true });
158
- > 17 | __exportStar(require("./dist"), exports);
159
- | ^
160
- 18 |
161
-
162
- at Resolver._throwModNotFoundError (../../node_modules/jest-resolve/build/resolver.js:427:11)
163
- at Object.<anonymous> (../nestjs-common/index.js:17:14)
164
- at Object.<anonymous> (lib/errors/prisma-error.mapper.ts:1:1)
165
- at Object.<anonymous> (lib/filter/prisma-exception.filter.ts:6:1)
166
- at Object.<anonymous> (test/prisma-exception-filter.spec.ts:6:1)
167
-
168
- FAIL test/error-mapping.spec.ts
169
- ● Test suite failed to run
170
-
171
- Cannot find module './dist' from '../nestjs-common/index.js'
172
-
173
- Require stack:
174
- /home/ubuntu/subrina-coder/projects/nathapp-nestjs-core/repos/nathapp-nestjs/packages/nestjs-common/index.js
175
- test/error-mapping.spec.ts
176
-
177
- 15 | };
178
- 16 | Object.defineProperty(exports, "__esModule", { value: true });
179
- > 17 | __exportStar(require("./dist"), exports);
180
- | ^
181
- 18 |
182
-
183
- at Resolver._throwModNotFoundError (../../node_modules/jest-resolve/build/resolver.js:427:11)
184
- at Object.<anonymous> (../nestjs-common/index.js:17:14)
185
- at Object.<anonymous> (test/error-mapping.spec.ts:1:1)
186
-
187
- PASS test/prisma-transaction-manager.spec.ts
188
- PASS test/prisma.health.spec.ts
189
-
190
- Test Suites: 8 failed, 2 passed, 10 total
191
- Tests: 37 passed, 37 total
192
- Snapshots: 0 total
193
- Time: 5.12 s
194
- Ran all test suites.
195
- npm error Lifecycle script `test` failed with error:
196
- npm error code 1
197
- npm error path /home/ubuntu/subrina-coder/projects/nathapp-nestjs-core/repos/nathapp-nestjs/packages/nestjs-prisma
198
- npm error workspace @nathapp/nestjs-prisma@3.0.1
199
- npm error location /home/ubuntu/subrina-coder/projects/nathapp-nestjs-core/repos/nathapp-nestjs/packages/nestjs-prisma
200
- npm error command failed
201
- npm error command sh -c jest --config ./test/jest-test.json