@mikro-orm/core 7.0.0-dev.51 → 7.0.0-dev.53

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.
@@ -45,7 +45,7 @@ export class ArrayCollection {
45
45
  if (items.length === 0) {
46
46
  return [];
47
47
  }
48
- field ??= targetMeta.compositePK ? targetMeta.primaryKeys : targetMeta.serializedPrimaryKey;
48
+ field ??= targetMeta.compositePK ? targetMeta.primaryKeys : (targetMeta.serializedPrimaryKey ?? targetMeta.primaryKeys[0]);
49
49
  const cb = (i, f) => {
50
50
  if (Utils.isEntity(i[f], true)) {
51
51
  return wrap(i[f], true).getPrimaryKey();
@@ -38,8 +38,8 @@ export class EntityFactory {
38
38
  this.hydrate(entity, meta, data, options);
39
39
  return entity;
40
40
  }
41
- if (this.platform.usesDifferentSerializedPrimaryKey()) {
42
- meta.primaryKeys.forEach(pk => this.denormalizePrimaryKey(data, pk, meta.properties[pk]));
41
+ if (meta.serializedPrimaryKey) {
42
+ this.denormalizePrimaryKey(meta, data);
43
43
  }
44
44
  const meta2 = this.processDiscriminatorColumn(meta, data);
45
45
  const exists = this.findEntity(data, meta2, options);
@@ -284,15 +284,15 @@ export class EntityFactory {
284
284
  /**
285
285
  * denormalize PK to value required by driver (e.g. ObjectId)
286
286
  */
287
- denormalizePrimaryKey(data, primaryKey, prop) {
288
- const pk = this.platform.getSerializedPrimaryKeyField(primaryKey);
289
- if (data[pk] != null || data[primaryKey] != null) {
290
- let id = (data[pk] || data[primaryKey]);
291
- if (prop.type.toLowerCase() === 'objectid') {
292
- id = this.platform.denormalizePrimaryKey(id);
293
- }
294
- delete data[pk];
295
- data[primaryKey] = id;
287
+ denormalizePrimaryKey(meta, data) {
288
+ const pk = meta.getPrimaryProp();
289
+ const spk = meta.properties[meta.serializedPrimaryKey];
290
+ if (!spk?.serializedPrimaryKey) {
291
+ return;
292
+ }
293
+ if (pk.type.toLowerCase() === 'objectid' && (data[pk.name] != null || data[spk.name] != null)) {
294
+ data[pk.name] = this.platform.denormalizePrimaryKey((data[spk.name] || data[pk.name]));
295
+ delete data[spk.name];
296
296
  }
297
297
  }
298
298
  /**
@@ -89,7 +89,7 @@ export class EntityValidator {
89
89
  }
90
90
  }
91
91
  validatePrimaryKey(entity, meta) {
92
- const pkExists = meta.primaryKeys.every(pk => entity[pk] != null) || entity[meta.serializedPrimaryKey] != null;
92
+ const pkExists = meta.primaryKeys.every(pk => entity[pk] != null) || (meta.serializedPrimaryKey && entity[meta.serializedPrimaryKey] != null);
93
93
  if (!entity || !pkExists) {
94
94
  throw ValidationError.fromMergeWithoutPK(meta);
95
95
  }
@@ -75,7 +75,7 @@ export class EntitySchema {
75
75
  }
76
76
  addSerializedPrimaryKey(name, type, options = {}) {
77
77
  this._meta.serializedPrimaryKey = name;
78
- this.addProperty(name, type, options);
78
+ this.addProperty(name, type, { serializedPrimaryKey: true, ...options });
79
79
  }
80
80
  addEmbedded(name, options) {
81
81
  this.renameCompositeOptions(name, options);
@@ -497,10 +497,9 @@ export class MetadataDiscovery {
497
497
  }
498
498
  this.initOwnColumns(meta);
499
499
  meta.simplePK = pks.length === 1 && pks[0].kind === ReferenceKind.SCALAR && !pks[0].customType && pks[0].runtimeType !== 'Date';
500
- meta.serializedPrimaryKey = this.platform.getSerializedPrimaryKeyField(meta.primaryKeys[0]);
501
- const serializedPKProp = meta.properties[meta.serializedPrimaryKey];
502
- if (serializedPKProp && meta.serializedPrimaryKey !== meta.primaryKeys[0]) {
503
- serializedPKProp.persist = false;
500
+ meta.serializedPrimaryKey ??= meta.props.find(prop => prop.serializedPrimaryKey)?.name;
501
+ if (meta.serializedPrimaryKey && meta.serializedPrimaryKey !== meta.primaryKeys[0]) {
502
+ meta.properties[meta.serializedPrimaryKey].persist ??= false;
504
503
  }
505
504
  if (this.platform.usesPivotTable()) {
506
505
  return Object.values(meta.properties)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mikro-orm/core",
3
3
  "type": "module",
4
- "version": "7.0.0-dev.51",
4
+ "version": "7.0.0-dev.53",
5
5
  "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.",
6
6
  "exports": {
7
7
  "./package.json": "./package.json",
@@ -54,7 +54,7 @@
54
54
  "dataloader": "2.2.3",
55
55
  "dotenv": "17.2.3",
56
56
  "esprima": "4.0.1",
57
- "mikro-orm": "7.0.0-dev.51",
57
+ "mikro-orm": "7.0.0-dev.53",
58
58
  "reflect-metadata": "0.2.2",
59
59
  "tinyglobby": "0.2.13"
60
60
  }
@@ -48,11 +48,6 @@ export declare abstract class Platform {
48
48
  * Converts scalar primary key representation to native driver wrapper (e.g. string to mongodb's ObjectId)
49
49
  */
50
50
  denormalizePrimaryKey(data: IPrimaryKey): IPrimaryKey;
51
- /**
52
- * Used when serializing via toObject and toJSON methods, allows to use different PK field name (like `id` instead of `_id`)
53
- */
54
- getSerializedPrimaryKeyField(field: string): string;
55
- usesDifferentSerializedPrimaryKey(): boolean;
56
51
  /**
57
52
  * Returns the SQL specific for the platform to get the current timestamp
58
53
  */
@@ -78,15 +78,6 @@ export class Platform {
78
78
  denormalizePrimaryKey(data) {
79
79
  return data;
80
80
  }
81
- /**
82
- * Used when serializing via toObject and toJSON methods, allows to use different PK field name (like `id` instead of `_id`)
83
- */
84
- getSerializedPrimaryKeyField(field) {
85
- return field;
86
- }
87
- usesDifferentSerializedPrimaryKey() {
88
- return false;
89
- }
90
81
  /**
91
82
  * Returns the SQL specific for the platform to get the current timestamp
92
83
  */
@@ -85,7 +85,7 @@ export class EntitySerializer {
85
85
  }
86
86
  const visible = typeof val !== 'undefined' && !(val === null && options.skipNull);
87
87
  if (visible) {
88
- ret[this.propertyName(meta, prop, wrapped.__platform)] = val;
88
+ ret[this.propertyName(meta, prop)] = val;
89
89
  }
90
90
  }
91
91
  if (contextCreated) {
@@ -99,26 +99,26 @@ export class EntitySerializer {
99
99
  if (prop.getterName != null) {
100
100
  const visible = entity[prop.getterName] instanceof Function && isVisible(meta, prop.name, options);
101
101
  if (visible) {
102
- ret[this.propertyName(meta, prop.name, wrapped.__platform)] = this.processProperty(prop.getterName, entity, options);
102
+ ret[this.propertyName(meta, prop.name)] = this.processProperty(prop.getterName, entity, options);
103
103
  }
104
104
  }
105
105
  else {
106
106
  // decorated getters
107
107
  const visible = typeof entity[prop.name] !== 'undefined' && isVisible(meta, prop.name, options);
108
108
  if (visible) {
109
- ret[this.propertyName(meta, prop.name, wrapped.__platform)] = this.processProperty(prop.name, entity, options);
109
+ ret[this.propertyName(meta, prop.name)] = this.processProperty(prop.name, entity, options);
110
110
  }
111
111
  }
112
112
  }
113
113
  return ret;
114
114
  }
115
- static propertyName(meta, prop, platform) {
115
+ static propertyName(meta, prop) {
116
116
  /* v8 ignore next 3 */
117
117
  if (meta.properties[prop]?.serializedName) {
118
118
  return meta.properties[prop].serializedName;
119
119
  }
120
- if (meta.properties[prop]?.primary && platform) {
121
- return platform.getSerializedPrimaryKeyField(prop);
120
+ if (meta.properties[prop]?.primary && meta.serializedPrimaryKey) {
121
+ return meta.serializedPrimaryKey;
122
122
  }
123
123
  return prop;
124
124
  }
@@ -77,7 +77,7 @@ export class EntityTransformer {
77
77
  if (typeof val === 'undefined') {
78
78
  continue;
79
79
  }
80
- ret[this.propertyName(meta, prop, wrapped.__platform, raw)] = val;
80
+ ret[this.propertyName(meta, prop, raw)] = val;
81
81
  }
82
82
  if (!wrapped.isInitialized() && wrapped.hasPrimaryKey()) {
83
83
  return ret;
@@ -88,7 +88,7 @@ export class EntityTransformer {
88
88
  const visible = !prop.hidden && entity[prop.getterName] instanceof Function;
89
89
  const populated = root.isMarkedAsPopulated(meta.className, prop.name);
90
90
  if (visible) {
91
- ret[this.propertyName(meta, prop.name, wrapped.__platform, raw)] = this.processProperty(prop.getterName, entity, raw, populated);
91
+ ret[this.propertyName(meta, prop.name, raw)] = this.processProperty(prop.getterName, entity, raw, populated);
92
92
  }
93
93
  }
94
94
  else {
@@ -96,7 +96,7 @@ export class EntityTransformer {
96
96
  const visible = !prop.hidden && typeof entity[prop.name] !== 'undefined';
97
97
  const populated = root.isMarkedAsPopulated(meta.className, prop.name);
98
98
  if (visible) {
99
- ret[this.propertyName(meta, prop.name, wrapped.__platform, raw)] = this.processProperty(prop.name, entity, raw, populated);
99
+ ret[this.propertyName(meta, prop.name, raw)] = this.processProperty(prop.name, entity, raw, populated);
100
100
  }
101
101
  }
102
102
  }
@@ -105,15 +105,15 @@ export class EntityTransformer {
105
105
  }
106
106
  return ret;
107
107
  }
108
- static propertyName(meta, prop, platform, raw) {
108
+ static propertyName(meta, prop, raw) {
109
109
  if (raw) {
110
110
  return prop;
111
111
  }
112
112
  if (meta.properties[prop].serializedName) {
113
113
  return meta.properties[prop].serializedName;
114
114
  }
115
- if (meta.properties[prop].primary && platform) {
116
- return platform.getSerializedPrimaryKeyField(prop);
115
+ if (meta.properties[prop].primary && meta.serializedPrimaryKey) {
116
+ return meta.serializedPrimaryKey;
117
117
  }
118
118
  return prop;
119
119
  }
package/typings.d.ts CHANGED
@@ -462,7 +462,7 @@ export interface EntityMetadata<T = any> {
462
462
  compositePK: boolean;
463
463
  versionProperty: EntityKey<T>;
464
464
  concurrencyCheckKeys: Set<EntityKey<T>>;
465
- serializedPrimaryKey: EntityKey<T>;
465
+ serializedPrimaryKey?: EntityKey<T>;
466
466
  properties: {
467
467
  [K in EntityKey<T>]: EntityProperty<T>;
468
468
  };
package/utils/Utils.d.ts CHANGED
@@ -134,7 +134,7 @@ export declare class Utils {
134
134
  static getCompositeKeyHash<T>(data: EntityData<T>, meta: EntityMetadata<T>, convertCustomTypes?: boolean, platform?: Platform, flat?: boolean): string;
135
135
  static getPrimaryKeyHash(pks: (string | Buffer | Date)[]): string;
136
136
  static splitPrimaryKeys<T extends object>(key: string): EntityKey<T>[];
137
- static getPrimaryKeyValues<T>(entity: T, primaryKeys: string[] | EntityMetadata<T>, allowScalar?: boolean, convertCustomTypes?: boolean): any;
137
+ static getPrimaryKeyValues<T>(entity: T, meta: EntityMetadata<T>, allowScalar?: boolean, convertCustomTypes?: boolean): any;
138
138
  static getPrimaryKeyCond<T>(entity: T, primaryKeys: EntityKey<T>[]): Record<string, Primary<T>> | null;
139
139
  /**
140
140
  * Maps nested FKs from `[1, 2, 3]` to `[1, [2, 3]]`.
package/utils/Utils.js CHANGED
@@ -6,7 +6,7 @@ import { existsSync, mkdirSync, readFileSync } from 'node:fs';
6
6
  import { createHash } from 'node:crypto';
7
7
  import { tokenize } from 'esprima';
8
8
  import { clone } from './clone.js';
9
- import { ARRAY_OPERATORS, JSON_KEY_OPERATORS, GroupOperator, PlainObject, QueryOperator, ReferenceKind } from '../enums.js';
9
+ import { ARRAY_OPERATORS, GroupOperator, JSON_KEY_OPERATORS, PlainObject, QueryOperator, ReferenceKind, } from '../enums.js';
10
10
  import { helper } from '../entity/wrap.js';
11
11
  export const ObjectBindingPattern = Symbol('ObjectBindingPattern');
12
12
  function compareConstructors(a, b) {
@@ -447,7 +447,7 @@ export class Utils {
447
447
  if (meta.compositePK) {
448
448
  return this.getCompositeKeyValue(data, meta);
449
449
  }
450
- return data[meta.primaryKeys[0]] || data[meta.serializedPrimaryKey] || null;
450
+ return data[meta.primaryKeys[0]] ?? data[meta.serializedPrimaryKey] ?? null;
451
451
  }
452
452
  return null;
453
453
  }
@@ -486,8 +486,7 @@ export class Utils {
486
486
  static splitPrimaryKeys(key) {
487
487
  return key.split(this.PK_SEPARATOR);
488
488
  }
489
- // TODO v7: remove support for `primaryKeys: string[]`
490
- static getPrimaryKeyValues(entity, primaryKeys, allowScalar = false, convertCustomTypes = false) {
489
+ static getPrimaryKeyValues(entity, meta, allowScalar = false, convertCustomTypes = false) {
491
490
  /* v8 ignore next 3 */
492
491
  if (entity == null) {
493
492
  return entity;
@@ -498,15 +497,13 @@ export class Utils {
498
497
  }
499
498
  return val;
500
499
  }
501
- const meta = Array.isArray(primaryKeys) ? undefined : primaryKeys;
502
- primaryKeys = Array.isArray(primaryKeys) ? primaryKeys : meta.primaryKeys;
503
500
  let pk;
504
501
  if (Utils.isEntity(entity, true)) {
505
502
  pk = helper(entity).getPrimaryKey(convertCustomTypes);
506
503
  }
507
504
  else {
508
- pk = primaryKeys.reduce((o, pk) => {
509
- const targetMeta = meta?.properties[pk].targetMeta;
505
+ pk = meta.primaryKeys.reduce((o, pk) => {
506
+ const targetMeta = meta.properties[pk].targetMeta;
510
507
  if (targetMeta && Utils.isPlainObject(entity[pk])) {
511
508
  o[pk] = Utils.getPrimaryKeyValues(entity[pk], targetMeta, allowScalar, convertCustomTypes);
512
509
  }
@@ -516,12 +513,12 @@ export class Utils {
516
513
  return o;
517
514
  }, {});
518
515
  }
519
- if (primaryKeys.length > 1) {
516
+ if (meta.primaryKeys.length > 1) {
520
517
  return toArray(pk);
521
518
  }
522
519
  if (allowScalar) {
523
520
  if (Utils.isPlainObject(pk)) {
524
- return pk[primaryKeys[0]];
521
+ return pk[(meta.primaryKeys)[0]];
525
522
  }
526
523
  return pk;
527
524
  }