@nathapp/nestjs-prisma 3.0.1 → 3.1.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.
- package/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +28 -0
- package/CLAUDE.md +35 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/prisma.interfaces.d.ts +1 -0
- package/dist/prisma.module.js +60 -4
- package/dist/prisma.module.js.map +1 -1
- package/dist/repository/abstract-prisma.repository.d.ts +51 -0
- package/dist/repository/abstract-prisma.repository.js +69 -0
- package/dist/repository/abstract-prisma.repository.js.map +1 -0
- package/dist/repository/index.d.ts +1 -0
- package/dist/repository/index.js +18 -0
- package/dist/repository/index.js.map +1 -0
- package/dist/transaction/index.d.ts +1 -0
- package/dist/transaction/index.js +1 -0
- package/dist/transaction/index.js.map +1 -1
- package/dist/transaction/prisma-transaction-manager.d.ts +2 -1
- package/dist/transaction/prisma-transaction-manager.js +6 -3
- package/dist/transaction/prisma-transaction-manager.js.map +1 -1
- package/dist/transaction/prisma-transaction.constants.d.ts +2 -0
- package/dist/transaction/prisma-transaction.constants.js +9 -0
- package/dist/transaction/prisma-transaction.constants.js.map +1 -0
- package/lib/index.ts +1 -0
- package/lib/prisma.interfaces.ts +9 -0
- package/lib/prisma.module.ts +76 -6
- package/lib/repository/abstract-prisma.repository.ts +120 -0
- package/lib/repository/index.ts +1 -0
- package/lib/transaction/index.ts +1 -0
- package/lib/transaction/prisma-transaction-manager.ts +10 -3
- package/lib/transaction/prisma-transaction.constants.ts +13 -0
- package/package.json +2 -2
- package/test/jest-test.json +4 -0
- package/test/repository/abstract-prisma.repository.spec.ts +157 -0
- package/test/transparent-transaction.spec.ts +332 -0
- package/.turbo/turbo-test.log +0 -43
|
@@ -0,0 +1,332 @@
|
|
|
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 } 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>(TRANSACTION_MANAGER);
|
|
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
|
+
});
|
package/.turbo/turbo-test.log
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
> @nathapp/nestjs-prisma@3.0.0-rc.0 test
|
|
3
|
-
> jest --config ./test/jest-test.json
|
|
4
|
-
|
|
5
|
-
PASS test/prisma.health.spec.ts (10.382 s)
|
|
6
|
-
PASS test/prisma.service.spec.ts (19.029 s)
|
|
7
|
-
PASS test/lifecycle.spec.ts (19.171 s)
|
|
8
|
-
FAIL test/pagination/paginate.spec.ts (19.04 s)
|
|
9
|
-
● Paginate › concurrent execution › should call findMany and count in parallel
|
|
10
|
-
|
|
11
|
-
expect(received).toBeLessThan(expected)
|
|
12
|
-
|
|
13
|
-
Expected: < 100
|
|
14
|
-
Received: 114
|
|
15
|
-
|
|
16
|
-
334 | // In CI environments, timer precision may vary.
|
|
17
|
-
335 | // We just want to ensure it's reasonably fast and definitely parallel (not 200ms+ serial)
|
|
18
|
-
> 336 | expect(duration).toBeLessThan(100);
|
|
19
|
-
| ^
|
|
20
|
-
337 | expect(mockModel.findMany).toHaveBeenCalled();
|
|
21
|
-
338 | expect(mockModel.count).toHaveBeenCalled();
|
|
22
|
-
339 | });
|
|
23
|
-
|
|
24
|
-
at Object.<anonymous> (test/pagination/paginate.spec.ts:336:24)
|
|
25
|
-
|
|
26
|
-
PASS test/transaction.spec.ts (19.436 s)
|
|
27
|
-
PASS test/prisma.module.spec.ts (19.565 s)
|
|
28
|
-
PASS test/prisma.decorator.spec.ts (19.261 s)
|
|
29
|
-
PASS test/error-mapping.spec.ts (9.508 s)
|
|
30
|
-
PASS test/prisma-exception-filter.spec.ts
|
|
31
|
-
|
|
32
|
-
Test Suites: 1 failed, 8 passed, 9 total
|
|
33
|
-
Tests: 1 failed, 1 skipped, 134 passed, 136 total
|
|
34
|
-
Snapshots: 0 total
|
|
35
|
-
Time: 24.395 s
|
|
36
|
-
Ran all test suites.
|
|
37
|
-
npm error Lifecycle script `test` failed with error:
|
|
38
|
-
npm error code 1
|
|
39
|
-
npm error path /Users/subrinaai/Desktop/workspace/subrina-coder/projects/nathapp-nestjs-core/repos/nathapp-nestjs/packages/nestjs-prisma
|
|
40
|
-
npm error workspace @nathapp/nestjs-prisma@3.0.0-rc.0
|
|
41
|
-
npm error location /Users/subrinaai/Desktop/workspace/subrina-coder/projects/nathapp-nestjs-core/repos/nathapp-nestjs/packages/nestjs-prisma
|
|
42
|
-
npm error command failed
|
|
43
|
-
npm error command sh -c jest --config ./test/jest-test.json
|