@nathapp/nestjs-prisma 4.2.4 → 4.3.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/README.md
CHANGED
|
@@ -198,6 +198,19 @@ await prismaService.withTransaction(async (tx) => {
|
|
|
198
198
|
Both `forRoot` and `forRootAsync` provide the same context to the service and
|
|
199
199
|
transaction manager for that connection.
|
|
200
200
|
|
|
201
|
+
### Retrying a rolled-back write conflict
|
|
202
|
+
|
|
203
|
+
`await tx.run(fn, { retryOnWriteConflict: true })` lets the outer transaction
|
|
204
|
+
owner replay `fn` on Prisma `P2034`. Each attempt starts a fresh transaction;
|
|
205
|
+
`maxWait`, `timeout` and `isolationLevel` apply per attempt. The default is one
|
|
206
|
+
attempt. Opting in allows three total, with a short jittered delay between them.
|
|
207
|
+
A nested call cannot enable retries. Duplicate keys, timeouts and failures with
|
|
208
|
+
an unknown commit outcome are not retried.
|
|
209
|
+
|
|
210
|
+
Only opt in if the entire callback is safe to repeat. Keep writes (including
|
|
211
|
+
outbox records) on the same transaction client, re-read decision inputs on each
|
|
212
|
+
attempt, and emit events or perform external I/O after the outer `run` resolves.
|
|
213
|
+
|
|
201
214
|
## Multi-DB and transactions
|
|
202
215
|
|
|
203
216
|
For multiple databases in one process, call `PrismaModule.forRoot` once per
|
|
@@ -18,6 +18,12 @@ const async_hooks_1 = require("async_hooks");
|
|
|
18
18
|
const prisma_constants_1 = require("../prisma.constants");
|
|
19
19
|
const prisma_transaction_constants_1 = require("./prisma-transaction.constants");
|
|
20
20
|
const prisma_tx_state_1 = require("./prisma-tx-state");
|
|
21
|
+
const promises_1 = require("node:timers/promises");
|
|
22
|
+
const MAX_WRITE_CONFLICT_ATTEMPTS = 3;
|
|
23
|
+
function isWriteConflict(error) {
|
|
24
|
+
return error instanceof Error && 'code' in error && error.code === 'P2034' &&
|
|
25
|
+
Object.prototype.toString.call(error) === '[object PrismaClientKnownRequestError]';
|
|
26
|
+
}
|
|
21
27
|
const toPrismaTxOptions = (o) => ({
|
|
22
28
|
maxWait: o.maxWait,
|
|
23
29
|
timeout: o.timeout,
|
|
@@ -39,14 +45,26 @@ let PrismaTransactionManager = class PrismaTransactionManager {
|
|
|
39
45
|
throw err;
|
|
40
46
|
}
|
|
41
47
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
48
|
+
const prismaOptions = options && toPrismaTxOptions(options);
|
|
49
|
+
const maxAttempts = (options === null || options === void 0 ? void 0 : options.retryOnWriteConflict) === true ? MAX_WRITE_CONFLICT_ATTEMPTS : 1;
|
|
50
|
+
for (let attempt = 1;; attempt++) {
|
|
51
|
+
try {
|
|
52
|
+
return await this.prisma.$transaction(async (tx) => {
|
|
53
|
+
const state = (0, prisma_tx_state_1.registerTxState)(tx);
|
|
54
|
+
const result = await this.als.run(tx, fn);
|
|
55
|
+
if (state.poisoned) {
|
|
56
|
+
throw new Error('Nested transaction failure: the outer transaction was rolled back because an inner run() threw (SEC-16)');
|
|
57
|
+
}
|
|
58
|
+
return result;
|
|
59
|
+
}, prismaOptions);
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
if (attempt >= maxAttempts || !isWriteConflict(error)) {
|
|
63
|
+
throw error;
|
|
64
|
+
}
|
|
65
|
+
await (0, promises_1.setTimeout)(1 + Math.floor(Math.random() * 10 * attempt));
|
|
47
66
|
}
|
|
48
|
-
|
|
49
|
-
}, options && toPrismaTxOptions(options));
|
|
67
|
+
}
|
|
50
68
|
}
|
|
51
69
|
getClient() {
|
|
52
70
|
var _a;
|
|
@@ -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,MAAM,iBAAiB,GAAG,CA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,EAAE,EAAE,EAAE,CAAC,CAAC;oBAC1C,IAAI,KAAK,CAAC,QAAQ,EAAE,CAAC;wBACnB,MAAM,IAAI,KAAK,CAAC,yGAAyG,CAAC,CAAC;oBAC7H,CAAC;oBACD,OAAO,MAAM,CAAC;gBAChB,CAAC,EAAE,aAAa,CAAC,CAAC;YACpB,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,IAAI,OAAO,IAAI,WAAW,IAAI,CAAC,eAAe,CAAC,KAAK,CAAC,EAAE,CAAC;oBACtD,MAAM,KAAK,CAAC;gBACd,CAAC;gBAED,MAAM,IAAA,qBAAK,EAAC,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,CAAC,CAAC;YAC5D,CAAC;QACH,CAAC;IACH,CAAC;IAMD,SAAS;;QACP,OAAO,CAAC,MAAA,IAAI,CAAC,GAAG,CAAC,QAAQ,EAAE,mCAAI,IAAI,CAAC,MAAM,CAAM,CAAC;IACnD,CAAC;IAKD,eAAe;QACb,OAAO,IAAI,CAAC,GAAG,CAAC,QAAQ,EAAE,KAAK,SAAS,CAAC;IAC3C,CAAC;CACF,CAAA;AAtEY,4DAAwB;mCAAxB,wBAAwB;IADpC,IAAA,mBAAU,GAAE;IAKR,WAAA,IAAA,eAAM,EAAC,IAAA,iCAAc,GAAE,CAAC,CAAA;IAGxB,WAAA,IAAA,iBAAQ,GAAE,CAAA;IAAE,WAAA,IAAA,eAAM,EAAC,qDAAsB,CAAC,CAAA;6CAAe,+BAAiB;GAPlE,wBAAwB,CAsEpC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nathapp/nestjs-prisma",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.3.1",
|
|
4
4
|
"description": "NestJS Prisma integration with multi-database support",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"clean": "rimraf dist/ .turbo/",
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
"types": "dist/index.d.ts",
|
|
21
21
|
"main": "dist/index.js",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@nathapp/nestjs-common": "^4.
|
|
24
|
-
"@nathapp/nestjs-data": "^4.
|
|
23
|
+
"@nathapp/nestjs-common": "^4.3.1",
|
|
24
|
+
"@nathapp/nestjs-data": "^4.3.1"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@nathapp/eslint-config": "1.0.1",
|