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

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.js CHANGED
@@ -1841,6 +1841,7 @@ export class EntityManager {
1841
1841
  options.populateWhere = this.createPopulateWhere({ ...where }, options);
1842
1842
  options.populateFilter = await this.getJoinedFilters(meta, options);
1843
1843
  validateParams(where);
1844
+ em.validateIndexUsage(meta, where, options);
1844
1845
  delete options.orderBy;
1845
1846
  await em.processUnionWhere(entityName, options, 'read');
1846
1847
  const cacheKey = em.cacheKey(entityName, options, 'em.count', where);
@@ -326,6 +326,8 @@ export interface CountOptions<T extends object, P extends string = never> extend
326
326
  flushMode?: FlushMode | `${FlushMode}`;
327
327
  /** SQL: appended to FROM clause (e.g. `'force index(my_index)'`); MongoDB: index name or spec passed as `hint`. */
328
328
  indexHint?: string | Dictionary;
329
+ /** @see FindOptions.using */
330
+ using?: IndexName<T> | IndexName<T>[];
329
331
  /** sql only */
330
332
  comments?: string | string[];
331
333
  /** sql only */
@@ -73,6 +73,7 @@ export declare class EntityLoader {
73
73
  private initializeCollections;
74
74
  private initializeOneToMany;
75
75
  private initializeManyToMany;
76
+ private initializePartitions;
76
77
  private findChildren;
77
78
  private mergePrimaryCondition;
78
79
  private populateField;
@@ -186,7 +186,7 @@ export class EntityLoader {
186
186
  if (prop.polymorphic && prop.polymorphTargets) {
187
187
  return this.populatePolymorphic(entities, prop, options, !!ref);
188
188
  }
189
- const { items, partial } = await this.findChildren(options.filtered ?? entities, prop, populate, {
189
+ const { items, partial, partitions } = await this.findChildren(options.filtered ?? entities, prop, populate, {
190
190
  ...options,
191
191
  where,
192
192
  orderBy: mergedOrderBy,
@@ -194,6 +194,10 @@ export class EntityLoader {
194
194
  const hasLimit = populate.limit != null;
195
195
  const isPartial = partial || hasLimit;
196
196
  const customOrder = mergedOrderBy.length > 0 || !!prop.orderBy || !!prop.targetMeta?.orderBy;
197
+ if (partitions) {
198
+ this.initializePartitions(filtered, items, field, partitions);
199
+ return items;
200
+ }
197
201
  this.initializeCollections(filtered, prop, field, items, customOrder, isPartial, hasLimit);
198
202
  return items;
199
203
  }
@@ -336,6 +340,18 @@ export class EntityLoader {
336
340
  }
337
341
  }
338
342
  }
343
+ initializePartitions(filtered, children, field, partitions) {
344
+ // a child belongs only to the owners whose slice returned it, not to every owner in its array
345
+ const key = (entity) => helper(entity).getSerializedPrimaryKey();
346
+ const owners = new Map(filtered.map(entity => [key(entity), []]));
347
+ const items = new Map(children.map(child => [key(child), child]));
348
+ for (const [owner, child] of partitions) {
349
+ owners.get('' + owner)?.push(items.get('' + child));
350
+ }
351
+ for (const entity of filtered) {
352
+ entity[field].hydrate(owners.get(key(entity)), true, true, true);
353
+ }
354
+ }
339
355
  async findChildren(entities, prop, populate, options, ref) {
340
356
  const children = Utils.unique(this.getChildReferences(entities, prop, options, ref, populate));
341
357
  const meta = prop.targetMeta;
@@ -434,11 +450,13 @@ export class EntityLoader {
434
450
  visited: options.visited,
435
451
  };
436
452
  if (populate.limit != null) {
437
- findOptions._partitionLimit = {
438
- partitionBy: fk,
439
- limit: populate.limit,
440
- offset: populate.offset,
441
- };
453
+ // the owners of an M:N without a pivot table are stored as an array, the driver slices per owner
454
+ const owners = prop.kind === ReferenceKind.MANY_TO_MANY && ownerProp ? ids : undefined;
455
+ findOptions._partitionLimit = { partitionBy: fk, limit: populate.limit, offset: populate.offset, owners };
456
+ // a cached result would not carry the per-owner partitions the driver writes back
457
+ if (owners) {
458
+ findOptions.cache = false;
459
+ }
442
460
  }
443
461
  const items = await this.#em.find(meta.class, where, findOptions);
444
462
  // For targetKey relations, wire up loaded entities to parent references
@@ -501,7 +519,7 @@ export class EntityLoader {
501
519
  this.#em.getUnitOfWork().unmarkAsLoaded(item);
502
520
  }
503
521
  }
504
- return { items, partial };
522
+ return { items, partial, partitions: findOptions._partitionLimit?.partitions };
505
523
  }
506
524
  mergePrimaryCondition(ids, pk, options, meta, metadata, platform) {
507
525
  const cond1 = QueryHelper.processWhere({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/core",
3
- "version": "7.2.8-dev.8",
3
+ "version": "7.2.8",
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",
@@ -76,12 +76,12 @@ export class EntitySerializer {
76
76
  const meta = wrapped.__meta;
77
77
  let contextCreated = false;
78
78
  options = prepareOptions(options);
79
- if (!wrapped.__serializationContext.root) {
80
- const root = new SerializationContext();
81
- SerializationContext.propagate(root, entity, (meta, prop) => meta.properties[prop]?.kind !== ReferenceKind.SCALAR);
79
+ let root = wrapped.__serializationContext.root;
80
+ if (!root) {
81
+ root = new SerializationContext();
82
82
  contextCreated = true;
83
83
  }
84
- const root = wrapped.__serializationContext.root;
84
+ SerializationContext.propagate(root, entity, (meta, prop) => meta.properties[prop]?.kind !== ReferenceKind.SCALAR);
85
85
  const ret = {};
86
86
  const props = new Set();
87
87
  if (meta.serializedPrimaryKey && !meta.compositePK) {
@@ -21,12 +21,12 @@ export class EntityTransformer {
21
21
  if (!wrapped) {
22
22
  return entity;
23
23
  }
24
- if (!wrapped.__serializationContext.root) {
25
- const root = new SerializationContext(wrapped.__serializationContext.populate, wrapped.__serializationContext.fields, wrapped.__serializationContext.exclude);
26
- SerializationContext.propagate(root, entity, isVisible);
24
+ let root = wrapped.__serializationContext.root;
25
+ if (!root) {
26
+ root = new SerializationContext(wrapped.__serializationContext.populate, wrapped.__serializationContext.fields, wrapped.__serializationContext.exclude);
27
27
  contextCreated = true;
28
28
  }
29
- const root = wrapped.__serializationContext.root;
29
+ SerializationContext.propagate(root, entity, isVisible);
30
30
  const meta = wrapped.__meta;
31
31
  const ret = {};
32
32
  const props = new Set();
@@ -18,7 +18,8 @@ export declare class SerializationContext<T extends object> {
18
18
  /** Cleans up the serialization context by removing root references from all tracked entities. */
19
19
  close(): void;
20
20
  /**
21
- * When initializing new context, we need to propagate it to the whole entity graph recursively.
21
+ * Propagates the context to the entity and its direct relations, so nested serialization calls pick it up.
22
+ * Runs for every serialized entity, so the context spreads along the serialized path instead of the whole graph.
22
23
  */
23
24
  static propagate(root: SerializationContext<any>, entity: AnyEntity, isVisible: (meta: EntityMetadata, prop: string) => boolean): void;
24
25
  /** Checks whether a property is explicitly listed in the populate hints for the current path. */
@@ -47,7 +47,8 @@ export class SerializationContext {
47
47
  }
48
48
  }
49
49
  /**
50
- * When initializing new context, we need to propagate it to the whole entity graph recursively.
50
+ * Propagates the context to the entity and its direct relations, so nested serialization calls pick it up.
51
+ * Runs for every serialized entity, so the context spreads along the serialized path instead of the whole graph.
51
52
  */
52
53
  static propagate(root, entity, isVisible) {
53
54
  root.register(entity);
@@ -59,14 +60,14 @@ export class SerializationContext {
59
60
  const target = entity[key];
60
61
  if (Utils.isEntity(target, true)) {
61
62
  if (!target.__helper.__serializationContext.root) {
62
- this.propagate(root, target, isVisible);
63
+ root.register(target);
63
64
  }
64
65
  continue;
65
66
  }
66
67
  if (Utils.isCollection(target)) {
67
68
  for (const item of target.getItems(false)) {
68
69
  if (!item.__helper.__serializationContext.root) {
69
- this.propagate(root, item, isVisible);
70
+ root.register(item);
70
71
  }
71
72
  }
72
73
  }
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.8-dev.8';
156
+ static #ORM_VERSION = '7.2.8';
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}`;