@mikro-orm/core 6.4.17-dev.20 → 6.4.17-dev.22

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.
@@ -2,6 +2,7 @@ import type { TransactionOptions } from '../enums';
2
2
  import type { ContextProvider } from '../typings';
3
3
  type TransactionalOptions<T> = TransactionOptions & {
4
4
  context?: ContextProvider<T>;
5
+ contextName?: string;
5
6
  };
6
7
  /**
7
8
  * This decorator wraps the method with `em.transactional()`, so you can provide `TransactionOptions` just like with `em.transactional()`.
@@ -17,10 +17,10 @@ function Transactional(options = {}) {
17
17
  throw new Error('@Transactional() should be use with async functions');
18
18
  }
19
19
  descriptor.value = async function (...args) {
20
- const { context, ...txOptions } = options;
20
+ const { context, contextName, ...txOptions } = options;
21
21
  const em = await (0, resolveContextProvider_1.resolveContextProvider)(this, context)
22
- || TransactionContext_1.TransactionContext.getEntityManager()
23
- || RequestContext_1.RequestContext.getEntityManager();
22
+ || TransactionContext_1.TransactionContext.getEntityManager(contextName)
23
+ || RequestContext_1.RequestContext.getEntityManager(contextName);
24
24
  if (!em) {
25
25
  throw new Error(`@Transactional() decorator can only be applied to methods of classes with \`orm: MikroORM\` property, \`em: EntityManager\` property, or with a callback parameter like \`@Transactional(() => orm)\` that returns one of those types. The parameter will contain a reference to current \`this\`. Returning an EntityRepository from it is also supported.`);
26
26
  }
@@ -74,7 +74,7 @@ class EntityFactory {
74
74
  continue;
75
75
  }
76
76
  if ([enums_1.ReferenceKind.MANY_TO_ONE, enums_1.ReferenceKind.ONE_TO_ONE].includes(prop.kind) && Utils_1.Utils.isPlainObject(data[prop.name])) {
77
- data[prop.name] = Utils_1.Utils.getPrimaryKeyValues(data[prop.name], prop.targetMeta.primaryKeys, true);
77
+ data[prop.name] = Utils_1.Utils.getPrimaryKeyValues(data[prop.name], prop.targetMeta, true);
78
78
  }
79
79
  data[prop.name] = prop.customType.convertToDatabaseValue(data[prop.name], this.platform, { key: prop.name, mode: 'hydration' });
80
80
  }
@@ -155,7 +155,7 @@ class EntityHelper {
155
155
  wrapped.__data[prop.name] = Reference_1.Reference.wrapReference(val, prop);
156
156
  // when propagation from inside hydration, we set the FK to the entity data immediately
157
157
  if (val && hydrator.isRunning() && wrapped.__originalEntityData && prop.owner) {
158
- wrapped.__originalEntityData[prop.name] = Utils_1.Utils.getPrimaryKeyValues(wrapped.__data[prop.name], prop.targetMeta.primaryKeys, true);
158
+ wrapped.__originalEntityData[prop.name] = Utils_1.Utils.getPrimaryKeyValues(wrapped.__data[prop.name], prop.targetMeta, true);
159
159
  }
160
160
  else {
161
161
  wrapped.__touched = !hydrator.isRunning();
@@ -154,7 +154,7 @@ class EntityLoader {
154
154
  }
155
155
  async populateScalar(meta, filtered, options) {
156
156
  const pk = Utils_1.Utils.getPrimaryKeyHash(meta.primaryKeys);
157
- const ids = Utils_1.Utils.unique(filtered.map(e => Utils_1.Utils.getPrimaryKeyValues(e, meta.primaryKeys, true)));
157
+ const ids = Utils_1.Utils.unique(filtered.map(e => Utils_1.Utils.getPrimaryKeyValues(e, meta, true)));
158
158
  const where = this.mergePrimaryCondition(ids, pk, options, meta, this.metadata, this.driver.getPlatform());
159
159
  const { filters, convertCustomTypes, lockMode, strategy, populateWhere, connectionType, logging, fields } = options;
160
160
  await this.em.find(meta.className, where, {
@@ -153,7 +153,7 @@ class WrappedEntity {
153
153
  return this.__em?.config ?? this.entity.__config;
154
154
  }
155
155
  get __primaryKeys() {
156
- return Utils_1.Utils.getPrimaryKeyValues(this.entity, this.__meta.primaryKeys);
156
+ return Utils_1.Utils.getPrimaryKeyValues(this.entity, this.__meta);
157
157
  }
158
158
  /** @ignore */
