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

Function findOne

packages/sql/src/AbstractSqlDriver.ts:279–302  ·  view source on GitHub ↗
(
    entityName: EntityName<T>,
    where: ObjectQuery<T>,
    options?: FindOneOptions<T, P, F, E>,
  )

Source from the content-addressed store, hash-verified

277 }
278
279 async findOne<T extends object, P extends string = never, F extends string = never, E extends string = never>(
280 entityName: EntityName<T>,
281 where: ObjectQuery<T>,
282 options?: FindOneOptions<T, P, F, E>,
283 ): Promise<EntityData<T> | null> {
284 const opts = { populate: [], ...options } as FindOptions<T>;
285 const meta = this.metadata.find(entityName)!;
286 const populate = this.autoJoinOneToOneOwner(meta, opts.populate as unknown as PopulateOptions<T>[], opts.fields);
287 const joinedProps = this.joinedProps(meta, populate, options);
288 const hasToManyJoins = joinedProps.some(hint => this.hasToManyJoins(hint, meta));
289
290 if (joinedProps.length === 0 || !hasToManyJoins) {
291 opts.limit = 1;
292 }
293
294 if (opts.limit! > 0 && !opts.flags?.includes(QueryFlag.DISABLE_PAGINATE)) {
295 opts.flags ??= [];
296 opts.flags.push(QueryFlag.DISABLE_PAGINATE);
297 }
298
299 const res = await this.find<T>(entityName, where, opts);
300
301 return res[0] || null;
302 }
303
304 protected hasToManyJoins<T extends object>(hint: PopulateOptions<T>, meta: EntityMetadata<T>): boolean {
305 const [propName] = hint.field.split(':', 2) as [EntityKey<T>];

Callers

nothing calls this directly

Calls 3

hasToManyJoinsMethod · 0.80
pushMethod · 0.80
findMethod · 0.65

Tested by

no test coverage detected