@yandjin-mikro-orm/core 6.1.4-rc-sti-changes-1
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 +553 -0
- package/EntityManager.js +1736 -0
- package/LICENSE +21 -0
- package/MikroORM.d.ts +102 -0
- package/MikroORM.js +258 -0
- package/README.md +383 -0
- package/cache/CacheAdapter.d.ts +40 -0
- package/cache/CacheAdapter.js +2 -0
- package/cache/FileCacheAdapter.d.ts +31 -0
- package/cache/FileCacheAdapter.js +90 -0
- package/cache/GeneratedCacheAdapter.d.ts +25 -0
- package/cache/GeneratedCacheAdapter.js +38 -0
- package/cache/MemoryCacheAdapter.d.ts +24 -0
- package/cache/MemoryCacheAdapter.js +44 -0
- package/cache/NullCacheAdapter.d.ts +19 -0
- package/cache/NullCacheAdapter.js +30 -0
- package/cache/index.d.ts +5 -0
- package/cache/index.js +21 -0
- package/connections/Connection.d.ts +85 -0
- package/connections/Connection.js +128 -0
- package/connections/index.d.ts +1 -0
- package/connections/index.js +17 -0
- package/decorators/Check.d.ts +3 -0
- package/decorators/Check.js +17 -0
- package/decorators/CreateRequestContext.d.ts +2 -0
- package/decorators/CreateRequestContext.js +31 -0
- package/decorators/Embeddable.d.ts +8 -0
- package/decorators/Embeddable.js +15 -0
- package/decorators/Embedded.d.ts +13 -0
- package/decorators/Embedded.js +21 -0
- package/decorators/EnsureRequestContext.d.ts +2 -0
- package/decorators/EnsureRequestContext.js +26 -0
- package/decorators/Entity.d.ts +18 -0
- package/decorators/Entity.js +17 -0
- package/decorators/Enum.d.ts +9 -0
- package/decorators/Enum.js +20 -0
- package/decorators/Filter.d.ts +2 -0
- package/decorators/Filter.js +12 -0
- package/decorators/Formula.d.ts +5 -0
- package/decorators/Formula.js +19 -0
- package/decorators/Indexed.d.ts +12 -0
- package/decorators/Indexed.js +25 -0
- package/decorators/ManyToMany.d.ts +21 -0
- package/decorators/ManyToMany.js +17 -0
- package/decorators/ManyToOne.d.ts +15 -0
- package/decorators/ManyToOne.js +17 -0
- package/decorators/OneToMany.d.ts +18 -0
- package/decorators/OneToMany.js +21 -0
- package/decorators/OneToOne.d.ts +12 -0
- package/decorators/OneToOne.js +11 -0
- package/decorators/PrimaryKey.d.ts +8 -0
- package/decorators/PrimaryKey.js +24 -0
- package/decorators/Property.d.ts +218 -0
- package/decorators/Property.js +35 -0
- package/decorators/Subscriber.d.ts +1 -0
- package/decorators/Subscriber.js +2 -0
- package/decorators/hooks.d.ts +16 -0
- package/decorators/hooks.js +60 -0
- package/decorators/index.d.ts +17 -0
- package/decorators/index.js +36 -0
- package/drivers/DatabaseDriver.d.ts +72 -0
- package/drivers/DatabaseDriver.js +358 -0
- package/drivers/IDatabaseDriver.d.ts +193 -0
- package/drivers/IDatabaseDriver.js +4 -0
- package/drivers/index.d.ts +2 -0
- package/drivers/index.js +18 -0
- package/entity/ArrayCollection.d.ts +113 -0
- package/entity/ArrayCollection.js +386 -0
- package/entity/BaseEntity.d.ts +22 -0
- package/entity/BaseEntity.js +47 -0
- package/entity/Collection.d.ts +104 -0
- package/entity/Collection.js +373 -0
- package/entity/EntityAssigner.d.ts +28 -0
- package/entity/EntityAssigner.js +226 -0
- package/entity/EntityFactory.d.ts +41 -0
- package/entity/EntityFactory.js +302 -0
- package/entity/EntityHelper.d.ts +29 -0
- package/entity/EntityHelper.js +250 -0
- package/entity/EntityIdentifier.d.ts +10 -0
- package/entity/EntityIdentifier.js +19 -0
- package/entity/EntityLoader.d.ts +65 -0
- package/entity/EntityLoader.js +579 -0
- package/entity/EntityRepository.d.ts +161 -0
- package/entity/EntityRepository.js +207 -0
- package/entity/EntityValidator.d.ts +19 -0
- package/entity/EntityValidator.js +152 -0
- package/entity/Reference.d.ts +89 -0
- package/entity/Reference.js +242 -0
- package/entity/WrappedEntity.d.ts +67 -0
- package/entity/WrappedEntity.js +146 -0
- package/entity/index.d.ts +13 -0
- package/entity/index.js +29 -0
- package/entity/wrap.d.ts +15 -0
- package/entity/wrap.js +26 -0
- package/enums.d.ts +160 -0
- package/enums.js +169 -0
- package/errors.d.ts +65 -0
- package/errors.js +222 -0
- package/events/EventManager.d.ts +17 -0
- package/events/EventManager.js +77 -0
- package/events/EventSubscriber.d.ts +39 -0
- package/events/EventSubscriber.js +2 -0
- package/events/TransactionEventBroadcaster.d.ts +11 -0
- package/events/TransactionEventBroadcaster.js +17 -0
- package/events/index.d.ts +3 -0
- package/events/index.js +19 -0
- package/exceptions.d.ts +104 -0
- package/exceptions.js +130 -0
- package/hydration/Hydrator.d.ts +23 -0
- package/hydration/Hydrator.js +51 -0
- package/hydration/ObjectHydrator.d.ts +24 -0
- package/hydration/ObjectHydrator.js +332 -0
- package/hydration/index.d.ts +2 -0
- package/hydration/index.js +18 -0
- package/index.d.ts +25 -0
- package/index.js +50 -0
- package/index.mjs +192 -0
- package/logging/DefaultLogger.d.ts +32 -0
- package/logging/DefaultLogger.js +90 -0
- package/logging/Logger.d.ts +56 -0
- package/logging/Logger.js +2 -0
- package/logging/SimpleLogger.d.ts +17 -0
- package/logging/SimpleLogger.js +31 -0
- package/logging/colors.d.ts +9 -0
- package/logging/colors.js +20 -0
- package/logging/index.d.ts +4 -0
- package/logging/index.js +20 -0
- package/metadata/EntitySchema.d.ts +74 -0
- package/metadata/EntitySchema.js +293 -0
- package/metadata/MetadataDiscovery.d.ts +71 -0
- package/metadata/MetadataDiscovery.js +1244 -0
- package/metadata/MetadataProvider.d.ts +11 -0
- package/metadata/MetadataProvider.js +23 -0
- package/metadata/MetadataStorage.d.ts +22 -0
- package/metadata/MetadataStorage.js +87 -0
- package/metadata/MetadataValidator.d.ts +24 -0
- package/metadata/MetadataValidator.js +213 -0
- package/metadata/ReflectMetadataProvider.d.ts +8 -0
- package/metadata/ReflectMetadataProvider.js +48 -0
- package/metadata/index.d.ts +6 -0
- package/metadata/index.js +22 -0
- package/naming-strategy/AbstractNamingStrategy.d.ts +18 -0
- package/naming-strategy/AbstractNamingStrategy.js +48 -0
- package/naming-strategy/EntityCaseNamingStrategy.d.ts +12 -0
- package/naming-strategy/EntityCaseNamingStrategy.js +32 -0
- package/naming-strategy/MongoNamingStrategy.d.ts +9 -0
- package/naming-strategy/MongoNamingStrategy.js +25 -0
- package/naming-strategy/NamingStrategy.d.ts +52 -0
- package/naming-strategy/NamingStrategy.js +2 -0
- package/naming-strategy/UnderscoreNamingStrategy.d.ts +10 -0
- package/naming-strategy/UnderscoreNamingStrategy.js +28 -0
- package/naming-strategy/index.d.ts +5 -0
- package/naming-strategy/index.js +21 -0
- package/package.json +70 -0
- package/platforms/ExceptionConverter.d.ts +5 -0
- package/platforms/ExceptionConverter.js +11 -0
- package/platforms/Platform.d.ts +201 -0
- package/platforms/Platform.js +452 -0
- package/platforms/index.d.ts +2 -0
- package/platforms/index.js +18 -0
- package/serialization/EntitySerializer.d.ts +34 -0
- package/serialization/EntitySerializer.js +206 -0
- package/serialization/EntityTransformer.d.ts +8 -0
- package/serialization/EntityTransformer.js +192 -0
- package/serialization/SerializationContext.d.ts +30 -0
- package/serialization/SerializationContext.js +111 -0
- package/serialization/index.d.ts +3 -0
- package/serialization/index.js +19 -0
- package/types/ArrayType.d.ts +13 -0
- package/types/ArrayType.js +47 -0
- package/types/BigIntType.d.ts +16 -0
- package/types/BigIntType.js +45 -0
- package/types/BlobType.d.ts +10 -0
- package/types/BlobType.js +27 -0
- package/types/BooleanType.d.ts +8 -0
- package/types/BooleanType.js +16 -0
- package/types/DateTimeType.d.ts +8 -0
- package/types/DateTimeType.js +16 -0
- package/types/DateType.d.ts +8 -0
- package/types/DateType.js +16 -0
- package/types/DecimalType.d.ts +11 -0
- package/types/DecimalType.js +22 -0
- package/types/DoubleType.d.ts +11 -0
- package/types/DoubleType.js +22 -0
- package/types/EnumArrayType.d.ts +9 -0
- package/types/EnumArrayType.js +32 -0
- package/types/EnumType.d.ts +8 -0
- package/types/EnumType.js +16 -0
- package/types/FloatType.d.ts +8 -0
- package/types/FloatType.js +16 -0
- package/types/IntegerType.d.ts +8 -0
- package/types/IntegerType.js +16 -0
- package/types/IntervalType.d.ts +8 -0
- package/types/IntervalType.js +16 -0
- package/types/JsonType.d.ts +13 -0
- package/types/JsonType.js +34 -0
- package/types/MediumIntType.d.ts +6 -0
- package/types/MediumIntType.js +10 -0
- package/types/SmallIntType.d.ts +8 -0
- package/types/SmallIntType.js +16 -0
- package/types/StringType.d.ts +8 -0
- package/types/StringType.js +16 -0
- package/types/TextType.d.ts +8 -0
- package/types/TextType.js +16 -0
- package/types/TimeType.d.ts +9 -0
- package/types/TimeType.js +23 -0
- package/types/TinyIntType.d.ts +8 -0
- package/types/TinyIntType.js +16 -0
- package/types/Type.d.ts +64 -0
- package/types/Type.js +84 -0
- package/types/Uint8ArrayType.d.ts +11 -0
- package/types/Uint8ArrayType.js +37 -0
- package/types/UnknownType.d.ts +7 -0
- package/types/UnknownType.js +13 -0
- package/types/UuidType.d.ts +7 -0
- package/types/UuidType.js +13 -0
- package/types/index.d.ts +75 -0
- package/types/index.js +77 -0
- package/typings.d.ts +767 -0
- package/typings.js +198 -0
- package/unit-of-work/ChangeSet.d.ts +34 -0
- package/unit-of-work/ChangeSet.js +62 -0
- package/unit-of-work/ChangeSetComputer.d.ts +26 -0
- package/unit-of-work/ChangeSetComputer.js +153 -0
- package/unit-of-work/ChangeSetPersister.d.ts +50 -0
- package/unit-of-work/ChangeSetPersister.js +361 -0
- package/unit-of-work/CommitOrderCalculator.d.ts +62 -0
- package/unit-of-work/CommitOrderCalculator.js +113 -0
- package/unit-of-work/IdentityMap.d.ts +17 -0
- package/unit-of-work/IdentityMap.js +84 -0
- package/unit-of-work/UnitOfWork.d.ts +124 -0
- package/unit-of-work/UnitOfWork.js +1013 -0
- package/unit-of-work/index.d.ts +6 -0
- package/unit-of-work/index.js +22 -0
- package/utils/AbstractSchemaGenerator.d.ts +38 -0
- package/utils/AbstractSchemaGenerator.js +101 -0
- package/utils/Configuration.d.ts +390 -0
- package/utils/Configuration.js +357 -0
- package/utils/ConfigurationLoader.d.ts +29 -0
- package/utils/ConfigurationLoader.js +282 -0
- package/utils/Cursor.d.ts +77 -0
- package/utils/Cursor.js +169 -0
- package/utils/DataloaderUtils.d.ts +43 -0
- package/utils/DataloaderUtils.js +194 -0
- package/utils/EntityComparator.d.ts +73 -0
- package/utils/EntityComparator.js +568 -0
- package/utils/NullHighlighter.d.ts +4 -0
- package/utils/NullHighlighter.js +9 -0
- package/utils/QueryHelper.d.ts +28 -0
- package/utils/QueryHelper.js +228 -0
- package/utils/RawQueryFragment.d.ts +96 -0
- package/utils/RawQueryFragment.js +188 -0
- package/utils/RequestContext.d.ts +34 -0
- package/utils/RequestContext.js +54 -0
- package/utils/TransactionContext.d.ts +19 -0
- package/utils/TransactionContext.js +34 -0
- package/utils/Utils.d.ts +274 -0
- package/utils/Utils.js +1073 -0
- package/utils/clone.d.ts +6 -0
- package/utils/clone.js +127 -0
- package/utils/index.d.ts +13 -0
- package/utils/index.js +29 -0
- package/utils/upsert-utils.d.ts +6 -0
- package/utils/upsert-utils.js +33 -0
package/typings.js
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EntityMetadata = exports.Config = exports.HiddenProps = exports.EagerProps = exports.OptionalProps = exports.PrimaryKeyProp = exports.EntityRepositoryType = void 0;
|
|
4
|
+
const enums_1 = require("./enums");
|
|
5
|
+
const entity_1 = require("./entity");
|
|
6
|
+
const Utils_1 = require("./utils/Utils");
|
|
7
|
+
const EntityComparator_1 = require("./utils/EntityComparator");
|
|
8
|
+
exports.EntityRepositoryType = Symbol('EntityRepositoryType');
|
|
9
|
+
exports.PrimaryKeyProp = Symbol('PrimaryKeyProp');
|
|
10
|
+
exports.OptionalProps = Symbol('OptionalProps');
|
|
11
|
+
exports.EagerProps = Symbol('EagerProps');
|
|
12
|
+
exports.HiddenProps = Symbol('HiddenProps');
|
|
13
|
+
exports.Config = Symbol('Config');
|
|
14
|
+
class EntityMetadata {
|
|
15
|
+
static counter = 0;
|
|
16
|
+
_id = 1000 * EntityMetadata.counter++; // keep the id >= 1000 to allow computing cache keys by simple addition
|
|
17
|
+
propertyOrder = new Map();
|
|
18
|
+
constructor(meta = {}) {
|
|
19
|
+
this.properties = {};
|
|
20
|
+
this.props = [];
|
|
21
|
+
this.primaryKeys = [];
|
|
22
|
+
this.filters = {};
|
|
23
|
+
this.hooks = {};
|
|
24
|
+
this.indexes = [];
|
|
25
|
+
this.uniques = [];
|
|
26
|
+
this.checks = [];
|
|
27
|
+
this.referencingProperties = [];
|
|
28
|
+
this.concurrencyCheckKeys = new Set();
|
|
29
|
+
this.stiChildren = [];
|
|
30
|
+
Object.assign(this, meta);
|
|
31
|
+
}
|
|
32
|
+
getStiRoot() {
|
|
33
|
+
// if (this.root.stiRoot) {
|
|
34
|
+
// return this.root.stiRoot;
|
|
35
|
+
// }
|
|
36
|
+
// const children = this.root.stiChildren as EntityMetadata<any>[];
|
|
37
|
+
// if (children.length === 0) {
|
|
38
|
+
// return Utils.copy(this.root, false);
|
|
39
|
+
// }
|
|
40
|
+
// const newRoot: EntityMetadata<any> = Utils.copy(this.root, false);
|
|
41
|
+
// for (const meta of children) {
|
|
42
|
+
// Object.values(meta.properties).forEach((prop) => {
|
|
43
|
+
// const exists = newRoot.properties[prop.name];
|
|
44
|
+
// prop = Utils.copy(prop, false);
|
|
45
|
+
// prop.nullable = true;
|
|
46
|
+
// if (!exists) {
|
|
47
|
+
// prop.inherited = true;
|
|
48
|
+
// }
|
|
49
|
+
// newRoot.addProperty(prop);
|
|
50
|
+
// });
|
|
51
|
+
// newRoot.indexes = Utils.unique([...newRoot.indexes, ...meta.indexes]);
|
|
52
|
+
// newRoot.uniques = Utils.unique([...newRoot.uniques, ...meta.uniques]);
|
|
53
|
+
// }
|
|
54
|
+
// this.root.stiRoot = newRoot;
|
|
55
|
+
// return newRoot;
|
|
56
|
+
return this.root.stiRoot ?? this.root;
|
|
57
|
+
}
|
|
58
|
+
addProperty(prop, sync = true) {
|
|
59
|
+
if (prop.pivotTable && !prop.pivotEntity) {
|
|
60
|
+
prop.pivotEntity = prop.pivotTable;
|
|
61
|
+
}
|
|
62
|
+
this.properties[prop.name] = prop;
|
|
63
|
+
this.propertyOrder.set(prop.name, this.props.length);
|
|
64
|
+
/* istanbul ignore next */
|
|
65
|
+
if (sync) {
|
|
66
|
+
this.sync();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
removeProperty(name, sync = true) {
|
|
70
|
+
delete this.properties[name];
|
|
71
|
+
this.propertyOrder.delete(name);
|
|
72
|
+
/* istanbul ignore next */
|
|
73
|
+
if (sync) {
|
|
74
|
+
this.sync();
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
getPrimaryProps() {
|
|
78
|
+
return this.primaryKeys.map(pk => this.properties[pk]);
|
|
79
|
+
}
|
|
80
|
+
get tableName() {
|
|
81
|
+
return this.collection;
|
|
82
|
+
}
|
|
83
|
+
set tableName(name) {
|
|
84
|
+
this.collection = name;
|
|
85
|
+
}
|
|
86
|
+
sync(initIndexes = false) {
|
|
87
|
+
this.root ??= this;
|
|
88
|
+
const props = Object.values(this.properties).sort((a, b) => this.propertyOrder.get(a.name) - this.propertyOrder.get(b.name));
|
|
89
|
+
this.props = [...props.filter(p => p.primary), ...props.filter(p => !p.primary)];
|
|
90
|
+
this.relations = this.props.filter(prop => typeof prop.kind !== 'undefined' && prop.kind !== enums_1.ReferenceKind.SCALAR && prop.kind !== enums_1.ReferenceKind.EMBEDDED);
|
|
91
|
+
this.bidirectionalRelations = this.relations.filter(prop => prop.mappedBy || prop.inversedBy);
|
|
92
|
+
this.uniqueProps = this.props.filter(prop => prop.unique);
|
|
93
|
+
this.comparableProps = this.props.filter(prop => EntityComparator_1.EntityComparator.isComparable(prop, this));
|
|
94
|
+
this.hydrateProps = this.props.filter(prop => {
|
|
95
|
+
// `prop.userDefined` is either `undefined` or `false`
|
|
96
|
+
const discriminator = this.root.discriminatorColumn === prop.name && prop.userDefined === false;
|
|
97
|
+
// even if we don't have a setter, do not ignore value from database!
|
|
98
|
+
const onlyGetter = prop.getter && !prop.setter;
|
|
99
|
+
return !prop.inherited && prop.hydrate !== false && !discriminator && !prop.embedded && !onlyGetter;
|
|
100
|
+
});
|
|
101
|
+
this.trackingProps = this.hydrateProps
|
|
102
|
+
.filter(prop => !prop.getter && !prop.setter)
|
|
103
|
+
.filter(prop => ![enums_1.ReferenceKind.ONE_TO_MANY, enums_1.ReferenceKind.MANY_TO_MANY].includes(prop.kind))
|
|
104
|
+
.filter(prop => !prop.serializedPrimaryKey);
|
|
105
|
+
this.selfReferencing = this.relations.some(prop => [this.className, this.root.className].includes(prop.type));
|
|
106
|
+
this.hasUniqueProps = this.uniques.length + this.uniqueProps.length > 0;
|
|
107
|
+
this.virtual = !!this.expression;
|
|
108
|
+
this.checks = Utils_1.Utils.removeDuplicates(this.checks);
|
|
109
|
+
this.indexes = Utils_1.Utils.removeDuplicates(this.indexes);
|
|
110
|
+
this.uniques = Utils_1.Utils.removeDuplicates(this.uniques);
|
|
111
|
+
for (const hook of Utils_1.Utils.keys(this.hooks)) {
|
|
112
|
+
this.hooks[hook] = Utils_1.Utils.removeDuplicates(this.hooks[hook]);
|
|
113
|
+
}
|
|
114
|
+
if (this.virtual) {
|
|
115
|
+
this.readonly = true;
|
|
116
|
+
}
|
|
117
|
+
if (initIndexes && this.name) {
|
|
118
|
+
this.props.forEach(prop => this.initIndexes(prop));
|
|
119
|
+
}
|
|
120
|
+
this.definedProperties = this.trackingProps.reduce((o, prop) => {
|
|
121
|
+
const isCollection = [enums_1.ReferenceKind.ONE_TO_MANY, enums_1.ReferenceKind.MANY_TO_MANY].includes(prop.kind);
|
|
122
|
+
const isReference = [enums_1.ReferenceKind.ONE_TO_ONE, enums_1.ReferenceKind.MANY_TO_ONE].includes(prop.kind) && (prop.inversedBy || prop.mappedBy) && !prop.mapToPk;
|
|
123
|
+
if (isReference) {
|
|
124
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
125
|
+
const meta = this;
|
|
126
|
+
o[prop.name] = {
|
|
127
|
+
get() {
|
|
128
|
+
return this.__helper.__data[prop.name];
|
|
129
|
+
},
|
|
130
|
+
set(val) {
|
|
131
|
+
const wrapped = this.__helper;
|
|
132
|
+
const hydrator = wrapped.hydrator;
|
|
133
|
+
const entity = entity_1.Reference.unwrapReference(val ?? wrapped.__data[prop.name]);
|
|
134
|
+
const old = entity_1.Reference.unwrapReference(wrapped.__data[prop.name]);
|
|
135
|
+
wrapped.__data[prop.name] = entity_1.Reference.wrapReference(val, prop);
|
|
136
|
+
// when propagation from inside hydration, we set the FK to the entity data immediately
|
|
137
|
+
if (val && hydrator.isRunning() && wrapped.__originalEntityData && prop.owner) {
|
|
138
|
+
wrapped.__originalEntityData[prop.name] = Utils_1.Utils.getPrimaryKeyValues(val, prop.targetMeta.primaryKeys, true);
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
wrapped.__touched = !hydrator.isRunning();
|
|
142
|
+
}
|
|
143
|
+
entity_1.EntityHelper.propagate(meta, entity, this, prop, entity_1.Reference.unwrapReference(val), old);
|
|
144
|
+
},
|
|
145
|
+
enumerable: true,
|
|
146
|
+
configurable: true,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
if (prop.inherited || prop.primary || isCollection || prop.persist === false || prop.trackChanges === false || isReference || prop.embedded) {
|
|
150
|
+
return o;
|
|
151
|
+
}
|
|
152
|
+
o[prop.name] = {
|
|
153
|
+
get() {
|
|
154
|
+
return this.__helper.__data[prop.name];
|
|
155
|
+
},
|
|
156
|
+
set(val) {
|
|
157
|
+
if (typeof val === 'object' && !!val && '__raw' in val) {
|
|
158
|
+
val.assign();
|
|
159
|
+
}
|
|
160
|
+
this.__helper.__data[prop.name] = val;
|
|
161
|
+
this.__helper.__touched = !this.__helper.hydrator.isRunning();
|
|
162
|
+
},
|
|
163
|
+
enumerable: true,
|
|
164
|
+
configurable: true,
|
|
165
|
+
};
|
|
166
|
+
return o;
|
|
167
|
+
}, { __gettersDefined: { value: true, enumerable: false } });
|
|
168
|
+
}
|
|
169
|
+
initIndexes(prop) {
|
|
170
|
+
const simpleIndex = this.indexes.find(index => index.properties === prop.name && !index.options && !index.type && !index.expression);
|
|
171
|
+
const simpleUnique = this.uniques.find(index => index.properties === prop.name && !index.options);
|
|
172
|
+
const owner = prop.kind === enums_1.ReferenceKind.MANY_TO_ONE;
|
|
173
|
+
if (!prop.index && simpleIndex) {
|
|
174
|
+
Utils_1.Utils.defaultValue(simpleIndex, 'name', true);
|
|
175
|
+
prop.index = simpleIndex.name;
|
|
176
|
+
this.indexes.splice(this.indexes.indexOf(simpleIndex), 1);
|
|
177
|
+
}
|
|
178
|
+
if (!prop.unique && simpleUnique) {
|
|
179
|
+
Utils_1.Utils.defaultValue(simpleUnique, 'name', true);
|
|
180
|
+
prop.unique = simpleUnique.name;
|
|
181
|
+
this.uniques.splice(this.uniques.indexOf(simpleUnique), 1);
|
|
182
|
+
}
|
|
183
|
+
if (prop.index && owner && prop.fieldNames.length > 1) {
|
|
184
|
+
this.indexes.push({ properties: prop.name });
|
|
185
|
+
prop.index = false;
|
|
186
|
+
}
|
|
187
|
+
/* istanbul ignore next */
|
|
188
|
+
if (owner && prop.fieldNames.length > 1 && prop.unique) {
|
|
189
|
+
this.uniques.push({ properties: prop.name });
|
|
190
|
+
prop.unique = false;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
/** @internal */
|
|
194
|
+
clone() {
|
|
195
|
+
return this;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
exports.EntityMetadata = EntityMetadata;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { inspect } from 'util';
|
|
3
|
+
import type { EntityData, EntityMetadata, EntityDictionary, Primary } from '../typings';
|
|
4
|
+
export declare class ChangeSet<T extends object> {
|
|
5
|
+
entity: T;
|
|
6
|
+
type: ChangeSetType;
|
|
7
|
+
payload: EntityDictionary<T>;
|
|
8
|
+
meta: EntityMetadata<T>;
|
|
9
|
+
private primaryKey?;
|
|
10
|
+
private serializedPrimaryKey?;
|
|
11
|
+
constructor(entity: T, type: ChangeSetType, payload: EntityDictionary<T>, meta: EntityMetadata<T>);
|
|
12
|
+
getPrimaryKey(object?: boolean): Primary<T> | null;
|
|
13
|
+
getSerializedPrimaryKey(): string | null;
|
|
14
|
+
/** @ignore */
|
|
15
|
+
[inspect.custom](depth: number): string;
|
|
16
|
+
}
|
|
17
|
+
export interface ChangeSet<T> {
|
|
18
|
+
name: string;
|
|
19
|
+
rootName: string;
|
|
20
|
+
collection: string;
|
|
21
|
+
schema?: string;
|
|
22
|
+
type: ChangeSetType;
|
|
23
|
+
entity: T;
|
|
24
|
+
payload: EntityDictionary<T>;
|
|
25
|
+
persisted: boolean;
|
|
26
|
+
originalEntity?: EntityData<T>;
|
|
27
|
+
}
|
|
28
|
+
export declare enum ChangeSetType {
|
|
29
|
+
CREATE = "create",
|
|
30
|
+
UPDATE = "update",
|
|
31
|
+
DELETE = "delete",
|
|
32
|
+
UPDATE_EARLY = "update_early",
|
|
33
|
+
DELETE_EARLY = "delete_early"
|
|
34
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ChangeSetType = exports.ChangeSet = void 0;
|
|
4
|
+
const util_1 = require("util");
|
|
5
|
+
const wrap_1 = require("../entity/wrap");
|
|
6
|
+
const Utils_1 = require("../utils/Utils");
|
|
7
|
+
class ChangeSet {
|
|
8
|
+
entity;
|
|
9
|
+
type;
|
|
10
|
+
payload;
|
|
11
|
+
meta;
|
|
12
|
+
primaryKey;
|
|
13
|
+
serializedPrimaryKey;
|
|
14
|
+
constructor(entity, type, payload, meta) {
|
|
15
|
+
this.entity = entity;
|
|
16
|
+
this.type = type;
|
|
17
|
+
this.payload = payload;
|
|
18
|
+
this.meta = meta;
|
|
19
|
+
this.name = meta.className;
|
|
20
|
+
this.rootName = meta.root.className;
|
|
21
|
+
this.collection = meta.root.collection;
|
|
22
|
+
this.schema = (0, wrap_1.helper)(entity).__schema ?? meta.root.schema;
|
|
23
|
+
}
|
|
24
|
+
getPrimaryKey(object = false) {
|
|
25
|
+
if (!this.originalEntity) {
|
|
26
|
+
this.primaryKey ??= (0, wrap_1.helper)(this.entity).getPrimaryKey(true);
|
|
27
|
+
}
|
|
28
|
+
else if (this.meta.compositePK) {
|
|
29
|
+
this.primaryKey = this.meta.primaryKeys.map(pk => this.originalEntity[pk]);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
this.primaryKey = this.originalEntity[this.meta.primaryKeys[0]];
|
|
33
|
+
}
|
|
34
|
+
if (object && this.primaryKey != null) {
|
|
35
|
+
return Utils_1.Utils.primaryKeyToObject(this.meta, this.primaryKey);
|
|
36
|
+
}
|
|
37
|
+
return this.primaryKey ?? null;
|
|
38
|
+
}
|
|
39
|
+
getSerializedPrimaryKey() {
|
|
40
|
+
this.serializedPrimaryKey ??= (0, wrap_1.helper)(this.entity).getSerializedPrimaryKey();
|
|
41
|
+
return this.serializedPrimaryKey;
|
|
42
|
+
}
|
|
43
|
+
/** @ignore */
|
|
44
|
+
[util_1.inspect.custom](depth) {
|
|
45
|
+
const object = { ...this };
|
|
46
|
+
const hidden = ['meta', 'serializedPrimaryKey'];
|
|
47
|
+
hidden.forEach(k => delete object[k]);
|
|
48
|
+
const ret = (0, util_1.inspect)(object, { depth });
|
|
49
|
+
const name = `${this.constructor.name}<${this.meta.className}>`;
|
|
50
|
+
/* istanbul ignore next */
|
|
51
|
+
return ret === '[Object]' ? `[${name}]` : name + ' ' + ret;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
exports.ChangeSet = ChangeSet;
|
|
55
|
+
var ChangeSetType;
|
|
56
|
+
(function (ChangeSetType) {
|
|
57
|
+
ChangeSetType["CREATE"] = "create";
|
|
58
|
+
ChangeSetType["UPDATE"] = "update";
|
|
59
|
+
ChangeSetType["DELETE"] = "delete";
|
|
60
|
+
ChangeSetType["UPDATE_EARLY"] = "update_early";
|
|
61
|
+
ChangeSetType["DELETE_EARLY"] = "delete_early";
|
|
62
|
+
})(ChangeSetType || (exports.ChangeSetType = ChangeSetType = {}));
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type Configuration } from '../utils';
|
|
2
|
+
import type { MetadataStorage } from '../metadata';
|
|
3
|
+
import type { AnyEntity } from '../typings';
|
|
4
|
+
import { ChangeSet } from './ChangeSet';
|
|
5
|
+
import { type Collection, type EntityValidator } from '../entity';
|
|
6
|
+
import type { Platform } from '../platforms';
|
|
7
|
+
import type { EntityManager } from '../EntityManager';
|
|
8
|
+
export declare class ChangeSetComputer {
|
|
9
|
+
private readonly validator;
|
|
10
|
+
private readonly collectionUpdates;
|
|
11
|
+
private readonly metadata;
|
|
12
|
+
private readonly platform;
|
|
13
|
+
private readonly config;
|
|
14
|
+
private readonly em;
|
|
15
|
+
private readonly comparator;
|
|
16
|
+
constructor(validator: EntityValidator, collectionUpdates: Set<Collection<AnyEntity>>, metadata: MetadataStorage, platform: Platform, config: Configuration, em: EntityManager);
|
|
17
|
+
computeChangeSet<T extends object>(entity: T): ChangeSet<T> | null;
|
|
18
|
+
/**
|
|
19
|
+
* Traverses entity graph and executes `onCreate` and `onUpdate` methods, assigning the values to given properties.
|
|
20
|
+
*/
|
|
21
|
+
private processPropertyInitializers;
|
|
22
|
+
private computePayload;
|
|
23
|
+
private processProperty;
|
|
24
|
+
private processToOne;
|
|
25
|
+
private processToMany;
|
|
26
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ChangeSetComputer = void 0;
|
|
4
|
+
const utils_1 = require("../utils");
|
|
5
|
+
const ChangeSet_1 = require("./ChangeSet");
|
|
6
|
+
const entity_1 = require("../entity");
|
|
7
|
+
const enums_1 = require("../enums");
|
|
8
|
+
class ChangeSetComputer {
|
|
9
|
+
validator;
|
|
10
|
+
collectionUpdates;
|
|
11
|
+
metadata;
|
|
12
|
+
platform;
|
|
13
|
+
config;
|
|
14
|
+
em;
|
|
15
|
+
comparator;
|
|
16
|
+
constructor(validator, collectionUpdates, metadata, platform, config, em) {
|
|
17
|
+
this.validator = validator;
|
|
18
|
+
this.collectionUpdates = collectionUpdates;
|
|
19
|
+
this.metadata = metadata;
|
|
20
|
+
this.platform = platform;
|
|
21
|
+
this.config = config;
|
|
22
|
+
this.em = em;
|
|
23
|
+
this.comparator = this.config.getComparator(this.metadata);
|
|
24
|
+
}
|
|
25
|
+
computeChangeSet(entity) {
|
|
26
|
+
const wrapped = (0, entity_1.helper)(entity);
|
|
27
|
+
const meta = this.metadata.get(wrapped.__meta.className);
|
|
28
|
+
if (meta.readonly) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
const type = wrapped.__originalEntityData ? ChangeSet_1.ChangeSetType.UPDATE : ChangeSet_1.ChangeSetType.CREATE;
|
|
32
|
+
const map = new Map();
|
|
33
|
+
// Execute `onCreate` and `onUpdate` on properties recursively, saves `onUpdate` results
|
|
34
|
+
// to the `map` as we want to apply those only if something else changed.
|
|
35
|
+
if (type === ChangeSet_1.ChangeSetType.CREATE) { // run update hooks only after we know there are other changes
|
|
36
|
+
for (const prop of meta.hydrateProps) {
|
|
37
|
+
this.processPropertyInitializers(entity, prop, type, map);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (type === ChangeSet_1.ChangeSetType.UPDATE && !wrapped.__initialized && !wrapped.isTouched()) {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
const changeSet = new ChangeSet_1.ChangeSet(entity, type, this.computePayload(entity), meta);
|
|
44
|
+
changeSet.originalEntity = wrapped.__originalEntityData;
|
|
45
|
+
if (this.config.get('validate')) {
|
|
46
|
+
this.validator.validate(changeSet.entity, changeSet.payload, meta);
|
|
47
|
+
}
|
|
48
|
+
for (const prop of meta.relations.filter(prop => prop.persist !== false || prop.userDefined === false)) {
|
|
49
|
+
this.processProperty(changeSet, prop);
|
|
50
|
+
}
|
|
51
|
+
if (changeSet.type === ChangeSet_1.ChangeSetType.UPDATE && !utils_1.Utils.hasObjectKeys(changeSet.payload)) {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
// Execute `onCreate` and `onUpdate` on properties recursively, saves `onUpdate` results
|
|
55
|
+
// to the `map` as we want to apply those only if something else changed.
|
|
56
|
+
if (type === ChangeSet_1.ChangeSetType.UPDATE) {
|
|
57
|
+
for (const prop of meta.hydrateProps) {
|
|
58
|
+
this.processPropertyInitializers(entity, prop, type, map);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (map.size > 0) {
|
|
62
|
+
for (const [entity, pairs] of map) {
|
|
63
|
+
for (const [prop, value] of pairs) {
|
|
64
|
+
entity[prop] = value;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
// Recompute the changeset, we need to merge this as here we ignore relations.
|
|
68
|
+
const diff = this.computePayload(entity, true);
|
|
69
|
+
utils_1.Utils.merge(changeSet.payload, diff);
|
|
70
|
+
}
|
|
71
|
+
return changeSet;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Traverses entity graph and executes `onCreate` and `onUpdate` methods, assigning the values to given properties.
|
|
75
|
+
*/
|
|
76
|
+
processPropertyInitializers(entity, prop, type, map, nested) {
|
|
77
|
+
if (prop.onCreate && type === ChangeSet_1.ChangeSetType.CREATE && entity[prop.name] == null) {
|
|
78
|
+
entity[prop.name] = prop.onCreate(entity, this.em);
|
|
79
|
+
}
|
|
80
|
+
if (prop.onUpdate && type === ChangeSet_1.ChangeSetType.UPDATE) {
|
|
81
|
+
const pairs = map.get(entity) ?? [];
|
|
82
|
+
pairs.push([prop.name, prop.onUpdate(entity, this.em)]);
|
|
83
|
+
map.set(entity, pairs);
|
|
84
|
+
}
|
|
85
|
+
if (prop.kind === enums_1.ReferenceKind.EMBEDDED && entity[prop.name]) {
|
|
86
|
+
for (const embeddedProp of prop.targetMeta.hydrateProps) {
|
|
87
|
+
this.processPropertyInitializers(entity[prop.name], embeddedProp, type, map, nested || prop.object);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
computePayload(entity, ignoreUndefined = false) {
|
|
92
|
+
const data = this.comparator.prepareEntity(entity);
|
|
93
|
+
const entityName = (0, entity_1.helper)(entity).__meta.className;
|
|
94
|
+
const originalEntityData = (0, entity_1.helper)(entity).__originalEntityData;
|
|
95
|
+
if (originalEntityData) {
|
|
96
|
+
const comparator = this.comparator.getEntityComparator(entityName);
|
|
97
|
+
const diff = comparator(originalEntityData, data);
|
|
98
|
+
if (ignoreUndefined) {
|
|
99
|
+
utils_1.Utils.keys(diff)
|
|
100
|
+
.filter(k => diff[k] === undefined)
|
|
101
|
+
.forEach(k => delete diff[k]);
|
|
102
|
+
}
|
|
103
|
+
return diff;
|
|
104
|
+
}
|
|
105
|
+
return data;
|
|
106
|
+
}
|
|
107
|
+
processProperty(changeSet, prop, target) {
|
|
108
|
+
if (!target) {
|
|
109
|
+
const targets = utils_1.Utils.unwrapProperty(changeSet.entity, changeSet.meta, prop);
|
|
110
|
+
targets.forEach(([t]) => this.processProperty(changeSet, prop, t));
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
if (utils_1.Utils.isCollection(target)) { // m:n or 1:m
|
|
114
|
+
this.processToMany(prop, changeSet);
|
|
115
|
+
}
|
|
116
|
+
if ([enums_1.ReferenceKind.MANY_TO_ONE, enums_1.ReferenceKind.ONE_TO_ONE].includes(prop.kind)) {
|
|
117
|
+
this.processToOne(prop, changeSet);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
processToOne(prop, changeSet) {
|
|
121
|
+
const isToOneOwner = prop.kind === enums_1.ReferenceKind.MANY_TO_ONE || (prop.kind === enums_1.ReferenceKind.ONE_TO_ONE && prop.owner);
|
|
122
|
+
if (!isToOneOwner || prop.mapToPk) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
const targets = utils_1.Utils.unwrapProperty(changeSet.entity, changeSet.meta, prop);
|
|
126
|
+
targets.forEach(([target, idx]) => {
|
|
127
|
+
if (!target.__helper.hasPrimaryKey()) {
|
|
128
|
+
utils_1.Utils.setPayloadProperty(changeSet.payload, this.metadata.find(changeSet.name), prop, target.__helper.__identifier, idx);
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
processToMany(prop, changeSet) {
|
|
133
|
+
const target = changeSet.entity[prop.name];
|
|
134
|
+
if (!target.isDirty()) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
if (prop.owner || target.getItems(false).filter(item => !item.__helper.__initialized).length > 0) {
|
|
138
|
+
if (this.platform.usesPivotTable()) {
|
|
139
|
+
this.collectionUpdates.add(target);
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
changeSet.payload[prop.name] = target.getItems(false).map((item) => item.__helper.__identifier ?? item.__helper.getPrimaryKey());
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
else if (prop.kind === enums_1.ReferenceKind.ONE_TO_MANY && target.getSnapshot() === undefined) {
|
|
146
|
+
this.collectionUpdates.add(target);
|
|
147
|
+
}
|
|
148
|
+
else if (prop.kind === enums_1.ReferenceKind.MANY_TO_MANY && !prop.owner) {
|
|
149
|
+
this.collectionUpdates.add(target);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
exports.ChangeSetComputer = ChangeSetComputer;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { MetadataStorage } from '../metadata';
|
|
2
|
+
import type { Dictionary, EntityDictionary, EntityMetadata, IHydrator } from '../typings';
|
|
3
|
+
import { type EntityFactory, type EntityValidator } from '../entity';
|
|
4
|
+
import { type ChangeSet } from './ChangeSet';
|
|
5
|
+
import { type Configuration } from '../utils';
|
|
6
|
+
import type { DriverMethodOptions, IDatabaseDriver } from '../drivers';
|
|
7
|
+
export declare class ChangeSetPersister {
|
|
8
|
+
private readonly driver;
|
|
9
|
+
private readonly metadata;
|
|
10
|
+
private readonly hydrator;
|
|
11
|
+
private readonly factory;
|
|
12
|
+
private readonly validator;
|
|
13
|
+
private readonly config;
|
|
14
|
+
private readonly platform;
|
|
15
|
+
private readonly comparator;
|
|
16
|
+
constructor(driver: IDatabaseDriver, metadata: MetadataStorage, hydrator: IHydrator, factory: EntityFactory, validator: EntityValidator, config: Configuration);
|
|
17
|
+
executeInserts<T extends object>(changeSets: ChangeSet<T>[], options?: DriverMethodOptions, withSchema?: boolean): Promise<void>;
|
|
18
|
+
executeUpdates<T extends object>(changeSets: ChangeSet<T>[], batched: boolean, options?: DriverMethodOptions, withSchema?: boolean): Promise<void>;
|
|
19
|
+
executeDeletes<T extends object>(changeSets: ChangeSet<T>[], options?: DriverMethodOptions, withSchema?: boolean): Promise<void>;
|
|
20
|
+
private runForEachSchema;
|
|
21
|
+
private processProperties;
|
|
22
|
+
private persistNewEntity;
|
|
23
|
+
private persistNewEntities;
|
|
24
|
+
private propagateSchemaFromMetadata;
|
|
25
|
+
private persistNewEntitiesBatch;
|
|
26
|
+
private persistManagedEntity;
|
|
27
|
+
private persistManagedEntities;
|
|
28
|
+
private checkConcurrencyKeys;
|
|
29
|
+
private persistManagedEntitiesBatch;
|
|
30
|
+
private mapPrimaryKey;
|
|
31
|
+
/**
|
|
32
|
+
* Sets populate flag to new entities so they are serialized like if they were loaded from the db
|
|
33
|
+
*/
|
|
34
|
+
private markAsPopulated;
|
|
35
|
+
private updateEntity;
|
|
36
|
+
private checkOptimisticLocks;
|
|
37
|
+
private checkOptimisticLock;
|
|
38
|
+
/**
|
|
39
|
+
* This method also handles reloading of database default values for inserts and raw property updates,
|
|
40
|
+
* so we use a single query in case of both versioning and default values is used.
|
|
41
|
+
*/
|
|
42
|
+
private reloadVersionValues;
|
|
43
|
+
private processProperty;
|
|
44
|
+
/**
|
|
45
|
+
* Maps values returned via `returning` statement (postgres) or the inserted id (other sql drivers).
|
|
46
|
+
* No need to handle composite keys here as they need to be set upfront.
|
|
47
|
+
* We do need to map to the change set payload too, as it will be used in the originalEntityData for new entities.
|
|
48
|
+
*/
|
|
49
|
+
mapReturnedValues<T extends object>(entity: T, payload: EntityDictionary<T>, row: Dictionary | undefined, meta: EntityMetadata<T>): void;
|
|
50
|
+
}
|