@nathapp/nestjs-prisma 4.3.1 → 4.3.2
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/README.md
CHANGED
|
@@ -211,6 +211,44 @@ Only opt in if the entire callback is safe to repeat. Keep writes (including
|
|
|
211
211
|
outbox records) on the same transaction client, re-read decision inputs on each
|
|
212
212
|
attempt, and emit events or perform external I/O after the outer `run` resolves.
|
|
213
213
|
|
|
214
|
+
### Independent transactions (`requiresNew`)
|
|
215
|
+
|
|
216
|
+
By default `run` joins an active transaction. `await tx.run(fn, { propagation: 'requiresNew' })`
|
|
217
|
+
instead always starts a new transaction on the root client. It is the active
|
|
218
|
+
transaction only inside `fn`; the outer transaction resumes afterwards, and a plain
|
|
219
|
+
`run` nested in `fn` joins the new one. Use it for writes that must persist even if
|
|
220
|
+
the caller later rolls back (security revocations, audit records, lockout counters):
|
|
221
|
+
|
|
222
|
+
- The inner commit survives an outer rollback.
|
|
223
|
+
- An error thrown inside it rolls back only the inner transaction. The outer
|
|
224
|
+
transaction is not poisoned (SEC-16 applies to joined runs only), and the error
|
|
225
|
+
still reaches the caller of `run`.
|
|
226
|
+
- `maxWait`, `timeout`, `isolationLevel` and `retryOnWriteConflict` apply to the new
|
|
227
|
+
transaction; a retry replays only the inner callback.
|
|
228
|
+
- An outer run with `retryOnWriteConflict` replays its callback, so each attempt
|
|
229
|
+
commits a new `requiresNew` transaction. Inner commits do not roll back, so the
|
|
230
|
+
inner work must be safe to repeat.
|
|
231
|
+
- An unknown `propagation` value (for example `'requires_new'`) throws instead of
|
|
232
|
+
silently joining.
|
|
233
|
+
|
|
234
|
+
Caveats:
|
|
235
|
+
|
|
236
|
+
- It needs a second pooled connection while the outer transaction still holds one.
|
|
237
|
+
On single-connection setups, for example the Prisma 7 SQLite adapter, which
|
|
238
|
+
serializes transactions, it deadlocks and is unsupported. The manager throws a
|
|
239
|
+
clear error when it detects a SQLite adapter and `requiresNew` runs inside an
|
|
240
|
+
active transaction; other single-connection adapters cannot be detected, so make
|
|
241
|
+
sure the pool has at least two connections.
|
|
242
|
+
- Each concurrent outer transaction using `requiresNew` holds two connections. Size
|
|
243
|
+
the pool for at least 2x the peak number of concurrent outer transactions; when
|
|
244
|
+
it is exhausted, Prisma fails after `maxWait` (default 2000 ms).
|
|
245
|
+
- If the outer transaction holds row locks on rows the inner one writes, the inner
|
|
246
|
+
transaction waits on its own caller until the lock timeout. Use it only for rows
|
|
247
|
+
the outer transaction has not locked.
|
|
248
|
+
|
|
249
|
+
The real-database proof is opt-in and needs Docker: `npm run test:db` starts
|
|
250
|
+
`postgres:16` and `mysql:8.4` with testcontainers. It is not part of `npm test`.
|
|
251
|
+
|
|
214
252
|
## Multi-DB and transactions
|
|
215
253
|
|
|
216
254
|
For multiple databases in one process, call `PrismaModule.forRoot` once per
|
|
@@ -24,6 +24,11 @@ function isWriteConflict(error) {
|
|
|
24
24
|
return error instanceof Error && 'code' in error && error.code === 'P2034' &&
|
|
25
25
|
Object.prototype.toString.call(error) === '[object PrismaClientKnownRequestError]';
|
|
26
26
|
}
|
|
27
|
+
function usesSingleConnectionAdapter(client) {
|
|
28
|
+
var _a;
|
|
29
|
+
const adapter = (_a = client._engineConfig) === null || _a === void 0 ? void 0 : _a.adapter;
|
|
30
|
+
return (adapter === null || adapter === void 0 ? void 0 : adapter.provider) === 'sqlite';
|
|
31
|
+
}
|
|
27
32
|
const toPrismaTxOptions = (o) => ({
|
|
28
33
|
maxWait: o.maxWait,
|
|
29
34
|
timeout: o.timeout,
|
|
@@ -35,8 +40,17 @@ let PrismaTransactionManager = class PrismaTransactionManager {
|
|
|
35
40
|
this.als = externalAls !== null && externalAls !== void 0 ? externalAls : new async_hooks_1.AsyncLocalStorage();
|
|
36
41
|
}
|
|
37
42
|
async run(fn, options) {
|
|
43
|
+
const propagation = options === null || options === void 0 ? void 0 : options.propagation;
|
|
44
|
+
if (propagation !== undefined && propagation !== 'required' && propagation !== 'requiresNew') {
|
|
45
|
+
throw new Error(`Unknown transaction propagation ${JSON.stringify(propagation)}: expected 'required' or 'requiresNew'`);
|
|
46
|
+
}
|
|
38
47
|
const current = this.als.getStore();
|
|
39
|
-
|
|
48
|
+
const requiresNew = (options === null || options === void 0 ? void 0 : options.propagation) === 'requiresNew';
|
|
49
|
+
if (current && requiresNew && usesSingleConnectionAdapter(this.prisma)) {
|
|
50
|
+
throw new Error("propagation 'requiresNew' inside an active transaction is not supported on single-connection " +
|
|
51
|
+
'adapters (for example SQLite): it needs a second connection and would deadlock');
|
|
52
|
+
}
|
|
53
|
+
if (current && !requiresNew) {
|
|
40
54
|
try {
|
|
41
55
|
return await fn();
|
|
42
56
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prisma-transaction-manager.js","sourceRoot":"","sources":["../../lib/transaction/prisma-transaction-manager.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAA8D;AAC9D,6CAAgD;AAGhD,0DAAqD;AACrD,iFAAwE;AACxE,uDAA8D;AAE9D,mDAA2D;AAE3D,MAAM,2BAA2B,GAAG,CAAC,CAAC;AAItC,SAAS,eAAe,CAAC,KAAc;IACrC,OAAO,KAAK,YAAY,KAAK,IAAI,MAAM,IAAI,KAAK,IAAI,KAAK,CAAC,IAAI,KAAK,OAAO;QACxE,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,wCAAwC,CAAC;AACvF,CAAC;AAED,MAAM,iBAAiB,GAAG,CAAC,CAAwB,EAAE,EAAE,CAAC,CAAC;IACvD,OAAO,EAAE,CAAC,CAAC,OAAO;IAClB,OAAO,EAAE,CAAC,CAAC,OAAO;IAClB,cAAc,EAAE,CAAC,CAAC,cAAuB;CAC1C,CAAC,CAAC;AA+BI,IAAM,wBAAwB,GAA9B,MAAM,wBAAwB;IAGnC,YAC6C,MAE1C,EAC2C,WAAiD;QAHlD,WAAM,GAAN,MAAM,CAEhD;QAGD,IAAI,CAAC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+
"version": "4.3.2",
|
|
4
4
|
"description": "NestJS Prisma integration with multi-database support",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"clean": "rimraf dist/ .turbo/",
|
|
7
7
|
"build": "rimraf dist && tsc -p tsconfig.json",
|
|
8
8
|
"test": "jest --config ./test/jest-test.json",
|
|
9
9
|
"test:cov": "jest --config ./test/jest-test.json --coverage",
|
|
10
|
+
"test:db": "jest --config ./test/jest-db.json --runInBand --forceExit",
|
|
10
11
|
"type-check": "tsc --noEmit -p tsconfig.json",
|
|
11
12
|
"lint": "eslint -c .eslintrc.js \"lib/**/*.ts\" \"test/**/*.ts\"",
|
|
12
13
|
"lint:fix": "eslint -c .eslintrc.js --fix \"lib/**/*.ts\" \"test/**/*.ts\"",
|
|
@@ -20,16 +21,19 @@
|
|
|
20
21
|
"types": "dist/index.d.ts",
|
|
21
22
|
"main": "dist/index.js",
|
|
22
23
|
"dependencies": {
|
|
23
|
-
"@nathapp/nestjs-common": "^4.3.
|
|
24
|
-
"@nathapp/nestjs-data": "^4.3.
|
|
24
|
+
"@nathapp/nestjs-common": "^4.3.2",
|
|
25
|
+
"@nathapp/nestjs-data": "^4.3.2"
|
|
25
26
|
},
|
|
26
27
|
"devDependencies": {
|
|
27
28
|
"@nathapp/eslint-config": "1.0.1",
|
|
28
29
|
"@nathapp/typescript-config": "1.0.1",
|
|
29
30
|
"@golevelup/ts-jest": "^3.0.0",
|
|
31
|
+
"@prisma/adapter-mariadb": "7.10.0",
|
|
32
|
+
"@prisma/adapter-pg": "7.10.0",
|
|
30
33
|
"@prisma/client": "^7.10.0",
|
|
31
34
|
"eslint": "^8.57.0",
|
|
32
35
|
"prisma": "^7.10.0",
|
|
36
|
+
"testcontainers": "12.0.3",
|
|
33
37
|
"typescript": "^5"
|
|
34
38
|
},
|
|
35
39
|
"peerDependencies": {
|