@nathapp/nestjs-prisma 3.3.0 → 4.0.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/package.json +10 -7
- package/.eslintrc.js +0 -8
- package/.turbo/turbo-build.log +0 -4
- package/CHANGELOG.md +0 -279
- package/docs/20260215-fix-plan-prisma.md +0 -37
- package/docs/20260215-review-prisma.md +0 -598
- package/docs/20260216-plan-prisma-a-minus.md +0 -70
- package/lib/errors/prisma-error.mapper.ts +0 -164
- package/lib/filter/prisma-exception.filter.ts +0 -67
- package/lib/index.ts +0 -12
- package/lib/pagination/index.ts +0 -1
- package/lib/pagination/paginate.ts +0 -44
- package/lib/prisma.constants.ts +0 -40
- package/lib/prisma.decorator.ts +0 -8
- package/lib/prisma.health.ts +0 -40
- package/lib/prisma.interfaces.ts +0 -51
- package/lib/prisma.module.ts +0 -281
- package/lib/prisma.service.ts +0 -179
- package/lib/repository/abstract-prisma.repository.ts +0 -138
- package/lib/repository/index.ts +0 -1
- package/lib/testing/index.ts +0 -1
- package/lib/testing/prisma-testing.utils.ts +0 -29
- package/lib/transaction/index.ts +0 -2
- package/lib/transaction/prisma-transaction-manager.ts +0 -77
- package/lib/transaction/prisma-transaction.constants.ts +0 -29
- package/test/error-mapping.spec.ts +0 -231
- package/test/jest-nax.json +0 -27
- package/test/jest-test.json +0 -29
- package/test/lifecycle.spec.ts +0 -241
- package/test/multi-db-als.spec.ts +0 -142
- package/test/multi-db-transaction.spec.ts +0 -75
- package/test/pagination/paginate.spec.ts +0 -359
- package/test/prisma-exception-filter.spec.ts +0 -213
- package/test/prisma-transaction-manager.spec.ts +0 -285
- package/test/prisma.decorator.spec.ts +0 -257
- package/test/prisma.health.spec.ts +0 -226
- package/test/prisma.module.spec.ts +0 -506
- package/test/prisma.service.spec.ts +0 -359
- package/test/repository/abstract-prisma.repository.spec.ts +0 -157
- package/test/transaction-manager-e2e.spec.ts +0 -496
- package/test/transaction.spec.ts +0 -371
- package/test/transparent-transaction.spec.ts +0 -500
- package/tsconfig.json +0 -9
|
@@ -1,213 +0,0 @@
|
|
|
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: {}, // raw meta not exposed in args (security)
|
|
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: {}, // raw meta not exposed in args (security)
|
|
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: {}, // raw meta not exposed in args (security)
|
|
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: {}, // raw meta not exposed in args (security)
|
|
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: {}, // raw error message not exposed in args (security)
|
|
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
|
-
});
|
|
@@ -1,285 +0,0 @@
|
|
|
1
|
-
import { Test, TestingModule } from '@nestjs/testing';
|
|
2
|
-
import { PrismaTransactionManager } from '../lib/transaction/prisma-transaction-manager';
|
|
3
|
-
import { getPrismaToken } from '../lib/prisma.constants';
|
|
4
|
-
import type { PrismaClientLike } from '../lib/prisma.interfaces';
|
|
5
|
-
|
|
6
|
-
interface MockPrismaClient extends PrismaClientLike {
|
|
7
|
-
$transaction: jest.Mock;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
describe('PrismaTransactionManager', () => {
|
|
11
|
-
let transactionManager: PrismaTransactionManager;
|
|
12
|
-
let mockPrismaClient: MockPrismaClient;
|
|
13
|
-
let mockTransactionClient: Partial<PrismaClientLike>;
|
|
14
|
-
|
|
15
|
-
beforeEach(async () => {
|
|
16
|
-
mockTransactionClient = {
|
|
17
|
-
$connect: jest.fn().mockResolvedValue(undefined),
|
|
18
|
-
$disconnect: jest.fn().mockResolvedValue(undefined),
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
mockPrismaClient = {
|
|
22
|
-
$connect: jest.fn().mockResolvedValue(undefined),
|
|
23
|
-
$disconnect: jest.fn().mockResolvedValue(undefined),
|
|
24
|
-
$transaction: jest.fn().mockImplementation(async (callback: Function) => {
|
|
25
|
-
return callback(mockTransactionClient);
|
|
26
|
-
}),
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
const module: TestingModule = await Test.createTestingModule({
|
|
30
|
-
providers: [
|
|
31
|
-
PrismaTransactionManager,
|
|
32
|
-
{
|
|
33
|
-
provide: getPrismaToken(),
|
|
34
|
-
useValue: mockPrismaClient,
|
|
35
|
-
},
|
|
36
|
-
],
|
|
37
|
-
}).compile();
|
|
38
|
-
|
|
39
|
-
transactionManager = module.get<PrismaTransactionManager>(PrismaTransactionManager);
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
afterEach(() => {
|
|
43
|
-
jest.clearAllMocks();
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
describe('run()', () => {
|
|
47
|
-
it('should execute callback within a transaction', async () => {
|
|
48
|
-
const callback = jest.fn().mockResolvedValue('success');
|
|
49
|
-
|
|
50
|
-
const result = await transactionManager.run(callback);
|
|
51
|
-
|
|
52
|
-
expect(mockPrismaClient.$transaction).toHaveBeenCalledTimes(1);
|
|
53
|
-
expect(callback).toHaveBeenCalledTimes(1);
|
|
54
|
-
expect(result).toBe('success');
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it('should commit on successful callback execution', async () => {
|
|
58
|
-
const callback = jest.fn().mockResolvedValue({ id: 1, name: 'Test' });
|
|
59
|
-
|
|
60
|
-
const result = await transactionManager.run(callback);
|
|
61
|
-
|
|
62
|
-
expect(result).toEqual({ id: 1, name: 'Test' });
|
|
63
|
-
expect(mockPrismaClient.$transaction).toHaveBeenCalled();
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it('should roll back on callback error', async () => {
|
|
67
|
-
const error = new Error('Transaction failed');
|
|
68
|
-
const callback = jest.fn().mockRejectedValue(error);
|
|
69
|
-
|
|
70
|
-
await expect(transactionManager.run(callback)).rejects.toThrow('Transaction failed');
|
|
71
|
-
|
|
72
|
-
expect(mockPrismaClient.$transaction).toHaveBeenCalled();
|
|
73
|
-
expect(callback).toHaveBeenCalled();
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
it('should support nested transactions by reusing parent transaction', async () => {
|
|
77
|
-
const nestedCallback = jest.fn().mockResolvedValue('nested-result');
|
|
78
|
-
const outerCallback = jest.fn().mockImplementation(async () => {
|
|
79
|
-
const nestedResult = await transactionManager.run(nestedCallback);
|
|
80
|
-
return `outer-${nestedResult}`;
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
const result = await transactionManager.run(outerCallback);
|
|
84
|
-
|
|
85
|
-
expect(result).toBe('outer-nested-result');
|
|
86
|
-
expect(mockPrismaClient.$transaction).toHaveBeenCalledTimes(1);
|
|
87
|
-
expect(outerCallback).toHaveBeenCalledTimes(1);
|
|
88
|
-
expect(nestedCallback).toHaveBeenCalledTimes(1);
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
it('should handle concurrent transactions independently', async () => {
|
|
92
|
-
const callback1 = jest.fn().mockResolvedValue('tx1');
|
|
93
|
-
const callback2 = jest.fn().mockResolvedValue('tx2');
|
|
94
|
-
const callback3 = jest.fn().mockResolvedValue('tx3');
|
|
95
|
-
|
|
96
|
-
const results = await Promise.all([
|
|
97
|
-
transactionManager.run(callback1),
|
|
98
|
-
transactionManager.run(callback2),
|
|
99
|
-
transactionManager.run(callback3),
|
|
100
|
-
]);
|
|
101
|
-
|
|
102
|
-
expect(results).toEqual(['tx1', 'tx2', 'tx3']);
|
|
103
|
-
expect(mockPrismaClient.$transaction).toHaveBeenCalledTimes(3);
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
it('should preserve error details when callback throws', async () => {
|
|
107
|
-
const error = new Error('Detailed error message');
|
|
108
|
-
error.stack = 'Custom stack trace';
|
|
109
|
-
const callback = jest.fn().mockRejectedValue(error);
|
|
110
|
-
|
|
111
|
-
try {
|
|
112
|
-
await transactionManager.run(callback);
|
|
113
|
-
fail('Should have thrown an error');
|
|
114
|
-
} catch (e: any) {
|
|
115
|
-
expect(e.message).toBe('Detailed error message');
|
|
116
|
-
expect(e.stack).toContain('Custom stack trace');
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
describe('getClient()', () => {
|
|
122
|
-
it('should return root client when not in a transaction', () => {
|
|
123
|
-
const client = transactionManager.getClient();
|
|
124
|
-
|
|
125
|
-
expect(client).toBe(mockPrismaClient);
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
it('should return transaction client when inside run()', async () => {
|
|
129
|
-
let capturedClient: any;
|
|
130
|
-
|
|
131
|
-
await transactionManager.run(async () => {
|
|
132
|
-
capturedClient = transactionManager.getClient();
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
expect(capturedClient).toBe(mockTransactionClient);
|
|
136
|
-
expect(capturedClient).not.toBe(mockPrismaClient);
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
it('should return root client after transaction completes', async () => {
|
|
140
|
-
await transactionManager.run(async () => {
|
|
141
|
-
const inTx = transactionManager.getClient();
|
|
142
|
-
expect(inTx).toBe(mockTransactionClient);
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
const afterTx = transactionManager.getClient();
|
|
146
|
-
expect(afterTx).toBe(mockPrismaClient);
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
it('should return same transaction client in nested calls', async () => {
|
|
150
|
-
let outerClient: any;
|
|
151
|
-
let innerClient: any;
|
|
152
|
-
|
|
153
|
-
await transactionManager.run(async () => {
|
|
154
|
-
outerClient = transactionManager.getClient();
|
|
155
|
-
|
|
156
|
-
await transactionManager.run(async () => {
|
|
157
|
-
innerClient = transactionManager.getClient();
|
|
158
|
-
});
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
expect(outerClient).toBe(mockTransactionClient);
|
|
162
|
-
expect(innerClient).toBe(mockTransactionClient);
|
|
163
|
-
expect(outerClient).toBe(innerClient);
|
|
164
|
-
});
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
describe('isInTransaction()', () => {
|
|
168
|
-
it('should return false when not in a transaction', () => {
|
|
169
|
-
expect(transactionManager.isInTransaction()).toBe(false);
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
it('should return true when inside run()', async () => {
|
|
173
|
-
let insideCheck: boolean | undefined;
|
|
174
|
-
|
|
175
|
-
await transactionManager.run(async () => {
|
|
176
|
-
insideCheck = transactionManager.isInTransaction();
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
expect(insideCheck).toBe(true);
|
|
180
|
-
});
|
|
181
|
-
|
|
182
|
-
it('should return false after transaction completes', async () => {
|
|
183
|
-
await transactionManager.run(async () => {
|
|
184
|
-
expect(transactionManager.isInTransaction()).toBe(true);
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
expect(transactionManager.isInTransaction()).toBe(false);
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
it('should return true in nested transactions', async () => {
|
|
191
|
-
let outerCheck: boolean | undefined;
|
|
192
|
-
let innerCheck: boolean | undefined;
|
|
193
|
-
|
|
194
|
-
await transactionManager.run(async () => {
|
|
195
|
-
outerCheck = transactionManager.isInTransaction();
|
|
196
|
-
|
|
197
|
-
await transactionManager.run(async () => {
|
|
198
|
-
innerCheck = transactionManager.isInTransaction();
|
|
199
|
-
});
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
expect(outerCheck).toBe(true);
|
|
203
|
-
expect(innerCheck).toBe(true);
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
it('should handle concurrent transactions correctly', async () => {
|
|
207
|
-
const checks: boolean[] = [];
|
|
208
|
-
|
|
209
|
-
await Promise.all([
|
|
210
|
-
transactionManager.run(async () => {
|
|
211
|
-
checks.push(transactionManager.isInTransaction());
|
|
212
|
-
}),
|
|
213
|
-
transactionManager.run(async () => {
|
|
214
|
-
checks.push(transactionManager.isInTransaction());
|
|
215
|
-
}),
|
|
216
|
-
transactionManager.run(async () => {
|
|
217
|
-
checks.push(transactionManager.isInTransaction());
|
|
218
|
-
}),
|
|
219
|
-
]);
|
|
220
|
-
|
|
221
|
-
expect(checks).toEqual([true, true, true]);
|
|
222
|
-
expect(transactionManager.isInTransaction()).toBe(false);
|
|
223
|
-
});
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
describe('ITransactionManager interface compliance', () => {
|
|
227
|
-
it('should implement all required methods', () => {
|
|
228
|
-
expect(transactionManager.run).toBeDefined();
|
|
229
|
-
expect(transactionManager.getClient).toBeDefined();
|
|
230
|
-
expect(transactionManager.isInTransaction).toBeDefined();
|
|
231
|
-
expect(typeof transactionManager.run).toBe('function');
|
|
232
|
-
expect(typeof transactionManager.getClient).toBe('function');
|
|
233
|
-
expect(typeof transactionManager.isInTransaction).toBe('function');
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
it('should handle async operations in transaction', async () => {
|
|
237
|
-
const asyncWork = jest.fn().mockImplementation(async () => {
|
|
238
|
-
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
239
|
-
return 'async-result';
|
|
240
|
-
});
|
|
241
|
-
|
|
242
|
-
const result = await transactionManager.run(asyncWork);
|
|
243
|
-
|
|
244
|
-
expect(result).toBe('async-result');
|
|
245
|
-
expect(asyncWork).toHaveBeenCalled();
|
|
246
|
-
});
|
|
247
|
-
});
|
|
248
|
-
|
|
249
|
-
describe('error scenarios', () => {
|
|
250
|
-
it('should not leak transaction context on error', async () => {
|
|
251
|
-
const error = new Error('Test error');
|
|
252
|
-
const callback = jest.fn().mockRejectedValue(error);
|
|
253
|
-
|
|
254
|
-
try {
|
|
255
|
-
await transactionManager.run(callback);
|
|
256
|
-
} catch (e) {
|
|
257
|
-
// Expected
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
expect(transactionManager.isInTransaction()).toBe(false);
|
|
261
|
-
expect(transactionManager.getClient()).toBe(mockPrismaClient);
|
|
262
|
-
});
|
|
263
|
-
|
|
264
|
-
it('should handle synchronous errors in callback', async () => {
|
|
265
|
-
const callback = jest.fn().mockImplementation(() => {
|
|
266
|
-
throw new Error('Sync error');
|
|
267
|
-
});
|
|
268
|
-
|
|
269
|
-
await expect(transactionManager.run(callback)).rejects.toThrow('Sync error');
|
|
270
|
-
expect(transactionManager.isInTransaction()).toBe(false);
|
|
271
|
-
});
|
|
272
|
-
|
|
273
|
-
it('should roll back on nested transaction error', async () => {
|
|
274
|
-
const nestedError = new Error('Nested error');
|
|
275
|
-
const nestedCallback = jest.fn().mockRejectedValue(nestedError);
|
|
276
|
-
const outerCallback = jest.fn().mockImplementation(async () => {
|
|
277
|
-
await transactionManager.run(nestedCallback);
|
|
278
|
-
});
|
|
279
|
-
|
|
280
|
-
await expect(transactionManager.run(outerCallback)).rejects.toThrow('Nested error');
|
|
281
|
-
expect(mockPrismaClient.$transaction).toHaveBeenCalledTimes(1);
|
|
282
|
-
expect(transactionManager.isInTransaction()).toBe(false);
|
|
283
|
-
});
|
|
284
|
-
});
|
|
285
|
-
});
|