159
159
  [node_util_1.inspect.custom]() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/core",
3
- "version": "6.4.17-dev.20",
3
+ "version": "6.4.17-dev.22",
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
  "main": "index.js",
6
6
  "module": "index.mjs",
@@ -64,7 +64,7 @@
64
64
  "esprima": "4.0.1",
65
65
  "fs-extra": "11.3.0",
66
66
  "globby": "11.1.0",
67
- "mikro-orm": "6.4.17-dev.20",
67
+ "mikro-orm": "6.4.17-dev.22",
68
68
  "reflect-metadata": "0.2.2"
69
69
  }
70
70
  }
package/typings.js CHANGED
@@ -115,7 +115,7 @@ class EntityMetadata {
115
115
  wrapped.__data[prop.name] = entity_1.Reference.wrapReference(val, prop);
116
116
  // when propagation from inside hydration, we set the FK to the entity data immediately
117
117
  if (val && hydrator.isRunning() && wrapped.__originalEntityData && prop.owner) {
118
- wrapped.__originalEntityData[prop.name] = Utils_1.Utils.getPrimaryKeyValues(val, prop.targetMeta.primaryKeys, true);
118
+ wrapped.__originalEntityData[prop.name] = Utils_1.Utils.getPrimaryKeyValues(val, prop.targetMeta, true);
119
119
  }
120
120
  else {
121
121
  wrapped.__touched = !hydrator.isRunning();
@@ -88,7 +88,7 @@ class UnitOfWork {
88
88
  }
89
89
  wrapped.__loadedProperties.add(key);
90
90
  if ([enums_1.ReferenceKind.MANY_TO_ONE, enums_1.ReferenceKind.ONE_TO_ONE].includes(prop.kind) && Utils_1.Utils.isPlainObject(data[prop.name])) {
91
- data[prop.name] = Utils_1.Utils.getPrimaryKeyValues(data[prop.name], prop.targetMeta.primaryKeys, true);
91
+ data[prop.name] = Utils_1.Utils.getPrimaryKeyValues(data[prop.name], prop.targetMeta, true);
92
92
  }
93
93
  else if (prop.kind === enums_1.ReferenceKind.EMBEDDED && !prop.object && Utils_1.Utils.isPlainObject(data[prop.name])) {
94
94
  for (const p of prop.targetMeta.props) {
@@ -96,7 +96,7 @@ class UnitOfWork {
96
96
  const prefix = prop.prefix === false ? '' : prop.prefix === true ? prop.name + '_' : prop.prefix;
97
97
  data[prefix + p.name] = data[prop.name][p.name];
98
98
  }
99
- data[prop.name] = Utils_1.Utils.getPrimaryKeyValues(data[prop.name], prop.targetMeta.primaryKeys, true);
99
+ data[prop.name] = Utils_1.Utils.getPrimaryKeyValues(data[prop.name], prop.targetMeta, true);
100
100
  }
101
101
  if (forceUndefined) {
102
102
  if (data[key] === null) {
@@ -40,7 +40,7 @@ class QueryHelper {
40
40
  if (Array.isArray(where)) {
41
41
  where.forEach((item, i) => {
42
42
  if (this.inlinePrimaryKeyObjects(item, meta, metadata, key)) {
43
- where[i] = Utils_1.Utils.getPrimaryKeyValues(item, meta.primaryKeys, false);
43
+ where[i] = Utils_1.Utils.getPrimaryKeyValues(item, meta, false);
44
44
  }
45
45
  });
46
46
  }
@@ -61,7 +61,7 @@ class QueryHelper {
61
61
  Object.keys(where).forEach(k => {
62
62
  const meta2 = metadata.find(meta.properties[k]?.type) || meta;
63
63
  if (this.inlinePrimaryKeyObjects(where[k], meta2, metadata, k)) {
64
- where[k] = Utils_1.Utils.getPrimaryKeyValues(where[k], meta2.primaryKeys, true);
64
+ where[k] = Utils_1.Utils.getPrimaryKeyValues(where[k], meta2, true);
65
65
  }
66
66
  });
67
67
  return false;
package/utils/Utils.d.ts CHANGED
@@ -134,14 +134,14 @@ 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[], allowScalar?: boolean, convertCustomTypes?: boolean): any;
137
+ static getPrimaryKeyValues<T>(entity: T, primaryKeys: string[] | 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]]`.
141
141
  */
142
142
  static mapFlatCompositePrimaryKey(fk: Primary<any>[], prop: EntityProperty, fieldNames?: string[], idx?: number): Primary<any> | Primary<any>[];
143
143
  static getPrimaryKeyCondFromArray<T extends object>(pks: Primary<T>[], meta: EntityMetadata<T>): Record<string, Primary<T>>;
144
- static getOrderedPrimaryKeys<T>(id: Primary<T> | Record<string, Primary<T>>, meta: EntityMetadata<T>, platform?: Platform, convertCustomTypes?: boolean): Primary<T>[];
144
+ static getOrderedPrimaryKeys<T>(id: Primary<T> | Record<string, Primary<T>>, meta: EntityMetadata<T>, platform?: Platform, convertCustomTypes?: boolean, allowScalar?: boolean): Primary<T>[];
145
145
  /**
146
146
  * Checks whether given object is an entity instance.
147
147
  */
package/utils/Utils.js CHANGED
@@ -498,6 +498,7 @@ class Utils {
498
498
  static splitPrimaryKeys(key) {
499
499
  return key.split(this.PK_SEPARATOR);
500
500
  }
501
+ // TODO v7: remove support for `primaryKeys: string[]`
501
502
  static getPrimaryKeyValues(entity, primaryKeys, allowScalar = false, convertCustomTypes = false) {
502
503
  /* istanbul ignore next */
503
504
  if (entity == null) {
@@ -509,9 +510,24 @@ class Utils {
509
510
  }
510
511
  return val;
511
512
  }
512
- const pk = Utils.isEntity(entity, true)
513
- ? (0, wrap_1.helper)(entity).getPrimaryKey(convertCustomTypes)
514
- : primaryKeys.reduce((o, pk) => { o[pk] = entity[pk]; return o; }, {});
513
+ const meta = Array.isArray(primaryKeys) ? undefined : primaryKeys;
514
+ primaryKeys = Array.isArray(primaryKeys) ? primaryKeys : meta.primaryKeys;
515
+ let pk;
516
+ if (Utils.isEntity(entity, true)) {
517
+ pk = (0, wrap_1.helper)(entity).getPrimaryKey(convertCustomTypes);
518
+ }
519
+ else {
520
+ pk = primaryKeys.reduce((o, pk) => {
521
+ const targetMeta = meta?.properties[pk].targetMeta;
522
+ if (targetMeta && Utils.isPlainObject(entity[pk])) {
523
+ o[pk] = Utils.getPrimaryKeyValues(entity[pk], targetMeta, allowScalar, convertCustomTypes);
524
+ }
525
+ else {
526
+ o[pk] = entity[pk];
527
+ }
528
+ return o;
529
+ }, {});
530
+ }
515
531
  if (primaryKeys.length > 1) {
516
532
  return toArray(pk);
517
533
  }
@@ -561,7 +577,7 @@ class Utils {
561
577
  return o;
562
578
  }, {});
563
579
  }
564
- static getOrderedPrimaryKeys(id, meta, platform, convertCustomTypes = false) {
580
+ static getOrderedPrimaryKeys(id, meta, platform, convertCustomTypes = false, allowScalar = false) {
565
581
  const data = (Utils.isPrimaryKey(id) ? { [meta.primaryKeys[0]]: id } : id);
566
582
  const pks = meta.primaryKeys.map((pk, idx) => {
567
583
  const prop = meta.properties[pk];
@@ -571,11 +587,14 @@ class Utils {
571
587
  value = prop.customType.convertToJSValue(value, platform);
572
588
  }
573
589
  if (prop.kind !== enums_1.ReferenceKind.SCALAR && prop.targetMeta) {
574
- const value2 = this.getOrderedPrimaryKeys(value, prop.targetMeta, platform, convertCustomTypes);
590
+ const value2 = this.getOrderedPrimaryKeys(value, prop.targetMeta, platform, convertCustomTypes, allowScalar);
575
591
  value = value2.length > 1 ? value2 : value2[0];
576
592
  }
577
593
  return value;
578
594
  });
595
+ if (allowScalar && pks.length === 1) {
596
+ return pks[0];
597
+ }
579
598
  // we need to flatten the PKs as composite PKs can be build from another composite PKs
580
599
  // and this method is used to get the PK hash in identity map, that expects flat array
581
600
  return Utils.flatten(pks);