@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,359 @@
|
|
|
1
|
+
import { Paginate } from '../../lib/pagination/paginate';
|
|
2
|
+
import { Page, PageOption } from '@nathapp/nestjs-common';
|
|
3
|
+
|
|
4
|
+
describe('Paginate', () => {
|
|
5
|
+
interface User {
|
|
6
|
+
id: number;
|
|
7
|
+
name: string;
|
|
8
|
+
email: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
let mockModel: {
|
|
12
|
+
findMany: jest.Mock;
|
|
13
|
+
count: jest.Mock;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
mockModel = {
|
|
18
|
+
findMany: jest.fn(),
|
|
19
|
+
count: jest.fn(),
|
|
20
|
+
};
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
describe('basic pagination', () => {
|
|
24
|
+
it('should paginate with correct skip/take for first page', async () => {
|
|
25
|
+
const users: User[] = [
|
|
26
|
+
{ id: 1, name: 'Alice', email: 'alice@test.com' },
|
|
27
|
+
{ id: 2, name: 'Bob', email: 'bob@test.com' },
|
|
28
|
+
];
|
|
29
|
+
|
|
30
|
+
mockModel.findMany.mockResolvedValue(users);
|
|
31
|
+
mockModel.count.mockResolvedValue(10);
|
|
32
|
+
|
|
33
|
+
const pageOption = new PageOption(1, 2);
|
|
34
|
+
const result = await Paginate(mockModel, pageOption);
|
|
35
|
+
|
|
36
|
+
expect(mockModel.findMany).toHaveBeenCalledWith({
|
|
37
|
+
skip: 0,
|
|
38
|
+
take: 2,
|
|
39
|
+
});
|
|
40
|
+
expect(result).toBeInstanceOf(Page);
|
|
41
|
+
expect(result.records).toEqual(users);
|
|
42
|
+
expect(result.total).toBe(10);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('should paginate with correct skip/take for second page', async () => {
|
|
46
|
+
const users: User[] = [
|
|
47
|
+
{ id: 3, name: 'Charlie', email: 'charlie@test.com' },
|
|
48
|
+
{ id: 4, name: 'Diana', email: 'diana@test.com' },
|
|
49
|
+
];
|
|
50
|
+
|
|
51
|
+
mockModel.findMany.mockResolvedValue(users);
|
|
52
|
+
mockModel.count.mockResolvedValue(10);
|
|
53
|
+
|
|
54
|
+
const pageOption = new PageOption(2, 2);
|
|
55
|
+
const result = await Paginate(mockModel, pageOption);
|
|
56
|
+
|
|
57
|
+
expect(mockModel.findMany).toHaveBeenCalledWith({
|
|
58
|
+
skip: 2,
|
|
59
|
+
take: 2,
|
|
60
|
+
});
|
|
61
|
+
expect(result.records).toEqual(users);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('should paginate with correct skip/take for larger page size', async () => {
|
|
65
|
+
mockModel.findMany.mockResolvedValue([]);
|
|
66
|
+
mockModel.count.mockResolvedValue(100);
|
|
67
|
+
|
|
68
|
+
const pageOption = new PageOption(3, 20);
|
|
69
|
+
await Paginate(mockModel, pageOption);
|
|
70
|
+
|
|
71
|
+
expect(mockModel.findMany).toHaveBeenCalledWith({
|
|
72
|
+
skip: 40,
|
|
73
|
+
take: 20,
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
describe('page metadata', () => {
|
|
79
|
+
it('should return correct total count', async () => {
|
|
80
|
+
mockModel.findMany.mockResolvedValue([]);
|
|
81
|
+
mockModel.count.mockResolvedValue(42);
|
|
82
|
+
|
|
83
|
+
const pageOption = new PageOption(1, 10);
|
|
84
|
+
const result = await Paginate(mockModel, pageOption);
|
|
85
|
+
|
|
86
|
+
expect(result.total).toBe(42);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('should calculate hasNext correctly when more pages exist', async () => {
|
|
90
|
+
mockModel.findMany.mockResolvedValue([{ id: 1 }, { id: 2 }]);
|
|
91
|
+
mockModel.count.mockResolvedValue(10);
|
|
92
|
+
|
|
93
|
+
const pageOption = new PageOption(1, 2);
|
|
94
|
+
const result = await Paginate(mockModel, pageOption);
|
|
95
|
+
|
|
96
|
+
expect(result.hasNext).toBe(true);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('should calculate hasNext correctly on last page', async () => {
|
|
100
|
+
mockModel.findMany.mockResolvedValue([{ id: 9 }, { id: 10 }]);
|
|
101
|
+
mockModel.count.mockResolvedValue(10);
|
|
102
|
+
|
|
103
|
+
const pageOption = new PageOption(5, 2);
|
|
104
|
+
const result = await Paginate(mockModel, pageOption);
|
|
105
|
+
|
|
106
|
+
expect(result.hasNext).toBe(false);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('should calculate hasPrev correctly on first page', async () => {
|
|
110
|
+
mockModel.findMany.mockResolvedValue([{ id: 1 }, { id: 2 }]);
|
|
111
|
+
mockModel.count.mockResolvedValue(10);
|
|
112
|
+
|
|
113
|
+
const pageOption = new PageOption(1, 2);
|
|
114
|
+
const result = await Paginate(mockModel, pageOption);
|
|
115
|
+
|
|
116
|
+
expect(result.hasPrev).toBe(false);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('should calculate hasPrev correctly on second page', async () => {
|
|
120
|
+
mockModel.findMany.mockResolvedValue([{ id: 3 }, { id: 4 }]);
|
|
121
|
+
mockModel.count.mockResolvedValue(10);
|
|
122
|
+
|
|
123
|
+
const pageOption = new PageOption(2, 2);
|
|
124
|
+
const result = await Paginate(mockModel, pageOption);
|
|
125
|
+
|
|
126
|
+
expect(result.hasPrev).toBe(true);
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
describe('query arguments', () => {
|
|
131
|
+
it('should pass where clause to findMany', async () => {
|
|
132
|
+
mockModel.findMany.mockResolvedValue([]);
|
|
133
|
+
mockModel.count.mockResolvedValue(0);
|
|
134
|
+
|
|
135
|
+
const pageOption = new PageOption(1, 10);
|
|
136
|
+
const where = { status: 'active' };
|
|
137
|
+
|
|
138
|
+
await Paginate(mockModel, pageOption, { where });
|
|
139
|
+
|
|
140
|
+
expect(mockModel.findMany).toHaveBeenCalledWith({
|
|
141
|
+
where,
|
|
142
|
+
skip: 0,
|
|
143
|
+
take: 10,
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('should pass where clause to count', async () => {
|
|
148
|
+
mockModel.findMany.mockResolvedValue([]);
|
|
149
|
+
mockModel.count.mockResolvedValue(0);
|
|
150
|
+
|
|
151
|
+
const pageOption = new PageOption(1, 10);
|
|
152
|
+
const where = { status: 'active' };
|
|
153
|
+
|
|
154
|
+
await Paginate(mockModel, pageOption, { where });
|
|
155
|
+
|
|
156
|
+
expect(mockModel.count).toHaveBeenCalledWith({ where });
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('should pass orderBy to findMany', async () => {
|
|
160
|
+
mockModel.findMany.mockResolvedValue([]);
|
|
161
|
+
mockModel.count.mockResolvedValue(0);
|
|
162
|
+
|
|
163
|
+
const pageOption = new PageOption(1, 10);
|
|
164
|
+
const orderBy = { createdAt: 'desc' };
|
|
165
|
+
|
|
166
|
+
await Paginate(mockModel, pageOption, { orderBy });
|
|
167
|
+
|
|
168
|
+
expect(mockModel.findMany).toHaveBeenCalledWith({
|
|
169
|
+
orderBy,
|
|
170
|
+
skip: 0,
|
|
171
|
+
take: 10,
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it('should pass include to findMany', async () => {
|
|
176
|
+
mockModel.findMany.mockResolvedValue([]);
|
|
177
|
+
mockModel.count.mockResolvedValue(0);
|
|
178
|
+
|
|
179
|
+
const pageOption = new PageOption(1, 10);
|
|
180
|
+
const include = { posts: true };
|
|
181
|
+
|
|
182
|
+
await Paginate(mockModel, pageOption, { include });
|
|
183
|
+
|
|
184
|
+
expect(mockModel.findMany).toHaveBeenCalledWith({
|
|
185
|
+
include,
|
|
186
|
+
skip: 0,
|
|
187
|
+
take: 10,
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it('should pass select to findMany', async () => {
|
|
192
|
+
mockModel.findMany.mockResolvedValue([]);
|
|
193
|
+
mockModel.count.mockResolvedValue(0);
|
|
194
|
+
|
|
195
|
+
const pageOption = new PageOption(1, 10);
|
|
196
|
+
const select = { id: true, name: true };
|
|
197
|
+
|
|
198
|
+
await Paginate(mockModel, pageOption, { select });
|
|
199
|
+
|
|
200
|
+
expect(mockModel.findMany).toHaveBeenCalledWith({
|
|
201
|
+
select,
|
|
202
|
+
skip: 0,
|
|
203
|
+
take: 10,
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it('should pass all args combined to findMany', async () => {
|
|
208
|
+
mockModel.findMany.mockResolvedValue([]);
|
|
209
|
+
mockModel.count.mockResolvedValue(0);
|
|
210
|
+
|
|
211
|
+
const pageOption = new PageOption(2, 5);
|
|
212
|
+
const args = {
|
|
213
|
+
where: { status: 'active' },
|
|
214
|
+
orderBy: { createdAt: 'desc' },
|
|
215
|
+
include: { posts: true },
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
await Paginate(mockModel, pageOption, args);
|
|
219
|
+
|
|
220
|
+
expect(mockModel.findMany).toHaveBeenCalledWith({
|
|
221
|
+
...args,
|
|
222
|
+
skip: 5,
|
|
223
|
+
take: 5,
|
|
224
|
+
});
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it('should not pass orderBy/include/select to count', async () => {
|
|
228
|
+
mockModel.findMany.mockResolvedValue([]);
|
|
229
|
+
mockModel.count.mockResolvedValue(0);
|
|
230
|
+
|
|
231
|
+
const pageOption = new PageOption(1, 10);
|
|
232
|
+
const args = {
|
|
233
|
+
where: { status: 'active' },
|
|
234
|
+
orderBy: { createdAt: 'desc' },
|
|
235
|
+
include: { posts: true },
|
|
236
|
+
select: { id: true },
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
await Paginate(mockModel, pageOption, args);
|
|
240
|
+
|
|
241
|
+
// Only where should be passed to count
|
|
242
|
+
expect(mockModel.count).toHaveBeenCalledWith({ where: args.where });
|
|
243
|
+
expect(mockModel.count).not.toHaveBeenCalledWith(
|
|
244
|
+
expect.objectContaining({ orderBy: expect.anything() }),
|
|
245
|
+
);
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
describe('edge cases', () => {
|
|
250
|
+
it('should handle empty results', async () => {
|
|
251
|
+
mockModel.findMany.mockResolvedValue([]);
|
|
252
|
+
mockModel.count.mockResolvedValue(0);
|
|
253
|
+
|
|
254
|
+
const pageOption = new PageOption(1, 10);
|
|
255
|
+
const result = await Paginate(mockModel, pageOption);
|
|
256
|
+
|
|
257
|
+
expect(result.records).toEqual([]);
|
|
258
|
+
expect(result.total).toBe(0);
|
|
259
|
+
expect(result.hasNext).toBe(false);
|
|
260
|
+
expect(result.hasPrev).toBe(false);
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
it('should handle single page of results', async () => {
|
|
264
|
+
const users = [
|
|
265
|
+
{ id: 1, name: 'Alice', email: 'alice@test.com' },
|
|
266
|
+
{ id: 2, name: 'Bob', email: 'bob@test.com' },
|
|
267
|
+
];
|
|
268
|
+
mockModel.findMany.mockResolvedValue(users);
|
|
269
|
+
mockModel.count.mockResolvedValue(2);
|
|
270
|
+
|
|
271
|
+
const pageOption = new PageOption(1, 10);
|
|
272
|
+
const result = await Paginate(mockModel, pageOption);
|
|
273
|
+
|
|
274
|
+
expect(result.records).toEqual(users);
|
|
275
|
+
expect(result.total).toBe(2);
|
|
276
|
+
expect(result.hasNext).toBe(false);
|
|
277
|
+
expect(result.hasPrev).toBe(false);
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
it('should handle page beyond available records', async () => {
|
|
281
|
+
mockModel.findMany.mockResolvedValue([]);
|
|
282
|
+
mockModel.count.mockResolvedValue(10);
|
|
283
|
+
|
|
284
|
+
const pageOption = new PageOption(100, 10);
|
|
285
|
+
const result = await Paginate(mockModel, pageOption);
|
|
286
|
+
|
|
287
|
+
expect(result.records).toEqual([]);
|
|
288
|
+
expect(result.total).toBe(10);
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
it('should work with no args parameter', async () => {
|
|
292
|
+
mockModel.findMany.mockResolvedValue([]);
|
|
293
|
+
mockModel.count.mockResolvedValue(0);
|
|
294
|
+
|
|
295
|
+
const pageOption = new PageOption(1, 10);
|
|
296
|
+
const result = await Paginate(mockModel, pageOption);
|
|
297
|
+
|
|
298
|
+
expect(mockModel.findMany).toHaveBeenCalledWith({
|
|
299
|
+
skip: 0,
|
|
300
|
+
take: 10,
|
|
301
|
+
});
|
|
302
|
+
expect(mockModel.count).toHaveBeenCalledWith({ where: undefined });
|
|
303
|
+
expect(result).toBeInstanceOf(Page);
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it('should work with undefined where in args', async () => {
|
|
307
|
+
mockModel.findMany.mockResolvedValue([]);
|
|
308
|
+
mockModel.count.mockResolvedValue(0);
|
|
309
|
+
|
|
310
|
+
const pageOption = new PageOption(1, 10);
|
|
311
|
+
await Paginate(mockModel, pageOption, {});
|
|
312
|
+
|
|
313
|
+
expect(mockModel.count).toHaveBeenCalledWith({ where: undefined });
|
|
314
|
+
});
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
describe('concurrent execution', () => {
|
|
318
|
+
it('should call findMany and count in parallel', async () => {
|
|
319
|
+
const findManyPromise = new Promise((resolve) =>
|
|
320
|
+
setTimeout(() => resolve([]), 10),
|
|
321
|
+
);
|
|
322
|
+
const countPromise = new Promise((resolve) => setTimeout(() => resolve(5), 10));
|
|
323
|
+
|
|
324
|
+
mockModel.findMany.mockReturnValue(findManyPromise);
|
|
325
|
+
mockModel.count.mockReturnValue(countPromise);
|
|
326
|
+
|
|
327
|
+
const pageOption = new PageOption(1, 10);
|
|
328
|
+
const startTime = Date.now();
|
|
329
|
+
|
|
330
|
+
await Paginate(mockModel, pageOption);
|
|
331
|
+
|
|
332
|
+
const duration = Date.now() - startTime;
|
|
333
|
+
|
|
334
|
+
// In CI environments, timer precision may vary.
|
|
335
|
+
// We just want to ensure it's reasonably fast and definitely parallel (not 200ms+ serial)
|
|
336
|
+
expect(duration).toBeLessThan(100);
|
|
337
|
+
expect(mockModel.findMany).toHaveBeenCalled();
|
|
338
|
+
expect(mockModel.count).toHaveBeenCalled();
|
|
339
|
+
});
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
describe('type safety', () => {
|
|
343
|
+
it('should return correctly typed Page<T>', async () => {
|
|
344
|
+
const users: User[] = [
|
|
345
|
+
{ id: 1, name: 'Alice', email: 'alice@test.com' },
|
|
346
|
+
];
|
|
347
|
+
mockModel.findMany.mockResolvedValue(users);
|
|
348
|
+
mockModel.count.mockResolvedValue(1);
|
|
349
|
+
|
|
350
|
+
const pageOption = new PageOption(1, 10);
|
|
351
|
+
const result = await Paginate<User>(mockModel, pageOption);
|
|
352
|
+
|
|
353
|
+
expect(result).toBeInstanceOf(Page);
|
|
354
|
+
expect(result.records[0].id).toBe(1);
|
|
355
|
+
expect(result.records[0].name).toBe('Alice');
|
|
356
|
+
expect(result.records[0].email).toBe('alice@test.com');
|
|
357
|
+
});
|
|
358
|
+
});
|
|
359
|
+
});
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { ArgumentsHost } from '@nestjs/common';
|
|
2
|
+
import {
|
|
3
|
+
PrismaClientKnownRequestError,
|
|
4
|
+
PrismaClientValidationError,
|
|
5
|
+
} from '@prisma/client/runtime/library';
|
|
6
|
+
import { PrismaExceptionFilter } from '../lib/filter/prisma-exception.filter';
|
|
7
|
+
import { PRISMA_ERROR_CODES } from '../lib/errors/prisma-error.mapper';
|
|
8
|
+
|
|
9
|
+
describe('PrismaExceptionFilter', () => {
|
|
10
|
+
let filter: PrismaExceptionFilter;
|
|
11
|
+
let mockArgumentsHost: ArgumentsHost;
|
|
12
|
+
let mockResponse: any;
|
|
13
|
+
|
|
14
|
+
beforeEach(() => {
|
|
15
|
+
filter = new PrismaExceptionFilter();
|
|
16
|
+
|
|
17
|
+
// Mock response object
|
|
18
|
+
mockResponse = {
|
|
19
|
+
status: jest.fn().mockReturnThis(),
|
|
20
|
+
json: jest.fn().mockReturnThis(),
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// Mock ArgumentsHost
|
|
24
|
+
mockArgumentsHost = {
|
|
25
|
+
switchToHttp: jest.fn().mockReturnValue({
|
|
26
|
+
getResponse: jest.fn().mockReturnValue(mockResponse),
|
|
27
|
+
getRequest: jest.fn(),
|
|
28
|
+
}),
|
|
29
|
+
getArgByIndex: jest.fn(),
|
|
30
|
+
getArgs: jest.fn(),
|
|
31
|
+
getType: jest.fn(),
|
|
32
|
+
switchToRpc: jest.fn(),
|
|
33
|
+
switchToWs: jest.fn(),
|
|
34
|
+
} as any;
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe('PrismaClientKnownRequestError handling', () => {
|
|
38
|
+
it('should map P2002 (unique constraint) to 409 Conflict', () => {
|
|
39
|
+
const exception = new PrismaClientKnownRequestError(
|
|
40
|
+
'Unique constraint failed on the fields: (`email`)',
|
|
41
|
+
{
|
|
42
|
+
code: 'P2002',
|
|
43
|
+
clientVersion: '5.0.0',
|
|
44
|
+
meta: { target: ['email'] },
|
|
45
|
+
}
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
filter.catch(exception, mockArgumentsHost);
|
|
49
|
+
|
|
50
|
+
expect(mockResponse.status).toHaveBeenCalledWith(409);
|
|
51
|
+
expect(mockResponse.json).toHaveBeenCalledWith({
|
|
52
|
+
success: false,
|
|
53
|
+
error: {
|
|
54
|
+
code: PRISMA_ERROR_CODES.DUPLICATE_ENTRY,
|
|
55
|
+
message: expect.any(String),
|
|
56
|
+
args: { target: ['email'] },
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('should map P2025 (record not found) to 404 Not Found', () => {
|
|
62
|
+
const exception = new PrismaClientKnownRequestError(
|
|
63
|
+
'Record to delete does not exist',
|
|
64
|
+
{
|
|
65
|
+
code: 'P2025',
|
|
66
|
+
clientVersion: '5.0.0',
|
|
67
|
+
meta: { cause: 'Record to delete does not exist.' },
|
|
68
|
+
}
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
filter.catch(exception, mockArgumentsHost);
|
|
72
|
+
|
|
73
|
+
expect(mockResponse.status).toHaveBeenCalledWith(404);
|
|
74
|
+
expect(mockResponse.json).toHaveBeenCalledWith({
|
|
75
|
+
success: false,
|
|
76
|
+
error: {
|
|
77
|
+
code: PRISMA_ERROR_CODES.RECORD_NOT_FOUND,
|
|
78
|
+
message: expect.any(String),
|
|
79
|
+
args: { cause: 'Record to delete does not exist.' },
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('should map P2003 (foreign key constraint) to 400 Bad Request', () => {
|
|
85
|
+
const exception = new PrismaClientKnownRequestError(
|
|
86
|
+
'Foreign key constraint failed on the field: `userId`',
|
|
87
|
+
{
|
|
88
|
+
code: 'P2003',
|
|
89
|
+
clientVersion: '5.0.0',
|
|
90
|
+
meta: { field_name: 'userId' },
|
|
91
|
+
}
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
filter.catch(exception, mockArgumentsHost);
|
|
95
|
+
|
|
96
|
+
expect(mockResponse.status).toHaveBeenCalledWith(400);
|
|
97
|
+
expect(mockResponse.json).toHaveBeenCalledWith({
|
|
98
|
+
success: false,
|
|
99
|
+
error: {
|
|
100
|
+
code: PRISMA_ERROR_CODES.INVALID_REFERENCE,
|
|
101
|
+
message: expect.any(String),
|
|
102
|
+
args: { field_name: 'userId' },
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('should map P1008 (connection timeout) to 503 Service Unavailable', () => {
|
|
108
|
+
const exception = new PrismaClientKnownRequestError(
|
|
109
|
+
'Operations timed out',
|
|
110
|
+
{
|
|
111
|
+
code: 'P1008',
|
|
112
|
+
clientVersion: '5.0.0',
|
|
113
|
+
meta: { timeout: '10s' },
|
|
114
|
+
}
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
filter.catch(exception, mockArgumentsHost);
|
|
118
|
+
|
|
119
|
+
expect(mockResponse.status).toHaveBeenCalledWith(503);
|
|
120
|
+
expect(mockResponse.json).toHaveBeenCalledWith({
|
|
121
|
+
success: false,
|
|
122
|
+
error: {
|
|
123
|
+
code: PRISMA_ERROR_CODES.CONNECTION_TIMEOUT,
|
|
124
|
+
message: expect.any(String),
|
|
125
|
+
args: { timeout: '10s' },
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('should map unknown error codes to 500 Internal Server Error', () => {
|
|
131
|
+
const exception = new PrismaClientKnownRequestError(
|
|
132
|
+
'Unknown database error',
|
|
133
|
+
{
|
|
134
|
+
code: 'P9999',
|
|
135
|
+
clientVersion: '5.0.0',
|
|
136
|
+
}
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
filter.catch(exception, mockArgumentsHost);
|
|
140
|
+
|
|
141
|
+
expect(mockResponse.status).toHaveBeenCalledWith(500);
|
|
142
|
+
expect(mockResponse.json).toHaveBeenCalledWith({
|
|
143
|
+
success: false,
|
|
144
|
+
error: {
|
|
145
|
+
code: PRISMA_ERROR_CODES.DATABASE_ERROR,
|
|
146
|
+
message: expect.any(String),
|
|
147
|
+
args: expect.objectContaining({ prismaCode: 'P9999' }),
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
describe('PrismaClientValidationError handling', () => {
|
|
154
|
+
it('should map validation errors to 400 Bad Request', () => {
|
|
155
|
+
// PrismaClientValidationError doesn't have a public constructor
|
|
156
|
+
// So we create a mock object that matches its structure
|
|
157
|
+
const exception = {
|
|
158
|
+
name: 'PrismaClientValidationError',
|
|
159
|
+
message: 'Invalid query parameters: missing required field',
|
|
160
|
+
} as PrismaClientValidationError;
|
|
161
|
+
|
|
162
|
+
// Mock instanceof check by using Object.setPrototypeOf
|
|
163
|
+
Object.setPrototypeOf(exception, PrismaClientValidationError.prototype);
|
|
164
|
+
|
|
165
|
+
filter.catch(exception, mockArgumentsHost);
|
|
166
|
+
|
|
167
|
+
expect(mockResponse.status).toHaveBeenCalledWith(400);
|
|
168
|
+
expect(mockResponse.json).toHaveBeenCalledWith({
|
|
169
|
+
success: false,
|
|
170
|
+
error: {
|
|
171
|
+
code: PRISMA_ERROR_CODES.DATABASE_ERROR,
|
|
172
|
+
message: 'Invalid query parameters',
|
|
173
|
+
args: { message: 'Invalid query parameters: missing required field' },
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
describe('error response format', () => {
|
|
180
|
+
it('should always include success: false in response', () => {
|
|
181
|
+
const exception = new PrismaClientKnownRequestError(
|
|
182
|
+
'Test error',
|
|
183
|
+
{
|
|
184
|
+
code: 'P2002',
|
|
185
|
+
clientVersion: '5.0.0',
|
|
186
|
+
}
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
filter.catch(exception, mockArgumentsHost);
|
|
190
|
+
|
|
191
|
+
const jsonCall = mockResponse.json.mock.calls[0][0];
|
|
192
|
+
expect(jsonCall.success).toBe(false);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it('should include error code, message, and args', () => {
|
|
196
|
+
const exception = new PrismaClientKnownRequestError(
|
|
197
|
+
'Test error',
|
|
198
|
+
{
|
|
199
|
+
code: 'P2002',
|
|
200
|
+
clientVersion: '5.0.0',
|
|
201
|
+
meta: { target: ['email'] },
|
|
202
|
+
}
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
filter.catch(exception, mockArgumentsHost);
|
|
206
|
+
|
|
207
|
+
const jsonCall = mockResponse.json.mock.calls[0][0];
|
|
208
|
+
expect(jsonCall.error).toHaveProperty('code');
|
|
209
|
+
expect(jsonCall.error).toHaveProperty('message');
|
|
210
|
+
expect(jsonCall.error).toHaveProperty('args');
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
});
|