@mikro-orm/core 6.4.17-dev.94 → 6.4.17-dev.95

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/EntityManager.js CHANGED
@@ -13,6 +13,7 @@ const enums_1 = require("./enums");
13
13
  const events_1 = require("./events");
14
14
  const errors_1 = require("./errors");
15
15
  const utils_2 = require("./entity/utils");
16
+ const TransactionManager_1 = require("./utils/TransactionManager");
16
17
  /**
17
18
  * The EntityManager is the central access point to ORM functionality. It is a facade to all different ORM subsystems
18
19
  * such as UnitOfWork, Query Language, and Repository API.
@@ -970,38 +971,8 @@ class EntityManager {
970
971
  if (this.disableTransactions || em.disableTransactions) {
971
972
  return cb(em);
972
973
  }
973
- const fork = em.fork({
974
- clear: options.clear ?? false, // state will be merged once resolves
975
- flushMode: options.flushMode,
976
- cloneEventManager: true,
977
- disableTransactions: options.ignoreNestedTransactions,
978
- loggerContext: options.loggerContext,
979
- });
980
- options.ctx ??= em.transactionContext;
981
- const propagateToUpperContext = !em.global || this.config.get('allowGlobalContext');
982
- return utils_1.TransactionContext.create(fork, async () => {
983
- return fork.getConnection().transactional(async (trx) => {
984
- fork.transactionContext = trx;
985
- if (propagateToUpperContext) {
986
- fork.eventManager.registerSubscriber({
987
- afterFlush(args) {
988
- args.uow.getChangeSets()
989
- .filter(cs => [unit_of_work_1.ChangeSetType.DELETE, unit_of_work_1.ChangeSetType.DELETE_EARLY].includes(cs.type))
990
- .forEach(cs => em.unitOfWork.unsetIdentity(cs.entity));
991
- },
992
- });
993
- }
994
- const ret = await cb(fork);
995
- await fork.flush();
996
- if (propagateToUpperContext) {
997
- // ensure all entities from inner context are merged to the upper one
998
- for (const entity of fork.unitOfWork.getIdentityMap()) {
999
- em.merge(entity, { disableContextResolution: true, keepIdentity: true, refresh: true });
1000
- }
1001
- }
1002
- return ret;
1003
- }, { ...options, eventBroadcaster: new events_1.TransactionEventBroadcaster(fork, undefined, { topLevelTransaction: !options.ctx }) });
1004
- });
974
+ const manager = new TransactionManager_1.TransactionManager(this);
975
+ return manager.handle(cb, options);
1005
976
  }
1006
977
  /**
1007
978
  * Starts new transaction bound to this EntityManager. Use `ctx` parameter to provide the parent when nesting transactions.
package/enums.d.ts CHANGED
@@ -158,8 +158,18 @@ export declare enum EventType {
158
158
  }
159
159
  export declare const EventTypeMap: Record<EventType, number>;
160
160
  export type TransactionEventType = EventType.beforeTransactionStart | EventType.afterTransactionStart | EventType.beforeTransactionCommit | EventType.afterTransactionCommit | EventType.beforeTransactionRollback | EventType.afterTransactionRollback;
161
+ export declare enum TransactionPropagation {
162
+ REQUIRED = "required",
163
+ REQUIRES_NEW = "requires_new",
164
+ NESTED = "nested",
165
+ NOT_SUPPORTED = "not_supported",
166
+ SUPPORTS = "supports",
167
+ MANDATORY = "mandatory",
168
+ NEVER = "never"
169
+ }
161
170
  export interface TransactionOptions {
162
171
  ctx?: Transaction;
172
+ propagation?: TransactionPropagation;
163
173
  isolationLevel?: IsolationLevel;
164
174
  readOnly?: boolean;
165
175
  clear?: boolean;
package/enums.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DeferMode = exports.PlainObject = exports.EventTypeMap = exports.EventType = exports.IsolationLevel = exports.LockMode = exports.DataloaderType = exports.LoadStrategy = exports.Cascade = exports.ReferenceKind = exports.SCALAR_TYPES = exports.QueryFlag = exports.QueryOrderNumeric = exports.QueryOrder = exports.JSON_KEY_OPERATORS = exports.ARRAY_OPERATORS = exports.QueryOperator = exports.GroupOperator = exports.PopulatePath = exports.PopulateHint = exports.FlushMode = void 0;
3
+ exports.DeferMode = exports.PlainObject = exports.TransactionPropagation = exports.EventTypeMap = exports.EventType = exports.IsolationLevel = exports.LockMode = exports.DataloaderType = exports.LoadStrategy = exports.Cascade = exports.ReferenceKind = exports.SCALAR_TYPES = exports.QueryFlag = exports.QueryOrderNumeric = exports.QueryOrder = exports.JSON_KEY_OPERATORS = exports.ARRAY_OPERATORS = exports.QueryOperator = exports.GroupOperator = exports.PopulatePath = exports.PopulateHint = exports.FlushMode = void 0;
4
4
  var FlushMode;
5
5
  (function (FlushMode) {
6
6
  /** The `EntityManager` delays the flush until the current Transaction is committed. */
