@mikro-orm/core 7.3.0-dev.21 → 7.3.0-dev.23
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 +10 -7
- package/EntityManager.js +57 -31
- package/drivers/DatabaseDriver.d.ts +2 -0
- package/drivers/DatabaseDriver.js +9 -1
- package/entity/Collection.d.ts +2 -1
- package/entity/Collection.js +21 -13
- package/entity/EntityAssigner.js +4 -1
- package/entity/EntityFactory.js +19 -7
- package/entity/EntityHelper.d.ts +8 -3
- package/entity/EntityHelper.js +28 -3
- package/entity/EntityLoader.js +44 -1
- package/entity/Reference.js +6 -5
- package/entity/WrappedEntity.js +1 -1
- package/entity/utils.js +3 -1
- package/entity/validators.d.ts +6 -0
- package/entity/validators.js +14 -0
- package/errors.d.ts +1 -0
- package/errors.js +3 -0
- package/hydration/ObjectHydrator.js +16 -10
- package/metadata/MetadataDiscovery.js +26 -5
- package/package.json +1 -1
- package/platforms/Platform.js +4 -1
- package/serialization/EntitySerializer.js +53 -44
- package/serialization/EntityTransformer.js +86 -78
- package/serialization/SerializationContext.js +1 -1
- package/typings.d.ts +5 -1
- package/typings.js +36 -34
- package/unit-of-work/ChangeSetComputer.js +56 -54
- package/unit-of-work/ChangeSetPersister.d.ts +2 -0
- package/unit-of-work/ChangeSetPersister.js +10 -5
- package/unit-of-work/UnitOfWork.js +53 -23
- package/utils/Configuration.d.ts +2 -2
- package/utils/Configuration.js +2 -2
- package/utils/EntityComparator.js +2 -8
- package/utils/QueryHelper.d.ts +6 -0
- package/utils/QueryHelper.js +42 -0
- package/utils/TransactionManager.js +3 -1
- package/utils/Utils.js +27 -6
- package/utils/clone.js +6 -1
- package/utils/upsert-utils.js +13 -6
|
@@ -3,11 +3,13 @@ import { Utils } from '../utils/Utils.js';
|
|
|
3
3
|
import { ReferenceKind } from '../enums.js';
|
|
4
4
|
import { SerializationContext } from './SerializationContext.js';
|
|
5
5
|
import { isRaw } from '../utils/RawQueryFragment.js';
|
|
6
|
-
function
|
|
6
|
+
function isPropertyVisible(prop, ignoreFields) {
|
|
7
|
+
const prefixed = !prop.primary && !prop.accessor && prop.name.startsWith('_'); // ignore prefixed properties, if it's not a PK
|
|
8
|
+
return !prop.hidden && !prefixed && !ignoreFields?.includes(prop.name);
|
|
9
|
+
}
|
|
10
|
+
function isVisible(meta, propName) {
|
|
7
11
|
const prop = meta.properties[propName];
|
|
8
|
-
|
|
9
|
-
const prefixed = prop && !prop.primary && !prop.accessor && propName.startsWith('_'); // ignore prefixed properties, if it's not a PK
|
|
10
|
-
return visible && !prefixed && !ignoreFields.includes(propName);
|
|
12
|
+
return !!prop && isPropertyVisible(prop);
|
|
11
13
|
}
|
|
12
14
|
/** Converts entity instances to plain objects via `toObject()`, respecting populate hints, hidden fields, and serialization context. */
|
|
13
15
|
export class EntityTransformer {
|
|
@@ -26,103 +28,109 @@ export class EntityTransformer {
|
|
|
26
28
|
root = new SerializationContext(wrapped.__serializationContext.populate, wrapped.__serializationContext.fields, wrapped.__serializationContext.exclude);
|
|
27
29
|
contextCreated = true;
|
|
28
30
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
props
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
if (wrapped.isInitialized() || !wrapped.hasPrimaryKey()) {
|
|
40
|
-
const entityKeys = new Set(Object.keys(entity));
|
|
41
|
-
for (const prop of meta.props) {
|
|
42
|
-
if (entityKeys.has(prop.name) || (prop.getter && prop.accessor === prop.name)) {
|
|
43
|
-
props.add(prop.name);
|
|
31
|
+
try {
|
|
32
|
+
SerializationContext.propagate(root, entity, isVisible);
|
|
33
|
+
const meta = wrapped.__meta;
|
|
34
|
+
const ret = {};
|
|
35
|
+
const pks = meta.serializedPrimaryKey && !meta.compositePK ? [meta.serializedPrimaryKey] : meta.primaryKeys;
|
|
36
|
+
const props = [];
|
|
37
|
+
for (const pk of pks) {
|
|
38
|
+
if (meta.properties[pk]) {
|
|
39
|
+
props.push(meta.properties[pk]);
|
|
44
40
|
}
|
|
45
41
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
42
|
+
// own keys that are not entity properties are never visible, so only the metadata properties are collected
|
|
43
|
+
if (wrapped.isInitialized() || !wrapped.hasPrimaryKey()) {
|
|
44
|
+
for (const prop of meta.props) {
|
|
45
|
+
if ((Object.prototype.propertyIsEnumerable.call(entity, prop.name) ||
|
|
46
|
+
(prop.getter && prop.accessor === prop.name)) &&
|
|
47
|
+
!pks.includes(prop.name)) {
|
|
48
|
+
props.push(prop);
|
|
49
|
+
}
|
|
49
50
|
}
|
|
50
51
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
root.visited.add(entity);
|
|
56
|
-
}
|
|
57
|
-
for (const prop of props) {
|
|
58
|
-
const visible = raw ? meta.properties[prop] : isVisible(meta, prop, ignoreFields);
|
|
59
|
-
if (!visible) {
|
|
60
|
-
continue;
|
|
52
|
+
const visited = root.visited.has(entity);
|
|
53
|
+
const includePrimaryKeys = wrapped.__config.get('serialization').includePrimaryKeys;
|
|
54
|
+
if (!visited) {
|
|
55
|
+
root.visited.add(entity);
|
|
61
56
|
}
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const isPrimary = includePrimaryKeys && meta.properties[prop].primary;
|
|
66
|
-
if (!partiallyLoaded && !populated && !isPrimary) {
|
|
57
|
+
for (const property of props) {
|
|
58
|
+
const prop = property.name;
|
|
59
|
+
if (!raw && !isPropertyVisible(property, ignoreFields)) {
|
|
67
60
|
continue;
|
|
68
61
|
}
|
|
69
|
-
|
|
62
|
+
const populated = root.isMarkedAsPopulated(meta.class, prop);
|
|
63
|
+
if (!raw) {
|
|
64
|
+
const partiallyLoaded = root.isPartiallyLoaded(meta.class, prop);
|
|
65
|
+
const isPrimary = includePrimaryKeys && property.primary;
|
|
66
|
+
if (!partiallyLoaded && !populated && !isPrimary) {
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (root.isExcluded(meta.class, prop) && !populated) {
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
const cycle = root.visit(meta.class, prop);
|
|
74
|
+
if (cycle && visited) {
|
|
70
75
|
continue;
|
|
71
76
|
}
|
|
77
|
+
let val;
|
|
78
|
+
try {
|
|
79
|
+
val = EntityTransformer.processProperty(prop, entity, raw, populated);
|
|
80
|
+
}
|
|
81
|
+
finally {
|
|
82
|
+
if (!cycle) {
|
|
83
|
+
root.leave(meta.class, prop);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (isRaw(val)) {
|
|
87
|
+
throw new Error(`Trying to serialize raw SQL fragment: '${val.sql}'`);
|
|
88
|
+
}
|
|
89
|
+
if (typeof val === 'undefined') {
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
ret[this.propertyName(meta, property, raw)] = val;
|
|
72
93
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
continue;
|
|
76
|
-
}
|
|
77
|
-
const val = EntityTransformer.processProperty(prop, entity, raw, populated);
|
|
78
|
-
if (!cycle) {
|
|
79
|
-
root.leave(meta.class, prop);
|
|
80
|
-
}
|
|
81
|
-
if (isRaw(val)) {
|
|
82
|
-
throw new Error(`Trying to serialize raw SQL fragment: '${val.sql}'`);
|
|
83
|
-
}
|
|
84
|
-
if (typeof val === 'undefined') {
|
|
85
|
-
continue;
|
|
94
|
+
if (!wrapped.isInitialized() && wrapped.hasPrimaryKey()) {
|
|
95
|
+
return ret;
|
|
86
96
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
const visible = !prop.hidden && entity[prop.getterName] instanceof Function;
|
|
96
|
-
const populated = root.isMarkedAsPopulated(meta.class, prop.name);
|
|
97
|
-
if (visible) {
|
|
98
|
-
ret[this.propertyName(meta, prop.name, raw)] = this.processProperty(prop.getterName, entity, raw, populated);
|
|
97
|
+
for (const prop of meta.getterProps) {
|
|
98
|
+
// decorated get methods
|
|
99
|
+
if (prop.getterName != null) {
|
|
100
|
+
const visible = !prop.hidden && entity[prop.getterName] instanceof Function;
|
|
101
|
+
const populated = root.isMarkedAsPopulated(meta.class, prop.name);
|
|
102
|
+
if (visible) {
|
|
103
|
+
ret[this.propertyName(meta, prop, raw)] = this.processProperty(prop.getterName, entity, raw, populated);
|
|
104
|
+
}
|
|
99
105
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
106
|
+
else {
|
|
107
|
+
// decorated getters
|
|
108
|
+
const visible = !prop.hidden && typeof entity[prop.name] !== 'undefined';
|
|
109
|
+
const populated = root.isMarkedAsPopulated(meta.class, prop.name);
|
|
110
|
+
if (visible) {
|
|
111
|
+
ret[this.propertyName(meta, prop, raw)] = this.processProperty(prop.name, entity, raw, populated);
|
|
112
|
+
}
|
|
107
113
|
}
|
|
108
114
|
}
|
|
115
|
+
return ret;
|
|
109
116
|
}
|
|
110
|
-
|
|
111
|
-
|
|
117
|
+
finally {
|
|
118
|
+
if (contextCreated) {
|
|
119
|
+
root.close();
|
|
120
|
+
}
|
|
112
121
|
}
|
|
113
|
-
return ret;
|
|
114
122
|
}
|
|
115
123
|
static propertyName(meta, prop, raw) {
|
|
116
124
|
if (raw) {
|
|
117
|
-
return prop;
|
|
125
|
+
return prop.name;
|
|
118
126
|
}
|
|
119
|
-
if (
|
|
120
|
-
return
|
|
127
|
+
if (prop.serializedName) {
|
|
128
|
+
return prop.serializedName;
|
|
121
129
|
}
|
|
122
|
-
if (
|
|
130
|
+
if (prop.primary && meta.serializedPrimaryKey) {
|
|
123
131
|
return meta.serializedPrimaryKey;
|
|
124
132
|
}
|
|
125
|
-
return prop;
|
|
133
|
+
return prop.name;
|
|
126
134
|
}
|
|
127
135
|
static processProperty(prop, entity, raw, populated) {
|
|
128
136
|
const wrapped = helper(entity);
|
|
@@ -43,7 +43,7 @@ export class SerializationContext {
|
|
|
43
43
|
/** Cleans up the serialization context by removing root references from all tracked entities. */
|
|
44
44
|
close() {
|
|
45
45
|
for (const entity of this.#entities) {
|
|
46
|
-
|
|
46
|
+
helper(entity).__serializationContext.root = undefined;
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
/**
|
package/typings.d.ts
CHANGED
|
@@ -384,8 +384,9 @@ export type FilterQuery<T> = ObjectQuery<T> | NonNullable<ExpandScalar<Primary<T
|
|
|
384
384
|
/**
|
|
385
385
|
* `FilterQuery` restricted to only properties covered by the specified index(es).
|
|
386
386
|
* Used when `using` option is set in `FindOptions` to enforce type-safe index usage.
|
|
387
|
+
* Falls back to `FilterQuery` when `Using` is not a specific index name (e.g. inferred from the options type).
|
|
387
388
|
*/
|
|
388
|
-
export type IndexFilterQuery<T, Using extends string> = [Using] extends [never] ? FilterQuery<T> : (OperatorMap<T> & {
|
|
389
|
+
export type IndexFilterQuery<T, Using extends string> = [Using] extends [never] ? FilterQuery<T> : string extends Using ? FilterQuery<T> : (OperatorMap<T> & {
|
|
389
390
|
-readonly [K in Extract<EntityKey<T>, IndexColumns<T, Using>>]?: ExpandQuery<ExpandProperty<FilterObjectProp<T, K>>> | ExpandQueryMerged<ExpandProperty<FilterObjectProp<T, K>>> | FilterValue<ExpandProperty<FilterObjectProp<T, K>>> | ArrayFilter<FilterObjectProp<T, K>> | null;
|
|
390
391
|
}) | NonNullable<ExpandScalar<Primary<T>>> | IndexFilterQuery<T, Using>[];
|
|
391
392
|
/** Replaces `where` and `using` on an options type with index-aware variants when `Using` is specified. */
|
|
@@ -1074,6 +1075,8 @@ export interface EntityProperty<Owner = any, Target = any> {
|
|
|
1074
1075
|
referencedColumnNames: string[];
|
|
1075
1076
|
referencedTableName: string;
|
|
1076
1077
|
referencedPKs: EntityKey<Owner>[];
|
|
1078
|
+
/** @internal target properties behind a composite FK that references more than the target PK and owns all its columns */
|
|
1079
|
+
referencedProps?: EntityProperty[];
|
|
1077
1080
|
targetKey?: string;
|
|
1078
1081
|
serializer?: (value: any, options?: SerializeOptions<any>) => any;
|
|
1079
1082
|
serializedName?: string;
|
|
@@ -1190,6 +1193,7 @@ export interface EntityMetadata<Entity = any, Class extends EntityCtor<Entity> =
|
|
|
1190
1193
|
comparableProps: EntityProperty<Entity>[];
|
|
1191
1194
|
trackingProps: EntityProperty<Entity>[];
|
|
1192
1195
|
hydrateProps: EntityProperty<Entity>[];
|
|
1196
|
+
updateHookProps: EntityProperty<Entity>[];
|
|
1193
1197
|
validateProps: EntityProperty<Entity>[];
|
|
1194
1198
|
uniqueProps: EntityProperty<Entity>[];
|
|
1195
1199
|
getterProps: EntityProperty<Entity>[];
|
package/typings.js
CHANGED
|
@@ -216,8 +216,13 @@ export class EntityMetadata {
|
|
|
216
216
|
const onlyGetter = prop.getter && !prop.setter && prop.persist === false;
|
|
217
217
|
return !prop.inherited && prop.hydrate !== false && !discriminator && !prop.embedded && !onlyGetter;
|
|
218
218
|
});
|
|
219
|
+
this.updateHookProps = this.hydrateProps.filter(prop => prop.onUpdate || prop.kind === ReferenceKind.EMBEDDED);
|
|
219
220
|
this.trackingProps = this.hydrateProps.filter(prop => {
|
|
220
|
-
return !prop.getter &&
|
|
221
|
+
return (!prop.getter &&
|
|
222
|
+
!prop.setter &&
|
|
223
|
+
[ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) &&
|
|
224
|
+
(prop.inversedBy || prop.mappedBy) &&
|
|
225
|
+
!prop.mapToPk);
|
|
221
226
|
});
|
|
222
227
|
this.selfReferencing = this.relations.some(prop => {
|
|
223
228
|
return this.root.uniqueName === prop.targetMeta?.root.uniqueName;
|
|
@@ -266,40 +271,37 @@ export class EntityMetadata {
|
|
|
266
271
|
this.props.forEach(prop => this.initIndexes(prop));
|
|
267
272
|
}
|
|
268
273
|
this.definedProperties = this.trackingProps.reduce((o, prop) => {
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
if (
|
|
292
|
-
|
|
293
|
-
if (targetMeta) {
|
|
294
|
-
wrapped.__originalEntityData[prop.name] = Utils.getPrimaryKeyValues(val, targetMeta, true);
|
|
295
|
-
}
|
|
274
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
275
|
+
const meta = this;
|
|
276
|
+
o[prop.name] = {
|
|
277
|
+
get() {
|
|
278
|
+
return this.__helper.__data[prop.name];
|
|
279
|
+
},
|
|
280
|
+
set(val) {
|
|
281
|
+
const wrapped = this.__helper;
|
|
282
|
+
const hydrator = wrapped.hydrator;
|
|
283
|
+
const entity = Reference.unwrapReference(val ?? wrapped.__data[prop.name]);
|
|
284
|
+
const old = Reference.unwrapReference(wrapped.__data[prop.name]);
|
|
285
|
+
if (old &&
|
|
286
|
+
old !== entity &&
|
|
287
|
+
prop.kind === ReferenceKind.MANY_TO_ONE &&
|
|
288
|
+
prop.inversedBy &&
|
|
289
|
+
old[prop.inversedBy]) {
|
|
290
|
+
old[prop.inversedBy].removeWithoutPropagation(this);
|
|
291
|
+
}
|
|
292
|
+
wrapped.__data[prop.name] = Reference.wrapReference(val, prop);
|
|
293
|
+
// when propagation from inside hydration, we set the FK to the entity data immediately
|
|
294
|
+
if (val && hydrator.isRunning() && wrapped.__originalEntityData && prop.owner) {
|
|
295
|
+
const targetMeta = prop.targetMeta ?? helper(entity)?.__meta;
|
|
296
|
+
if (targetMeta) {
|
|
297
|
+
wrapped.__originalEntityData[prop.name] = EntityHelper.getForeignKeyValue(entity, prop, targetMeta);
|
|
296
298
|
}
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
}
|
|
299
|
+
}
|
|
300
|
+
EntityHelper.propagate(meta, entity, this, prop, Reference.unwrapReference(val), old);
|
|
301
|
+
},
|
|
302
|
+
enumerable: true,
|
|
303
|
+
configurable: true,
|
|
304
|
+
};
|
|
303
305
|
return o;
|
|
304
306
|
}, { __gettersDefined: { value: true, enumerable: false } });
|
|
305
307
|
}
|
|
@@ -4,6 +4,7 @@ import { ChangeSet, ChangeSetType } from './ChangeSet.js';
|
|
|
4
4
|
import { helper } from '../entity/wrap.js';
|
|
5
5
|
import { validateEntity } from '../entity/validators.js';
|
|
6
6
|
import { Reference } from '../entity/Reference.js';
|
|
7
|
+
import { EntityHelper } from '../entity/EntityHelper.js';
|
|
7
8
|
import { PolymorphicRef } from '../entity/PolymorphicRef.js';
|
|
8
9
|
import { ReferenceKind } from '../enums.js';
|
|
9
10
|
import { isRaw } from '../utils/RawQueryFragment.js';
|
|
@@ -32,13 +33,10 @@ export class ChangeSetComputer {
|
|
|
32
33
|
}
|
|
33
34
|
const wrapped = helper(entity);
|
|
34
35
|
const type = wrapped.__originalEntityData ? ChangeSetType.UPDATE : ChangeSetType.CREATE;
|
|
35
|
-
|
|
36
|
-
// Execute `onCreate` and `onUpdate` on properties recursively, saves `onUpdate` results
|
|
37
|
-
// to the `map` as we want to apply those only if something else changed.
|
|
36
|
+
// execute `onCreate` on properties recursively, update hooks run only after we know there are other changes
|
|
38
37
|
if (type === ChangeSetType.CREATE) {
|
|
39
|
-
// run update hooks only after we know there are other changes
|
|
40
38
|
for (const prop of meta.hydrateProps) {
|
|
41
|
-
this.processPropertyInitializers(entity, prop, type
|
|
39
|
+
this.processPropertyInitializers(entity, prop, type);
|
|
42
40
|
}
|
|
43
41
|
}
|
|
44
42
|
if (type === ChangeSetType.UPDATE && !wrapped.__initialized) {
|
|
@@ -56,17 +54,20 @@ export class ChangeSetComputer {
|
|
|
56
54
|
}
|
|
57
55
|
const changeSet = new ChangeSet(entity, type, this.computePayload(entity), meta);
|
|
58
56
|
changeSet.originalEntity = wrapped.__originalEntityData;
|
|
59
|
-
for (const prop of meta.relations
|
|
60
|
-
|
|
57
|
+
for (const prop of meta.relations) {
|
|
58
|
+
if (prop.persist !== false || prop.userDefined === false) {
|
|
59
|
+
this.processProperty(changeSet, prop);
|
|
60
|
+
}
|
|
61
61
|
}
|
|
62
62
|
if (changeSet.type === ChangeSetType.UPDATE && !Utils.hasObjectKeys(changeSet.payload)) {
|
|
63
63
|
return null;
|
|
64
64
|
}
|
|
65
65
|
validateEntity(changeSet.entity, meta);
|
|
66
|
+
const map = new Map();
|
|
66
67
|
// Execute `onCreate` and `onUpdate` on properties recursively, saves `onUpdate` results
|
|
67
68
|
// to the `map` as we want to apply those only if something else changed.
|
|
68
69
|
if (type === ChangeSetType.UPDATE) {
|
|
69
|
-
for (const prop of meta.
|
|
70
|
+
for (const prop of meta.updateHookProps) {
|
|
70
71
|
this.processPropertyInitializers(entity, prop, type, map);
|
|
71
72
|
}
|
|
72
73
|
}
|
|
@@ -106,8 +107,9 @@ export class ChangeSetComputer {
|
|
|
106
107
|
}
|
|
107
108
|
if (prop.kind === ReferenceKind.EMBEDDED && entity[prop.name]) {
|
|
108
109
|
const items = prop.array ? entity[prop.name] : [entity[prop.name]];
|
|
110
|
+
const embeddedProps = type === ChangeSetType.UPDATE ? prop.targetMeta.updateHookProps : prop.targetMeta.hydrateProps;
|
|
109
111
|
for (const item of items) {
|
|
110
|
-
for (const embeddedProp of
|
|
112
|
+
for (const embeddedProp of embeddedProps) {
|
|
111
113
|
this.processPropertyInitializers(item, embeddedProp, type, map, nested || prop.object);
|
|
112
114
|
}
|
|
113
115
|
}
|
|
@@ -142,58 +144,58 @@ export class ChangeSetComputer {
|
|
|
142
144
|
}
|
|
143
145
|
return data;
|
|
144
146
|
}
|
|
145
|
-
processProperty(changeSet, prop
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
147
|
+
processProperty(changeSet, prop) {
|
|
148
|
+
if (prop.kind !== ReferenceKind.MANY_TO_ONE && prop.kind !== ReferenceKind.ONE_TO_ONE) {
|
|
149
|
+
// m:n or 1:m
|
|
150
|
+
if (Utils.isCollection(changeSet.entity[prop.name])) {
|
|
151
|
+
this.processToMany(prop, changeSet);
|
|
152
|
+
}
|
|
150
153
|
return;
|
|
151
154
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
+
// only owning to-one sides referencing an entity can add an FK value to the payload
|
|
156
|
+
if ((prop.kind === ReferenceKind.ONE_TO_ONE && !prop.owner) || prop.mapToPk) {
|
|
157
|
+
return;
|
|
155
158
|
}
|
|
156
|
-
|
|
157
|
-
|
|
159
|
+
// perf: a relation outside of embeddables has a single target, so skip the allocations of `unwrapProperty`
|
|
160
|
+
if (!prop.object && !prop.array && !prop.embedded) {
|
|
161
|
+
const target = changeSet.entity[prop.name];
|
|
162
|
+
if (target != null) {
|
|
163
|
+
this.processToOne(prop, changeSet, target, []);
|
|
164
|
+
}
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
const targets = Utils.unwrapProperty(changeSet.entity, changeSet.meta, prop);
|
|
168
|
+
for (const [target, idx] of targets) {
|
|
169
|
+
this.processToOne(prop, changeSet, target, idx);
|
|
158
170
|
}
|
|
159
171
|
}
|
|
160
|
-
processToOne(prop, changeSet) {
|
|
161
|
-
const
|
|
162
|
-
|
|
172
|
+
processToOne(prop, changeSet, rawTarget, idx) {
|
|
173
|
+
const target = Reference.unwrapReference(rawTarget);
|
|
174
|
+
// a same-named scalar of another polymorphic embeddable variant is not a reference
|
|
175
|
+
const needsProcessing = Utils.isEntity(target) && (prop.targetKey != null || !target.__helper.hasPrimaryKey());
|
|
176
|
+
if (!needsProcessing) {
|
|
163
177
|
return;
|
|
164
178
|
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
value = new PolymorphicRef(discriminator, value);
|
|
186
|
-
}
|
|
187
|
-
// an unchanged `targetKey` FK is not part of the diff
|
|
188
|
-
if (prop.targetKey &&
|
|
189
|
-
changeSet.type === ChangeSetType.UPDATE &&
|
|
190
|
-
!(prop.name in changeSet.payload) &&
|
|
191
|
-
Utils.equals(changeSet.originalEntity?.[prop.name], value)) {
|
|
192
|
-
return;
|
|
193
|
-
}
|
|
194
|
-
Utils.setPayloadProperty(changeSet.payload, changeSet.meta, prop, value, idx);
|
|
195
|
-
}
|
|
196
|
-
});
|
|
179
|
+
// a reference known only by its `targetKey` has no PK, and is never inserted to generate one
|
|
180
|
+
if (!prop.targetKey && !target.__helper.__initialized) {
|
|
181
|
+
throw ValidationError.referenceWithoutPK(target);
|
|
182
|
+
}
|
|
183
|
+
// use the actual target's meta, a polymorphic relation has several
|
|
184
|
+
let value = prop.targetKey
|
|
185
|
+
? EntityHelper.getForeignKeyValue(target, prop, target.__meta)
|
|
186
|
+
: target.__helper.__identifier;
|
|
187
|
+
if (prop.polymorphic) {
|
|
188
|
+
const discriminator = QueryHelper.findDiscriminatorValue(prop.discriminatorMap, target.constructor);
|
|
189
|
+
value = new PolymorphicRef(discriminator, value);
|
|
190
|
+
}
|
|
191
|
+
// an unchanged `targetKey` FK is not part of the diff
|
|
192
|
+
if (prop.targetKey &&
|
|
193
|
+
changeSet.type === ChangeSetType.UPDATE &&
|
|
194
|
+
!(prop.name in changeSet.payload) &&
|
|
195
|
+
Utils.equals(changeSet.originalEntity?.[prop.name], value)) {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
Utils.setPayloadProperty(changeSet.payload, changeSet.meta, prop, value, idx);
|
|
197
199
|
}
|
|
198
200
|
processToMany(prop, changeSet) {
|
|
199
201
|
const target = changeSet.entity[prop.name];
|
|
@@ -35,6 +35,8 @@ export declare class ChangeSetPersister {
|
|
|
35
35
|
*/
|
|
36
36
|
private markAsPopulated;
|
|
37
37
|
private updateEntity;
|
|
38
|
+
/** The payload (and so the entity snapshot) keeps the target PK, the driver gets the whole composite FK value. */
|
|
39
|
+
private getDriverPayload;
|
|
38
40
|
private checkOptimisticLocks;
|
|
39
41
|
private checkOptimisticLock;
|
|
40
42
|
/**
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { EntityIdentifier } from '../entity/EntityIdentifier.js';
|
|
2
2
|
import { PolymorphicRef } from '../entity/PolymorphicRef.js';
|
|
3
|
+
import { QueryHelper } from '../utils/QueryHelper.js';
|
|
3
4
|
import { helper } from '../entity/wrap.js';
|
|
4
5
|
import { ChangeSetType } from './ChangeSet.js';
|
|
5
6
|
import { isRaw } from '../utils/RawQueryFragment.js';
|
|
@@ -127,7 +128,7 @@ export class ChangeSetPersister {
|
|
|
127
128
|
});
|
|
128
129
|
this.resolveTPTIdentifiers(changeSet);
|
|
129
130
|
// Use changeSet's own meta for STI entities to get correct field mappings
|
|
130
|
-
const res = await this.#driver.nativeInsertMany(changeSet.meta.class, [changeSet
|
|
131
|
+
const res = await this.#driver.nativeInsertMany(changeSet.meta.class, [this.getDriverPayload(changeSet)], options);
|
|
131
132
|
if (!wrapped.hasPrimaryKey()) {
|
|
132
133
|
this.mapPrimaryKey(meta, res.insertId ?? res.row?.[meta.primaryKeys[0]], changeSet);
|
|
133
134
|
}
|
|
@@ -168,7 +169,7 @@ export class ChangeSetPersister {
|
|
|
168
169
|
for (const changeSet of changeSets) {
|
|
169
170
|
this.resolveTPTIdentifiers(changeSet);
|
|
170
171
|
}
|
|
171
|
-
const res = await this.#driver.nativeInsertMany(meta.class, changeSets.map(cs => cs
|
|
172
|
+
const res = await this.#driver.nativeInsertMany(meta.class, changeSets.map(cs => this.getDriverPayload(cs)), options);
|
|
172
173
|
for (let i = 0; i < changeSets.length; i++) {
|
|
173
174
|
const changeSet = changeSets[i];
|
|
174
175
|
const wrapped = helper(changeSet.entity);
|
|
@@ -239,7 +240,7 @@ export class ChangeSetPersister {
|
|
|
239
240
|
where[meta.versionProperty] = this.#platform.convertVersionValue(changeSet.entity[meta.versionProperty], meta.properties[meta.versionProperty]);
|
|
240
241
|
}
|
|
241
242
|
cond.push(where);
|
|
242
|
-
payload.push(changeSet
|
|
243
|
+
payload.push(this.getDriverPayload(changeSet));
|
|
243
244
|
}
|
|
244
245
|
const res = await this.#driver.nativeUpdateMany(meta.class, cond, payload, options);
|
|
245
246
|
const map = new Map();
|
|
@@ -324,13 +325,17 @@ export class ChangeSetPersister {
|
|
|
324
325
|
});
|
|
325
326
|
if (meta.getOwnConcurrencyCheckKeys().length === 0 &&
|
|
326
327
|
(!meta.ownsVersionProperty() || changeSet.entity[meta.versionProperty] == null)) {
|
|
327
|
-
return this.#driver.nativeUpdate(changeSet.meta.class, cond, changeSet
|
|
328
|
+
return this.#driver.nativeUpdate(changeSet.meta.class, cond, this.getDriverPayload(changeSet), options);
|
|
328
329
|
}
|
|
329
330
|
if (meta.ownsVersionProperty()) {
|
|
330
331
|
cond[meta.versionProperty] = this.#platform.convertVersionValue(changeSet.entity[meta.versionProperty], meta.properties[meta.versionProperty]);
|
|
331
332
|
}
|
|
332
333
|
this.checkConcurrencyKeys(meta, changeSet, cond);
|
|
333
|
-
return this.#driver.nativeUpdate(changeSet.meta.class, cond, changeSet
|
|
334
|
+
return this.#driver.nativeUpdate(changeSet.meta.class, cond, this.getDriverPayload(changeSet), options);
|
|
335
|
+
}
|
|
336
|
+
/** The payload (and so the entity snapshot) keeps the target PK, the driver gets the whole composite FK value. */
|
|
337
|
+
getDriverPayload(changeSet) {
|
|
338
|
+
return QueryHelper.withReferencedValues(changeSet.meta, changeSet.payload, changeSet.entity, this.#platform);
|
|
334
339
|
}
|
|
335
340
|
async checkOptimisticLocks(meta, changeSets, options) {
|
|
336
341
|
const concurrencyCheckKeys = meta.getOwnConcurrencyCheckKeys();
|