@mikro-orm/core 6.4.17-dev.94 → 6.4.17-dev.96
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 +3 -32
- package/entity/defineEntity.d.ts +19 -10
- package/entity/defineEntity.js +19 -0
- package/enums.d.ts +10 -0
- package/enums.js +11 -1
- package/errors.d.ts +5 -0
- package/errors.js +13 -1
- package/index.mjs +12 -0
- package/package.json +2 -2
- package/utils/TransactionManager.d.ts +65 -0
- package/utils/TransactionManager.js +203 -0
- package/utils/index.d.ts +1 -0
- package/utils/index.js +1 -0
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
|
|
974
|
-
|
|
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/entity/defineEntity.d.ts
CHANGED
|
@@ -14,7 +14,8 @@ import type { Collection } from './Collection';
|
|
|
14
14
|
import type { IType, Type } from '../types/Type';
|
|
15
15
|
import { types } from '../types';
|
|
16
16
|
import { EntitySchema } from '../metadata/EntitySchema';
|
|
17
|
-
|
|
17
|
+
/** @internal */
|
|
18
|
+
export declare class PropertyOptionsBuilder<Value> {
|
|
18
19
|
'~options': PropertyOptions<any>;
|
|
19
20
|
'~type'?: {
|
|
20
21
|
value: Value;
|
|
@@ -262,7 +263,8 @@ declare class PropertyOptionsBuilder<Value> {
|
|
|
262
263
|
*/
|
|
263
264
|
$type<Runtime, Raw, Serialized = Raw>(): PropertyOptionsBuilder<IType<Runtime, Raw, Serialized>>;
|
|
264
265
|
}
|
|
265
|
-
|
|
266
|
+
/** @internal */
|
|
267
|
+
export declare class EnumOptionsBuilder<Value> extends PropertyOptionsBuilder<Value> {
|
|
266
268
|
'~options': {
|
|
267
269
|
enum: true;
|
|
268
270
|
} & EnumOptions<any>;
|
|
@@ -271,7 +273,8 @@ declare class EnumOptionsBuilder<Value> extends PropertyOptionsBuilder<Value> {
|
|
|
271
273
|
/** for postgres, by default it uses text column with check constraint */
|
|
272
274
|
nativeEnumName(nativeEnumName: string): EnumOptionsBuilder<Value>;
|
|
273
275
|
}
|
|
274
|
-
|
|
276
|
+
/** @internal */
|
|
277
|
+
export declare class EmbeddedOptionsBuilder<Value> extends PropertyOptionsBuilder<Value> {
|
|
275
278
|
'~options': ({
|
|
276
279
|
kind: 'embedded';
|
|
277
280
|
entity: () => EntitySchema<any, any> | EntitySchema<any, any>[];
|
|
@@ -282,7 +285,8 @@ declare class EmbeddedOptionsBuilder<Value> extends PropertyOptionsBuilder<Value
|
|
|
282
285
|
object(object?: boolean): EmbeddedOptionsBuilder<Value>;
|
|
283
286
|
array<T extends boolean = true>(array?: T): EmbeddedOptionsBuilder<T extends true ? Value[] : UnwrapArray<Value>>;
|
|
284
287
|
}
|
|
285
|
-
|
|
288
|
+
/** @internal */
|
|
289
|
+
export declare class ReferenceOptionsBuilder<Value extends object> extends PropertyOptionsBuilder<Value> {
|
|
286
290
|
'~options': ReferenceOptions<any, any>;
|
|
287
291
|
constructor(options: ReferenceOptionsBuilder<Value>['~options']);
|
|
288
292
|
/** Set what actions on owning entity should be cascaded to the relationship. Defaults to [Cascade.PERSIST, Cascade.MERGE] (see {@doclink cascading}). */
|
|
@@ -302,7 +306,8 @@ declare class ReferenceOptionsBuilder<Value extends object> extends PropertyOpti
|
|
|
302
306
|
*/
|
|
303
307
|
primary(primary?: boolean): ReferenceOptionsBuilder<any>;
|
|
304
308
|
}
|
|
305
|
-
|
|
309
|
+
/** @internal */
|
|
310
|
+
export declare class ManyToManyOptionsBuilder<TargetValue extends object> extends ReferenceOptionsBuilder<TargetValue> {
|
|
306
311
|
'~options': ({
|
|
307
312
|
kind: 'm:n';
|
|
308
313
|
entity: () => EntitySchema<any, any>;
|
|
@@ -343,7 +348,8 @@ declare class ManyToManyOptionsBuilder<TargetValue extends object> extends Refer
|
|
|
343
348
|
/** What to do when the reference to the target entity gets updated. */
|
|
344
349
|
updateRule(updateRule: 'cascade' | 'no action' | 'set null' | 'set default' | AnyString): ManyToManyOptionsBuilder<TargetValue>;
|
|
345
350
|
}
|
|
346
|
-
|
|
351
|
+
/** @internal */
|
|
352
|
+
export declare class ManyToOneOptionsBuilder<TargetValue extends object> extends ReferenceOptionsBuilder<TargetValue> {
|
|
347
353
|
'~options': ({
|
|
348
354
|
kind: 'm:1';
|
|
349
355
|
entity: () => EntitySchema<any, any>;
|
|
@@ -374,7 +380,8 @@ declare class ManyToOneOptionsBuilder<TargetValue extends object> extends Refere
|
|
|
374
380
|
/** Set the constraint type. Immediate constraints are checked for each statement, while deferred ones are only checked at the end of the transaction. Only for postgres unique constraints. */
|
|
375
381
|
deferMode(deferMode: DeferMode | `${DeferMode}`): ManyToOneOptionsBuilder<TargetValue>;
|
|
376
382
|
}
|
|
377
|
-
|
|
383
|
+
/** @internal */
|
|
384
|
+
export declare class OneToManyOptionsBuilder<TargetValue extends object> extends ReferenceOptionsBuilder<TargetValue> {
|
|
378
385
|
'~options': ({
|
|
379
386
|
kind: '1:m';
|
|
380
387
|
entity: () => EntitySchema<TargetValue>;
|
|
@@ -399,7 +406,8 @@ declare class OneToManyOptionsBuilder<TargetValue extends object> extends Refere
|
|
|
399
406
|
/** Override the default database column name on the target entity (see {@doclink naming-strategy | Naming Strategy}). This option is suitable for composite keys, where one property is represented by multiple columns. */
|
|
400
407
|
referencedColumnNames(...referencedColumnNames: string[]): OneToManyOptionsBuilder<TargetValue>;
|
|
401
408
|
}
|
|
402
|
-
|
|
409
|
+
/** @internal */
|
|
410
|
+
export declare class OneToManyOptionsBuilderOnlyMappedBy<TargetValue extends object> {
|
|
403
411
|
'~options': ({
|
|
404
412
|
kind: '1:m';
|
|
405
413
|
entity: () => EntitySchema<TargetValue>;
|
|
@@ -408,7 +416,8 @@ declare class OneToManyOptionsBuilderOnlyMappedBy<TargetValue extends object> {
|
|
|
408
416
|
/** Point to the owning side property name. */
|
|
409
417
|
mappedBy(mappedBy: (AnyString & keyof UnwrapCollection<TargetValue>) | ((e: UnwrapCollection<TargetValue>) => any)): OneToManyOptionsBuilder<TargetValue>;
|
|
410
418
|
}
|
|
411
|
-
|
|
419
|
+
/** @internal */
|
|
420
|
+
export declare class OneToOneOptionsBuilder<TargetValue extends object> extends ReferenceOptionsBuilder<TargetValue> {
|
|
412
421
|
'~options': ({
|
|
413
422
|
kind: '1:1';
|
|
414
423
|
entity: () => EntitySchema<any, any>;
|
|
@@ -510,7 +519,7 @@ type InferPropertyValueType<T extends PropertyValueType> = T extends string ? In
|
|
|
510
519
|
type InferTypeByString<T extends string> = T extends keyof typeof types ? InferJSType<typeof types[T]> : InferColumnType<T>;
|
|
511
520
|
type InferJSType<T> = T extends typeof Type<infer TValue, any> ? NonNullable<TValue> : never;
|
|
512
521
|
type InferColumnType<T extends string> = T extends 'int' | 'int4' | 'integer' | 'bigint' | 'int8' | 'int2' | 'tinyint' | 'smallint' | 'mediumint' ? number : T extends 'double' | 'double precision' | 'real' | 'float8' | 'decimal' | 'numeric' | 'float' | 'float4' ? number : T extends 'datetime' | 'time' | 'time with time zone' | 'timestamp' | 'timestamp with time zone' | 'timetz' | 'timestamptz' | 'date' | 'interval' ? Date : T extends 'ObjectId' | 'objectId' | 'character varying' | 'varchar' | 'char' | 'character' | 'uuid' | 'text' | 'tinytext' | 'mediumtext' | 'longtext' | 'enum' ? string : T extends 'boolean' | 'bool' | 'bit' ? boolean : T extends 'blob' | 'tinyblob' | 'mediumblob' | 'longblob' | 'bytea' ? Buffer : T extends 'point' | 'line' | 'lseg' | 'box' | 'circle' | 'path' | 'polygon' | 'geometry' ? number[] : T extends 'tsvector' | 'tsquery' ? string[] : T extends 'json' | 'jsonb' ? any : any;
|
|
513
|
-
type InferEntityFromProperties<Properties extends Record<string, any>> = {
|
|
522
|
+
export type InferEntityFromProperties<Properties extends Record<string, any>> = {
|
|
514
523
|
-readonly [K in keyof Properties]: Properties[K] extends (() => any) ? InferBuilderValue<ReturnType<Properties[K]>> : InferBuilderValue<Properties[K]>;
|
|
515
524
|
};
|
|
516
525
|
type InferBuilderValue<Builder> = Builder extends {
|
package/entity/defineEntity.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OneToOneOptionsBuilder = exports.OneToManyOptionsBuilderOnlyMappedBy = exports.OneToManyOptionsBuilder = exports.ManyToOneOptionsBuilder = exports.ManyToManyOptionsBuilder = exports.ReferenceOptionsBuilder = exports.EmbeddedOptionsBuilder = exports.EnumOptionsBuilder = exports.PropertyOptionsBuilder = void 0;
|
|
3
4
|
exports.defineEntity = defineEntity;
|
|
4
5
|
const types_1 = require("../types");
|
|
5
6
|
const EntitySchema_1 = require("../metadata/EntitySchema");
|
|
7
|
+
/** @internal */
|
|
6
8
|
class PropertyOptionsBuilder {
|
|
7
9
|
'~options';
|
|
8
10
|
'~type';
|
|
@@ -325,6 +327,8 @@ class PropertyOptionsBuilder {
|
|
|
325
327
|
return new PropertyOptionsBuilder({ ...this['~options'] });
|
|
326
328
|
}
|
|
327
329
|
}
|
|
330
|
+
exports.PropertyOptionsBuilder = PropertyOptionsBuilder;
|
|
331
|
+
/** @internal */
|
|
328
332
|
class EnumOptionsBuilder extends PropertyOptionsBuilder {
|
|
329
333
|
constructor(options) {
|
|
330
334
|
super(options);
|
|
@@ -338,6 +342,8 @@ class EnumOptionsBuilder extends PropertyOptionsBuilder {
|
|
|
338
342
|
return new EnumOptionsBuilder({ ...this['~options'], nativeEnumName });
|
|
339
343
|
}
|
|
340
344
|
}
|
|
345
|
+
exports.EnumOptionsBuilder = EnumOptionsBuilder;
|
|
346
|
+
/** @internal */
|
|
341
347
|
class EmbeddedOptionsBuilder extends PropertyOptionsBuilder {
|
|
342
348
|
constructor(options) {
|
|
343
349
|
super(options);
|
|
@@ -356,6 +362,8 @@ class EmbeddedOptionsBuilder extends PropertyOptionsBuilder {
|
|
|
356
362
|
return new EmbeddedOptionsBuilder({ ...this['~options'], array });
|
|
357
363
|
}
|
|
358
364
|
}
|
|
365
|
+
exports.EmbeddedOptionsBuilder = EmbeddedOptionsBuilder;
|
|
366
|
+
/** @internal */
|
|
359
367
|
class ReferenceOptionsBuilder extends PropertyOptionsBuilder {
|
|
360
368
|
constructor(options) {
|
|
361
369
|
super(options);
|
|
@@ -390,6 +398,8 @@ class ReferenceOptionsBuilder extends PropertyOptionsBuilder {
|
|
|
390
398
|
return new ReferenceOptionsBuilder({ ...this['~options'], primary });
|
|
391
399
|
}
|
|
392
400
|
}
|
|
401
|
+
exports.ReferenceOptionsBuilder = ReferenceOptionsBuilder;
|
|
402
|
+
/** @internal */
|
|
393
403
|
class ManyToManyOptionsBuilder extends ReferenceOptionsBuilder {
|
|
394
404
|
constructor(options) {
|
|
395
405
|
super(options);
|
|
@@ -464,6 +474,8 @@ class ManyToManyOptionsBuilder extends ReferenceOptionsBuilder {
|
|
|
464
474
|
return new ManyToManyOptionsBuilder({ ...this['~options'], updateRule });
|
|
465
475
|
}
|
|
466
476
|
}
|
|
477
|
+
exports.ManyToManyOptionsBuilder = ManyToManyOptionsBuilder;
|
|
478
|
+
/** @internal */
|
|
467
479
|
class ManyToOneOptionsBuilder extends ReferenceOptionsBuilder {
|
|
468
480
|
constructor(options) {
|
|
469
481
|
super(options);
|
|
@@ -518,6 +530,8 @@ class ManyToOneOptionsBuilder extends ReferenceOptionsBuilder {
|
|
|
518
530
|
return new ManyToOneOptionsBuilder({ ...this['~options'], deferMode });
|
|
519
531
|
}
|
|
520
532
|
}
|
|
533
|
+
exports.ManyToOneOptionsBuilder = ManyToOneOptionsBuilder;
|
|
534
|
+
/** @internal */
|
|
521
535
|
class OneToManyOptionsBuilder extends ReferenceOptionsBuilder {
|
|
522
536
|
constructor(options) {
|
|
523
537
|
super(options);
|
|
@@ -560,6 +574,8 @@ class OneToManyOptionsBuilder extends ReferenceOptionsBuilder {
|
|
|
560
574
|
return new OneToManyOptionsBuilder({ ...this['~options'], referencedColumnNames });
|
|
561
575
|
}
|
|
562
576
|
}
|
|
577
|
+
exports.OneToManyOptionsBuilder = OneToManyOptionsBuilder;
|
|
578
|
+
/** @internal */
|
|
563
579
|
class OneToManyOptionsBuilderOnlyMappedBy {
|
|
564
580
|
constructor(options) {
|
|
565
581
|
this['~options'] = options;
|
|
@@ -569,6 +585,8 @@ class OneToManyOptionsBuilderOnlyMappedBy {
|
|
|
569
585
|
return new OneToManyOptionsBuilder({ ...this['~options'], mappedBy });
|
|
570
586
|
}
|
|
571
587
|
}
|
|
588
|
+
exports.OneToManyOptionsBuilderOnlyMappedBy = OneToManyOptionsBuilderOnlyMappedBy;
|
|
589
|
+
/** @internal */
|
|
572
590
|
class OneToOneOptionsBuilder extends ReferenceOptionsBuilder {
|
|
573
591
|
constructor(options) {
|
|
574
592
|
super(options);
|
|
@@ -611,6 +629,7 @@ class OneToOneOptionsBuilder extends ReferenceOptionsBuilder {
|
|
|
611
629
|
return new OneToOneOptionsBuilder({ ...this['~options'], deferMode });
|
|
612
630
|
}
|
|
613
631
|
}
|
|
632
|
+
exports.OneToOneOptionsBuilder = OneToOneOptionsBuilder;
|
|
614
633
|
function createPropertyBuilders(options) {
|
|
615
634
|
return Object.fromEntries(Object.entries(options).map(([key, value]) => [key, () => new PropertyOptionsBuilder({ type: value })]));
|
|
616
635
|
}
|
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
|
@@ -55,6 +55,7 @@ export const DriverException = mod.DriverException;
|
|
|
55
55
|
export const EagerProps = mod.EagerProps;
|
|
56
56
|
export const Embeddable = mod.Embeddable;
|
|
57
57
|
export const Embedded = mod.Embedded;
|
|
58
|
+
export const EmbeddedOptionsBuilder = mod.EmbeddedOptionsBuilder;
|
|
58
59
|
export const EnsureRequestContext = mod.EnsureRequestContext;
|
|
59
60
|
export const Entity = mod.Entity;
|
|
60
61
|
export const EntityAssigner = mod.EntityAssigner;
|
|
@@ -75,6 +76,7 @@ export const EntityTransformer = mod.EntityTransformer;
|
|
|
75
76
|
export const EntityValidator = mod.EntityValidator;
|
|
76
77
|
export const Enum = mod.Enum;
|
|
77
78
|
export const EnumArrayType = mod.EnumArrayType;
|
|
79
|
+
export const EnumOptionsBuilder = mod.EnumOptionsBuilder;
|
|
78
80
|
export const EnumType = mod.EnumType;
|
|
79
81
|
export const EventManager = mod.EventManager;
|
|
80
82
|
export const EventType = mod.EventType;
|
|
@@ -103,7 +105,9 @@ export const LoadStrategy = mod.LoadStrategy;
|
|
|
103
105
|
export const LockMode = mod.LockMode;
|
|
104
106
|
export const LockWaitTimeoutException = mod.LockWaitTimeoutException;
|
|
105
107
|
export const ManyToMany = mod.ManyToMany;
|
|
108
|
+
export const ManyToManyOptionsBuilder = mod.ManyToManyOptionsBuilder;
|
|
106
109
|
export const ManyToOne = mod.ManyToOne;
|
|
110
|
+
export const ManyToOneOptionsBuilder = mod.ManyToOneOptionsBuilder;
|
|
107
111
|
export const MediumIntType = mod.MediumIntType;
|
|
108
112
|
export const MemoryCacheAdapter = mod.MemoryCacheAdapter;
|
|
109
113
|
export const MetadataDiscovery = mod.MetadataDiscovery;
|
|
@@ -124,7 +128,10 @@ export const ObjectHydrator = mod.ObjectHydrator;
|
|
|
124
128
|
export const OnInit = mod.OnInit;
|
|
125
129
|
export const OnLoad = mod.OnLoad;
|
|
126
130
|
export const OneToMany = mod.OneToMany;
|
|
131
|
+
export const OneToManyOptionsBuilder = mod.OneToManyOptionsBuilder;
|
|
132
|
+
export const OneToManyOptionsBuilderOnlyMappedBy = mod.OneToManyOptionsBuilderOnlyMappedBy;
|
|
127
133
|
export const OneToOne = mod.OneToOne;
|
|
134
|
+
export const OneToOneOptionsBuilder = mod.OneToOneOptionsBuilder;
|
|
128
135
|
export const OptimisticLockError = mod.OptimisticLockError;
|
|
129
136
|
export const OptionalProps = mod.OptionalProps;
|
|
130
137
|
export const PlainObject = mod.PlainObject;
|
|
@@ -134,6 +141,7 @@ export const PopulatePath = mod.PopulatePath;
|
|
|
134
141
|
export const PrimaryKey = mod.PrimaryKey;
|
|
135
142
|
export const PrimaryKeyProp = mod.PrimaryKeyProp;
|
|
136
143
|
export const Property = mod.Property;
|
|
144
|
+
export const PropertyOptionsBuilder = mod.PropertyOptionsBuilder;
|
|
137
145
|
export const QueryFlag = mod.QueryFlag;
|
|
138
146
|
export const QueryHelper = mod.QueryHelper;
|
|
139
147
|
export const QueryOperator = mod.QueryOperator;
|
|
@@ -144,6 +152,7 @@ export const ReadOnlyException = mod.ReadOnlyException;
|
|
|
144
152
|
export const Ref = mod.Ref;
|
|
145
153
|
export const Reference = mod.Reference;
|
|
146
154
|
export const ReferenceKind = mod.ReferenceKind;
|
|
155
|
+
export const ReferenceOptionsBuilder = mod.ReferenceOptionsBuilder;
|
|
147
156
|
export const ReflectMetadataProvider = mod.ReflectMetadataProvider;
|
|
148
157
|
export const RequestContext = mod.RequestContext;
|
|
149
158
|
export const SCALAR_TYPES = mod.SCALAR_TYPES;
|
|
@@ -162,6 +171,9 @@ export const TimeType = mod.TimeType;
|
|
|
162
171
|
export const TinyIntType = mod.TinyIntType;
|
|
163
172
|
export const TransactionContext = mod.TransactionContext;
|
|
164
173
|
export const TransactionEventBroadcaster = mod.TransactionEventBroadcaster;
|
|
174
|
+
export const TransactionManager = mod.TransactionManager;
|
|
175
|
+
export const TransactionPropagation = mod.TransactionPropagation;
|
|
176
|
+
export const TransactionStateError = mod.TransactionStateError;
|
|
165
177
|
export const Transactional = mod.Transactional;
|
|
166
178
|
export const Type = mod.Type;
|
|
167
179
|
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.
|
|
3
|
+
"version": "6.4.17-dev.96",
|
|
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.
|
|
67
|
+
"mikro-orm": "6.4.17-dev.96",
|
|
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);
|