@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
package/typings.js
CHANGED
|
@@ -1,18 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
exports.HiddenProps = Symbol('HiddenProps');
|
|
14
|
-
exports.Config = Symbol('Config');
|
|
15
|
-
class EntityMetadata {
|
|
1
|
+
import { ReferenceKind, } from './enums.js';
|
|
2
|
+
import { Reference } from './entity/Reference.js';
|
|
3
|
+
import { EntityHelper } from './entity/EntityHelper.js';
|
|
4
|
+
import { Utils } from './utils/Utils.js';
|
|
5
|
+
import { EntityComparator } from './utils/EntityComparator.js';
|
|
6
|
+
export const EntityRepositoryType = Symbol('EntityRepositoryType');
|
|
7
|
+
export const PrimaryKeyProp = Symbol('PrimaryKeyProp');
|
|
8
|
+
export const OptionalProps = Symbol('OptionalProps');
|
|
9
|
+
export const EagerProps = Symbol('EagerProps');
|
|
10
|
+
export const HiddenProps = Symbol('HiddenProps');
|
|
11
|
+
export const Config = Symbol('Config');
|
|
12
|
+
export class EntityMetadata {
|
|
16
13
|
static counter = 0;
|
|
17
14
|
_id = 1000 * EntityMetadata.counter++; // keep the id >= 1000 to allow computing cache keys by simple addition
|
|
18
15
|
propertyOrder = new Map();
|
|
@@ -35,7 +32,7 @@ class EntityMetadata {
|
|
|
35
32
|
}
|
|
36
33
|
this.properties[prop.name] = prop;
|
|
37
34
|
this.propertyOrder.set(prop.name, this.props.length);
|
|
38
|
-
/*
|
|
35
|
+
/* v8 ignore next */
|
|
39
36
|
if (sync) {
|
|
40
37
|
this.sync();
|
|
41
38
|
}
|
|
@@ -43,17 +40,34 @@ class EntityMetadata {
|
|
|
43
40
|
removeProperty(name, sync = true) {
|
|
44
41
|
delete this.properties[name];
|
|
45
42
|
this.propertyOrder.delete(name);
|
|
46
|
-
/*
|
|
43
|
+
/* v8 ignore next */
|
|
47
44
|
if (sync) {
|
|
48
45
|
this.sync();
|
|
49
46
|
}
|
|
50
47
|
}
|
|
51
|
-
getPrimaryProps() {
|
|
52
|
-
|
|
48
|
+
getPrimaryProps(flatten = false) {
|
|
49
|
+
const pks = this.primaryKeys.map(pk => this.properties[pk]);
|
|
50
|
+
if (flatten) {
|
|
51
|
+
return pks.flatMap(pk => {
|
|
52
|
+
if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(pk.kind)) {
|
|
53
|
+
return pk.targetMeta.getPrimaryProps(true);
|
|
54
|
+
}
|
|
55
|
+
return [pk];
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
return pks;
|
|
53
59
|
}
|
|
54
60
|
getPrimaryProp() {
|
|
55
61
|
return this.properties[this.primaryKeys[0]];
|
|
56
62
|
}
|
|
63
|
+
createColumnMappingObject() {
|
|
64
|
+
return Object.values(this.properties).reduce((o, prop) => {
|
|
65
|
+
if (prop.fieldNames) {
|
|
66
|
+
o[prop.name] = prop.fieldNames[0];
|
|
67
|
+
}
|
|
68
|
+
return o;
|
|
69
|
+
}, {});
|
|
70
|
+
}
|
|
57
71
|
get tableName() {
|
|
58
72
|
return this.collection;
|
|
59
73
|
}
|
|
@@ -64,28 +78,35 @@ class EntityMetadata {
|
|
|
64
78
|
this.root ??= this;
|
|
65
79
|
const props = Object.values(this.properties).sort((a, b) => this.propertyOrder.get(a.name) - this.propertyOrder.get(b.name));
|
|
66
80
|
this.props = [...props.filter(p => p.primary), ...props.filter(p => !p.primary)];
|
|
67
|
-
this.relations = this.props.filter(prop => typeof prop.kind !== 'undefined' && prop.kind !==
|
|
81
|
+
this.relations = this.props.filter(prop => typeof prop.kind !== 'undefined' && prop.kind !== ReferenceKind.SCALAR && prop.kind !== ReferenceKind.EMBEDDED);
|
|
68
82
|
this.bidirectionalRelations = this.relations.filter(prop => prop.mappedBy || prop.inversedBy);
|
|
69
83
|
this.uniqueProps = this.props.filter(prop => prop.unique);
|
|
70
84
|
this.getterProps = this.props.filter(prop => prop.getter);
|
|
71
|
-
this.comparableProps = this.props.filter(prop =>
|
|
85
|
+
this.comparableProps = this.props.filter(prop => EntityComparator.isComparable(prop, this));
|
|
86
|
+
this.validateProps = this.props.filter(prop => {
|
|
87
|
+
if (prop.inherited || (prop.persist === false && prop.userDefined !== false)) {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
return prop.kind === ReferenceKind.SCALAR && ['string', 'number', 'boolean', 'Date'].includes(prop.type);
|
|
91
|
+
});
|
|
72
92
|
this.hydrateProps = this.props.filter(prop => {
|
|
73
93
|
// `prop.userDefined` is either `undefined` or `false`
|
|
74
94
|
const discriminator = this.root.discriminatorColumn === prop.name && prop.userDefined === false;
|
|
75
95
|
// even if we don't have a setter, do not ignore value from database!
|
|
76
|
-
const onlyGetter = prop.getter && !prop.setter;
|
|
96
|
+
const onlyGetter = prop.getter && !prop.setter && prop.persist === false;
|
|
77
97
|
return !prop.inherited && prop.hydrate !== false && !discriminator && !prop.embedded && !onlyGetter;
|
|
78
98
|
});
|
|
79
|
-
this.trackingProps = this.hydrateProps
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
99
|
+
this.trackingProps = this.hydrateProps.filter(prop => {
|
|
100
|
+
return !prop.getter && !prop.setter && [ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind);
|
|
101
|
+
});
|
|
102
|
+
this.selfReferencing = this.relations.some(prop => {
|
|
103
|
+
return [this.className, this.root.className].includes(prop.targetMeta?.root.className ?? prop.type);
|
|
104
|
+
});
|
|
84
105
|
this.hasUniqueProps = this.uniques.length + this.uniqueProps.length > 0;
|
|
85
106
|
this.virtual = !!this.expression;
|
|
86
107
|
if (config) {
|
|
87
108
|
for (const prop of this.props) {
|
|
88
|
-
if (prop.enum && !prop.nativeEnumName && prop.items?.every(item =>
|
|
109
|
+
if (prop.enum && !prop.nativeEnumName && prop.items?.every(item => typeof item === 'string')) {
|
|
89
110
|
const name = config.getNamingStrategy().indexName(this.tableName, prop.fieldNames, 'check');
|
|
90
111
|
const exists = this.checks.findIndex(check => check.name === name);
|
|
91
112
|
if (exists !== -1) {
|
|
@@ -99,11 +120,11 @@ class EntityMetadata {
|
|
|
99
120
|
}
|
|
100
121
|
}
|
|
101
122
|
}
|
|
102
|
-
this.checks =
|
|
103
|
-
this.indexes =
|
|
104
|
-
this.uniques =
|
|
105
|
-
for (const hook of
|
|
106
|
-
this.hooks[hook] =
|
|
123
|
+
this.checks = Utils.removeDuplicates(this.checks);
|
|
124
|
+
this.indexes = Utils.removeDuplicates(this.indexes);
|
|
125
|
+
this.uniques = Utils.removeDuplicates(this.uniques);
|
|
126
|
+
for (const hook of Utils.keys(this.hooks)) {
|
|
127
|
+
this.hooks[hook] = Utils.removeDuplicates(this.hooks[hook]);
|
|
107
128
|
}
|
|
108
129
|
if (this.virtual) {
|
|
109
130
|
this.readonly = true;
|
|
@@ -112,8 +133,7 @@ class EntityMetadata {
|
|
|
112
133
|
this.props.forEach(prop => this.initIndexes(prop));
|
|
113
134
|
}
|
|
114
135
|
this.definedProperties = this.trackingProps.reduce((o, prop) => {
|
|
115
|
-
const
|
|
116
|
-
const isReference = [enums_1.ReferenceKind.ONE_TO_ONE, enums_1.ReferenceKind.MANY_TO_ONE].includes(prop.kind) && (prop.inversedBy || prop.mappedBy) && !prop.mapToPk;
|
|
136
|
+
const isReference = (prop.inversedBy || prop.mappedBy) && !prop.mapToPk;
|
|
117
137
|
if (isReference) {
|
|
118
138
|
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
119
139
|
const meta = this;
|
|
@@ -124,53 +144,36 @@ class EntityMetadata {
|
|
|
124
144
|
set(val) {
|
|
125
145
|
const wrapped = this.__helper;
|
|
126
146
|
const hydrator = wrapped.hydrator;
|
|
127
|
-
const entity =
|
|
128
|
-
const old =
|
|
129
|
-
|
|
147
|
+
const entity = Reference.unwrapReference(val ?? wrapped.__data[prop.name]);
|
|
148
|
+
const old = Reference.unwrapReference(wrapped.__data[prop.name]);
|
|
149
|
+
if (old && old !== entity && prop.kind === ReferenceKind.MANY_TO_ONE && prop.inversedBy && old[prop.inversedBy]) {
|
|
150
|
+
old[prop.inversedBy].removeWithoutPropagation(this);
|
|
151
|
+
}
|
|
152
|
+
wrapped.__data[prop.name] = Reference.wrapReference(val, prop);
|
|
130
153
|
// when propagation from inside hydration, we set the FK to the entity data immediately
|
|
131
154
|
if (val && hydrator.isRunning() && wrapped.__originalEntityData && prop.owner) {
|
|
132
|
-
wrapped.__originalEntityData[prop.name] =
|
|
155
|
+
wrapped.__originalEntityData[prop.name] = Utils.getPrimaryKeyValues(val, prop.targetMeta, true);
|
|
133
156
|
}
|
|
134
|
-
|
|
135
|
-
wrapped.__touched = !hydrator.isRunning();
|
|
136
|
-
}
|
|
137
|
-
EntityHelper_1.EntityHelper.propagate(meta, entity, this, prop, Reference_1.Reference.unwrapReference(val), old);
|
|
157
|
+
EntityHelper.propagate(meta, entity, this, prop, Reference.unwrapReference(val), old);
|
|
138
158
|
},
|
|
139
159
|
enumerable: true,
|
|
140
160
|
configurable: true,
|
|
141
161
|
};
|
|
142
162
|
}
|
|
143
|
-
if (prop.inherited || prop.primary || isCollection || prop.persist === false || prop.trackChanges === false || isReference || prop.embedded) {
|
|
144
|
-
return o;
|
|
145
|
-
}
|
|
146
|
-
o[prop.name] = {
|
|
147
|
-
get() {
|
|
148
|
-
return this.__helper.__data[prop.name];
|
|
149
|
-
},
|
|
150
|
-
set(val) {
|
|
151
|
-
if (typeof val === 'object' && !!val && '__raw' in val) {
|
|
152
|
-
val.assign();
|
|
153
|
-
}
|
|
154
|
-
this.__helper.__data[prop.name] = val;
|
|
155
|
-
this.__helper.__touched = !this.__helper.hydrator.isRunning();
|
|
156
|
-
},
|
|
157
|
-
enumerable: true,
|
|
158
|
-
configurable: true,
|
|
159
|
-
};
|
|
160
163
|
return o;
|
|
161
164
|
}, { __gettersDefined: { value: true, enumerable: false } });
|
|
162
165
|
}
|
|
163
166
|
initIndexes(prop) {
|
|
164
167
|
const simpleIndex = this.indexes.find(index => index.properties === prop.name && !index.options && !index.type && !index.expression);
|
|
165
168
|
const simpleUnique = this.uniques.find(index => index.properties === prop.name && !index.options);
|
|
166
|
-
const owner = prop.kind ===
|
|
169
|
+
const owner = prop.kind === ReferenceKind.MANY_TO_ONE;
|
|
167
170
|
if (!prop.index && simpleIndex) {
|
|
168
|
-
|
|
171
|
+
Utils.defaultValue(simpleIndex, 'name', true);
|
|
169
172
|
prop.index = simpleIndex.name;
|
|
170
173
|
this.indexes.splice(this.indexes.indexOf(simpleIndex), 1);
|
|
171
174
|
}
|
|
172
175
|
if (!prop.unique && simpleUnique) {
|
|
173
|
-
|
|
176
|
+
Utils.defaultValue(simpleUnique, 'name', true);
|
|
174
177
|
prop.unique = simpleUnique.name;
|
|
175
178
|
this.uniques.splice(this.uniques.indexOf(simpleUnique), 1);
|
|
176
179
|
}
|
|
@@ -178,7 +181,7 @@ class EntityMetadata {
|
|
|
178
181
|
this.indexes.push({ properties: prop.name });
|
|
179
182
|
prop.index = false;
|
|
180
183
|
}
|
|
181
|
-
/*
|
|
184
|
+
/* v8 ignore next */
|
|
182
185
|
if (owner && prop.fieldNames.length > 1 && prop.unique) {
|
|
183
186
|
this.uniques.push({ properties: prop.name });
|
|
184
187
|
prop.unique = false;
|
|
@@ -189,4 +192,3 @@ class EntityMetadata {
|
|
|
189
192
|
return this;
|
|
190
193
|
}
|
|
191
194
|
}
|
|
192
|
-
exports.EntityMetadata = EntityMetadata;
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type { EntityData, EntityMetadata, EntityDictionary, Primary } from '../typings';
|
|
1
|
+
import type { EntityData, EntityMetadata, EntityDictionary, Primary } from '../typings.js';
|
|
3
2
|
export declare class ChangeSet<T extends object> {
|
|
4
3
|
entity: T;
|
|
5
4
|
type: ChangeSetType;
|
|
@@ -10,8 +9,6 @@ export declare class ChangeSet<T extends object> {
|
|
|
10
9
|
constructor(entity: T, type: ChangeSetType, payload: EntityDictionary<T>, meta: EntityMetadata<T>);
|
|
11
10
|
getPrimaryKey(object?: boolean): Primary<T> | null;
|
|
12
11
|
getSerializedPrimaryKey(): string | null;
|
|
13
|
-
/** @ignore */
|
|
14
|
-
[inspect.custom](depth?: number): string;
|
|
15
12
|
}
|
|
16
13
|
export interface ChangeSet<T> {
|
|
17
14
|
name: string;
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const wrap_1 = require("../entity/wrap");
|
|
6
|
-
const Utils_1 = require("../utils/Utils");
|
|
7
|
-
class ChangeSet {
|
|
1
|
+
import { helper } from '../entity/wrap.js';
|
|
2
|
+
import { Utils } from '../utils/Utils.js';
|
|
3
|
+
import { inspect } from '../logging/inspect.js';
|
|
4
|
+
export class ChangeSet {
|
|
8
5
|
entity;
|
|
9
6
|
type;
|
|
10
7
|
payload;
|
|
@@ -19,11 +16,11 @@ class ChangeSet {
|
|
|
19
16
|
this.name = meta.className;
|
|
20
17
|
this.rootName = meta.root.className;
|
|
21
18
|
this.collection = meta.root.collection;
|
|
22
|
-
this.schema =
|
|
19
|
+
this.schema = helper(entity).__schema ?? meta.root.schema;
|
|
23
20
|
}
|
|
24
21
|
getPrimaryKey(object = false) {
|
|
25
22
|
if (!this.originalEntity) {
|
|
26
|
-
this.primaryKey ??=
|
|
23
|
+
this.primaryKey ??= helper(this.entity).getPrimaryKey(true);
|
|
27
24
|
}
|
|
28
25
|
else if (this.meta.compositePK) {
|
|
29
26
|
this.primaryKey = this.meta.primaryKeys.map(pk => this.originalEntity[pk]);
|
|
@@ -40,31 +37,30 @@ class ChangeSet {
|
|
|
40
37
|
});
|
|
41
38
|
}
|
|
42
39
|
if (object && this.primaryKey != null) {
|
|
43
|
-
return
|
|
40
|
+
return Utils.primaryKeyToObject(this.meta, this.primaryKey);
|
|
44
41
|
}
|
|
45
42
|
return this.primaryKey ?? null;
|
|
46
43
|
}
|
|
47
44
|
getSerializedPrimaryKey() {
|
|
48
|
-
this.serializedPrimaryKey ??=
|
|
45
|
+
this.serializedPrimaryKey ??= helper(this.entity).getSerializedPrimaryKey();
|
|
49
46
|
return this.serializedPrimaryKey;
|
|
50
47
|
}
|
|
51
48
|
/** @ignore */
|
|
52
|
-
[
|
|
49
|
+
[Symbol.for('nodejs.util.inspect.custom')](depth = 2) {
|
|
53
50
|
const object = { ...this };
|
|
54
51
|
const hidden = ['meta', 'serializedPrimaryKey'];
|
|
55
52
|
hidden.forEach(k => delete object[k]);
|
|
56
|
-
const ret =
|
|
53
|
+
const ret = inspect(object, { depth });
|
|
57
54
|
const name = `${this.constructor.name}<${this.meta.className}>`;
|
|
58
|
-
/*
|
|
55
|
+
/* v8 ignore next */
|
|
59
56
|
return ret === '[Object]' ? `[${name}]` : name + ' ' + ret;
|
|
60
57
|
}
|
|
61
58
|
}
|
|
62
|
-
|
|
63
|
-
var ChangeSetType;
|
|
59
|
+
export var ChangeSetType;
|
|
64
60
|
(function (ChangeSetType) {
|
|
65
61
|
ChangeSetType["CREATE"] = "create";
|
|
66
62
|
ChangeSetType["UPDATE"] = "update";
|
|
67
63
|
ChangeSetType["DELETE"] = "delete";
|
|
68
64
|
ChangeSetType["UPDATE_EARLY"] = "update_early";
|
|
69
65
|
ChangeSetType["DELETE_EARLY"] = "delete_early";
|
|
70
|
-
})(ChangeSetType || (
|
|
66
|
+
})(ChangeSetType || (ChangeSetType = {}));
|
|
@@ -1,19 +1,18 @@
|
|
|
1
|
-
import { type Configuration } from '../utils/Configuration';
|
|
2
|
-
import type { MetadataStorage } from '../metadata';
|
|
3
|
-
import type { AnyEntity } from '../typings';
|
|
4
|
-
import { ChangeSet } from './ChangeSet';
|
|
5
|
-
import { type Collection
|
|
6
|
-
import type { Platform } from '../platforms';
|
|
7
|
-
import type { EntityManager } from '../EntityManager';
|
|
1
|
+
import { type Configuration } from '../utils/Configuration.js';
|
|
2
|
+
import type { MetadataStorage } from '../metadata/MetadataStorage.js';
|
|
3
|
+
import type { AnyEntity } from '../typings.js';
|
|
4
|
+
import { ChangeSet } from './ChangeSet.js';
|
|
5
|
+
import { type Collection } from '../entity/Collection.js';
|
|
6
|
+
import type { Platform } from '../platforms/Platform.js';
|
|
7
|
+
import type { EntityManager } from '../EntityManager.js';
|
|
8
8
|
export declare class ChangeSetComputer {
|
|
9
|
-
private readonly validator;
|
|
10
9
|
private readonly collectionUpdates;
|
|
11
10
|
private readonly metadata;
|
|
12
11
|
private readonly platform;
|
|
13
12
|
private readonly config;
|
|
14
13
|
private readonly em;
|
|
15
14
|
private readonly comparator;
|
|
16
|
-
constructor(
|
|
15
|
+
constructor(collectionUpdates: Set<Collection<AnyEntity>>, metadata: MetadataStorage, platform: Platform, config: Configuration, em: EntityManager);
|
|
17
16
|
computeChangeSet<T extends object>(entity: T): ChangeSet<T> | null;
|
|
18
17
|
/**
|
|
19
18
|
* Traverses entity graph and executes `onCreate` and `onUpdate` methods, assigning the values to given properties.
|
|
@@ -1,20 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const enums_1 = require("../enums");
|
|
8
|
-
class ChangeSetComputer {
|
|
9
|
-
validator;
|
|
1
|
+
import { Utils } from '../utils/Utils.js';
|
|
2
|
+
import { ChangeSet, ChangeSetType } from './ChangeSet.js';
|
|
3
|
+
import { helper } from '../entity/wrap.js';
|
|
4
|
+
import { validateEntity } from '../entity/validators.js';
|
|
5
|
+
import { ReferenceKind } from '../enums.js';
|
|
6
|
+
export class ChangeSetComputer {
|
|
10
7
|
collectionUpdates;
|
|
11
8
|
metadata;
|
|
12
9
|
platform;
|
|
13
10
|
config;
|
|
14
11
|
em;
|
|
15
12
|
comparator;
|
|
16
|
-
constructor(
|
|
17
|
-
this.validator = validator;
|
|
13
|
+
constructor(collectionUpdates, metadata, platform, config, em) {
|
|
18
14
|
this.collectionUpdates = collectionUpdates;
|
|
19
15
|
this.metadata = metadata;
|
|
20
16
|
this.platform = platform;
|
|
@@ -27,33 +23,34 @@ class ChangeSetComputer {
|
|
|
27
23
|
if (meta.readonly) {
|
|
28
24
|
return null;
|
|
29
25
|
}
|
|
30
|
-
const wrapped =
|
|
31
|
-
const type = wrapped.__originalEntityData ?
|
|
26
|
+
const wrapped = helper(entity);
|
|
27
|
+
const type = wrapped.__originalEntityData ? ChangeSetType.UPDATE : ChangeSetType.CREATE;
|
|
32
28
|
const map = new Map();
|
|
33
29
|
// Execute `onCreate` and `onUpdate` on properties recursively, saves `onUpdate` results
|
|
34
30
|
// to the `map` as we want to apply those only if something else changed.
|
|
35
|
-
if (type ===
|
|
31
|
+
if (type === ChangeSetType.CREATE) { // run update hooks only after we know there are other changes
|
|
36
32
|
for (const prop of meta.hydrateProps) {
|
|
37
33
|
this.processPropertyInitializers(entity, prop, type, map);
|
|
38
34
|
}
|
|
39
35
|
}
|
|
40
|
-
if (type ===
|
|
41
|
-
|
|
36
|
+
if (type === ChangeSetType.UPDATE && !wrapped.__initialized) {
|
|
37
|
+
const data = this.comparator.prepareEntity(entity);
|
|
38
|
+
if (Utils.equals(data, wrapped.__originalEntityData)) {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
42
41
|
}
|
|
43
|
-
const changeSet = new
|
|
42
|
+
const changeSet = new ChangeSet(entity, type, this.computePayload(entity), meta);
|
|
44
43
|
changeSet.originalEntity = wrapped.__originalEntityData;
|
|
45
|
-
if (this.config.get('validate')) {
|
|
46
|
-
this.validator.validate(changeSet.entity, changeSet.payload, meta);
|
|
47
|
-
}
|
|
48
44
|
for (const prop of meta.relations.filter(prop => prop.persist !== false || prop.userDefined === false)) {
|
|
49
45
|
this.processProperty(changeSet, prop);
|
|
50
46
|
}
|
|
51
|
-
if (changeSet.type ===
|
|
47
|
+
if (changeSet.type === ChangeSetType.UPDATE && !Utils.hasObjectKeys(changeSet.payload)) {
|
|
52
48
|
return null;
|
|
53
49
|
}
|
|
50
|
+
validateEntity(changeSet.entity, meta);
|
|
54
51
|
// Execute `onCreate` and `onUpdate` on properties recursively, saves `onUpdate` results
|
|
55
52
|
// to the `map` as we want to apply those only if something else changed.
|
|
56
|
-
if (type ===
|
|
53
|
+
if (type === ChangeSetType.UPDATE) {
|
|
57
54
|
for (const prop of meta.hydrateProps) {
|
|
58
55
|
this.processPropertyInitializers(entity, prop, type, map);
|
|
59
56
|
}
|
|
@@ -66,7 +63,7 @@ class ChangeSetComputer {
|
|
|
66
63
|
}
|
|
67
64
|
// Recompute the changeset, we need to merge this as here we ignore relations.
|
|
68
65
|
const diff = this.computePayload(entity, true);
|
|
69
|
-
|
|
66
|
+
Utils.merge(changeSet.payload, diff);
|
|
70
67
|
}
|
|
71
68
|
return changeSet;
|
|
72
69
|
}
|
|
@@ -75,17 +72,17 @@ class ChangeSetComputer {
|
|
|
75
72
|
*/
|
|
76
73
|
processPropertyInitializers(entity, prop, type, map, nested) {
|
|
77
74
|
if (prop.onCreate
|
|
78
|
-
&& type ===
|
|
75
|
+
&& type === ChangeSetType.CREATE
|
|
79
76
|
&& (entity[prop.name] == null
|
|
80
|
-
|| (
|
|
77
|
+
|| (Utils.isScalarReference(entity[prop.name]) && entity[prop.name].unwrap() == null))) {
|
|
81
78
|
entity[prop.name] = prop.onCreate(entity, this.em);
|
|
82
79
|
}
|
|
83
|
-
if (prop.onUpdate && type ===
|
|
80
|
+
if (prop.onUpdate && type === ChangeSetType.UPDATE) {
|
|
84
81
|
const pairs = map.get(entity) ?? [];
|
|
85
82
|
pairs.push([prop.name, prop.onUpdate(entity, this.em)]);
|
|
86
83
|
map.set(entity, pairs);
|
|
87
84
|
}
|
|
88
|
-
if (prop.kind ===
|
|
85
|
+
if (prop.kind === ReferenceKind.EMBEDDED && entity[prop.name]) {
|
|
89
86
|
for (const embeddedProp of prop.targetMeta.hydrateProps) {
|
|
90
87
|
this.processPropertyInitializers(entity[prop.name], embeddedProp, type, map, nested || prop.object);
|
|
91
88
|
}
|
|
@@ -93,7 +90,7 @@ class ChangeSetComputer {
|
|
|
93
90
|
}
|
|
94
91
|
computePayload(entity, ignoreUndefined = false) {
|
|
95
92
|
const data = this.comparator.prepareEntity(entity);
|
|
96
|
-
const wrapped =
|
|
93
|
+
const wrapped = helper(entity);
|
|
97
94
|
const entityName = wrapped.__meta.className;
|
|
98
95
|
const originalEntityData = wrapped.__originalEntityData;
|
|
99
96
|
if (!wrapped.__initialized) {
|
|
@@ -106,7 +103,7 @@ class ChangeSetComputer {
|
|
|
106
103
|
const comparator = this.comparator.getEntityComparator(entityName);
|
|
107
104
|
const diff = comparator(originalEntityData, data);
|
|
108
105
|
if (ignoreUndefined) {
|
|
109
|
-
|
|
106
|
+
Utils.keys(diff)
|
|
110
107
|
.filter(k => diff[k] === undefined)
|
|
111
108
|
.forEach(k => delete diff[k]);
|
|
112
109
|
}
|
|
@@ -116,38 +113,39 @@ class ChangeSetComputer {
|
|
|
116
113
|
}
|
|
117
114
|
processProperty(changeSet, prop, target) {
|
|
118
115
|
if (!target) {
|
|
119
|
-
const targets =
|
|
116
|
+
const targets = Utils.unwrapProperty(changeSet.entity, changeSet.meta, prop);
|
|
120
117
|
targets.forEach(([t]) => this.processProperty(changeSet, prop, t));
|
|
121
118
|
return;
|
|
122
119
|
}
|
|
123
|
-
if (
|
|
120
|
+
if (Utils.isCollection(target)) { // m:n or 1:m
|
|
124
121
|
this.processToMany(prop, changeSet);
|
|
125
122
|
}
|
|
126
|
-
if ([
|
|
123
|
+
if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind)) {
|
|
127
124
|
this.processToOne(prop, changeSet);
|
|
128
125
|
}
|
|
129
126
|
}
|
|
130
127
|
processToOne(prop, changeSet) {
|
|
131
|
-
const isToOneOwner = prop.kind ===
|
|
128
|
+
const isToOneOwner = prop.kind === ReferenceKind.MANY_TO_ONE || (prop.kind === ReferenceKind.ONE_TO_ONE && prop.owner);
|
|
132
129
|
if (!isToOneOwner || prop.mapToPk) {
|
|
133
130
|
return;
|
|
134
131
|
}
|
|
135
|
-
const targets =
|
|
132
|
+
const targets = Utils.unwrapProperty(changeSet.entity, changeSet.meta, prop);
|
|
136
133
|
targets.forEach(([target, idx]) => {
|
|
137
134
|
if (!target.__helper.hasPrimaryKey()) {
|
|
138
|
-
|
|
135
|
+
Utils.setPayloadProperty(changeSet.payload, this.metadata.find(changeSet.name), prop, target.__helper.__identifier, idx);
|
|
139
136
|
}
|
|
140
137
|
});
|
|
141
138
|
}
|
|
142
139
|
processToMany(prop, changeSet) {
|
|
143
140
|
const target = changeSet.entity[prop.name];
|
|
144
|
-
if (!target.isDirty() && changeSet.type !==
|
|
141
|
+
if (!target.isDirty() && changeSet.type !== ChangeSetType.CREATE) {
|
|
145
142
|
return;
|
|
146
143
|
}
|
|
147
|
-
|
|
144
|
+
if (target.isDirty()) {
|
|
145
|
+
this.collectionUpdates.add(target);
|
|
146
|
+
}
|
|
148
147
|
if (prop.owner && !this.platform.usesPivotTable()) {
|
|
149
148
|
changeSet.payload[prop.name] = target.getItems(false).map((item) => item.__helper.__identifier ?? item.__helper.getPrimaryKey());
|
|
150
149
|
}
|
|
151
150
|
}
|
|
152
151
|
}
|
|
153
|
-
exports.ChangeSetComputer = ChangeSetComputer;
|
|
@@ -1,28 +1,30 @@
|
|
|
1
|
-
import type { MetadataStorage } from '../metadata';
|
|
2
|
-
import type { Dictionary, EntityDictionary, EntityMetadata, IHydrator } from '../typings';
|
|
3
|
-
import { type EntityFactory
|
|
4
|
-
import { type ChangeSet } from './ChangeSet';
|
|
5
|
-
import { type Configuration } from '../utils/Configuration';
|
|
6
|
-
import type { DriverMethodOptions, IDatabaseDriver } from '../drivers';
|
|
1
|
+
import type { MetadataStorage } from '../metadata/MetadataStorage.js';
|
|
2
|
+
import type { Dictionary, EntityDictionary, EntityMetadata, IHydrator } from '../typings.js';
|
|
3
|
+
import { type EntityFactory } from '../entity/EntityFactory.js';
|
|
4
|
+
import { type ChangeSet } from './ChangeSet.js';
|
|
5
|
+
import { type Configuration } from '../utils/Configuration.js';
|
|
6
|
+
import type { DriverMethodOptions, IDatabaseDriver } from '../drivers/IDatabaseDriver.js';
|
|
7
|
+
import type { EntityManager } from '../EntityManager.js';
|
|
7
8
|
export declare class ChangeSetPersister {
|
|
8
9
|
private readonly driver;
|
|
9
10
|
private readonly metadata;
|
|
10
11
|
private readonly hydrator;
|
|
11
12
|
private readonly factory;
|
|
12
|
-
private readonly validator;
|
|
13
13
|
private readonly config;
|
|
14
|
+
private readonly em;
|
|
14
15
|
private readonly platform;
|
|
15
16
|
private readonly comparator;
|
|
16
17
|
private readonly usesReturningStatement;
|
|
17
|
-
constructor(driver: IDatabaseDriver, metadata: MetadataStorage, hydrator: IHydrator, factory: EntityFactory,
|
|
18
|
+
constructor(driver: IDatabaseDriver, metadata: MetadataStorage, hydrator: IHydrator, factory: EntityFactory, config: Configuration, em: EntityManager);
|
|
18
19
|
executeInserts<T extends object>(changeSets: ChangeSet<T>[], options?: DriverMethodOptions, withSchema?: boolean): Promise<void>;
|
|
19
20
|
executeUpdates<T extends object>(changeSets: ChangeSet<T>[], batched: boolean, options?: DriverMethodOptions, withSchema?: boolean): Promise<void>;
|
|
20
21
|
executeDeletes<T extends object>(changeSets: ChangeSet<T>[], options?: DriverMethodOptions, withSchema?: boolean): Promise<void>;
|
|
21
22
|
private runForEachSchema;
|
|
23
|
+
private validateRequired;
|
|
22
24
|
private processProperties;
|
|
23
25
|
private persistNewEntity;
|
|
24
26
|
private persistNewEntities;
|
|
25
|
-
private
|
|
27
|
+
private prepareOptions;
|
|
26
28
|
private persistNewEntitiesBatch;
|
|
27
29
|
private persistManagedEntity;
|
|
28
30
|
private persistManagedEntities;
|