@nathapp/nestjs-prisma 3.0.0-rc.4
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/.eslintrc.js +8 -0
- package/.turbo/turbo-build.log +4 -0
- package/.turbo/turbo-test.log +43 -0
- package/CHANGELOG.md +38 -0
- package/CLAUDE.md +17 -0
- package/dist/errors/prisma-error.mapper.d.ts +20 -0
- package/dist/errors/prisma-error.mapper.js +53 -0
- package/dist/errors/prisma-error.mapper.js.map +1 -0
- package/dist/filter/prisma-exception.filter.d.ts +5 -0
- package/dist/filter/prisma-exception.filter.js +43 -0
- package/dist/filter/prisma-exception.filter.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -0
- package/dist/pagination/index.d.ts +1 -0
- package/dist/pagination/index.js +18 -0
- package/dist/pagination/index.js.map +1 -0
- package/dist/pagination/paginate.d.ts +11 -0
- package/dist/pagination/paginate.js +27 -0
- package/dist/pagination/paginate.js.map +1 -0
- package/dist/prisma.constants.d.ts +3 -0
- package/dist/prisma.constants.js +10 -0
- package/dist/prisma.constants.js.map +1 -0
- package/dist/prisma.decorator.d.ts +1 -0
- package/dist/prisma.decorator.js +8 -0
- package/dist/prisma.decorator.js.map +1 -0
- package/dist/prisma.health.d.ts +10 -0
- package/dist/prisma.health.js +46 -0
- package/dist/prisma.health.js.map +1 -0
- package/dist/prisma.interfaces.d.ts +24 -0
- package/dist/prisma.interfaces.js +3 -0
- package/dist/prisma.interfaces.js.map +1 -0
- package/dist/prisma.module.d.ts +7 -0
- package/dist/prisma.module.js +109 -0
- package/dist/prisma.module.js.map +1 -0
- package/dist/prisma.service.d.ts +17 -0
- package/dist/prisma.service.js +122 -0
- package/dist/prisma.service.js.map +1 -0
- package/dist/testing/index.d.ts +1 -0
- package/dist/testing/index.js +18 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/prisma-testing.utils.d.ts +8 -0
- package/dist/testing/prisma-testing.utils.js +24 -0
- package/dist/testing/prisma-testing.utils.js.map +1 -0
- package/docs/20260215-fix-plan-prisma.md +37 -0
- package/docs/20260215-review-prisma.md +598 -0
- package/docs/20260216-plan-prisma-a-minus.md +70 -0
- package/lib/errors/prisma-error.mapper.ts +140 -0
- package/lib/filter/prisma-exception.filter.ts +62 -0
- package/lib/index.ts +10 -0
- package/lib/pagination/index.ts +1 -0
- package/lib/pagination/paginate.ts +44 -0
- package/lib/prisma.constants.ts +10 -0
- package/lib/prisma.decorator.ts +8 -0
- package/lib/prisma.health.ts +40 -0
- package/lib/prisma.interfaces.ts +41 -0
- package/lib/prisma.module.ts +115 -0
- package/lib/prisma.service.ts +180 -0
- package/lib/testing/index.ts +1 -0
- package/lib/testing/prisma-testing.utils.ts +29 -0
- package/package.json +31 -0
- package/test/error-mapping.spec.ts +192 -0
- package/test/jest-test.json +24 -0
- package/test/lifecycle.spec.ts +241 -0
- package/test/pagination/paginate.spec.ts +359 -0
- package/test/prisma-exception-filter.spec.ts +213 -0
- package/test/prisma.decorator.spec.ts +257 -0
- package/test/prisma.health.spec.ts +226 -0
- package/test/prisma.module.spec.ts +397 -0
- package/test/prisma.service.spec.ts +359 -0
- package/test/transaction.spec.ts +371 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
import { Logger } from '@nestjs/common';
|
|
2
|
+
import { PrismaService } from '../lib/prisma.service';
|
|
3
|
+
import type { PrismaModuleOptions, PrismaClientLike } from '../lib/prisma.interfaces';
|
|
4
|
+
|
|
5
|
+
// Transaction manager type that doesn't exist yet
|
|
6
|
+
interface TransactionClient {
|
|
7
|
+
$executeRaw: (query: TemplateStringsArray | string, ...values: any[]) => Promise<number>;
|
|
8
|
+
$queryRaw: (query: TemplateStringsArray | string, ...values: any[]) => Promise<any[]>;
|
|
9
|
+
[key: string]: any;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
describe('PrismaService - Transaction Support', () => {
|
|
13
|
+
let mockClient: jest.Mocked<PrismaClientLike>;
|
|
14
|
+
let mockTransactionClient: jest.Mocked<TransactionClient>;
|
|
15
|
+
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
mockTransactionClient = {
|
|
18
|
+
$executeRaw: jest.fn().mockResolvedValue(1),
|
|
19
|
+
$queryRaw: jest.fn().mockResolvedValue([]),
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
mockClient = {
|
|
23
|
+
$connect: jest.fn().mockResolvedValue(undefined),
|
|
24
|
+
$disconnect: jest.fn().mockResolvedValue(undefined),
|
|
25
|
+
$on: jest.fn(),
|
|
26
|
+
$transaction: jest.fn().mockImplementation(async (callback) => {
|
|
27
|
+
return callback(mockTransactionClient);
|
|
28
|
+
}),
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
jest.spyOn(Logger.prototype, 'log').mockImplementation();
|
|
32
|
+
jest.spyOn(Logger.prototype, 'error').mockImplementation();
|
|
33
|
+
jest.spyOn(Logger.prototype, 'debug').mockImplementation();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
afterEach(() => {
|
|
37
|
+
jest.restoreAllMocks();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
describe('withTransaction()', () => {
|
|
41
|
+
it('should provide a transaction helper method', async () => {
|
|
42
|
+
const ClientClass = jest.fn(() => mockClient);
|
|
43
|
+
const options: PrismaModuleOptions = {
|
|
44
|
+
client: ClientClass as any,
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const service = new PrismaService(options);
|
|
48
|
+
await service.onModuleInit();
|
|
49
|
+
|
|
50
|
+
// Method doesn't exist yet - test will fail
|
|
51
|
+
expect(service.withTransaction).toBeDefined();
|
|
52
|
+
expect(typeof service.withTransaction).toBe('function');
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('should execute callback within a transaction', async () => {
|
|
56
|
+
const ClientClass = jest.fn(() => mockClient);
|
|
57
|
+
const options: PrismaModuleOptions = {
|
|
58
|
+
client: ClientClass as any,
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const service = new PrismaService(options);
|
|
62
|
+
await service.onModuleInit();
|
|
63
|
+
|
|
64
|
+
const workCallback = jest.fn().mockResolvedValue('result');
|
|
65
|
+
|
|
66
|
+
// This will fail because withTransaction doesn't exist
|
|
67
|
+
const result = await service.withTransaction(workCallback);
|
|
68
|
+
|
|
69
|
+
expect(mockClient.$transaction).toHaveBeenCalled();
|
|
70
|
+
expect(workCallback).toHaveBeenCalledWith(expect.any(Object));
|
|
71
|
+
expect(result).toBe('result');
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('should pass transaction client to callback', async () => {
|
|
75
|
+
const ClientClass = jest.fn(() => mockClient);
|
|
76
|
+
const options: PrismaModuleOptions = {
|
|
77
|
+
client: ClientClass as any,
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const service = new PrismaService(options);
|
|
81
|
+
await service.onModuleInit();
|
|
82
|
+
|
|
83
|
+
let receivedClient: any;
|
|
84
|
+
const workCallback = jest.fn().mockImplementation(async (tx) => {
|
|
85
|
+
receivedClient = tx;
|
|
86
|
+
return 'success';
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// Will fail - method doesn't exist
|
|
90
|
+
await service.withTransaction(workCallback);
|
|
91
|
+
|
|
92
|
+
expect(receivedClient).toBeDefined();
|
|
93
|
+
expect(receivedClient).toBe(mockTransactionClient);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('should commit transaction on successful callback execution', async () => {
|
|
97
|
+
const ClientClass = jest.fn(() => mockClient);
|
|
98
|
+
const options: PrismaModuleOptions = {
|
|
99
|
+
client: ClientClass as any,
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const service = new PrismaService(options);
|
|
103
|
+
await service.onModuleInit();
|
|
104
|
+
|
|
105
|
+
const workCallback = jest.fn().mockResolvedValue('committed');
|
|
106
|
+
|
|
107
|
+
// Will fail - method doesn't exist
|
|
108
|
+
const result = await service.withTransaction(workCallback);
|
|
109
|
+
|
|
110
|
+
expect(result).toBe('committed');
|
|
111
|
+
expect(mockClient.$transaction).toHaveBeenCalled();
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('should rollback transaction when callback throws error', async () => {
|
|
115
|
+
const ClientClass = jest.fn(() => mockClient);
|
|
116
|
+
const options: PrismaModuleOptions = {
|
|
117
|
+
client: ClientClass as any,
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const service = new PrismaService(options);
|
|
121
|
+
await service.onModuleInit();
|
|
122
|
+
|
|
123
|
+
const testError = new Error('Transaction failed');
|
|
124
|
+
const workCallback = jest.fn().mockRejectedValue(testError);
|
|
125
|
+
|
|
126
|
+
// Will fail - method doesn't exist
|
|
127
|
+
await expect(service.withTransaction(workCallback)).rejects.toThrow('Transaction failed');
|
|
128
|
+
|
|
129
|
+
expect(mockClient.$transaction).toHaveBeenCalled();
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('should map Prisma errors to AppException during transaction', async () => {
|
|
133
|
+
const ClientClass = jest.fn(() => mockClient);
|
|
134
|
+
const options: PrismaModuleOptions = {
|
|
135
|
+
client: ClientClass as any,
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
const service = new PrismaService(options);
|
|
139
|
+
await service.onModuleInit();
|
|
140
|
+
|
|
141
|
+
// Simulate Prisma error with P2002 (unique constraint)
|
|
142
|
+
const prismaError = {
|
|
143
|
+
code: 'P2002',
|
|
144
|
+
meta: { target: ['email'] },
|
|
145
|
+
message: 'Unique constraint failed',
|
|
146
|
+
clientVersion: '5.0.0',
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
const workCallback = jest.fn().mockRejectedValue(prismaError);
|
|
150
|
+
|
|
151
|
+
// Will fail - method doesn't exist and error mapping not implemented
|
|
152
|
+
await expect(service.withTransaction(workCallback)).rejects.toMatchObject({
|
|
153
|
+
httpStatus: 409,
|
|
154
|
+
code: 'DUPLICATE_ENTRY',
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it('should support nested transactions via savepoints', async () => {
|
|
159
|
+
const ClientClass = jest.fn(() => mockClient);
|
|
160
|
+
const options: PrismaModuleOptions = {
|
|
161
|
+
client: ClientClass as any,
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
const service = new PrismaService(options);
|
|
165
|
+
await service.onModuleInit();
|
|
166
|
+
|
|
167
|
+
const nestedCallback = jest.fn().mockResolvedValue('nested-result');
|
|
168
|
+
const outerCallback = jest.fn().mockImplementation(async (tx) => {
|
|
169
|
+
// Try to start nested transaction
|
|
170
|
+
await service.withTransaction(nestedCallback);
|
|
171
|
+
return 'outer-result';
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
// Will fail - method doesn't exist and nested transaction support not implemented
|
|
175
|
+
await service.withTransaction(outerCallback);
|
|
176
|
+
|
|
177
|
+
expect(nestedCallback).toHaveBeenCalled();
|
|
178
|
+
expect(outerCallback).toHaveBeenCalled();
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it('should accept transaction options (isolation level, timeout)', async () => {
|
|
182
|
+
const ClientClass = jest.fn(() => mockClient);
|
|
183
|
+
const options: PrismaModuleOptions = {
|
|
184
|
+
client: ClientClass as any,
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
const service = new PrismaService(options);
|
|
188
|
+
await service.onModuleInit();
|
|
189
|
+
|
|
190
|
+
const workCallback = jest.fn().mockResolvedValue('result');
|
|
191
|
+
const transactionOptions = {
|
|
192
|
+
isolationLevel: 'Serializable',
|
|
193
|
+
maxWait: 5000,
|
|
194
|
+
timeout: 10000,
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
// Will fail - method doesn't exist
|
|
198
|
+
await service.withTransaction(workCallback, transactionOptions);
|
|
199
|
+
|
|
200
|
+
expect(mockClient.$transaction).toHaveBeenCalledWith(
|
|
201
|
+
expect.any(Function),
|
|
202
|
+
transactionOptions
|
|
203
|
+
);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('should log transaction start and completion', async () => {
|
|
207
|
+
const ClientClass = jest.fn(() => mockClient);
|
|
208
|
+
const options: PrismaModuleOptions = {
|
|
209
|
+
client: ClientClass as any,
|
|
210
|
+
name: 'analytics',
|
|
211
|
+
logging: true,
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
const service = new PrismaService(options);
|
|
215
|
+
await service.onModuleInit();
|
|
216
|
+
|
|
217
|
+
const workCallback = jest.fn().mockResolvedValue('logged');
|
|
218
|
+
|
|
219
|
+
// Will fail - method doesn't exist
|
|
220
|
+
await service.withTransaction(workCallback);
|
|
221
|
+
|
|
222
|
+
expect(Logger.prototype.debug).toHaveBeenCalledWith(
|
|
223
|
+
expect.stringContaining('Transaction started'),
|
|
224
|
+
expect.anything()
|
|
225
|
+
);
|
|
226
|
+
expect(Logger.prototype.debug).toHaveBeenCalledWith(
|
|
227
|
+
expect.stringContaining('Transaction committed'),
|
|
228
|
+
expect.anything()
|
|
229
|
+
);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it('should log transaction rollback on error', async () => {
|
|
233
|
+
const ClientClass = jest.fn(() => mockClient);
|
|
234
|
+
const options: PrismaModuleOptions = {
|
|
235
|
+
client: ClientClass as any,
|
|
236
|
+
logging: true,
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
const service = new PrismaService(options);
|
|
240
|
+
await service.onModuleInit();
|
|
241
|
+
|
|
242
|
+
const workCallback = jest.fn().mockRejectedValue(new Error('Rollback test'));
|
|
243
|
+
|
|
244
|
+
// Will fail - method doesn't exist
|
|
245
|
+
try {
|
|
246
|
+
await service.withTransaction(workCallback);
|
|
247
|
+
} catch (error) {
|
|
248
|
+
// Expected
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
expect(Logger.prototype.error).toHaveBeenCalledWith(
|
|
252
|
+
expect.stringContaining('Transaction rolled back'),
|
|
253
|
+
expect.anything()
|
|
254
|
+
);
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it('should return the result of the callback', async () => {
|
|
258
|
+
const ClientClass = jest.fn(() => mockClient);
|
|
259
|
+
const options: PrismaModuleOptions = {
|
|
260
|
+
client: ClientClass as any,
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
const service = new PrismaService(options);
|
|
264
|
+
await service.onModuleInit();
|
|
265
|
+
|
|
266
|
+
const expectedResult = { id: 1, name: 'Test User' };
|
|
267
|
+
const workCallback = jest.fn().mockResolvedValue(expectedResult);
|
|
268
|
+
|
|
269
|
+
// Will fail - method doesn't exist
|
|
270
|
+
const result = await service.withTransaction(workCallback);
|
|
271
|
+
|
|
272
|
+
expect(result).toEqual(expectedResult);
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
it('should handle concurrent transactions', async () => {
|
|
276
|
+
const ClientClass = jest.fn(() => mockClient);
|
|
277
|
+
const options: PrismaModuleOptions = {
|
|
278
|
+
client: ClientClass as any,
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
const service = new PrismaService(options);
|
|
282
|
+
await service.onModuleInit();
|
|
283
|
+
|
|
284
|
+
const workCallback1 = jest.fn().mockResolvedValue('tx1');
|
|
285
|
+
const workCallback2 = jest.fn().mockResolvedValue('tx2');
|
|
286
|
+
const workCallback3 = jest.fn().mockResolvedValue('tx3');
|
|
287
|
+
|
|
288
|
+
// Will fail - method doesn't exist
|
|
289
|
+
const results = await Promise.all([
|
|
290
|
+
service.withTransaction(workCallback1),
|
|
291
|
+
service.withTransaction(workCallback2),
|
|
292
|
+
service.withTransaction(workCallback3),
|
|
293
|
+
]);
|
|
294
|
+
|
|
295
|
+
expect(results).toEqual(['tx1', 'tx2', 'tx3']);
|
|
296
|
+
expect(mockClient.$transaction).toHaveBeenCalledTimes(3);
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
it('should clean up resources on transaction failure', async () => {
|
|
300
|
+
const ClientClass = jest.fn(() => mockClient);
|
|
301
|
+
const options: PrismaModuleOptions = {
|
|
302
|
+
client: ClientClass as any,
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
const service = new PrismaService(options);
|
|
306
|
+
await service.onModuleInit();
|
|
307
|
+
|
|
308
|
+
const workCallback = jest.fn().mockRejectedValue(new Error('Cleanup test'));
|
|
309
|
+
|
|
310
|
+
// Will fail - method doesn't exist
|
|
311
|
+
try {
|
|
312
|
+
await service.withTransaction(workCallback);
|
|
313
|
+
} catch (error) {
|
|
314
|
+
// Expected to throw
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// Verify no dangling connections or resources
|
|
318
|
+
// Implementation should clean up properly
|
|
319
|
+
expect(true).toBe(true); // Placeholder for resource verification
|
|
320
|
+
});
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
describe('Transaction Error Handling', () => {
|
|
324
|
+
it('should preserve error stack trace when mapping errors', async () => {
|
|
325
|
+
const ClientClass = jest.fn(() => mockClient);
|
|
326
|
+
const options: PrismaModuleOptions = {
|
|
327
|
+
client: ClientClass as any,
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
const service = new PrismaService(options);
|
|
331
|
+
await service.onModuleInit();
|
|
332
|
+
|
|
333
|
+
const originalError = new Error('Original error');
|
|
334
|
+
originalError.stack = 'Original stack trace';
|
|
335
|
+
const workCallback = jest.fn().mockRejectedValue(originalError);
|
|
336
|
+
|
|
337
|
+
// Will fail - method doesn't exist
|
|
338
|
+
try {
|
|
339
|
+
await service.withTransaction(workCallback);
|
|
340
|
+
} catch (error: any) {
|
|
341
|
+
expect(error.stack).toContain('Original stack trace');
|
|
342
|
+
}
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
it('should include transaction context in error logs', async () => {
|
|
346
|
+
const ClientClass = jest.fn(() => mockClient);
|
|
347
|
+
const options: PrismaModuleOptions = {
|
|
348
|
+
client: ClientClass as any,
|
|
349
|
+
name: 'transaction-db',
|
|
350
|
+
logging: true,
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
const service = new PrismaService(options);
|
|
354
|
+
await service.onModuleInit();
|
|
355
|
+
|
|
356
|
+
const workCallback = jest.fn().mockRejectedValue(new Error('Context test'));
|
|
357
|
+
|
|
358
|
+
// Will fail - method doesn't exist
|
|
359
|
+
try {
|
|
360
|
+
await service.withTransaction(workCallback);
|
|
361
|
+
} catch (error) {
|
|
362
|
+
// Expected
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
expect(Logger.prototype.error).toHaveBeenCalledWith(
|
|
366
|
+
expect.stringContaining('[transaction-db]'),
|
|
367
|
+
expect.anything()
|
|
368
|
+
);
|
|
369
|
+
});
|
|
370
|
+
});
|
|
371
|
+
});
|