@@ -180,6 +180,16 @@ exports.EventTypeMap = Object.keys(EventType).reduce((a, b, i) => {
180
180
  a[b] = i;
181
181
  return a;
182
182
  }, {});
183
+ var TransactionPropagation;
184
+ (function (TransactionPropagation) {
185
+ TransactionPropagation["REQUIRED"] = "required";
186
+ TransactionPropagation["REQUIRES_NEW"] = "requires_new";
187
+ TransactionPropagation["NESTED"] = "nested";
188
+ TransactionPropagation["NOT_SUPPORTED"] = "not_supported";
189
+ TransactionPropagation["SUPPORTS"] = "supports";
190
+ TransactionPropagation["MANDATORY"] = "mandatory";
191
+ TransactionPropagation["NEVER"] = "never";
192
+ })(TransactionPropagation || (exports.TransactionPropagation = TransactionPropagation = {}));
183
193
  class PlainObject {
184
194
  }
185
195
  exports.PlainObject = PlainObject;
package/errors.d.ts CHANGED
@@ -67,3 +67,8 @@ export declare class NotFoundError<T extends AnyEntity = AnyEntity> extends Vali
67
67
  static findOneFailed(name: string, where: Dictionary | IPrimaryKey): NotFoundError;
68
68
  static findExactlyOneFailed(name: string, where: Dictionary | IPrimaryKey): NotFoundError;
69
69
  }
70
+ export declare class TransactionStateError extends ValidationError {
71
+ static requiredTransactionNotFound(propagation: string): TransactionStateError;
72
+ static transactionNotAllowed(propagation: string): TransactionStateError;
73
+ static invalidPropagation(propagation: string): TransactionStateError;
74
+ }
package/errors.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.NotFoundError = exports.MetadataError = exports.OptimisticLockError = exports.CursorError = exports.ValidationError = void 0;
3
+ exports.TransactionStateError = exports.NotFoundError = exports.MetadataError = exports.OptimisticLockError = exports.CursorError = exports.ValidationError = void 0;
4
4
  const node_util_1 = require("node:util");
5
5
  class ValidationError extends Error {
6
6
  entity;
@@ -232,3 +232,15 @@ class NotFoundError extends ValidationError {
232
232
  }
233
233
  }
234
234
  exports.NotFoundError = NotFoundError;
235
+ class TransactionStateError extends ValidationError {
236
+ static requiredTransactionNotFound(propagation) {
237
+ return new TransactionStateError(`No existing transaction found for transaction marked with propagation "${propagation}"`);
238
+ }
239
+ static transactionNotAllowed(propagation) {
240
+ return new TransactionStateError(`Existing transaction found for transaction marked with propagation "${propagation}"`);
241
+ }
242
+ static invalidPropagation(propagation) {
243
+ return new TransactionStateError(`Unsupported transaction propagation type: ${propagation}`);
244
+ }
245
+ }
246
+ exports.TransactionStateError = TransactionStateError;
package/index.mjs CHANGED
@@ -162,6 +162,9 @@ export const TimeType = mod.TimeType;
162
162
  export const TinyIntType = mod.TinyIntType;
163
163
  export const TransactionContext = mod.TransactionContext;
164
164
  export const TransactionEventBroadcaster = mod.TransactionEventBroadcaster;
