@mikro-orm/core 7.0.0-dev.1 → 7.0.0-dev.100
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.d.ts +96 -58
- package/EntityManager.js +465 -395
- package/MikroORM.d.ts +45 -35
- package/MikroORM.js +109 -160
- package/README.md +3 -2
- package/cache/CacheAdapter.js +1 -2
- package/cache/FileCacheAdapter.d.ts +2 -2
- package/cache/FileCacheAdapter.js +20 -27
- package/cache/GeneratedCacheAdapter.d.ts +2 -3
- package/cache/GeneratedCacheAdapter.js +1 -7
- package/cache/MemoryCacheAdapter.d.ts +1 -1
- package/cache/MemoryCacheAdapter.js +1 -5
- package/cache/NullCacheAdapter.d.ts +1 -1
- package/cache/NullCacheAdapter.js +1 -5
- package/cache/index.d.ts +4 -5
- package/cache/index.js +4 -21
- package/connections/Connection.d.ts +22 -14
- package/connections/Connection.js +27 -23
- package/connections/index.d.ts +1 -1
- package/connections/index.js +1 -17
- package/drivers/DatabaseDriver.d.ts +25 -15
- package/drivers/DatabaseDriver.js +77 -64
- package/drivers/IDatabaseDriver.d.ts +40 -16
- package/drivers/IDatabaseDriver.js +1 -4
- package/drivers/index.d.ts +2 -2
- package/drivers/index.js +2 -18
- package/entity/BaseEntity.d.ts +6 -7
- package/entity/BaseEntity.js +16 -23
- package/entity/Collection.d.ts +98 -34
- package/entity/Collection.js +466 -131
- package/entity/EntityAssigner.d.ts +3 -3
- package/entity/EntityAssigner.js +67 -64
- package/entity/EntityFactory.d.ts +10 -3
- package/entity/EntityFactory.js +112 -91
- package/entity/EntityHelper.d.ts +2 -2
- package/entity/EntityHelper.js +66 -53
- package/entity/EntityIdentifier.d.ts +1 -1
- package/entity/EntityIdentifier.js +1 -5
- package/entity/EntityLoader.d.ts +8 -7
- package/entity/EntityLoader.js +161 -128
- package/entity/EntityRepository.d.ts +8 -8
- package/entity/EntityRepository.js +7 -11
- package/entity/Reference.d.ts +10 -13
- package/entity/Reference.js +64 -46
- package/entity/WrappedEntity.d.ts +12 -17
- package/entity/WrappedEntity.js +22 -31
- package/entity/defineEntity.d.ts +568 -0
- package/entity/defineEntity.js +529 -0
- package/entity/index.d.ts +14 -13
- package/entity/index.js +14 -29
- package/entity/utils.d.ts +8 -1
- package/entity/utils.js +22 -13
- package/entity/validators.d.ts +11 -0
- package/entity/validators.js +65 -0
- package/entity/wrap.d.ts +1 -1
- package/entity/wrap.js +2 -6
- package/enums.d.ts +24 -9
- package/enums.js +50 -41
- package/errors.d.ts +11 -3
- package/errors.js +42 -32
- package/events/EventManager.d.ts +5 -4
- package/events/EventManager.js +26 -22
- package/events/EventSubscriber.d.ts +8 -5
- package/events/EventSubscriber.js +1 -2
- package/events/TransactionEventBroadcaster.d.ts +3 -3
- package/events/TransactionEventBroadcaster.js +1 -5
- package/events/index.d.ts +3 -3
- package/events/index.js +3 -19
- package/exceptions.js +18 -39
- package/hydration/Hydrator.d.ts +5 -5
- package/hydration/Hydrator.js +2 -6
- package/hydration/ObjectHydrator.d.ts +7 -7
- package/hydration/ObjectHydrator.js +58 -50
- package/hydration/index.d.ts +2 -2
- package/hydration/index.js +2 -18
- package/index.d.ts +21 -21
- package/index.js +20 -46
- package/logging/DefaultLogger.d.ts +2 -2
- package/logging/DefaultLogger.js +10 -13
- package/logging/Logger.d.ts +1 -1
- package/logging/Logger.js +1 -2
- package/logging/SimpleLogger.d.ts +3 -3
- package/logging/SimpleLogger.js +2 -6
- package/logging/colors.js +1 -5
- package/logging/index.d.ts +5 -4
- package/logging/index.js +5 -20
- package/logging/inspect.d.ts +2 -0
- package/logging/inspect.js +16 -0
- package/metadata/EntitySchema.d.ts +14 -10
- package/metadata/EntitySchema.js +78 -64
- package/metadata/MetadataDiscovery.d.ts +11 -14
- package/metadata/MetadataDiscovery.js +278 -317
- package/metadata/MetadataProvider.d.ts +13 -4
- package/metadata/MetadataProvider.js +47 -8
- package/metadata/MetadataStorage.d.ts +2 -7
- package/metadata/MetadataStorage.js +19 -35
- package/metadata/MetadataValidator.d.ts +3 -10
- package/metadata/MetadataValidator.js +51 -64
- package/metadata/discover-entities.d.ts +5 -0
- package/metadata/discover-entities.js +40 -0
- package/metadata/index.d.ts +6 -6
- package/metadata/index.js +6 -22
- package/metadata/types.d.ts +480 -0
- package/metadata/types.js +1 -0
- package/naming-strategy/AbstractNamingStrategy.d.ts +7 -3
- package/naming-strategy/AbstractNamingStrategy.js +11 -9
- package/naming-strategy/EntityCaseNamingStrategy.d.ts +1 -1
- package/naming-strategy/EntityCaseNamingStrategy.js +2 -6
- package/naming-strategy/MongoNamingStrategy.d.ts +1 -1
- package/naming-strategy/MongoNamingStrategy.js +2 -6
- package/naming-strategy/NamingStrategy.d.ts +12 -2
- package/naming-strategy/NamingStrategy.js +1 -2
- package/naming-strategy/UnderscoreNamingStrategy.d.ts +1 -1
- package/naming-strategy/UnderscoreNamingStrategy.js +2 -6
- package/naming-strategy/index.d.ts +5 -5
- package/naming-strategy/index.js +5 -21
- package/not-supported.d.ts +2 -0
- package/not-supported.js +4 -0
- package/package.json +19 -20
- package/platforms/ExceptionConverter.d.ts +2 -2
- package/platforms/ExceptionConverter.js +4 -8
- package/platforms/Platform.d.ts +15 -22
- package/platforms/Platform.js +58 -88
- package/platforms/index.d.ts +2 -2
- package/platforms/index.js +2 -18
- package/serialization/EntitySerializer.d.ts +4 -2
- package/serialization/EntitySerializer.js +64 -51
- package/serialization/EntityTransformer.d.ts +1 -1
- package/serialization/EntityTransformer.js +48 -42
- package/serialization/SerializationContext.d.ts +2 -2
- package/serialization/SerializationContext.js +24 -25
- package/serialization/index.d.ts +3 -3
- package/serialization/index.js +3 -19
- package/types/ArrayType.d.ts +3 -3
- package/types/ArrayType.js +6 -11
- package/types/BigIntType.d.ts +12 -9
- package/types/BigIntType.js +6 -6
- package/types/BlobType.d.ts +3 -4
- package/types/BlobType.js +2 -11
- package/types/BooleanType.d.ts +5 -4
- package/types/BooleanType.js +5 -6
- package/types/CharacterType.d.ts +3 -3
- package/types/CharacterType.js +2 -6
- package/types/DateTimeType.d.ts +3 -3
- package/types/DateTimeType.js +2 -6
- package/types/DateType.d.ts +3 -3
- package/types/DateType.js +2 -6
- package/types/DecimalType.d.ts +9 -7
- package/types/DecimalType.js +5 -8
- package/types/DoubleType.d.ts +3 -3
- package/types/DoubleType.js +4 -7
- package/types/EnumArrayType.d.ts +4 -4
- package/types/EnumArrayType.js +4 -10
- package/types/EnumType.d.ts +3 -3
- package/types/EnumType.js +2 -6
- package/types/FloatType.d.ts +3 -3
- package/types/FloatType.js +2 -6
- package/types/IntegerType.d.ts +3 -3
- package/types/IntegerType.js +2 -6
- package/types/IntervalType.d.ts +3 -3
- package/types/IntervalType.js +2 -6
- package/types/JsonType.d.ts +4 -4
- package/types/JsonType.js +9 -8
- package/types/MediumIntType.d.ts +3 -3
- package/types/MediumIntType.js +2 -6
- package/types/SmallIntType.d.ts +3 -3
- package/types/SmallIntType.js +2 -6
- package/types/StringType.d.ts +3 -3
- package/types/StringType.js +2 -6
- package/types/TextType.d.ts +3 -3
- package/types/TextType.js +2 -6
- package/types/TimeType.d.ts +3 -3
- package/types/TimeType.js +4 -8
- package/types/TinyIntType.d.ts +3 -3
- package/types/TinyIntType.js +3 -6
- package/types/Type.d.ts +4 -6
- package/types/Type.js +6 -10
- package/types/Uint8ArrayType.d.ts +3 -4
- package/types/Uint8ArrayType.js +3 -12
- package/types/UnknownType.d.ts +3 -3
- package/types/UnknownType.js +2 -6
- package/types/UuidType.d.ts +3 -3
- package/types/UuidType.js +2 -6
- package/types/index.d.ts +25 -25
- package/types/index.js +52 -79
- package/typings.d.ts +134 -93
- package/typings.js +67 -65
- package/unit-of-work/ChangeSet.d.ts +1 -4
- package/unit-of-work/ChangeSet.js +13 -17
- package/unit-of-work/ChangeSetComputer.d.ts +8 -9
- package/unit-of-work/ChangeSetComputer.js +36 -38
- package/unit-of-work/ChangeSetPersister.d.ts +11 -9
- package/unit-of-work/ChangeSetPersister.js +100 -65
- package/unit-of-work/CommitOrderCalculator.d.ts +1 -1
- package/unit-of-work/CommitOrderCalculator.js +6 -10
- package/unit-of-work/IdentityMap.d.ts +1 -1
- package/unit-of-work/IdentityMap.js +1 -5
- package/unit-of-work/UnitOfWork.d.ts +16 -8
- package/unit-of-work/UnitOfWork.js +266 -209
- package/unit-of-work/index.d.ts +6 -6
- package/unit-of-work/index.js +6 -22
- package/utils/AbstractSchemaGenerator.d.ts +11 -11
- package/utils/AbstractSchemaGenerator.js +21 -20
- package/utils/Configuration.d.ts +774 -224
- package/utils/Configuration.js +166 -216
- package/utils/ConfigurationLoader.d.ts +1 -53
- package/utils/ConfigurationLoader.js +1 -367
- package/utils/Cursor.d.ts +6 -9
- package/utils/Cursor.js +25 -25
- package/utils/DataloaderUtils.d.ts +18 -8
- package/utils/DataloaderUtils.js +63 -21
- package/utils/EntityComparator.d.ts +9 -5
- package/utils/EntityComparator.js +155 -108
- package/utils/NullHighlighter.d.ts +1 -1
- package/utils/NullHighlighter.js +1 -5
- package/utils/QueryHelper.d.ts +12 -4
- package/utils/QueryHelper.js +110 -53
- package/utils/RawQueryFragment.d.ts +37 -14
- package/utils/RawQueryFragment.js +50 -33
- package/utils/RequestContext.d.ts +2 -2
- package/utils/RequestContext.js +3 -7
- package/utils/TransactionContext.d.ts +1 -1
- package/utils/TransactionContext.js +4 -8
- package/utils/TransactionManager.d.ts +65 -0
- package/utils/TransactionManager.js +223 -0
- package/utils/Utils.d.ts +16 -100
- package/utils/Utils.js +114 -332
- package/utils/clone.js +7 -11
- package/utils/env-vars.d.ts +3 -0
- package/utils/env-vars.js +87 -0
- package/utils/fs-utils.d.ts +12 -0
- package/utils/fs-utils.js +97 -0
- package/utils/index.d.ts +14 -13
- package/utils/index.js +14 -29
- package/utils/upsert-utils.d.ts +8 -3
- package/utils/upsert-utils.js +57 -10
- package/decorators/Check.d.ts +0 -3
- package/decorators/Check.js +0 -16
- package/decorators/CreateRequestContext.d.ts +0 -3
- package/decorators/CreateRequestContext.js +0 -33
- package/decorators/Embeddable.d.ts +0 -8
- package/decorators/Embeddable.js +0 -14
- package/decorators/Embedded.d.ts +0 -18
- package/decorators/Embedded.js +0 -20
- package/decorators/Entity.d.ts +0 -18
- package/decorators/Entity.js +0 -16
- package/decorators/Enum.d.ts +0 -9
- package/decorators/Enum.js +0 -19
- package/decorators/Filter.d.ts +0 -2
- package/decorators/Filter.js +0 -11
- package/decorators/Formula.d.ts +0 -5
- package/decorators/Formula.js +0 -18
- package/decorators/Indexed.d.ts +0 -17
- package/decorators/Indexed.js +0 -24
- package/decorators/ManyToMany.d.ts +0 -40
- package/decorators/ManyToMany.js +0 -16
- package/decorators/ManyToOne.d.ts +0 -30
- package/decorators/ManyToOne.js +0 -16
- package/decorators/OneToMany.d.ts +0 -28
- package/decorators/OneToMany.js +0 -20
- package/decorators/OneToOne.d.ts +0 -24
- package/decorators/OneToOne.js +0 -10
- package/decorators/PrimaryKey.d.ts +0 -9
- package/decorators/PrimaryKey.js +0 -23
- package/decorators/Property.d.ts +0 -250
- package/decorators/Property.js +0 -34
- package/decorators/Transactional.d.ts +0 -13
- package/decorators/Transactional.js +0 -31
- package/decorators/hooks.d.ts +0 -16
- package/decorators/hooks.js +0 -59
- package/decorators/index.d.ts +0 -17
- package/decorators/index.js +0 -36
- package/entity/ArrayCollection.d.ts +0 -116
- package/entity/ArrayCollection.js +0 -399
- package/entity/EntityValidator.d.ts +0 -19
- package/entity/EntityValidator.js +0 -154
- package/index.mjs +0 -199
- package/metadata/ReflectMetadataProvider.d.ts +0 -8
- package/metadata/ReflectMetadataProvider.js +0 -48
- package/utils/resolveContextProvider.d.ts +0 -10
- package/utils/resolveContextProvider.js +0 -31
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { EntityManager } from '../EntityManager.js';
|
|
2
|
+
import { type TransactionOptions } from '../enums.js';
|
|
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,223 @@
|
|
|
1
|
+
import { ReferenceKind, TransactionPropagation } from '../enums.js';
|
|
2
|
+
import { TransactionEventBroadcaster } from '../events/TransactionEventBroadcaster.js';
|
|
3
|
+
import { TransactionContext } from '../utils/TransactionContext.js';
|
|
4
|
+
import { ChangeSetType } from '../unit-of-work/ChangeSet.js';
|
|
5
|
+
import { TransactionStateError } from '../errors.js';
|
|
6
|
+
import { helper } from '../entity/wrap.js';
|
|
7
|
+
/**
|
|
8
|
+
* Manages transaction lifecycle and propagation for EntityManager.
|
|
9
|
+
*/
|
|
10
|
+
export class TransactionManager {
|
|
11
|
+
em;
|
|
12
|
+
constructor(em) {
|
|
13
|
+
this.em = em;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Main entry point for handling transactional operations with propagation support.
|
|
17
|
+
*/
|
|
18
|
+
async handle(cb, options = {}) {
|
|
19
|
+
const em = this.em.getContext(false);
|
|
20
|
+
options.propagation ??= TransactionPropagation.NESTED;
|
|
21
|
+
options.ctx ??= em.getTransactionContext();
|
|
22
|
+
const hasExistingTransaction = !!em.getTransactionContext();
|
|
23
|
+
return this.executeWithPropagation(options.propagation, em, cb, options, hasExistingTransaction);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Executes the callback with the specified propagation type.
|
|
27
|
+
*/
|
|
28
|
+
async executeWithPropagation(propagation, em, cb, options, hasExistingTransaction) {
|
|
29
|
+
switch (propagation) {
|
|
30
|
+
case TransactionPropagation.NOT_SUPPORTED:
|
|
31
|
+
return this.executeWithoutTransaction(em, cb, options);
|
|
32
|
+
case TransactionPropagation.REQUIRES_NEW:
|
|
33
|
+
return this.executeWithNewTransaction(em, cb, options, hasExistingTransaction);
|
|
34
|
+
case TransactionPropagation.REQUIRED:
|
|
35
|
+
if (hasExistingTransaction) {
|
|
36
|
+
return cb(em);
|
|
37
|
+
}
|
|
38
|
+
return this.createNewTransaction(em, cb, options);
|
|
39
|
+
case TransactionPropagation.NESTED:
|
|
40
|
+
if (hasExistingTransaction) {
|
|
41
|
+
return this.executeNestedTransaction(em, cb, options);
|
|
42
|
+
}
|
|
43
|
+
return this.createNewTransaction(em, cb, options);
|
|
44
|
+
case TransactionPropagation.SUPPORTS:
|
|
45
|
+
if (hasExistingTransaction) {
|
|
46
|
+
return cb(em);
|
|
47
|
+
}
|
|
48
|
+
return this.executeWithoutTransaction(em, cb, options);
|
|
49
|
+
case TransactionPropagation.MANDATORY:
|
|
50
|
+
if (!hasExistingTransaction) {
|
|
51
|
+
throw TransactionStateError.requiredTransactionNotFound(propagation);
|
|
52
|
+
}
|
|
53
|
+
return cb(em);
|
|
54
|
+
case TransactionPropagation.NEVER:
|
|
55
|
+
if (hasExistingTransaction) {
|
|
56
|
+
throw TransactionStateError.transactionNotAllowed(propagation);
|
|
57
|
+
}
|
|
58
|
+
return this.executeWithoutTransaction(em, cb, options);
|
|
59
|
+
default:
|
|
60
|
+
throw TransactionStateError.invalidPropagation(propagation);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Suspends the current transaction and returns the suspended resources.
|
|
65
|
+
*/
|
|
66
|
+
suspendTransaction(em) {
|
|
67
|
+
const suspended = em.getTransactionContext();
|
|
68
|
+
em.resetTransactionContext();
|
|
69
|
+
return suspended;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Resumes a previously suspended transaction.
|
|
73
|
+
*/
|
|
74
|
+
resumeTransaction(em, suspended) {
|
|
75
|
+
if (suspended != null) {
|
|
76
|
+
em.setTransactionContext(suspended);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Executes operation without transaction context.
|
|
81
|
+
*/
|
|
82
|
+
async executeWithoutTransaction(em, cb, options) {
|
|
83
|
+
const suspended = this.suspendTransaction(em);
|
|
84
|
+
const fork = this.createFork(em, { ...options, disableTransactions: true });
|
|
85
|
+
const propagateToUpperContext = this.shouldPropagateToUpperContext(em);
|
|
86
|
+
try {
|
|
87
|
+
return await this.executeTransactionFlow(fork, cb, propagateToUpperContext, em);
|
|
88
|
+
}
|
|
89
|
+
finally {
|
|
90
|
+
this.resumeTransaction(em, suspended);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Creates new independent transaction, suspending any existing one.
|
|
95
|
+
*/
|
|
96
|
+
async executeWithNewTransaction(em, cb, options, hasExistingTransaction) {
|
|
97
|
+
const fork = this.createFork(em, options);
|
|
98
|
+
let suspended = null;
|
|
99
|
+
// Suspend existing transaction if present
|
|
100
|
+
if (hasExistingTransaction) {
|
|
101
|
+
suspended = this.suspendTransaction(em);
|
|
102
|
+
}
|
|
103
|
+
const newOptions = { ...options, ctx: undefined };
|
|
104
|
+
try {
|
|
105
|
+
return await this.processTransaction(em, fork, cb, newOptions);
|
|
106
|
+
}
|
|
107
|
+
finally {
|
|
108
|
+
if (suspended != null) {
|
|
109
|
+
this.resumeTransaction(em, suspended);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Creates new transaction context.
|
|
115
|
+
*/
|
|
116
|
+
async createNewTransaction(em, cb, options) {
|
|
117
|
+
const fork = this.createFork(em, options);
|
|
118
|
+
return this.processTransaction(em, fork, cb, options);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Executes nested transaction with savepoint.
|
|
122
|
+
*/
|
|
123
|
+
async executeNestedTransaction(em, cb, options) {
|
|
124
|
+
const fork = this.createFork(em, options);
|
|
125
|
+
// Pass existing context to create savepoint
|
|
126
|
+
const nestedOptions = { ...options, ctx: em.getTransactionContext() };
|
|
127
|
+
return this.processTransaction(em, fork, cb, nestedOptions);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Creates a fork of the EntityManager with the given options.
|
|
131
|
+
*/
|
|
132
|
+
createFork(em, options) {
|
|
133
|
+
return em.fork({
|
|
134
|
+
clear: options.clear ?? false,
|
|
135
|
+
flushMode: options.flushMode,
|
|
136
|
+
cloneEventManager: true,
|
|
137
|
+
disableTransactions: options.ignoreNestedTransactions,
|
|
138
|
+
loggerContext: options.loggerContext,
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Determines if changes should be propagated to the upper context.
|
|
143
|
+
*/
|
|
144
|
+
shouldPropagateToUpperContext(em) {
|
|
145
|
+
return !em.global || this.em.config.get('allowGlobalContext');
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Merges entities from fork to parent EntityManager.
|
|
149
|
+
*/
|
|
150
|
+
mergeEntitiesToParent(fork, parent) {
|
|
151
|
+
const parentUoW = parent.getUnitOfWork(false);
|
|
152
|
+
// perf: if parent is empty, we can just move all entities from the fork to skip the `em.merge` overhead
|
|
153
|
+
if (parentUoW.getIdentityMap().keys().length === 0) {
|
|
154
|
+
for (const entity of fork.getUnitOfWork(false).getIdentityMap()) {
|
|
155
|
+
parentUoW.getIdentityMap().store(entity);
|
|
156
|
+
helper(entity).__em = parent;
|
|
157
|
+
}
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
for (const entity of fork.getUnitOfWork(false).getIdentityMap()) {
|
|
161
|
+
const wrapped = helper(entity);
|
|
162
|
+
const meta = wrapped.__meta;
|
|
163
|
+
// eslint-disable-next-line dot-notation
|
|
164
|
+
const parentEntity = parentUoW.getById(meta.className, wrapped.getPrimaryKey(), parent['_schema'], true);
|
|
165
|
+
if (parentEntity && parentEntity !== entity) {
|
|
166
|
+
const parentWrapped = helper(parentEntity);
|
|
167
|
+
parentWrapped.__data = wrapped.__data;
|
|
168
|
+
parentWrapped.__originalEntityData = wrapped.__originalEntityData;
|
|
169
|
+
for (const prop of meta.hydrateProps) {
|
|
170
|
+
if (prop.kind === ReferenceKind.SCALAR) {
|
|
171
|
+
parentEntity[prop.name] = entity[prop.name];
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
parentUoW.merge(entity, new Set([entity]));
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Registers a deletion handler to unset entity identities after flush.
|
|
182
|
+
*/
|
|
183
|
+
registerDeletionHandler(fork, parent) {
|
|
184
|
+
fork.getEventManager().registerSubscriber({
|
|
185
|
+
afterFlush: (args) => {
|
|
186
|
+
const deletionChangeSets = args.uow.getChangeSets()
|
|
187
|
+
.filter(cs => cs.type === ChangeSetType.DELETE || cs.type === ChangeSetType.DELETE_EARLY);
|
|
188
|
+
for (const cs of deletionChangeSets) {
|
|
189
|
+
parent.getUnitOfWork(false).unsetIdentity(cs.entity);
|
|
190
|
+
}
|
|
191
|
+
},
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Processes transaction execution.
|
|
196
|
+
*/
|
|
197
|
+
async processTransaction(em, fork, cb, options) {
|
|
198
|
+
const propagateToUpperContext = this.shouldPropagateToUpperContext(em);
|
|
199
|
+
const eventBroadcaster = new TransactionEventBroadcaster(fork, undefined);
|
|
200
|
+
return TransactionContext.create(fork, () => fork.getConnection().transactional(async (trx) => {
|
|
201
|
+
fork.setTransactionContext(trx);
|
|
202
|
+
return this.executeTransactionFlow(fork, cb, propagateToUpperContext, em);
|
|
203
|
+
}, { ...options, eventBroadcaster }));
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Executes transaction workflow with entity synchronization.
|
|
207
|
+
*/
|
|
208
|
+
async executeTransactionFlow(fork, cb, propagateToUpperContext, parentEm) {
|
|
209
|
+
if (!propagateToUpperContext) {
|
|
210
|
+
const ret = await cb(fork);
|
|
211
|
+
await fork.flush();
|
|
212
|
+
return ret;
|
|
213
|
+
}
|
|
214
|
+
// Setup: Register deletion handler before execution
|
|
215
|
+
this.registerDeletionHandler(fork, parentEm);
|
|
216
|
+
// Execute callback and flush
|
|
217
|
+
const ret = await cb(fork);
|
|
218
|
+
await fork.flush();
|
|
219
|
+
// Synchronization: Merge entities back to the parent
|
|
220
|
+
this.mergeEntitiesToParent(fork, parentEm);
|
|
221
|
+
return ret;
|
|
222
|
+
}
|
|
223
|
+
}
|
package/utils/Utils.d.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
4
|
-
import type {
|
|
5
|
-
import type { ScalarReference } from '../entity/Reference';
|
|
6
|
-
export declare const ObjectBindingPattern: unique symbol;
|
|
1
|
+
import type { Dictionary, EntityData, EntityDictionary, EntityKey, EntityMetadata, EntityName, EntityProperty, Primary } from '../typings.js';
|
|
2
|
+
import type { Collection } from '../entity/Collection.js';
|
|
3
|
+
import type { Platform } from '../platforms/Platform.js';
|
|
4
|
+
import type { ScalarReference } from '../entity/Reference.js';
|
|
7
5
|
export declare function compareObjects(a: any, b: any): boolean;
|
|
8
6
|
export declare function compareArrays(a: any[] | string, b: any[] | string): boolean;
|
|
9
7
|
export declare function compareBooleans(a: unknown, b: unknown): boolean;
|
|
@@ -14,34 +12,17 @@ export declare function compareBuffers(a: Uint8Array, b: Uint8Array): boolean;
|
|
|
14
12
|
export declare function equals(a: any, b: any): boolean;
|
|
15
13
|
export declare function parseJsonSafe<T = unknown>(value: unknown): T;
|
|
16
14
|
export declare class Utils {
|
|
15
|
+
#private;
|
|
17
16
|
static readonly PK_SEPARATOR = "~~~";
|
|
18
17
|
static dynamicImportProvider: (id: string) => Promise<any>;
|
|
19
|
-
/**
|
|
20
|
-
* Checks if the argument is not undefined
|
|
21
|
-
*/
|
|
22
|
-
static isDefined<T = Record<string, unknown>>(data: any): data is T;
|
|
23
18
|
/**
|
|
24
19
|
* Checks if the argument is instance of `Object`. Returns false for arrays.
|
|
25
20
|
*/
|
|
26
21
|
static isObject<T = Dictionary>(o: any): o is T;
|
|
27
|
-
/**
|
|
28
|
-
* Relation decorators allow using two signatures
|
|
29
|
-
* - using first parameter as options object
|
|
30
|
-
* - using all parameters
|
|
31
|
-
*
|
|
32
|
-
* This function validates those two ways are not mixed and returns the final options object.
|
|
33
|
-
* If the second way is used, we always consider the last parameter as options object.
|
|
34
|
-
* @internal
|
|
35
|
-
*/
|
|
36
|
-
static processDecoratorParameters<T>(params: Dictionary): T;
|
|
37
|
-
/**
|
|
38
|
-
* Checks if the argument is instance of `Object`, but not one of the blacklisted types. Returns false for arrays.
|
|
39
|
-
*/
|
|
40
|
-
static isNotObject<T = Dictionary>(o: any, not: any[]): o is T;
|
|
41
22
|
/**
|
|
42
23
|
* Removes `undefined` properties (recursively) so they are not saved as nulls
|
|
43
24
|
*/
|
|
44
|
-
static dropUndefinedProperties
|
|
25
|
+
static dropUndefinedProperties(o: any, value?: undefined | null, visited?: Set<unknown>): void;
|
|
45
26
|
/**
|
|
46
27
|
* Returns the number of properties on `obj`. This is 20x faster than Object.keys(obj).length.
|
|
47
28
|
* @see https://github.com/deepkit/deepkit-framework/blob/master/packages/core/src/core.ts
|
|
@@ -52,14 +33,6 @@ export declare class Utils {
|
|
|
52
33
|
* @see https://github.com/deepkit/deepkit-framework/blob/master/packages/core/src/core.ts
|
|
53
34
|
*/
|
|
54
35
|
static hasObjectKeys(object: Dictionary): boolean;
|
|
55
|
-
/**
|
|
56
|
-
* Checks if the argument is string
|
|
57
|
-
*/
|
|
58
|
-
static isString(s: any): s is string;
|
|
59
|
-
/**
|
|
60
|
-
* Checks if the argument is number
|
|
61
|
-
*/
|
|
62
|
-
static isNumber<T = number>(s: any): s is T;
|
|
63
36
|
/**
|
|
64
37
|
* Checks if arguments are deeply (but not strictly) equal.
|
|
65
38
|
*/
|
|
@@ -80,11 +53,6 @@ export declare class Utils {
|
|
|
80
53
|
* Merges all sources into the target recursively.
|
|
81
54
|
*/
|
|
82
55
|
private static _merge;
|
|
83
|
-
static getRootEntity(metadata: IMetadataStorage, meta: EntityMetadata): EntityMetadata;
|
|
84
|
-
/**
|
|
85
|
-
* Computes difference between two objects, ignoring items missing in `b`.
|
|
86
|
-
*/
|
|
87
|
-
static diff(a: Dictionary, b: Dictionary): Record<keyof (typeof a & typeof b), any>;
|
|
88
56
|
/**
|
|
89
57
|
* Creates deep copy of given object.
|
|
90
58
|
*/
|
|
@@ -102,26 +70,11 @@ export declare class Utils {
|
|
|
102
70
|
*/
|
|
103
71
|
static renameKey<T>(payload: T, from: string | keyof T, to: string): void;
|
|
104
72
|
/**
|
|
105
|
-
* Returns array of functions argument names. Uses
|
|
73
|
+
* Returns array of functions argument names. Uses basic regex for source code analysis, might not work with advanced syntax.
|
|
106
74
|
*/
|
|
107
|
-
static
|
|
75
|
+
static getConstructorParams(func: {
|
|
108
76
|
toString(): string;
|
|
109
|
-
}
|
|
110
|
-
type: string;
|
|
111
|
-
value: string;
|
|
112
|
-
}[]): {
|
|
113
|
-
type: string;
|
|
114
|
-
value: string;
|
|
115
|
-
}[];
|
|
116
|
-
/**
|
|
117
|
-
* Returns array of functions argument names. Uses `esprima` for source code analysis.
|
|
118
|
-
*/
|
|
119
|
-
static getParamNames(func: {
|
|
120
|
-
toString(): string;
|
|
121
|
-
} | string | {
|
|
122
|
-
type: string;
|
|
123
|
-
value: string;
|
|
124
|
-
}[], methodName?: string): string[];
|
|
77
|
+
}): string[] | undefined;
|
|
125
78
|
/**
|
|
126
79
|
* Checks whether the argument looks like primary key (string, number or ObjectId).
|
|
127
80
|
*/
|
|
@@ -134,14 +87,14 @@ export declare class Utils {
|
|
|
134
87
|
static getCompositeKeyHash<T>(data: EntityData<T>, meta: EntityMetadata<T>, convertCustomTypes?: boolean, platform?: Platform, flat?: boolean): string;
|
|
135
88
|
static getPrimaryKeyHash(pks: (string | Buffer | Date)[]): string;
|
|
136
89
|
static splitPrimaryKeys<T extends object>(key: string): EntityKey<T>[];
|
|
137
|
-
static getPrimaryKeyValues<T>(entity: T,
|
|
90
|
+
static getPrimaryKeyValues<T>(entity: T, meta: EntityMetadata<T>, allowScalar?: boolean, convertCustomTypes?: boolean): any;
|
|
138
91
|
static getPrimaryKeyCond<T>(entity: T, primaryKeys: EntityKey<T>[]): Record<string, Primary<T>> | null;
|
|
139
92
|
/**
|
|
140
93
|
* Maps nested FKs from `[1, 2, 3]` to `[1, [2, 3]]`.
|
|
141
94
|
*/
|
|
142
95
|
static mapFlatCompositePrimaryKey(fk: Primary<any>[], prop: EntityProperty, fieldNames?: string[], idx?: number): Primary<any> | Primary<any>[];
|
|
143
96
|
static getPrimaryKeyCondFromArray<T extends object>(pks: Primary<T>[], meta: EntityMetadata<T>): Record<string, Primary<T>>;
|
|
144
|
-
static getOrderedPrimaryKeys<T>(id: Primary<T> | Record<string, Primary<T>>, meta: EntityMetadata<T>, platform?: Platform, convertCustomTypes?: boolean): Primary<T>[];
|
|
97
|
+
static getOrderedPrimaryKeys<T>(id: Primary<T> | Record<string, Primary<T>>, meta: EntityMetadata<T>, platform?: Platform, convertCustomTypes?: boolean, allowScalar?: boolean): Primary<T>[];
|
|
145
98
|
/**
|
|
146
99
|
* Checks whether given object is an entity instance.
|
|
147
100
|
*/
|
|
@@ -150,10 +103,6 @@ export declare class Utils {
|
|
|
150
103
|
* Checks whether given object is a scalar reference.
|
|
151
104
|
*/
|
|
152
105
|
static isScalarReference<T = unknown>(data: any, allowReference?: boolean): data is ScalarReference<any> & {};
|
|
153
|
-
/**
|
|
154
|
-
* Checks whether the argument is ObjectId instance
|
|
155
|
-
*/
|
|
156
|
-
static isObjectID(key: any): boolean;
|
|
157
106
|
/**
|
|
158
107
|
* Checks whether the argument is empty (array without items, object without keys or falsy value).
|
|
159
108
|
*/
|
|
@@ -164,14 +113,9 @@ export declare class Utils {
|
|
|
164
113
|
static className<T>(classOrName: EntityName<T>): string;
|
|
165
114
|
static extractChildElements(items: string[], prefix: string, allSymbol?: string): string[];
|
|
166
115
|
/**
|
|
167
|
-
* Tries to detect
|
|
168
|
-
*/
|
|
169
|
-
static detectTsNode(): boolean;
|
|
170
|
-
/**
|
|
171
|
-
* Uses some dark magic to get source path to caller where decorator is used.
|
|
172
|
-
* Analyses stack trace of error created inside the function call.
|
|
116
|
+
* Tries to detect TypeScript support.
|
|
173
117
|
*/
|
|
174
|
-
static
|
|
118
|
+
static detectTypeScriptSupport(): boolean;
|
|
175
119
|
/**
|
|
176
120
|
* Gets the type of the argument.
|
|
177
121
|
*/
|
|
@@ -210,54 +154,26 @@ export declare class Utils {
|
|
|
210
154
|
static findDuplicates<T>(items: T[]): T[];
|
|
211
155
|
static removeDuplicates<T>(items: T[]): T[];
|
|
212
156
|
static randomInt(min: number, max: number): number;
|
|
213
|
-
static pathExists(path: string, options?: GlobbyOptions): Promise<boolean>;
|
|
214
157
|
/**
|
|
215
158
|
* Extracts all possible values of a TS enum. Works with both string and numeric enums.
|
|
216
159
|
*/
|
|
217
160
|
static extractEnumValues(target: Dictionary): (string | number)[];
|
|
218
161
|
static flatten<T>(arrays: T[][]): T[];
|
|
219
162
|
static isOperator(key: PropertyKey, includeGroupOperators?: boolean): boolean;
|
|
220
|
-
static isGroupOperator(key: PropertyKey): boolean;
|
|
221
|
-
static isArrayOperator(key: PropertyKey): boolean;
|
|
222
|
-
static isJsonKeyOperator(key: PropertyKey): boolean;
|
|
223
163
|
static hasNestedKey(object: unknown, key: string): boolean;
|
|
224
|
-
static getGlobalStorage(namespace: string): Dictionary;
|
|
225
|
-
/**
|
|
226
|
-
* Require a module from a specific location
|
|
227
|
-
* @param id The module to require
|
|
228
|
-
* @param [from] Location to start the node resolution
|
|
229
|
-
*/
|
|
230
|
-
static requireFrom<T extends Dictionary>(id: string, from?: string): T;
|
|
231
164
|
static dynamicImport<T = any>(id: string): Promise<T>;
|
|
232
|
-
static setDynamicImportProvider(provider: (id: string) => Promise<unknown>): void;
|
|
233
165
|
static getORMVersion(): string;
|
|
234
166
|
static createFunction(context: Map<string, any>, code: string): any;
|
|
235
167
|
static callCompiledFunction<T extends unknown[], R>(fn: (...args: T) => R, ...args: T): R;
|
|
236
|
-
/**
|
|
237
|
-
* @see https://github.com/mikro-orm/mikro-orm/issues/840
|
|
238
|
-
*/
|
|
239
|
-
static propertyDecoratorReturnValue(): any;
|
|
240
168
|
static unwrapProperty<T>(entity: T, meta: EntityMetadata<T>, prop: EntityProperty<T>, payload?: boolean): [unknown, number[]][];
|
|
241
169
|
static setPayloadProperty<T>(entity: EntityDictionary<T>, meta: EntityMetadata<T>, prop: EntityProperty<T>, value: unknown, idx: number[]): void;
|
|
242
|
-
static
|
|
170
|
+
static tryImport<T extends Dictionary = any>({ module, warning }: {
|
|
243
171
|
module: string;
|
|
244
|
-
warning
|
|
245
|
-
|
|
246
|
-
allowError?: string;
|
|
247
|
-
}): T | undefined;
|
|
248
|
-
static stripRelativePath(str: string): string;
|
|
249
|
-
/**
|
|
250
|
-
* simple process.argv parser, supports only properties with long names, prefixed with `--`
|
|
251
|
-
*/
|
|
252
|
-
static parseArgs<T extends Dictionary = Dictionary>(): T;
|
|
172
|
+
warning?: string;
|
|
173
|
+
}): Promise<T | undefined>;
|
|
253
174
|
static xor(a: boolean, b: boolean): boolean;
|
|
254
175
|
static keys<T extends object>(obj: T): (keyof T)[];
|
|
255
176
|
static values<T extends object>(obj: T): T[keyof T][];
|
|
256
177
|
static entries<T extends object>(obj: T): [keyof T, T[keyof T]][];
|
|
257
|
-
static isRawSql<T = {
|
|
258
|
-
sql: string;
|
|
259
|
-
params: unknown[];
|
|
260
|
-
use: () => void;
|
|
261
|
-
}>(value: unknown): value is T;
|
|
262
178
|
static primaryKeyToObject<T>(meta: EntityMetadata<T>, primaryKey: Primary<T> | T, visible?: (keyof T)[]): T;
|
|
263
179
|
}
|