@nathapp/nestjs-prisma 3.1.1 → 3.1.3
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/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +16 -0
- package/CLAUDE.md +79 -6
- package/dist/prisma.constants.d.ts +1 -0
- package/dist/prisma.constants.js +5 -1
- package/dist/prisma.constants.js.map +1 -1
- package/dist/prisma.module.d.ts +1 -0
- package/dist/prisma.module.js +84 -11
- package/dist/prisma.module.js.map +1 -1
- package/dist/repository/abstract-prisma.repository.js.map +1 -1
- package/dist/transaction/prisma-transaction.constants.js +3 -2
- package/dist/transaction/prisma-transaction.constants.js.map +1 -1
- package/lib/prisma.constants.ts +31 -1
- package/lib/prisma.module.ts +111 -14
- package/lib/repository/abstract-prisma.repository.ts +21 -3
- package/lib/transaction/prisma-transaction.constants.ts +20 -4
- package/package.json +2 -2
- package/test/multi-db-als.spec.ts +142 -0
- package/test/multi-db-transaction.spec.ts +75 -0
- package/test/prisma.module.spec.ts +88 -1
- package/test/transaction-manager-e2e.spec.ts +496 -0
- package/test/transparent-transaction.spec.ts +170 -2
|
@@ -0,0 +1,496 @@
|
|
|
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
|
+
});
|
|
@@ -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>(
|
|
322
|
+
const txManager = module.get<ITransactionManager>(getPrismaTransactionManagerToken('analytics'));
|
|
323
323
|
|
|
324
324
|
let resultInTx: any;
|
|
325
325
|
await txManager.run(async () => {
|
|
@@ -330,3 +330,171 @@ 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 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
|
+
});
|