MCPcopy Create free account
hub / github.com/mikro-orm/mikro-orm / matching

Method matching

packages/core/src/entity/Collection.ts:177–213  ·  view source on GitHub ↗

Queries a subset of the collection items from the database with custom filtering, ordering, and pagination.

(options: MatchingOptions<T, P>)

Source from the content-addressed store, hash-verified

175
176 /** Queries a subset of the collection items from the database with custom filtering, ordering, and pagination. */
177 async matching<TT extends T, P extends string = never>(options: MatchingOptions<T, P>): Promise<Loaded<TT, P>[]> {
178 const em = this.getEntityManager()!;
179 const { where, ctx, ...opts } = options;
180 let items: Loaded<TT, P>[];
181
182 if (this.property.kind === ReferenceKind.MANY_TO_MANY && em.getPlatform().usesPivotTable()) {
183 // M:N via pivot table bypasses em.find(), so merge all 3 levels here
184 opts.orderBy = QueryHelper.mergeOrderBy(opts.orderBy, this.property.orderBy, this.property.targetMeta?.orderBy);
185 options.populate = (await em.preparePopulate(this.property.targetMeta!.class, options)) as any;
186 const cond = (await em.applyFilters(
187 this.property.targetMeta!.class,
188 where,
189 options.filters ?? {},
190 'read',
191 )) as FilterQuery<T>;
192 const map = await em
193 .getDriver()
194 .loadFromPivotTable(this.property, [helper(this.owner).__primaryKeys], cond, opts.orderBy, ctx, options);
195 items = map[helper(this.owner).getSerializedPrimaryKey()].map((item: EntityData<TT>) =>
196 em.merge(this.property.targetMeta!.class, item, { convertCustomTypes: true }),
197 ) as any;
198 await em.populate(items, options.populate as any, options as any);
199 } else {
200 // em.find() merges entity-level orderBy, so only merge runtime + relation here
201 opts.orderBy = QueryHelper.mergeOrderBy(opts.orderBy, this.property.orderBy);
202 items = (await em.find(this.property.targetMeta!.class, this.createCondition(where), opts as any)) as any;
203 }
204
205 if (options.store) {
206 this.hydrate(items, true);
207 this.setSerializationContext(options);
208 this.populated();
209 this.#readonly = true;
210 }
211
212 return items;
213 }
214
215 /**
216 * Returns the items (the collection must be initialized)

Calls 15

getEntityManagerMethod · 0.95
createConditionMethod · 0.95
hydrateMethod · 0.95
populatedMethod · 0.95
helperFunction · 0.85
mergeOrderByMethod · 0.80
preparePopulateMethod · 0.80
loadFromPivotTableMethod · 0.80
getPlatformMethod · 0.65
populateMethod · 0.65

Tested by

no test coverage detected