@nathapp/nestjs-prisma 3.0.1 → 3.1.0
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/.turbo/turbo-test.log +186 -28
- package/CHANGELOG.md +20 -0
- 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/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/prisma.interfaces.ts +9 -0
- package/lib/prisma.module.ts +76 -6
- 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 +1 -1
- package/test/transparent-transaction.spec.ts +332 -0
package/.turbo/turbo-build.log
CHANGED
package/.turbo/turbo-test.log
CHANGED
|
@@ -1,43 +1,201 @@
|
|
|
1
1
|
|
|
2
|
-
> @nathapp/nestjs-prisma@3.0.
|
|
2
|
+
> @nathapp/nestjs-prisma@3.0.1 test
|
|
3
3
|
> jest --config ./test/jest-test.json
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
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
|
|
5
|
+
FAIL test/transaction.spec.ts
|
|
6
|
+
● Test suite failed to run
|
|
10
7
|
|
|
11
|
-
|
|
8
|
+
Cannot find module './dist' from '../nestjs-common/index.js'
|
|
12
9
|
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
Require stack:
|
|
11
|
+
/home/ubuntu/subrina-coder/projects/nathapp-nestjs-core/repos/nathapp-nestjs/packages/nestjs-common/index.js
|
|
12
|
+
lib/errors/prisma-error.mapper.ts
|
|
13
|
+
lib/prisma.service.ts
|
|
14
|
+
test/transaction.spec.ts
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
>
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
338 | expect(mockModel.count).toHaveBeenCalled();
|
|
22
|
-
339 | });
|
|
16
|
+
15 | };
|
|
17
|
+
16 | Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
> 17 | __exportStar(require("./dist"), exports);
|
|
19
|
+
| ^
|
|
20
|
+
18 |
|
|
23
21
|
|
|
24
|
-
at
|
|
22
|
+
at Resolver._throwModNotFoundError (../../node_modules/jest-resolve/build/resolver.js:427:11)
|
|
23
|
+
at Object.<anonymous> (../nestjs-common/index.js:17:14)
|
|
24
|
+
at Object.<anonymous> (lib/errors/prisma-error.mapper.ts:1:1)
|
|
25
|
+
at Object.<anonymous> (lib/prisma.service.ts:3:1)
|
|
26
|
+
at Object.<anonymous> (test/transaction.spec.ts:2:1)
|
|
25
27
|
|
|
26
|
-
|
|
27
|
-
|
|
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
|
|
28
|
+
FAIL test/prisma.module.spec.ts
|
|
29
|
+
● Test suite failed to run
|
|
31
30
|
|
|
32
|
-
|
|
33
|
-
|
|
31
|
+
Cannot find module './dist' from '../nestjs-common/index.js'
|
|
32
|
+
|
|
33
|
+
Require stack:
|
|
34
|
+
/home/ubuntu/subrina-coder/projects/nathapp-nestjs-core/repos/nathapp-nestjs/packages/nestjs-common/index.js
|
|
35
|
+
lib/errors/prisma-error.mapper.ts
|
|
36
|
+
lib/prisma.service.ts
|
|
37
|
+
lib/prisma.module.ts
|
|
38
|
+
test/prisma.module.spec.ts
|
|
39
|
+
|
|
40
|
+
15 | };
|
|
41
|
+
16 | Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
> 17 | __exportStar(require("./dist"), exports);
|
|
43
|
+
| ^
|
|
44
|
+
18 |
|
|
45
|
+
|
|
46
|
+
at Resolver._throwModNotFoundError (../../node_modules/jest-resolve/build/resolver.js:427:11)
|
|
47
|
+
at Object.<anonymous> (../nestjs-common/index.js:17:14)
|
|
48
|
+
at Object.<anonymous> (lib/errors/prisma-error.mapper.ts:1:1)
|
|
49
|
+
at Object.<anonymous> (lib/prisma.service.ts:3:1)
|
|
50
|
+
at Object.<anonymous> (lib/prisma.module.ts:3:1)
|
|
51
|
+
at Object.<anonymous> (test/prisma.module.spec.ts:3:1)
|
|
52
|
+
|
|
53
|
+
FAIL test/prisma.service.spec.ts
|
|
54
|
+
● Test suite failed to run
|
|
55
|
+
|
|
56
|
+
Cannot find module './dist' from '../nestjs-common/index.js'
|
|
57
|
+
|
|
58
|
+
Require stack:
|
|
59
|
+
/home/ubuntu/subrina-coder/projects/nathapp-nestjs-core/repos/nathapp-nestjs/packages/nestjs-common/index.js
|
|
60
|
+
lib/errors/prisma-error.mapper.ts
|
|
61
|
+
lib/prisma.service.ts
|
|
62
|
+
test/prisma.service.spec.ts
|
|
63
|
+
|
|
64
|
+
15 | };
|
|
65
|
+
16 | Object.defineProperty(exports, "__esModule", { value: true });
|
|
66
|
+
> 17 | __exportStar(require("./dist"), exports);
|
|
67
|
+
| ^
|
|
68
|
+
18 |
|
|
69
|
+
|
|
70
|
+
at Resolver._throwModNotFoundError (../../node_modules/jest-resolve/build/resolver.js:427:11)
|
|
71
|
+
at Object.<anonymous> (../nestjs-common/index.js:17:14)
|
|
72
|
+
at Object.<anonymous> (lib/errors/prisma-error.mapper.ts:1:1)
|
|
73
|
+
at Object.<anonymous> (lib/prisma.service.ts:3:1)
|
|
74
|
+
at Object.<anonymous> (test/prisma.service.spec.ts:2:1)
|
|
75
|
+
|
|
76
|
+
FAIL test/lifecycle.spec.ts
|
|
77
|
+
● Test suite failed to run
|
|
78
|
+
|
|
79
|
+
Cannot find module './dist' from '../nestjs-common/index.js'
|
|
80
|
+
|
|
81
|
+
Require stack:
|
|
82
|
+
/home/ubuntu/subrina-coder/projects/nathapp-nestjs-core/repos/nathapp-nestjs/packages/nestjs-common/index.js
|
|
83
|
+
lib/errors/prisma-error.mapper.ts
|
|
84
|
+
lib/prisma.service.ts
|
|
85
|
+
test/lifecycle.spec.ts
|
|
86
|
+
|
|
87
|
+
15 | };
|
|
88
|
+
16 | Object.defineProperty(exports, "__esModule", { value: true });
|
|
89
|
+
> 17 | __exportStar(require("./dist"), exports);
|
|
90
|
+
| ^
|
|
91
|
+
18 |
|
|
92
|
+
|
|
93
|
+
at Resolver._throwModNotFoundError (../../node_modules/jest-resolve/build/resolver.js:427:11)
|
|
94
|
+
at Object.<anonymous> (../nestjs-common/index.js:17:14)
|
|
95
|
+
at Object.<anonymous> (lib/errors/prisma-error.mapper.ts:1:1)
|
|
96
|
+
at Object.<anonymous> (lib/prisma.service.ts:3:1)
|
|
97
|
+
at Object.<anonymous> (test/lifecycle.spec.ts:2:1)
|
|
98
|
+
|
|
99
|
+
FAIL test/pagination/paginate.spec.ts
|
|
100
|
+
● Test suite failed to run
|
|
101
|
+
|
|
102
|
+
Cannot find module './dist' from '../nestjs-common/index.js'
|
|
103
|
+
|
|
104
|
+
Require stack:
|
|
105
|
+
/home/ubuntu/subrina-coder/projects/nathapp-nestjs-core/repos/nathapp-nestjs/packages/nestjs-common/index.js
|
|
106
|
+
lib/pagination/paginate.ts
|
|
107
|
+
test/pagination/paginate.spec.ts
|
|
108
|
+
|
|
109
|
+
15 | };
|
|
110
|
+
16 | Object.defineProperty(exports, "__esModule", { value: true });
|
|
111
|
+
> 17 | __exportStar(require("./dist"), exports);
|
|
112
|
+
| ^
|
|
113
|
+
18 |
|
|
114
|
+
|
|
115
|
+
at Resolver._throwModNotFoundError (../../node_modules/jest-resolve/build/resolver.js:427:11)
|
|
116
|
+
at Object.<anonymous> (../nestjs-common/index.js:17:14)
|
|
117
|
+
at Object.<anonymous> (lib/pagination/paginate.ts:1:1)
|
|
118
|
+
at Object.<anonymous> (test/pagination/paginate.spec.ts:1:1)
|
|
119
|
+
|
|
120
|
+
FAIL test/prisma.decorator.spec.ts
|
|
121
|
+
● Test suite failed to run
|
|
122
|
+
|
|
123
|
+
Cannot find module './dist' from '../nestjs-common/index.js'
|
|
124
|
+
|
|
125
|
+
Require stack:
|
|
126
|
+
/home/ubuntu/subrina-coder/projects/nathapp-nestjs-core/repos/nathapp-nestjs/packages/nestjs-common/index.js
|
|
127
|
+
lib/errors/prisma-error.mapper.ts
|
|
128
|
+
lib/prisma.service.ts
|
|
129
|
+
lib/prisma.module.ts
|
|
130
|
+
test/prisma.decorator.spec.ts
|
|
131
|
+
|
|
132
|
+
15 | };
|
|
133
|
+
16 | Object.defineProperty(exports, "__esModule", { value: true });
|
|
134
|
+
> 17 | __exportStar(require("./dist"), exports);
|
|
135
|
+
| ^
|
|
136
|
+
18 |
|
|
137
|
+
|
|
138
|
+
at Resolver._throwModNotFoundError (../../node_modules/jest-resolve/build/resolver.js:427:11)
|
|
139
|
+
at Object.<anonymous> (../nestjs-common/index.js:17:14)
|
|
140
|
+
at Object.<anonymous> (lib/errors/prisma-error.mapper.ts:1:1)
|
|
141
|
+
at Object.<anonymous> (lib/prisma.service.ts:3:1)
|
|
142
|
+
at Object.<anonymous> (lib/prisma.module.ts:3:1)
|
|
143
|
+
at Object.<anonymous> (test/prisma.decorator.spec.ts:4:1)
|
|
144
|
+
|
|
145
|
+
FAIL test/prisma-exception-filter.spec.ts
|
|
146
|
+
● Test suite failed to run
|
|
147
|
+
|
|
148
|
+
Cannot find module './dist' from '../nestjs-common/index.js'
|
|
149
|
+
|
|
150
|
+
Require stack:
|
|
151
|
+
/home/ubuntu/subrina-coder/projects/nathapp-nestjs-core/repos/nathapp-nestjs/packages/nestjs-common/index.js
|
|
152
|
+
lib/errors/prisma-error.mapper.ts
|
|
153
|
+
lib/filter/prisma-exception.filter.ts
|
|
154
|
+
test/prisma-exception-filter.spec.ts
|
|
155
|
+
|
|
156
|
+
15 | };
|
|
157
|
+
16 | Object.defineProperty(exports, "__esModule", { value: true });
|
|
158
|
+
> 17 | __exportStar(require("./dist"), exports);
|
|
159
|
+
| ^
|
|
160
|
+
18 |
|
|
161
|
+
|
|
162
|
+
at Resolver._throwModNotFoundError (../../node_modules/jest-resolve/build/resolver.js:427:11)
|
|
163
|
+
at Object.<anonymous> (../nestjs-common/index.js:17:14)
|
|
164
|
+
at Object.<anonymous> (lib/errors/prisma-error.mapper.ts:1:1)
|
|
165
|
+
at Object.<anonymous> (lib/filter/prisma-exception.filter.ts:6:1)
|
|
166
|
+
at Object.<anonymous> (test/prisma-exception-filter.spec.ts:6:1)
|
|
167
|
+
|
|
168
|
+
FAIL test/error-mapping.spec.ts
|
|
169
|
+
● Test suite failed to run
|
|
170
|
+
|
|
171
|
+
Cannot find module './dist' from '../nestjs-common/index.js'
|
|
172
|
+
|
|
173
|
+
Require stack:
|
|
174
|
+
/home/ubuntu/subrina-coder/projects/nathapp-nestjs-core/repos/nathapp-nestjs/packages/nestjs-common/index.js
|
|
175
|
+
test/error-mapping.spec.ts
|
|
176
|
+
|
|
177
|
+
15 | };
|
|
178
|
+
16 | Object.defineProperty(exports, "__esModule", { value: true });
|
|
179
|
+
> 17 | __exportStar(require("./dist"), exports);
|
|
180
|
+
| ^
|
|
181
|
+
18 |
|
|
182
|
+
|
|
183
|
+
at Resolver._throwModNotFoundError (../../node_modules/jest-resolve/build/resolver.js:427:11)
|
|
184
|
+
at Object.<anonymous> (../nestjs-common/index.js:17:14)
|
|
185
|
+
at Object.<anonymous> (test/error-mapping.spec.ts:1:1)
|
|
186
|
+
|
|
187
|
+
PASS test/prisma-transaction-manager.spec.ts
|
|
188
|
+
PASS test/prisma.health.spec.ts
|
|
189
|
+
|
|
190
|
+
Test Suites: 8 failed, 2 passed, 10 total
|
|
191
|
+
Tests: 37 passed, 37 total
|
|
34
192
|
Snapshots: 0 total
|
|
35
|
-
Time:
|
|
193
|
+
Time: 5.12 s
|
|
36
194
|
Ran all test suites.
|
|
37
195
|
npm error Lifecycle script `test` failed with error:
|
|
38
196
|
npm error code 1
|
|
39
|
-
npm error path /
|
|
40
|
-
npm error workspace @nathapp/nestjs-prisma@3.0.
|
|
41
|
-
npm error location /
|
|
197
|
+
npm error path /home/ubuntu/subrina-coder/projects/nathapp-nestjs-core/repos/nathapp-nestjs/packages/nestjs-prisma
|
|
198
|
+
npm error workspace @nathapp/nestjs-prisma@3.0.1
|
|
199
|
+
npm error location /home/ubuntu/subrina-coder/projects/nathapp-nestjs-core/repos/nathapp-nestjs/packages/nestjs-prisma
|
|
42
200
|
npm error command failed
|
|
43
201
|
npm error command sh -c jest --config ./test/jest-test.json
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
1
1
|
# @nathapp/nestjs-prisma
|
|
2
2
|
|
|
3
|
+
## 3.1.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- feat: add transparent transaction support via `transaction: true` option
|
|
8
|
+
|
|
9
|
+
**nestjs-prisma:**
|
|
10
|
+
|
|
11
|
+
- `PrismaModule.forRoot({ transaction: true })` wraps the injected client in a transparent proxy
|
|
12
|
+
- Repos automatically use the transaction client inside `txManager.run()` — no code changes needed
|
|
13
|
+
- Provides `TRANSACTION_MANAGER` token from `@nathapp/nestjs-data`
|
|
14
|
+
- `PrismaTransactionManager` accepts optional shared AsyncLocalStorage for proxy integration
|
|
15
|
+
|
|
16
|
+
**nestjs-typeorm:**
|
|
17
|
+
|
|
18
|
+
- `TypeOrmExModule.forRoot({ transaction: true })` provides `TRANSACTION_MANAGER` token
|
|
19
|
+
- TypeORM repos use explicit injection pattern (`@Optional() @Inject(TRANSACTION_MANAGER)`)
|
|
20
|
+
- Transparent proxy not feasible for TypeORM (repos hold manager references at creation time)
|
|
21
|
+
- `TypeOrmTransactionManager` accepts optional shared AsyncLocalStorage
|
|
22
|
+
|
|
3
23
|
## 3.0.1
|
|
4
24
|
|
|
5
25
|
### Patch Changes
|
package/dist/prisma.module.js
CHANGED
|
@@ -9,23 +9,71 @@ var PrismaModule_1;
|
|
|
9
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
10
|
exports.PrismaModule = void 0;
|
|
11
11
|
const common_1 = require("@nestjs/common");
|
|
12
|
+
const async_hooks_1 = require("async_hooks");
|
|
13
|
+
const nestjs_data_1 = require("@nathapp/nestjs-data");
|
|
12
14
|
const prisma_service_1 = require("./prisma.service");
|
|
15
|
+
const prisma_transaction_manager_1 = require("./transaction/prisma-transaction-manager");
|
|
13
16
|
const prisma_constants_1 = require("./prisma.constants");
|
|
17
|
+
const prisma_transaction_constants_1 = require("./transaction/prisma-transaction.constants");
|
|
18
|
+
function patchWithTransactionProxy(prismaService, als) {
|
|
19
|
+
const originalClient = prismaService.client;
|
|
20
|
+
const proxy = new Proxy(originalClient, {
|
|
21
|
+
get(target, prop, receiver) {
|
|
22
|
+
const txClient = als.getStore();
|
|
23
|
+
if (txClient)
|
|
24
|
+
return Reflect.get(txClient, prop, receiver);
|
|
25
|
+
return Reflect.get(target, prop, receiver);
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
Object.defineProperty(prismaService, 'client', {
|
|
29
|
+
get: () => proxy,
|
|
30
|
+
configurable: true,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
14
33
|
let PrismaModule = PrismaModule_1 = class PrismaModule {
|
|
15
34
|
static forRoot(options) {
|
|
16
|
-
const { isGlobal = true, name } = options;
|
|
35
|
+
const { isGlobal = true, name, transaction } = options;
|
|
17
36
|
const connectionToken = (0, prisma_constants_1.getPrismaToken)(name);
|
|
37
|
+
const providers = [];
|
|
38
|
+
let als;
|
|
39
|
+
if (transaction) {
|
|
40
|
+
als = new async_hooks_1.AsyncLocalStorage();
|
|
41
|
+
providers.push({
|
|
42
|
+
provide: prisma_transaction_constants_1.PRISMA_TRANSACTION_ALS,
|
|
43
|
+
useValue: als,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
const capturedAls = als;
|
|
47
|
+
let originalClient;
|
|
18
48
|
const prismaServiceProvider = {
|
|
19
49
|
provide: connectionToken,
|
|
20
|
-
useFactory: () =>
|
|
50
|
+
useFactory: () => {
|
|
51
|
+
const service = new prisma_service_1.PrismaService(options);
|
|
52
|
+
if (capturedAls) {
|
|
53
|
+
originalClient = service.client;
|
|
54
|
+
patchWithTransactionProxy(service, capturedAls);
|
|
55
|
+
}
|
|
56
|
+
return service;
|
|
57
|
+
},
|
|
21
58
|
};
|
|
22
|
-
|
|
59
|
+
providers.push(prismaServiceProvider);
|
|
23
60
|
if (!name || name === 'default') {
|
|
24
61
|
providers.push({
|
|
25
62
|
provide: prisma_service_1.PrismaService,
|
|
26
63
|
useExisting: connectionToken,
|
|
27
64
|
});
|
|
28
65
|
}
|
|
66
|
+
if (transaction && capturedAls) {
|
|
67
|
+
providers.push({
|
|
68
|
+
provide: prisma_transaction_manager_1.PrismaTransactionManager,
|
|
69
|
+
useFactory: () => new prisma_transaction_manager_1.PrismaTransactionManager(originalClient, capturedAls),
|
|
70
|
+
inject: [connectionToken],
|
|
71
|
+
});
|
|
72
|
+
providers.push({
|
|
73
|
+
provide: nestjs_data_1.TRANSACTION_MANAGER,
|
|
74
|
+
useExisting: prisma_transaction_manager_1.PrismaTransactionManager,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
29
77
|
return {
|
|
30
78
|
module: PrismaModule_1,
|
|
31
79
|
global: isGlobal,
|
|
@@ -62,7 +110,15 @@ let PrismaModule = PrismaModule_1 = class PrismaModule {
|
|
|
62
110
|
},
|
|
63
111
|
{
|
|
64
112
|
provide: connectionToken,
|
|
65
|
-
useFactory: (moduleOptions) =>
|
|
113
|
+
useFactory: (moduleOptions) => {
|
|
114
|
+
const service = new prisma_service_1.PrismaService(moduleOptions);
|
|
115
|
+
if (moduleOptions.transaction) {
|
|
116
|
+
const als = new async_hooks_1.AsyncLocalStorage();
|
|
117
|
+
patchWithTransactionProxy(service, als);
|
|
118
|
+
service.__transactionAls = als;
|
|
119
|
+
}
|
|
120
|
+
return service;
|
|
121
|
+
},
|
|
66
122
|
inject: [prisma_constants_1.PRISMA_MODULE_OPTIONS],
|
|
67
123
|
},
|
|
68
124
|
];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prisma.module.js","sourceRoot":"","sources":["../lib/prisma.module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAAiE;
|
|
1
|
+
{"version":3,"file":"prisma.module.js","sourceRoot":"","sources":["../lib/prisma.module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAAiE;AACjE,6CAAgD;AAChD,sDAA2D;AAE3D,qDAAiD;AACjD,yFAAoF;AACpF,yDAA2E;AAC3E,6FAAoF;AAMpF,SAAS,yBAAyB,CAChC,aAA+B,EAC/B,GAAwC;IAExC,MAAM,cAAc,GAAG,aAAa,CAAC,MAAM,CAAC;IAC5C,MAAM,KAAK,GAAG,IAAI,KAAK,CAAC,cAAc,EAAE;QACtC,GAAG,CAAC,MAAM,EAAE,IAAI,EAAE,QAAQ;YACxB,MAAM,QAAQ,GAAG,GAAG,CAAC,QAAQ,EAAE,CAAC;YAChC,IAAI,QAAQ;gBAAE,OAAO,OAAO,CAAC,GAAG,CAAC,QAAQ,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC;YAC3D,OAAO,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC;QAC7C,CAAC;KACF,CAAC,CAAC;IACH,MAAM,CAAC,cAAc,CAAC,aAAa,EAAE,QAAQ,EAAE;QAC7C,GAAG,EAAE,GAAG,EAAE,CAAC,KAAK;QAChB,YAAY,EAAE,IAAI;KACn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BACxB,UAAU,EAAE,CAAC,aAAkC,EAAE,EAAE,CAAC,IAAI,8BAAa,CAAC,aAAa,CAAC;oBACpF,MAAM,EAAE,CAAC,wCAAqB,CAAC;iBAChC;aACF,CAAC;QACJ,CAAC;QAED,MAAM,IAAI,KAAK,CAAC,qFAAqF,CAAC,CAAC;IACzG,CAAC;CACF,CAAA;AAxJY,oCAAY;uBAAZ,YAAY;IADxB,IAAA,eAAM,EAAC,EAAE,CAAC;GACE,YAAY,CAwJxB"}
|
|
@@ -15,4 +15,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./prisma-transaction-manager"), exports);
|
|
18
|
+
__exportStar(require("./prisma-transaction.constants"), exports);
|
|
18
19
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/transaction/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+DAA6C"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/transaction/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+DAA6C;AAC7C,iEAA+C"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'async_hooks';
|
|
1
2
|
import { ITransactionManager } from '@nathapp/nestjs-data';
|
|
2
3
|
import { PrismaClientLike } from '../prisma.interfaces';
|
|
3
4
|
export declare class PrismaTransactionManager implements ITransactionManager {
|
|
@@ -5,7 +6,7 @@ export declare class PrismaTransactionManager implements ITransactionManager {
|
|
|
5
6
|
private readonly als;
|
|
6
7
|
constructor(prisma: PrismaClientLike & {
|
|
7
8
|
$transaction: Function;
|
|
8
|
-
});
|
|
9
|
+
}, externalAls?: AsyncLocalStorage<PrismaClientLike>);
|
|
9
10
|
run<T>(fn: () => Promise<T>): Promise<T>;
|
|
10
11
|
getClient<C = unknown>(): C;
|
|
11
12
|
isInTransaction(): boolean;
|
|
@@ -16,10 +16,11 @@ exports.PrismaTransactionManager = void 0;
|
|
|
16
16
|
const common_1 = require("@nestjs/common");
|
|
17
17
|
const async_hooks_1 = require("async_hooks");
|
|
18
18
|
const prisma_constants_1 = require("../prisma.constants");
|
|
19
|
+
const prisma_transaction_constants_1 = require("./prisma-transaction.constants");
|
|
19
20
|
let PrismaTransactionManager = class PrismaTransactionManager {
|
|
20
|
-
constructor(prisma) {
|
|
21
|
+
constructor(prisma, externalAls) {
|
|
21
22
|
this.prisma = prisma;
|
|
22
|
-
this.als = new async_hooks_1.AsyncLocalStorage();
|
|
23
|
+
this.als = externalAls !== null && externalAls !== void 0 ? externalAls : new async_hooks_1.AsyncLocalStorage();
|
|
23
24
|
}
|
|
24
25
|
async run(fn) {
|
|
25
26
|
if (this.als.getStore()) {
|
|
@@ -41,6 +42,8 @@ exports.PrismaTransactionManager = PrismaTransactionManager;
|
|
|
41
42
|
exports.PrismaTransactionManager = PrismaTransactionManager = __decorate([
|
|
42
43
|
(0, common_1.Injectable)(),
|
|
43
44
|
__param(0, (0, common_1.Inject)((0, prisma_constants_1.getPrismaToken)())),
|
|
44
|
-
|
|
45
|
+
__param(1, (0, common_1.Optional)()),
|
|
46
|
+
__param(1, (0, common_1.Inject)(prisma_transaction_constants_1.PRISMA_TRANSACTION_ALS)),
|
|
47
|
+
__metadata("design:paramtypes", [Object, async_hooks_1.AsyncLocalStorage])
|
|
45
48
|
], PrismaTransactionManager);
|
|
46
49
|
//# sourceMappingURL=prisma-transaction-manager.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prisma-transaction-manager.js","sourceRoot":"","sources":["../../lib/transaction/prisma-transaction-manager.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"prisma-transaction-manager.js","sourceRoot":"","sources":["../../lib/transaction/prisma-transaction-manager.ts"],"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Token = exports.PRISMA_TRANSACTION_ALS = void 0;
|
|
4
|
+
exports.PRISMA_TRANSACTION_ALS = Symbol('PRISMA_TRANSACTION_ALS');
|
|
5
|
+
const getPrismaTransactionAlsToken = (name) => name && name !== 'default'
|
|
6
|
+
? `PRISMA_TRANSACTION_ALS_${name}`
|
|
7
|
+
: exports.PRISMA_TRANSACTION_ALS;
|
|
8
|
+
exports.getPrismaTransactionAlsToken = getPrismaTransactionAlsToken;
|
|
9
|
+
//# sourceMappingURL=prisma-transaction.constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prisma-transaction.constants.js","sourceRoot":"","sources":["../../lib/transaction/prisma-transaction.constants.ts"],"names":[],"mappings":";;;AAIa,QAAA,sBAAsB,GAAG,MAAM,CAAC,wBAAwB,CAAC,CAAC;AAKhE,MAAM,4BAA4B,GAAG,CAAC,IAAa,EAAmB,EAAE,CAC7E,IAAI,IAAI,IAAI,KAAK,SAAS;IACxB,CAAC,CAAC,0BAA0B,IAAI,EAAE;IAClC,CAAC,CAAC,8BAA6B,CAAC;AAHvB,QAAA,4BAA4B,gCAGL"}
|
package/lib/prisma.interfaces.ts
CHANGED
|
@@ -25,6 +25,15 @@ export interface PrismaModuleOptions {
|
|
|
25
25
|
|
|
26
26
|
/** Enable logging of Prisma queries. Default: false */
|
|
27
27
|
logging?: boolean;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Enable transparent transaction support.
|
|
31
|
+
* When true, the injected Prisma client is wrapped in a proxy that
|
|
32
|
+
* automatically resolves to the transaction client inside `txManager.run()`.
|
|
33
|
+
* Also provides TRANSACTION_MANAGER token.
|
|
34
|
+
* Default: false
|
|
35
|
+
*/
|
|
36
|
+
transaction?: boolean;
|
|
28
37
|
}
|
|
29
38
|
|
|
30
39
|
export interface PrismaModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
|
package/lib/prisma.module.ts
CHANGED
|
@@ -1,20 +1,67 @@
|
|
|
1
1
|
import { DynamicModule, Module, Provider } from '@nestjs/common';
|
|
2
|
-
import
|
|
2
|
+
import { AsyncLocalStorage } from 'async_hooks';
|
|
3
|
+
import { TRANSACTION_MANAGER } from '@nathapp/nestjs-data';
|
|
4
|
+
import type { PrismaModuleOptions, PrismaModuleAsyncOptions, PrismaOptionsFactory, PrismaClientLike } from './prisma.interfaces';
|
|
3
5
|
import { PrismaService } from './prisma.service';
|
|
6
|
+
import { PrismaTransactionManager } from './transaction/prisma-transaction-manager';
|
|
4
7
|
import { getPrismaToken, PRISMA_MODULE_OPTIONS } from './prisma.constants';
|
|
8
|
+
import { PRISMA_TRANSACTION_ALS } from './transaction/prisma-transaction.constants';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Patch a PrismaService's client getter to return a transparent proxy
|
|
12
|
+
* that delegates to the transaction client when inside a transaction.
|
|
13
|
+
*/
|
|
14
|
+
function patchWithTransactionProxy<T extends PrismaClientLike>(
|
|
15
|
+
prismaService: PrismaService<T>,
|
|
16
|
+
als: AsyncLocalStorage<PrismaClientLike>,
|
|
17
|
+
): void {
|
|
18
|
+
const originalClient = prismaService.client;
|
|
19
|
+
const proxy = new Proxy(originalClient, {
|
|
20
|
+
get(target, prop, receiver) {
|
|
21
|
+
const txClient = als.getStore();
|
|
22
|
+
if (txClient) return Reflect.get(txClient, prop, receiver);
|
|
23
|
+
return Reflect.get(target, prop, receiver);
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
Object.defineProperty(prismaService, 'client', {
|
|
27
|
+
get: () => proxy,
|
|
28
|
+
configurable: true,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
5
31
|
|
|
6
32
|
@Module({})
|
|
7
33
|
export class PrismaModule {
|
|
8
34
|
static forRoot(options: PrismaModuleOptions): DynamicModule {
|
|
9
|
-
const { isGlobal = true, name } = options;
|
|
35
|
+
const { isGlobal = true, name, transaction } = options;
|
|
10
36
|
const connectionToken = getPrismaToken(name);
|
|
11
37
|
|
|
38
|
+
const providers: Provider[] = [];
|
|
39
|
+
let als: AsyncLocalStorage<PrismaClientLike> | undefined;
|
|
40
|
+
|
|
41
|
+
if (transaction) {
|
|
42
|
+
als = new AsyncLocalStorage<PrismaClientLike>();
|
|
43
|
+
|
|
44
|
+
providers.push({
|
|
45
|
+
provide: PRISMA_TRANSACTION_ALS,
|
|
46
|
+
useValue: als,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const capturedAls = als;
|
|
51
|
+
// Store reference to original (unproxied) client for transaction manager
|
|
52
|
+
let originalClient: PrismaClientLike | undefined;
|
|
12
53
|
const prismaServiceProvider: Provider = {
|
|
13
54
|
provide: connectionToken,
|
|
14
|
-
useFactory: () =>
|
|
55
|
+
useFactory: () => {
|
|
56
|
+
const service = new PrismaService(options);
|
|
57
|
+
if (capturedAls) {
|
|
58
|
+
originalClient = service.client;
|
|
59
|
+
patchWithTransactionProxy(service, capturedAls);
|
|
60
|
+
}
|
|
61
|
+
return service;
|
|
62
|
+
},
|
|
15
63
|
};
|
|
16
|
-
|
|
17
|
-
const providers: Provider[] = [prismaServiceProvider];
|
|
64
|
+
providers.push(prismaServiceProvider);
|
|
18
65
|
|
|
19
66
|
if (!name || name === 'default') {
|
|
20
67
|
providers.push({
|
|
@@ -23,6 +70,20 @@ export class PrismaModule {
|
|
|
23
70
|
});
|
|
24
71
|
}
|
|
25
72
|
|
|
73
|
+
if (transaction && capturedAls) {
|
|
74
|
+
providers.push({
|
|
75
|
+
provide: PrismaTransactionManager,
|
|
76
|
+
useFactory: () =>
|
|
77
|
+
new PrismaTransactionManager(originalClient as any, capturedAls),
|
|
78
|
+
inject: [connectionToken], // ensure PrismaService is created first
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
providers.push({
|
|
82
|
+
provide: TRANSACTION_MANAGER,
|
|
83
|
+
useExisting: PrismaTransactionManager,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
26
87
|
return {
|
|
27
88
|
module: PrismaModule,
|
|
28
89
|
global: isGlobal,
|
|
@@ -68,7 +129,16 @@ export class PrismaModule {
|
|
|
68
129
|
},
|
|
69
130
|
{
|
|
70
131
|
provide: connectionToken,
|
|
71
|
-
useFactory: (moduleOptions: PrismaModuleOptions) =>
|
|
132
|
+
useFactory: (moduleOptions: PrismaModuleOptions) => {
|
|
133
|
+
const service = new PrismaService(moduleOptions);
|
|
134
|
+
if (moduleOptions.transaction) {
|
|
135
|
+
const als = new AsyncLocalStorage<PrismaClientLike>();
|
|
136
|
+
patchWithTransactionProxy(service, als);
|
|
137
|
+
// Store for potential transaction manager setup
|
|
138
|
+
(service as any).__transactionAls = als;
|
|
139
|
+
}
|
|
140
|
+
return service;
|
|
141
|
+
},
|
|
72
142
|
inject: [PRISMA_MODULE_OPTIONS],
|
|
73
143
|
},
|
|
74
144
|
];
|
package/lib/transaction/index.ts
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
|
-
import { Injectable, Inject } from '@nestjs/common';
|
|
1
|
+
import { Injectable, Inject, Optional } from '@nestjs/common';
|
|
2
2
|
import { AsyncLocalStorage } from 'async_hooks';
|
|
3
3
|
import { ITransactionManager } from '@nathapp/nestjs-data';
|
|
4
4
|
import { PrismaClientLike } from '../prisma.interfaces';
|
|
5
5
|
import { getPrismaToken } from '../prisma.constants';
|
|
6
|
+
import { PRISMA_TRANSACTION_ALS } from './prisma-transaction.constants';
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* Prisma implementation of ITransactionManager using AsyncLocalStorage.
|
|
9
10
|
* Provides automatic transaction management with support for nested transactions.
|
|
10
11
|
*
|
|
12
|
+
* Can accept an external AsyncLocalStorage instance (via PRISMA_TRANSACTION_ALS token)
|
|
13
|
+
* to share context with the transparent proxy in PrismaModule.
|
|
14
|
+
*
|
|
11
15
|
* @example
|
|
12
16
|
* ```typescript
|
|
13
17
|
* @Injectable()
|
|
@@ -31,11 +35,14 @@ import { getPrismaToken } from '../prisma.constants';
|
|
|
31
35
|
*/
|
|
32
36
|
@Injectable()
|
|
33
37
|
export class PrismaTransactionManager implements ITransactionManager {
|
|
34
|
-
private readonly als
|
|
38
|
+
private readonly als: AsyncLocalStorage<PrismaClientLike>;
|
|
35
39
|
|
|
36
40
|
constructor(
|
|
37
41
|
@Inject(getPrismaToken()) private readonly prisma: PrismaClientLike & { $transaction: Function },
|
|
38
|
-
|
|
42
|
+
@Optional() @Inject(PRISMA_TRANSACTION_ALS) externalAls?: AsyncLocalStorage<PrismaClientLike>,
|
|
43
|
+
) {
|
|
44
|
+
this.als = externalAls ?? new AsyncLocalStorage<PrismaClientLike>();
|
|
45
|
+
}
|
|
39
46
|
|
|
40
47
|
/**
|
|
41
48
|
* Execute fn within a transaction.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Injection token for the shared AsyncLocalStorage instance
|
|
3
|
+
* used by both the transparent proxy and PrismaTransactionManager.
|
|
4
|
+
*/
|
|
5
|
+
export const PRISMA_TRANSACTION_ALS = Symbol('PRISMA_TRANSACTION_ALS');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Get a named ALS token for multi-database setups.
|
|
9
|
+
*/
|
|
10
|
+
export const getPrismaTransactionAlsToken = (name?: string): symbol | string =>
|
|
11
|
+
name && name !== 'default'
|
|
12
|
+
? `PRISMA_TRANSACTION_ALS_${name}`
|
|
13
|
+
: PRISMA_TRANSACTION_ALS as any;
|
package/package.json
CHANGED
|
@@ -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
|
+
});
|