165
+ export const TransactionManager = mod.TransactionManager;
166
+ export const TransactionPropagation = mod.TransactionPropagation;
167
+ export const TransactionStateError = mod.TransactionStateError;
165
168
  export const Transactional = mod.Transactional;
166
169
  export const Type = mod.Type;
167
170
  export const Uint8ArrayType = mod.Uint8ArrayType;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/core",
3
- "version": "6.4.17-dev.94",
3
+ "version": "6.4.17-dev.95",
4
4
  "description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
5
5
  "main": "index.js",
6
6
  "module": "index.mjs",
@@ -64,7 +64,7 @@
64
64
  "esprima": "4.0.1",
65
65
  "fs-extra": "11.3.1",
66
66
  "globby": "11.1.0",
67
- "mikro-orm": "6.4.17-dev.94",
67
+ "mikro-orm": "6.4.17-dev.95",
68
68
  "reflect-metadata": "0.2.2"
69
69
  }
70
70
  }
@@ -0,0 +1,65 @@
1
+ import type { EntityManager } from '../EntityManager';
2
+ import { type TransactionOptions } from '../enums';
3
+ /**
4
+ * Manages transaction lifecycle and propagation for EntityManager.
5
+ */
6
+ export declare class TransactionManager {
7
+ private readonly em;
8
+ constructor(em: EntityManager);
9
+ /**
10
+ * Main entry point for handling transactional operations with propagation support.
11
+ */
12
+ handle<T>(cb: (em: EntityManager) => T | Promise<T>, options?: TransactionOptions): Promise<T>;
13
+ /**
14
+ * Executes the callback with the specified propagation type.
15
+ */
16
+ private executeWithPropagation;
17
+ /**
18
+ * Suspends the current transaction and returns the suspended resources.
19
+ */
20
+ private suspendTransaction;
21
+ /**
22
+ * Resumes a previously suspended transaction.
23
+ */
24
+ private resumeTransaction;
25
+ /**
26
+ * Executes operation without transaction context.
27
+ */
28
+ private executeWithoutTransaction;
29
+ /**
30
+ * Creates new independent transaction, suspending any existing one.
31
+ */
32
+ private executeWithNewTransaction;
33
+ /**
34
+ * Creates new transaction context.
35
+ */
36
+ private createNewTransaction;
37
+ /**
38
+ * Executes nested transaction with savepoint.
39
+ */
40
+ private executeNestedTransaction;
41
+ /**
42
+ * Creates a fork of the EntityManager with the given options.
43
+ */
44
+ private createFork;
45
+ /**
46
+ * Determines if changes should be propagated to the upper context.
47
+ */
48
+ private shouldPropagateToUpperContext;
49
+ /**
50
+ * Merges entities from fork to parent EntityManager.
51
+ */
52
+ private mergeEntitiesToParent;
53
+ /**
54
+ * Registers a deletion handler to unset entity identities after flush.
55
+ */
56
+ private registerDeletionHandler;
57
+ /**
58
+ * Processes transaction execution.
59
+ */
60
+ private processTransaction;
61
+ /**
62
+ * Executes transaction workflow with entity synchronization.
63
+ */
64
+ private executeTransactionFlow;
65
+ }
@@ -0,0 +1,203 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TransactionManager = void 0;
4
+ const enums_1 = require("../enums");
5
+ const events_1 = require("../events");
6
+ const TransactionContext_1 = require("../utils/TransactionContext");
7
+ const unit_of_work_1 = require("../unit-of-work");
8
+ const errors_1 = require("../errors");
9
+ /**
10
+ * Manages transaction lifecycle and propagation for EntityManager.
11
+ */
12
+ class TransactionManager {
13
+ em;
14
+ constructor(em) {
15
+ this.em = em;
16
+ }
17
+ /**
18
+ * Main entry point for handling transactional operations with propagation support.
19
+ */
20
+ async handle(cb, options = {}) {
21
+ const em = this.em.getContext(false);
22
+ // Set NESTED as the default propagation type
23
+ options.propagation ??= enums_1.TransactionPropagation.NESTED;
24
+ // Set the context to the current transaction context if not already set
25
+ options.ctx ??= em.getTransactionContext();
26
+ const hasExistingTransaction = !!em.getTransactionContext();
27
+ return this.executeWithPropagation(options.propagation, em, cb, options, hasExistingTransaction);
28
+ }
29
+ /**
30
+ * Executes the callback with the specified propagation type.
31
+ */
32
+ async executeWithPropagation(propagation, em, cb, options, hasExistingTransaction) {
33
+ switch (propagation) {
34
+ case enums_1.TransactionPropagation.NOT_SUPPORTED:
35
+ return this.executeWithoutTransaction(em, cb, options);
36
+ case enums_1.TransactionPropagation.REQUIRES_NEW:
37
+ return this.executeWithNewTransaction(em, cb, options, hasExistingTransaction);
38
+ case enums_1.TransactionPropagation.REQUIRED:
39
+ if (hasExistingTransaction) {
40
+ return cb(em);
41
+ }
42
+ return this.createNewTransaction(em, cb, options);
43
+ case enums_1.TransactionPropagation.NESTED:
44
+ if (hasExistingTransaction) {
45
+ return this.executeNestedTransaction(em, cb, options);
46
+ }
47
+ return this.createNewTransaction(em, cb, options);
48
+ case enums_1.TransactionPropagation.SUPPORTS:
49
+ if (hasExistingTransaction) {
50
+ return cb(em);
51
+ }
52
+ return this.executeWithoutTransaction(em, cb, options);
53
+ case enums_1.TransactionPropagation.MANDATORY:
54
+ if (!hasExistingTransaction) {
55
+ throw errors_1.TransactionStateError.requiredTransactionNotFound(propagation);
56
+ }
57
+ return cb(em);
58
+ case enums_1.TransactionPropagation.NEVER:
59
+ if (hasExistingTransaction) {
60
+ throw errors_1.TransactionStateError.transactionNotAllowed(propagation);
61
+ }
62
+ return this.executeWithoutTransaction(em, cb, options);
63
+ default:
64
+ throw errors_1.TransactionStateError.invalidPropagation(propagation);
65
+ }
66
+ }
67
+ /**
68
+ * Suspends the current transaction and returns the suspended resources.
69
+ */
70
+ suspendTransaction(em) {
71
+ const suspended = em.getTransactionContext();
72
+ em.resetTransactionContext();
73
+ return suspended;
74
+ }
75
+ /**
76
+ * Resumes a previously suspended transaction.
77
+ */
78
+ resumeTransaction(em, suspended) {
79
+ if (suspended != null) {
80
+ em.setTransactionContext(suspended);
81
+ }
82
+ }
83
+ /**
84
+ * Executes operation without transaction context.
85
+ */
86
+ async executeWithoutTransaction(em, cb, options) {
87
+ const suspended = this.suspendTransaction(em);
88
+ const fork = this.createFork(em, { ...options, disableTransactions: true });
89
+ const propagateToUpperContext = this.shouldPropagateToUpperContext(em);
90
+ try {
91
+ return await this.executeTransactionFlow(fork, cb, propagateToUpperContext, em);
92
+ }
93
+ finally {
94
+ this.resumeTransaction(em, suspended);
95
+ }
96
+ }
97
+ /**
98
+ * Creates new independent transaction, suspending any existing one.
99
+ */
100
+ async executeWithNewTransaction(em, cb, options, hasExistingTransaction) {
101
+ const fork = this.createFork(em, options);
102
+ let suspended = null;
103
+ // Suspend existing transaction if present
104
+ if (hasExistingTransaction) {
105
+ suspended = this.suspendTransaction(em);
106
+ }
107
+ const newOptions = { ...options, ctx: undefined };
108
+ try {
109
+ return await this.processTransaction(em, fork, cb, newOptions);
110
+ }
111
+ finally {
112
+ if (suspended != null) {
113
+ this.resumeTransaction(em, suspended);
114
+ }
115
+ }
116
+ }
117
+ /**
118
+ * Creates new transaction context.
119
+ */
120
+ async createNewTransaction(em, cb, options) {
121
+ const fork = this.createFork(em, options);
122
+ return this.processTransaction(em, fork, cb, options);
123
+ }
124
+ /**
125
+ * Executes nested transaction with savepoint.
126
+ */
127
+ async executeNestedTransaction(em, cb, options) {
128
+ const fork = this.createFork(em, options);
129
+ // Pass existing context to create savepoint
130
+ const nestedOptions = { ...options, ctx: em.getTransactionContext() };
131
+ return this.processTransaction(em, fork, cb, nestedOptions);
132
+ }
133
+ /**
134
+ * Creates a fork of the EntityManager with the given options.
135
+ */
136
+ createFork(em, options) {
137
+ return em.fork({
138
+ clear: options.clear ?? false,
139
+ flushMode: options.flushMode,
140
+ cloneEventManager: true,
141
+ disableTransactions: options.ignoreNestedTransactions,
142
+ loggerContext: options.loggerContext,
143
+ });
144
+ }
145
+ /**
146
+ * Determines if changes should be propagated to the upper context.
147
+ */
148
+ shouldPropagateToUpperContext(em) {
149
+ return !em.global || this.em.config.get('allowGlobalContext');
150
+ }
151
+ /**
152
+ * Merges entities from fork to parent EntityManager.
153
+ */
154
+ mergeEntitiesToParent(fork, parent) {
155
+ for (const entity of fork.getUnitOfWork(false).getIdentityMap()) {
156
+ parent.merge(entity, { disableContextResolution: true, keepIdentity: true, refresh: true });
157
+ }
158
+ }
159
+ /**
160
+ * Registers a deletion handler to unset entity identities after flush.
161
+ */
162
+ registerDeletionHandler(fork, parent) {
163
+ fork.getEventManager().registerSubscriber({
164
+ afterFlush: (args) => {
165
+ const deletionChangeSets = args.uow.getChangeSets()
166
+ .filter(cs => cs.type === unit_of_work_1.ChangeSetType.DELETE || cs.type === unit_of_work_1.ChangeSetType.DELETE_EARLY);
167
+ for (const cs of deletionChangeSets) {
168
+ parent.getUnitOfWork(false).unsetIdentity(cs.entity);
169
+ }
170
+ },
171
+ });
172
+ }
173
+ /**
174
+ * Processes transaction execution.
175
+ */
176
+ async processTransaction(em, fork, cb, options) {
177
+ const propagateToUpperContext = this.shouldPropagateToUpperContext(em);
178
+ const eventBroadcaster = new events_1.TransactionEventBroadcaster(fork, undefined, { topLevelTransaction: !options.ctx });
179
+ return TransactionContext_1.TransactionContext.create(fork, () => fork.getConnection().transactional(async (trx) => {
180
+ fork.setTransactionContext(trx);
181
+ return this.executeTransactionFlow(fork, cb, propagateToUpperContext, em);
182
+ }, { ...options, eventBroadcaster }));
183
+ }
184
+ /**
185
+ * Executes transaction workflow with entity synchronization.
186
+ */
187
+ async executeTransactionFlow(fork, cb, propagateToUpperContext, parentEm) {
188
+ if (!propagateToUpperContext) {
189
+ const ret = await cb(fork);
190
+ await fork.flush();
191
+ return ret;
192
+ }
193
+ // Setup: Register deletion handler before execution
194
+ this.registerDeletionHandler(fork, parentEm);
195
+ // Execute callback and flush
196
+ const ret = await cb(fork);
197
+ await fork.flush();
198
+ // Synchronization: Merge entities back to the parent
199
+ this.mergeEntitiesToParent(fork, parentEm);
200
+ return ret;
201
+ }
202
+ }
203
+ exports.TransactionManager = TransactionManager;
package/utils/index.d.ts CHANGED
@@ -5,6 +5,7 @@ export * from './DataloaderUtils';
5
5
  export * from './Utils';
6
6
  export * from './RequestContext';
7
7
  export * from './TransactionContext';
8
+ export * from './TransactionManager';
8
9
  export * from './QueryHelper';
9
10
  export * from './NullHighlighter';
10
11
  export * from './EntityComparator';
package/utils/index.js CHANGED
@@ -21,6 +21,7 @@ __exportStar(require("./DataloaderUtils"), exports);
21
21
  __exportStar(require("./Utils"), exports);
22
22
  __exportStar(require("./RequestContext"), exports);
23
23
  __exportStar(require("./TransactionContext"), exports);
24
+ __exportStar(require("./TransactionManager"), exports);
24
25
  __exportStar(require("./QueryHelper"), exports);
25
26
  __exportStar(require("./NullHighlighter"), exports);
26
27
  __exportStar(require("./EntityComparator"), exports);