@nestjs-transactional/core 1.0.0-alpha.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/LICENSE +21 -0
- package/README.md +265 -0
- package/dist/bootstrap/transactional-methods.bootstrap.d.ts +42 -0
- package/dist/bootstrap/transactional-methods.bootstrap.js +129 -0
- package/dist/bootstrap/transactional-methods.bootstrap.js.map +1 -0
- package/dist/context/transaction-context-view.d.ts +38 -0
- package/dist/context/transaction-context-view.js +48 -0
- package/dist/context/transaction-context-view.js.map +1 -0
- package/dist/context/transaction.context.d.ts +114 -0
- package/dist/context/transaction.context.js +112 -0
- package/dist/context/transaction.context.js.map +1 -0
- package/dist/decorators/inject-decorators.d.ts +36 -0
- package/dist/decorators/inject-decorators.js +45 -0
- package/dist/decorators/inject-decorators.js.map +1 -0
- package/dist/decorators/transactional.decorator.d.ts +64 -0
- package/dist/decorators/transactional.decorator.js +82 -0
- package/dist/decorators/transactional.decorator.js.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +35 -0
- package/dist/index.js.map +1 -0
- package/dist/interceptor/transactional.interceptor.d.ts +28 -0
- package/dist/interceptor/transactional.interceptor.js +56 -0
- package/dist/interceptor/transactional.interceptor.js.map +1 -0
- package/dist/internal/markers.d.ts +15 -0
- package/dist/internal/markers.js +18 -0
- package/dist/internal/markers.js.map +1 -0
- package/dist/manager/adapter.registry.d.ts +103 -0
- package/dist/manager/adapter.registry.js +179 -0
- package/dist/manager/adapter.registry.js.map +1 -0
- package/dist/manager/transaction.manager.d.ts +129 -0
- package/dist/manager/transaction.manager.js +412 -0
- package/dist/manager/transaction.manager.js.map +1 -0
- package/dist/module/transactional.module.d.ts +242 -0
- package/dist/module/transactional.module.js +374 -0
- package/dist/module/transactional.module.js.map +1 -0
- package/dist/observability/transaction-observer.d.ts +78 -0
- package/dist/observability/transaction-observer.js +18 -0
- package/dist/observability/transaction-observer.js.map +1 -0
- package/dist/testing/in-memory.adapter.d.ts +66 -0
- package/dist/testing/in-memory.adapter.js +83 -0
- package/dist/testing/in-memory.adapter.js.map +1 -0
- package/dist/testing/index.d.ts +2 -0
- package/dist/testing/index.js +18 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/tokens/constants.d.ts +14 -0
- package/dist/tokens/constants.js +17 -0
- package/dist/tokens/constants.js.map +1 -0
- package/dist/tokens/index.d.ts +3 -0
- package/dist/tokens/index.js +11 -0
- package/dist/tokens/index.js.map +1 -0
- package/dist/tokens/token-utils.d.ts +52 -0
- package/dist/tokens/token-utils.js +67 -0
- package/dist/tokens/token-utils.js.map +1 -0
- package/dist/types/domain-event.d.ts +18 -0
- package/dist/types/domain-event.js +3 -0
- package/dist/types/domain-event.js.map +1 -0
- package/dist/types/errors.d.ts +50 -0
- package/dist/types/errors.js +61 -0
- package/dist/types/errors.js.map +1 -0
- package/dist/types/isolation.d.ts +11 -0
- package/dist/types/isolation.js +3 -0
- package/dist/types/isolation.js.map +1 -0
- package/dist/types/propagation.d.ts +63 -0
- package/dist/types/propagation.js +67 -0
- package/dist/types/propagation.js.map +1 -0
- package/dist/types/transaction-adapter.d.ts +72 -0
- package/dist/types/transaction-adapter.js +3 -0
- package/dist/types/transaction-adapter.js.map +1 -0
- package/dist/types/transaction-handle.d.ts +23 -0
- package/dist/types/transaction-handle.js +3 -0
- package/dist/types/transaction-handle.js.map +1 -0
- package/dist/types/transaction-options.d.ts +88 -0
- package/dist/types/transaction-options.js +3 -0
- package/dist/types/transaction-options.js.map +1 -0
- package/package.json +74 -0
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var AdapterRegistry_1;
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.AdapterRegistry = exports.ADAPTER_REGISTRY = void 0;
|
|
11
|
+
const common_1 = require("@nestjs/common");
|
|
12
|
+
const errors_1 = require("../types/errors");
|
|
13
|
+
/**
|
|
14
|
+
* DI token for the {@link AdapterRegistry}. Consumers who need to register
|
|
15
|
+
* adapters manually (for example, integration tests or custom modules)
|
|
16
|
+
* can inject this token and call {@link AdapterRegistry.register}.
|
|
17
|
+
*/
|
|
18
|
+
exports.ADAPTER_REGISTRY = Symbol('ADAPTER_REGISTRY');
|
|
19
|
+
/**
|
|
20
|
+
* In-memory registry of {@link TransactionAdapter} instances, keyed by
|
|
21
|
+
* `(adapterName, instanceName)`. Each adapter type (e.g. `'typeorm'`) may
|
|
22
|
+
* have multiple instances (e.g. `'primary'`, `'billing'`) — the registry
|
|
23
|
+
* keeps them separate so that `@Transactional({ adapterInstance: 'billing' })`
|
|
24
|
+
* can target a specific one.
|
|
25
|
+
*
|
|
26
|
+
* The first adapter registered becomes the default; any later registration
|
|
27
|
+
* can override the default by passing `isDefault = true`.
|
|
28
|
+
*
|
|
29
|
+
* Implementation note: the internal key format is `${adapterName}:${instanceName}`.
|
|
30
|
+
* Neither `adapterName` nor `instanceName` may contain `:` — enforced by
|
|
31
|
+
* convention, not at runtime.
|
|
32
|
+
*/
|
|
33
|
+
let AdapterRegistry = AdapterRegistry_1 = class AdapterRegistry {
|
|
34
|
+
adapters = new Map();
|
|
35
|
+
defaultAdapterName = null;
|
|
36
|
+
defaultInstanceName = 'default';
|
|
37
|
+
/**
|
|
38
|
+
* Register an adapter under `(registration.adapterName, registration.instanceName)`.
|
|
39
|
+
*
|
|
40
|
+
* The first registration always becomes the default. Passing
|
|
41
|
+
* `isDefault = true` on any later registration switches the default to it.
|
|
42
|
+
*
|
|
43
|
+
* Re-registering the same pair overwrites the previously stored adapter;
|
|
44
|
+
* the default pointer is left unchanged unless `isDefault = true` is set.
|
|
45
|
+
*/
|
|
46
|
+
register(registration, isDefault = false) {
|
|
47
|
+
const key = AdapterRegistry_1.keyFor(registration.adapterName, registration.instanceName);
|
|
48
|
+
this.adapters.set(key, registration.adapter);
|
|
49
|
+
if (isDefault || this.defaultAdapterName === null) {
|
|
50
|
+
this.defaultAdapterName = registration.adapterName;
|
|
51
|
+
this.defaultInstanceName = registration.instanceName;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Look up an adapter by `(adapterName, instanceName)`.
|
|
56
|
+
*
|
|
57
|
+
* @throws {TransactionAdapterNotFoundError} If no adapter is registered
|
|
58
|
+
* under that pair.
|
|
59
|
+
*/
|
|
60
|
+
get(adapterName, instanceName) {
|
|
61
|
+
const adapter = this.adapters.get(AdapterRegistry_1.keyFor(adapterName, instanceName));
|
|
62
|
+
if (adapter === undefined) {
|
|
63
|
+
throw new errors_1.TransactionAdapterNotFoundError(adapterName, instanceName);
|
|
64
|
+
}
|
|
65
|
+
return adapter;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Look up an adapter by its public dataSource name (DD-020). Iterates
|
|
69
|
+
* the registered entries and returns the one whose `instanceName`
|
|
70
|
+
* matches `dataSource` — there is exactly one per dataSource by
|
|
71
|
+
* convention.
|
|
72
|
+
*
|
|
73
|
+
* If multiple registrations share the same `instanceName` across
|
|
74
|
+
* different adapter types (e.g. `typeorm:billing` AND
|
|
75
|
+
* `prisma:billing`), throws — that configuration is ambiguous and
|
|
76
|
+
* the user must disambiguate via the `adapter` + `adapterInstance`
|
|
77
|
+
* options instead.
|
|
78
|
+
*
|
|
79
|
+
* @throws {IllegalTransactionStateError} If two or more adapters share
|
|
80
|
+
* the same dataSource name.
|
|
81
|
+
* @throws {TransactionAdapterNotFoundError} If no adapter is registered
|
|
82
|
+
* under this dataSource.
|
|
83
|
+
*/
|
|
84
|
+
getByDataSource(dataSource) {
|
|
85
|
+
const matches = [];
|
|
86
|
+
for (const [key, adapter] of this.adapters) {
|
|
87
|
+
const colon = key.indexOf(':');
|
|
88
|
+
const adapterName = key.slice(0, colon);
|
|
89
|
+
const instanceName = key.slice(colon + 1);
|
|
90
|
+
if (instanceName === dataSource) {
|
|
91
|
+
matches.push({ adapterName, adapter });
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (matches.length === 0) {
|
|
95
|
+
throw new errors_1.TransactionAdapterNotFoundError('*', dataSource);
|
|
96
|
+
}
|
|
97
|
+
if (matches.length > 1) {
|
|
98
|
+
const names = matches.map((m) => `${m.adapterName}:${dataSource}`).join(', ');
|
|
99
|
+
throw new errors_1.IllegalTransactionStateError(`Multiple adapters registered for dataSource '${dataSource}' (${names}). ` +
|
|
100
|
+
`Use the explicit \`adapter\` + \`adapterInstance\` options to disambiguate.`);
|
|
101
|
+
}
|
|
102
|
+
// Length is exactly 1 here (checked above) — non-null is provably safe.
|
|
103
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
104
|
+
return matches[0].adapter;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Find the adapter type name (`'typeorm'`, `'prisma'`, ...) registered
|
|
108
|
+
* under the given dataSource. Used by the manager to compose the
|
|
109
|
+
* composite Map key when only the dataSource name is known.
|
|
110
|
+
*
|
|
111
|
+
* Same disambiguation rules as {@link getByDataSource}.
|
|
112
|
+
*/
|
|
113
|
+
getAdapterNameByDataSource(dataSource) {
|
|
114
|
+
const matches = [];
|
|
115
|
+
for (const key of this.adapters.keys()) {
|
|
116
|
+
const colon = key.indexOf(':');
|
|
117
|
+
const adapterName = key.slice(0, colon);
|
|
118
|
+
const instanceName = key.slice(colon + 1);
|
|
119
|
+
if (instanceName === dataSource) {
|
|
120
|
+
matches.push(adapterName);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if (matches.length === 0) {
|
|
124
|
+
throw new errors_1.TransactionAdapterNotFoundError('*', dataSource);
|
|
125
|
+
}
|
|
126
|
+
if (matches.length > 1) {
|
|
127
|
+
const names = matches.map((n) => `${n}:${dataSource}`).join(', ');
|
|
128
|
+
throw new errors_1.IllegalTransactionStateError(`Multiple adapters registered for dataSource '${dataSource}' (${names}). ` +
|
|
129
|
+
`Use the explicit \`adapter\` + \`adapterInstance\` options to disambiguate.`);
|
|
130
|
+
}
|
|
131
|
+
// Length is exactly 1 here (checked above) — non-null is provably safe.
|
|
132
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
133
|
+
return matches[0];
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Return the name of the adapter type currently marked default.
|
|
137
|
+
*
|
|
138
|
+
* @throws {IllegalTransactionStateError} If no adapter has been registered
|
|
139
|
+
* yet — there is no default to choose.
|
|
140
|
+
*/
|
|
141
|
+
getDefaultAdapterName() {
|
|
142
|
+
if (this.defaultAdapterName === null) {
|
|
143
|
+
throw new errors_1.IllegalTransactionStateError('No default adapter registered. Register at least one adapter via ' +
|
|
144
|
+
'TypeOrmTransactionalModule.forFeature() or the corresponding ' +
|
|
145
|
+
'transactional module for your ORM.');
|
|
146
|
+
}
|
|
147
|
+
return this.defaultAdapterName;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Return the name of the default adapter instance. Defaults to `'default'`
|
|
151
|
+
* before any adapter is registered — matches the common convention in
|
|
152
|
+
* per-ORM `forFeature({ instanceName: 'default' })` helpers.
|
|
153
|
+
*/
|
|
154
|
+
getDefaultInstanceName() {
|
|
155
|
+
return this.defaultInstanceName;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Return every registration as a new array. Used by bootstrap and
|
|
159
|
+
* observability code to enumerate configured adapters at runtime.
|
|
160
|
+
*/
|
|
161
|
+
getAll() {
|
|
162
|
+
return Array.from(this.adapters.entries(), ([key, adapter]) => {
|
|
163
|
+
const colon = key.indexOf(':');
|
|
164
|
+
return {
|
|
165
|
+
adapterName: key.slice(0, colon),
|
|
166
|
+
instanceName: key.slice(colon + 1),
|
|
167
|
+
adapter,
|
|
168
|
+
};
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
static keyFor(adapterName, instanceName) {
|
|
172
|
+
return `${adapterName}:${instanceName}`;
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
exports.AdapterRegistry = AdapterRegistry;
|
|
176
|
+
exports.AdapterRegistry = AdapterRegistry = AdapterRegistry_1 = __decorate([
|
|
177
|
+
(0, common_1.Injectable)()
|
|
178
|
+
], AdapterRegistry);
|
|
179
|
+
//# sourceMappingURL=adapter.registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter.registry.js","sourceRoot":"","sources":["../../src/manager/adapter.registry.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAA4C;AAE5C,4CAAgG;AAoBhG;;;;GAIG;AACU,QAAA,gBAAgB,GAAG,MAAM,CAAC,kBAAkB,CAAC,CAAC;AAE3D;;;;;;;;;;;;;GAaG;AAEI,IAAM,eAAe,uBAArB,MAAM,eAAe;IACT,QAAQ,GAAG,IAAI,GAAG,EAA8B,CAAC;IAC1D,kBAAkB,GAAkB,IAAI,CAAC;IACzC,mBAAmB,GAAG,SAAS,CAAC;IAExC;;;;;;;;OAQG;IACH,QAAQ,CAAC,YAAiC,EAAE,SAAS,GAAG,KAAK;QAC3D,MAAM,GAAG,GAAG,iBAAe,CAAC,MAAM,CAAC,YAAY,CAAC,WAAW,EAAE,YAAY,CAAC,YAAY,CAAC,CAAC;QACxF,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,EAAE,YAAY,CAAC,OAAO,CAAC,CAAC;QAE7C,IAAI,SAAS,IAAI,IAAI,CAAC,kBAAkB,KAAK,IAAI,EAAE,CAAC;YAClD,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ChF,CAAC;QACJ,CAAC;QACD,wEAAwE;QACxE,oEAAoE;QACpE,OAAO,OAAO,CAAC,CAAC,CAAE,CAAC;IACrB,CAAC;IAED;;;;;OAKG;IACH,qBAAqB;QACnB,IAAI,IAAI,CAAC,kBAAkB,KAAK,IAAI,EAAE,CAAC;YACrC,MAAM,IAAI,qCAA4B,CACpC,mEAAmE;gBACjE,+DAA+D;gBAC/D,oCAAoC,CACvC,CAAC;QACJ,CAAC;QACD,OAAO,IAAI,CAAC,kBAAkB,CAAC;IACjC,CAAC;IAED;;;;OAIG;IACH,sBAAsB;QACpB,OAAO,IAAI,CAAC,mBAAmB,CAAC;IAClC,CAAC;IAED;;;OAGG;IACH,MAAM;QACJ,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,GAAG,EAAE,OAAO,CAAC,EAAE,EAAE;YAC5D,MAAM,KAAK,GAAG,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;YAC/B,OAAO;gBACL,WAAW,EAAE,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC;gBAChC,YAAY,EAAE,GAAG,CAAC,KAAK,CAAC,KAAK,GAAG,CAAC,CAAC;gBAClC,OAAO;aACR,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;IAEO,MAAM,CAAC,MAAM,CAAC,WAAmB,EAAE,YAAoB;QAC7D,OAAO,GAAG,WAAW,IAAI,YAAY,EAAE,CAAC;IAC1C,CAAC;CACF,CAAA;AAhKY,0CAAe;0BAAf,eAAe;IAD3B,IAAA,mBAAU,GAAE;GACA,eAAe,CAgK3B"}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { type TransactionObserver } from '../observability/transaction-observer';
|
|
2
|
+
import type { ExtendedTransactionOptions } from '../types/transaction-options';
|
|
3
|
+
import { AdapterRegistry } from './adapter.registry';
|
|
4
|
+
/**
|
|
5
|
+
* Runtime that executes a callback inside a transaction, following the
|
|
6
|
+
* requested {@link PropagationMode}. Exposes registration points for
|
|
7
|
+
* before/after-commit and after-rollback hooks that the surrounding
|
|
8
|
+
* transaction fires at the appropriate phase.
|
|
9
|
+
*
|
|
10
|
+
* All seven Spring-compatible propagation modes are supported:
|
|
11
|
+
* `REQUIRED`, `REQUIRES_NEW`, `NESTED`, `SUPPORTS`, `NOT_SUPPORTED`,
|
|
12
|
+
* `NEVER`, `MANDATORY`. See {@link TransactionManager.run} for the
|
|
13
|
+
* per-mode behaviour.
|
|
14
|
+
*/
|
|
15
|
+
export declare class TransactionManager {
|
|
16
|
+
private readonly registry;
|
|
17
|
+
private readonly observers;
|
|
18
|
+
private readonly logger;
|
|
19
|
+
constructor(registry: AdapterRegistry, observers?: readonly TransactionObserver[]);
|
|
20
|
+
/**
|
|
21
|
+
* Execute `fn` inside a transaction managed by the adapter resolved from
|
|
22
|
+
* `options` (or defaults from {@link AdapterRegistry}).
|
|
23
|
+
*
|
|
24
|
+
* Behaviour by propagation mode:
|
|
25
|
+
* - `REQUIRED` (default): join the active transaction for this adapter
|
|
26
|
+
* instance if one exists; otherwise start a new transaction.
|
|
27
|
+
* - `REQUIRES_NEW`: always start a new, independent transaction. If an
|
|
28
|
+
* outer transaction is active, its {@link ActiveTransaction} entry is
|
|
29
|
+
* suspended out of the context Map for the duration of the inner call
|
|
30
|
+
* and restored afterwards.
|
|
31
|
+
* - `NESTED`: if an outer transaction is active, run inside a savepoint
|
|
32
|
+
* on that transaction (via {@link TransactionAdapter.runInSavepoint}).
|
|
33
|
+
* A savepoint rollback leaves the outer transaction intact. Lifecycle
|
|
34
|
+
* hooks registered inside a `NESTED` block attach to the outer
|
|
35
|
+
* transaction and fire on the outer commit/rollback. If no outer
|
|
36
|
+
* transaction is active, `NESTED` degrades to `REQUIRED`.
|
|
37
|
+
* - `SUPPORTS`: run `fn` in the outer transaction if present; otherwise
|
|
38
|
+
* run `fn` directly with no transaction and no lifecycle hooks.
|
|
39
|
+
* - `NOT_SUPPORTED`: suspend the outer transaction (remove its entry
|
|
40
|
+
* from the context Map) and run `fn` without transactional context.
|
|
41
|
+
* Restore on return. Note that the adapter-level connection/query
|
|
42
|
+
* runner is NOT actually suspended — this is a context-level opt-out.
|
|
43
|
+
* - `NEVER`: throw {@link IllegalTransactionStateError} if an outer
|
|
44
|
+
* transaction is active; otherwise run `fn` directly.
|
|
45
|
+
* - `MANDATORY`: throw {@link IllegalTransactionStateError} if no outer
|
|
46
|
+
* transaction is active; otherwise join it.
|
|
47
|
+
*
|
|
48
|
+
* Both inner and outer transactions share the surrounding
|
|
49
|
+
* {@link TransactionContext} store — same `correlationId`, same Map —
|
|
50
|
+
* only the Map slot at `instanceName` is swapped as propagation requires.
|
|
51
|
+
*/
|
|
52
|
+
run<T>(options: ExtendedTransactionOptions, fn: () => Promise<T>): Promise<T>;
|
|
53
|
+
/**
|
|
54
|
+
* Compose the {@link TransactionContext} key for a given adapter
|
|
55
|
+
* (type name) + instance pair. Manager and helper packages must agree
|
|
56
|
+
* on this format so that `@nestjs-transactional/typeorm`'s
|
|
57
|
+
* `getCurrentEntityManager` can find the transaction registered here.
|
|
58
|
+
*/
|
|
59
|
+
private static contextKey;
|
|
60
|
+
/**
|
|
61
|
+
* Translate the user-facing options into the internal `(adapterName,
|
|
62
|
+
* instanceName)` pair used to build the active-transaction Map key.
|
|
63
|
+
*
|
|
64
|
+
* Resolution order (DD-020):
|
|
65
|
+
* 1. `options.dataSource` set: lookup the unique adapter via the
|
|
66
|
+
* registry by dataSource name. The dataSource name becomes the
|
|
67
|
+
* `instanceName`; the registry hands back the adapter type.
|
|
68
|
+
* 2. Else: legacy path — `options.adapter` / `options.adapterInstance`
|
|
69
|
+
* fall back to the registry's defaults.
|
|
70
|
+
*/
|
|
71
|
+
private resolveAdapterIdentifiers;
|
|
72
|
+
/**
|
|
73
|
+
* Register a hook to fire after the current transaction commits
|
|
74
|
+
* successfully. Attaches to the first active transaction on the current
|
|
75
|
+
* async context — sufficient for single-adapter setups. Hook errors are
|
|
76
|
+
* swallowed with a warning and do not reject `run()`.
|
|
77
|
+
*
|
|
78
|
+
* @throws {IllegalTransactionStateError} If called outside an active transaction.
|
|
79
|
+
*/
|
|
80
|
+
registerAfterCommit(hook: () => Promise<void>): void;
|
|
81
|
+
/**
|
|
82
|
+
* Register a hook to fire after the current transaction rolls back. The
|
|
83
|
+
* hook receives the error that caused the rollback.
|
|
84
|
+
*
|
|
85
|
+
* @throws {IllegalTransactionStateError} If called outside an active transaction.
|
|
86
|
+
*/
|
|
87
|
+
registerAfterRollback(hook: (error: unknown) => Promise<void>): void;
|
|
88
|
+
/**
|
|
89
|
+
* Register a hook to fire just before the transaction commits. A throwing
|
|
90
|
+
* hook triggers the adapter's rollback — the transaction does not commit.
|
|
91
|
+
*
|
|
92
|
+
* @throws {IllegalTransactionStateError} If called outside an active transaction.
|
|
93
|
+
*/
|
|
94
|
+
registerBeforeCommit(hook: () => Promise<void>): void;
|
|
95
|
+
private startNew;
|
|
96
|
+
private notifyStart;
|
|
97
|
+
private notifyCommit;
|
|
98
|
+
private notifyRollback;
|
|
99
|
+
/**
|
|
100
|
+
* Run `fn` inside a savepoint on `parent.handle`. Used by
|
|
101
|
+
* {@link PropagationMode.NESTED} when there is an active outer transaction.
|
|
102
|
+
*
|
|
103
|
+
* The manager intentionally does NOT create a new
|
|
104
|
+
* {@link ActiveTransaction} for the savepoint — hook registrations inside
|
|
105
|
+
* `fn` fall through {@link currentTransaction} to the outer transaction,
|
|
106
|
+
* which is the Spring-style semantic: nested events "promote" to the
|
|
107
|
+
* enclosing transaction and fire when that transaction commits.
|
|
108
|
+
*
|
|
109
|
+
* Rollback semantics follow `shouldRollback`: errors that match the
|
|
110
|
+
* rollback rules cause the adapter to roll back to the savepoint (the
|
|
111
|
+
* outer transaction is unaffected); errors that do NOT match lead to a
|
|
112
|
+
* savepoint release (commit-inside-savepoint) and the error is re-raised
|
|
113
|
+
* to the caller after the release.
|
|
114
|
+
*/
|
|
115
|
+
private runNestedSavepoint;
|
|
116
|
+
private currentTransaction;
|
|
117
|
+
/**
|
|
118
|
+
* Spring-style decision on whether a thrown error should trigger rollback:
|
|
119
|
+
*
|
|
120
|
+
* 1. If `noRollbackFor` is set and matches, commit anyway (precedence).
|
|
121
|
+
* 2. Else if `rollbackFor` is set, roll back only when the error matches
|
|
122
|
+
* an entry — a non-match commits (explicit allow-list semantics).
|
|
123
|
+
* 3. Else (no rules set), any error triggers rollback — the default
|
|
124
|
+
* transactional behaviour.
|
|
125
|
+
*/
|
|
126
|
+
private shouldRollback;
|
|
127
|
+
private runHooks;
|
|
128
|
+
}
|
|
129
|
+
//# sourceMappingURL=transaction.manager.d.ts.map
|