@mikro-orm/core 7.2.9-dev.17 → 7.2.9-dev.19

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.
@@ -66,7 +66,7 @@ export declare class EntityManager<Driver extends IDatabaseDriver = IDatabaseDri
66
66
  /**
67
67
  * Finds all entities matching your `where` query. You can pass additional options via the `options` parameter.
68
68
  */
69
- find<Entity extends object, Hint extends string = never, Fields extends string = never, Excludes extends string = never, Using extends string = never>(entityName: EntityName<Entity>, where: [Using] extends [never] ? FilterQuery<NoInfer<Entity>> : IndexFilterQuery<NoInfer<Entity>, Using>, options?: FindOptions<Entity, Hint, Fields, Excludes> & {
69
+ find<Entity extends object, Hint extends string = never, Fields extends string = never, Excludes extends string = never, Using extends string = never>(entityName: EntityName<Entity>, where: NoInfer<[Using] extends [never] ? FilterQuery<Entity> : IndexFilterQuery<Entity, Using>>, options?: FindOptions<Entity, Hint, Fields, Excludes> & {
70
70
  using?: Using | Using[];
71
71
  }): Promise<Loaded<Entity, Hint, Fields, Excludes>[]>;
72
72
  /**
@@ -86,11 +86,11 @@ export declare class EntityManager<Driver extends IDatabaseDriver = IDatabaseDri
86
86
  * }
87
87
  * ```
88
88
  */
89
- stream<Entity extends object, Hint extends string = never, Fields extends string = never, Excludes extends string = never, Using extends string = never>(entityName: EntityName<Entity>, options?: WithUsingOptions<StreamOptions<NoInfer<Entity>, Hint, Fields, Excludes>, NoInfer<Entity>, Using>): AsyncIterableIterator<Loaded<Entity, Hint, Fields, Excludes>>;
89
+ stream<Entity extends object, Hint extends string = never, Fields extends string = never, Excludes extends string = never, Using extends string = never>(entityName: EntityName<Entity>, options?: WithUsingOptions<StreamOptions<Entity, Hint, Fields, Excludes>, NoInfer<Entity>, Using>): AsyncIterableIterator<Loaded<Entity, Hint, Fields, Excludes>>;
90
90
  /**
91
91
  * Finds all entities of given type, optionally matching the `where` condition provided in the `options` parameter.
92
92
  */
93
- findAll<Entity extends object, Hint extends string = never, Fields extends string = never, Excludes extends string = never, Using extends string = never>(entityName: EntityName<Entity>, options?: WithUsingOptions<FindAllOptions<NoInfer<Entity>, Hint, Fields, Excludes>, NoInfer<Entity>, Using>): Promise<Loaded<Entity, Hint, Fields, Excludes>[]>;
93
+ findAll<Entity extends object, Hint extends string = never, Fields extends string = never, Excludes extends string = never, Using extends string = never>(entityName: EntityName<Entity>, options?: WithUsingOptions<FindAllOptions<Entity, Hint, Fields, Excludes>, NoInfer<Entity>, Using>): Promise<Loaded<Entity, Hint, Fields, Excludes>[]>;
94
94
  private getPopulateWhere;
95
95
  /**
96
96
  * Registers global filter to this entity manager. Global filters are enabled by default (unless disabled via last parameter).
@@ -163,7 +163,7 @@ export declare class EntityManager<Driver extends IDatabaseDriver = IDatabaseDri
163
163
  * Calls `em.find()` and `em.count()` with the same arguments (where applicable) and returns the results as tuple
164
164
  * where the first element is the array of entities, and the second is the count.
165
165
  */
166
- findAndCount<Entity extends object, Hint extends string = never, Fields extends string = never, Excludes extends string = never, Using extends string = never>(entityName: EntityName<Entity>, where: [Using] extends [never] ? FilterQuery<NoInfer<Entity>> : IndexFilterQuery<NoInfer<Entity>, Using>, options?: FindOptions<Entity, Hint, Fields, Excludes> & {
166
+ findAndCount<Entity extends object, Hint extends string = never, Fields extends string = never, Excludes extends string = never, Using extends string = never>(entityName: EntityName<Entity>, where: NoInfer<[Using] extends [never] ? FilterQuery<Entity> : IndexFilterQuery<Entity, Using>>, options?: FindOptions<Entity, Hint, Fields, Excludes> & {
167
167
  using?: Using | Using[];
168
168
  }): Promise<[Loaded<Entity, Hint, Fields, Excludes>[], number]>;
169
169
  /**
@@ -222,7 +222,7 @@ export declare class EntityManager<Driver extends IDatabaseDriver = IDatabaseDri
222
222
  * }
223
223
  * ```
224
224
  */
225
- findByCursor<Entity extends object, Hint extends string = never, Fields extends string = never, Excludes extends string = never, IncludeCount extends boolean = true, Using extends string = never>(entityName: EntityName<Entity>, options: WithUsingOptions<FindByCursorOptions<NoInfer<Entity>, Hint, Fields, Excludes, IncludeCount>, NoInfer<Entity>, Using>): Promise<Cursor<Entity, Hint, Fields, Excludes, IncludeCount>>;
225
+ findByCursor<Entity extends object, Hint extends string = never, Fields extends string = never, Excludes extends string = never, IncludeCount extends boolean = true, Using extends string = never>(entityName: EntityName<Entity>, options: WithUsingOptions<FindByCursorOptions<Entity, Hint, Fields, Excludes, IncludeCount>, NoInfer<Entity>, Using>): Promise<Cursor<Entity, Hint, Fields, Excludes, IncludeCount>>;
226
226
  /**
227
227
  * Refreshes the persistent state of an entity from the database, overriding any local changes that have not yet been
228
228
  * persisted. Returns the same entity instance (same object reference), but re-hydrated. If the entity is no longer
@@ -238,7 +238,7 @@ export declare class EntityManager<Driver extends IDatabaseDriver = IDatabaseDri
238
238
  /**
239
239
  * Finds first entity matching your `where` query.
240
240
  */
241
- findOne<Entity extends object, Hint extends string = never, Fields extends string = never, Excludes extends string = never, Using extends string = never>(entityName: EntityName<Entity>, where: [Using] extends [never] ? FilterQuery<NoInfer<Entity>> : IndexFilterQuery<NoInfer<Entity>, Using>, options?: FindOneOptions<Entity, Hint, Fields, Excludes> & {
241
+ findOne<Entity extends object, Hint extends string = never, Fields extends string = never, Excludes extends string = never, Using extends string = never>(entityName: EntityName<Entity>, where: NoInfer<[Using] extends [never] ? FilterQuery<Entity> : IndexFilterQuery<Entity, Using>>, options?: FindOneOptions<Entity, Hint, Fields, Excludes> & {
242
242
  using?: Using | Using[];
243
243
  }): Promise<Loaded<Entity, Hint, Fields, Excludes> | null>;
244
244
  /**
@@ -247,7 +247,7 @@ export declare class EntityManager<Driver extends IDatabaseDriver = IDatabaseDri
247
247
  * You can override the factory for creating this method via `options.failHandler` locally
248
248
  * or via `Configuration.findOneOrFailHandler` (`findExactlyOneOrFailHandler` when specifying `strict`) globally.
249
249
  */
250
- findOneOrFail<Entity extends object, Hint extends string = never, Fields extends string = never, Excludes extends string = never, Using extends string = never>(entityName: EntityName<Entity>, where: [Using] extends [never] ? FilterQuery<NoInfer<Entity>> : IndexFilterQuery<NoInfer<Entity>, Using>, options?: FindOneOrFailOptions<Entity, Hint, Fields, Excludes> & {
250
+ findOneOrFail<Entity extends object, Hint extends string = never, Fields extends string = never, Excludes extends string = never, Using extends string = never>(entityName: EntityName<Entity>, where: NoInfer<[Using] extends [never] ? FilterQuery<Entity> : IndexFilterQuery<Entity, Using>>, options?: FindOneOrFailOptions<Entity, Hint, Fields, Excludes> & {
251
251
  using?: Using | Using[];
252
252
  }): Promise<Loaded<Entity, Hint, Fields, Excludes>>;
253
253
  /**
@@ -1,5 +1,5 @@
1
1
  import type { EntityManager } from '../EntityManager.js';
2
- import { type EntityMetadata, type EntityProperty, type IHydrator } from '../typings.js';
2
+ import { type AnyEntity, type EntityMetadata, type EntityProperty, type IHydrator } from '../typings.js';
3
3
  /**
4
4
  * @internal
5
5
  */
@@ -24,6 +24,11 @@ export declare class EntityHelper {
24
24
  private static defineProperties;
25
25
  static defineCustomInspect<T extends object>(meta: EntityMetadata<T>): void;
26
26
  static defineReferenceProperty<T extends object>(meta: EntityMetadata<T>, prop: EntityProperty<T>, ref: T, hydrator: IHydrator): void;
27
+ /**
28
+ * Returns the database value of the FK pointing to the given entity.
29
+ * @internal
30
+ */
31
+ static getForeignKeyValue(target: AnyEntity, prop: EntityProperty<any>, targetMeta: EntityMetadata): any;
27
32
  static propagate<T extends object>(meta: EntityMetadata<T>, entity: T, owner: T, prop: EntityProperty<T>, value?: T[keyof T & string], old?: T): void;
28
33
  private static propagateOneToOne;
29
34
  static ensurePropagation<T extends object>(entity: T): void;
@@ -192,7 +192,7 @@ export class EntityHelper {
192
192
  wrapped.__data[prop.name] = Reference.wrapReference(val, prop);
193
193
  // when propagation from inside hydration, we set the FK to the entity data immediately
194
194
  if (val && hydrator.isRunning() && wrapped.__originalEntityData && prop.owner) {
195
- wrapped.__originalEntityData[prop.name] = Utils.getPrimaryKeyValues(wrapped.__data[prop.name], prop.targetMeta, true);
195
+ wrapped.__originalEntityData[prop.name] = EntityHelper.getForeignKeyValue(entity, prop, prop.targetMeta);
196
196
  }
197
197
  EntityHelper.propagate(meta, entity, this, prop, Reference.unwrapReference(val), old);
198
198
  },
@@ -200,6 +200,21 @@ export class EntityHelper {
200
200
  configurable: true,
201
201
  });
202
202
  }
203
+ /**
204
+ * Returns the database value of the FK pointing to the given entity.
205
+ * @internal
206
+ */
207
+ static getForeignKeyValue(target, prop, targetMeta) {
208
+ if (!prop.targetKey) {
209
+ return Utils.getPrimaryKeyValues(target, targetMeta, true, true);
210
+ }
211
+ // with `targetKey`, the FK holds that value instead of the PK
212
+ const value = target[prop.targetKey];
213
+ const customType = targetMeta.properties[prop.targetKey]?.customType;
214
+ return customType
215
+ ? customType.convertToDatabaseValue(value, helper(target).__platform, { mode: 'serialization' })
216
+ : value;
217
+ }
203
218
  static propagate(meta, entity, owner, prop, value, old) {
204
219
  // For polymorphic relations, get bidirectional relations from the actual entity's metadata
205
220
  let bidirectionalRelations;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/core",
3
- "version": "7.2.9-dev.17",
3
+ "version": "7.2.9-dev.19",
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",
package/typings.d.ts CHANGED
@@ -382,8 +382,9 @@ export type FilterQuery<T> = ObjectQuery<T> | NonNullable<ExpandScalar<Primary<T
382
382
  /**
383
383
  * `FilterQuery` restricted to only properties covered by the specified index(es).
384
384
  * Used when `using` option is set in `FindOptions` to enforce type-safe index usage.
385
+ * Falls back to `FilterQuery` when `Using` is not a specific index name (e.g. inferred from the options type).
385
386
  */
386
- export type IndexFilterQuery<T, Using extends string> = [Using] extends [never] ? FilterQuery<T> : (OperatorMap<T> & {
387
+ export type IndexFilterQuery<T, Using extends string> = [Using] extends [never] ? FilterQuery<T> : string extends Using ? FilterQuery<T> : (OperatorMap<T> & {
387
388
  -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;
388
389
  }) | NonNullable<ExpandScalar<Primary<T>>> | IndexFilterQuery<T, Using>[];
389
390
  /** Replaces `where` and `using` on an options type with index-aware variants when `Using` is specified. */
package/typings.js CHANGED
@@ -291,7 +291,7 @@ export class EntityMetadata {
291
291
  if (val && hydrator.isRunning() && wrapped.__originalEntityData && prop.owner) {
292
292
  const targetMeta = prop.targetMeta ?? helper(entity)?.__meta;
293
293
  if (targetMeta) {
294
- wrapped.__originalEntityData[prop.name] = Utils.getPrimaryKeyValues(val, targetMeta, true);
294
+ wrapped.__originalEntityData[prop.name] = EntityHelper.getForeignKeyValue(entity, prop, targetMeta);
295
295
  }
296
296
  }
297
297
  EntityHelper.propagate(meta, entity, this, prop, Reference.unwrapReference(val), old);
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.9-dev.17';
156
+ static #ORM_VERSION = '7.2.9-dev.19';
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}`;
@@ -450,6 +450,10 @@ export class Utils {
450
450
  let pk;
451
451
  if (Utils.isEntity(entity, true)) {
452
452
  pk = helper(entity).getPrimaryKey(convertCustomTypes);
453
+ // the converted PK of a relation to a composite PK target is an object keyed by the target PKs
454
+ if (convertCustomTypes && meta.getPrimaryProps()[0].targetMeta && Utils.isPlainObject(pk)) {
455
+ return toArray(pk);
456
+ }
453
457
  }
454
458
  else {
455
459
  pk = meta.primaryKeys.reduce((o, pk) => {