@mikro-orm/core 7.2.10-dev.8 → 7.2.10

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.
@@ -4,6 +4,7 @@ import { EventType, ReferenceKind } from '../enums.js';
4
4
  import { Reference } from './Reference.js';
5
5
  import { helper } from './wrap.js';
6
6
  import { EntityHelper } from './EntityHelper.js';
7
+ import { PolymorphicRef } from './PolymorphicRef.js';
7
8
  import { JsonType } from '../types/JsonType.js';
8
9
  import { isRaw } from '../utils/RawQueryFragment.js';
9
10
  /** @internal Factory responsible for creating, merging, and hydrating entity instances. */
@@ -237,12 +238,20 @@ export class EntityFactory {
237
238
  const nullVal = this.#config.get('forceUndefined') ? undefined : null;
238
239
  Utils.keys(diff2).forEach(key => {
239
240
  const prop = meta.properties[key];
240
- if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) &&
241
- Utils.isPlainObject(data[prop.name])) {
242
- // oxfmt-ignore
243
- diff2[key] = entity[prop.name] ? helper(entity[prop.name]).getPrimaryKey(options.convertCustomTypes) : null;
241
+ // a joined polymorphic row carries its target class in `constructor`, so it is not a plain object
242
+ const joined = Utils.isPlainObject(data[prop.name]) ||
243
+ (prop.polymorphic && Utils.isObject(data[prop.name]) && !(data[prop.name] instanceof PolymorphicRef));
244
+ if ([ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE].includes(prop.kind) && joined) {
245
+ const target = entity[prop.name] ? Reference.unwrapReference(entity[prop.name]) : null;
246
+ // use the actual target's meta, a polymorphic relation has several
247
+ let value = target ? EntityHelper.getForeignKeyValue(target, prop, helper(target).__meta) : null;
248
+ if (target && prop.polymorphic) {
249
+ const discriminator = QueryHelper.findDiscriminatorValue(prop.discriminatorMap, target.constructor);
250
+ value = new PolymorphicRef(discriminator, value);
251
+ }
252
+ diff2[key] = value;
244
253
  }
245
- if (!options.convertCustomTypes &&
254
+ else if (!options.convertCustomTypes &&
246
255
  [ReferenceKind.MANY_TO_ONE, ReferenceKind.ONE_TO_ONE, ReferenceKind.SCALAR].includes(prop.kind) &&
247
256
  prop.customType?.ensureComparable(meta, prop) &&
248
257
  diff2[key] != null) {
@@ -730,7 +730,8 @@ export class EntityLoader {
730
730
  convertCustomTypes: true,
731
731
  schema: options.schema ?? this.#em.config.get('schema'),
732
732
  });
733
- return this.#em.getUnitOfWork().register(entity, item, { refresh, loaded: true });
733
+ // the factory already registered the snapshot, the raw row is not comparable
734
+ return this.#em.getUnitOfWork().register(entity, undefined, { loaded: true });
734
735
  });
735
736
  const hasLimit = populate?.limit != null;
736
737
  entity[prop.name].hydrate(items, true, hasLimit, hasLimit);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/core",
3
- "version": "7.2.10-dev.8",
3
+ "version": "7.2.10",
4
4
  "description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
5
5
  "keywords": [
6
6
  "data-mapper",
@@ -366,7 +366,7 @@ export class Platform {
366
366
  }
367
367
  /** Serializes a string array into its database storage format. */
368
368
  marshallArray(values) {
369
- return values.map(v => (/[,",\\]/.test(v) ? JSON.stringify(v) : v)).join(',');
369
+ return values.map(v => (/[,",\\]/.test(v) || (v === '' && values.length === 1) ? JSON.stringify(v) : v)).join(',');
370
370
  }
371
371
  /** Deserializes a database-stored array string back into a string array. */
372
372
  unmarshallArray(value) {
@@ -406,6 +406,9 @@ export class Platform {
406
406
  i = comma + 1;
407
407
  }
408
408
  }
409
+ if (value.endsWith(',')) {
410
+ result.push('');
411
+ }
409
412
  return result;
410
413
  }
411
414
  getBlobDeclarationSQL() {
package/typings.d.ts CHANGED
@@ -1190,6 +1190,7 @@ export interface EntityMetadata<Entity = any, Class extends EntityCtor<Entity> =
1190
1190
  comparableProps: EntityProperty<Entity>[];
1191
1191
  trackingProps: EntityProperty<Entity>[];
1192
1192
  hydrateProps: EntityProperty<Entity>[];
1193
+ updateHookProps: EntityProperty<Entity>[];
1193
1194
  validateProps: EntityProperty<Entity>[];
1194
1195
  uniqueProps: EntityProperty<Entity>[];
1195
1196
  getterProps: EntityProperty<Entity>[];
package/typings.js CHANGED
@@ -216,6 +216,7 @@ 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
221
  return (!prop.getter &&
221
222
  !prop.setter &&
@@ -67,7 +67,7 @@ export class ChangeSetComputer {
67
67
  // Execute `onCreate` and `onUpdate` on properties recursively, saves `onUpdate` results
68
68
  // to the `map` as we want to apply those only if something else changed.
69
69
  if (type === ChangeSetType.UPDATE) {
70
- for (const prop of meta.hydrateProps) {
70
+ for (const prop of meta.updateHookProps) {
71
71
  this.processPropertyInitializers(entity, prop, type, map);
72
72
  }
73
73
  }
@@ -107,8 +107,9 @@ export class ChangeSetComputer {
107
107
  }
108
108
  if (prop.kind === ReferenceKind.EMBEDDED && entity[prop.name]) {
109
109
  const items = prop.array ? entity[prop.name] : [entity[prop.name]];
110
+ const embeddedProps = type === ChangeSetType.UPDATE ? prop.targetMeta.updateHookProps : prop.targetMeta.hydrateProps;
110
111
  for (const item of items) {
111
- for (const embeddedProp of prop.targetMeta.hydrateProps) {
112
+ for (const embeddedProp of embeddedProps) {
112
113
  this.processPropertyInitializers(item, embeddedProp, type, map, nested || prop.object);
113
114
  }
114
115
  }
@@ -961,8 +961,8 @@ export interface Options<Driver extends IDatabaseDriver = IDatabaseDriver, EM ex
961
961
  };
962
962
  /**
963
963
  * Metadata provider class for entity discovery.
964
- * Built-in options: `ReflectMetadataProvider` (default), `TsMorphMetadataProvider`.
965
- * @default ReflectMetadataProvider
964
+ * Built-in options: `MetadataProvider` (default), `ReflectMetadataProvider` (from `@mikro-orm/decorators/legacy`), `TsMorphMetadataProvider`.
965
+ * @default MetadataProvider
966
966
  * @see https://mikro-orm.io/docs/metadata-providers
967
967
  */
968
968
  metadataProvider: {
@@ -25,7 +25,6 @@ const DEFAULTS = {
25
25
  warnWhenNoEntities: true,
26
26
  checkDuplicateTableNames: true,
27
27
  checkDuplicateFieldNames: true,
28
- checkDuplicateEntities: true,
29
28
  checkNonPersistentCompositeProps: true,
30
29
  inferDefaultValues: true,
31
30
  },
package/utils/Utils.js CHANGED
@@ -153,7 +153,7 @@ export function parseJsonSafe(value) {
153
153
  /** Collection of general-purpose utility methods used throughout the ORM. */
154
154
  export class Utils {
155
155
  static PK_SEPARATOR = '~~~';
156
- static #ORM_VERSION = '7.2.10-dev.8';
156
+ static #ORM_VERSION = '7.2.10';
157
157
  /** Default session variable name backing an RLS filter argument (`current_setting('mikro.<filter>.<arg>')`). */
158
158
  static getRlsSettingName(filterName, argName) {
159
159
  return `mikro.${filterName}.${argName}`;
@@ -155,14 +155,21 @@ export function getWhereCondition(meta, onConflictFields, data, where) {
155
155
  unique.findIndex(p => data[p] ?? data[p.substring(0, p.indexOf('.'))] != null);
156
156
  if (onConflictFields || where == null) {
157
157
  if (propIndex !== false && propIndex >= 0) {
158
- let key = unique[propIndex];
159
- if (key.includes('.')) {
160
- const prop = meta.properties[key.substring(0, key.indexOf('.'))];
161
- if (prop) {
162
- key = `${prop.fieldNames[0]}${key.substring(key.indexOf('.'))}`;
158
+ // `onConflictFields` form one composite key, while the unique props are each unique on their own
159
+ const keys = onConflictFields
160
+ ? unique.filter(p => (data[p] ?? data[p.substring(0, p.indexOf('.'))]) != null)
161
+ : [unique[propIndex]];
162
+ where = {};
163
+ for (const field of keys) {
164
+ let key = field;
165
+ if (key.includes('.')) {
166
+ const prop = meta.properties[key.substring(0, key.indexOf('.'))];
167
+ if (prop) {
168
+ key = `${prop.fieldNames[0]}${key.substring(key.indexOf('.'))}`;
169
+ }
163
170
  }
171
+ where[key] = getPropertyValue(data, field);
164
172
  }
165
- where = { [key]: getPropertyValue(data, unique[propIndex]) };
166
173
  }
167
174
  else {
168
175
  for (const u of uniques) {