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

Method findOne

packages/mongodb/src/MongoDriver.ts:229–264  ·  view source on GitHub ↗
(
    entityName: EntityName<T>,
    where: FilterQuery<T>,
    options: FindOneOptions<T, P, F, E> = { populate: [], orderBy: {} },
  )

Source from the content-addressed store, hash-verified

227 }
228
229 async findOne<T extends object, P extends string = never, F extends string = never, E extends string = never>(
230 entityName: EntityName<T>,
231 where: FilterQuery<T>,
232 options: FindOneOptions<T, P, F, E> = { populate: [], orderBy: {} },
233 ): Promise<EntityData<T> | null> {
234 if (this.metadata.find(entityName)?.virtual) {
235 const [item] = await this.findVirtual(entityName, where, options as FindOptions<T, any, any, any>);
236 /* v8 ignore next */
237 return item ?? null;
238 }
239
240 if (Utils.isPrimaryKey(where)) {
241 where = this.buildFilterById(entityName, where as string);
242 }
243
244 const fields = this.buildFields(
245 entityName,
246 (options.populate as unknown as PopulateOptions<T>[]) || [],
247 options.fields,
248 options.exclude as any[],
249 );
250 where = this.renameFields(entityName, where as T, true);
251 const orderBy = Utils.asArray(options.orderBy).map(orderBy => this.renameFields(entityName, orderBy as T, true));
252 const res = await this.rethrow(
253 this.getConnection('read').find(entityName, where, {
254 orderBy,
255 limit: 1,
256 fields,
257 ctx: options.ctx,
258 loggerContext: options.logging,
259 ...this.buildQueryOptions(options),
260 }),
261 );
262
263 return this.mapResult<T>(res[0], this.metadata.find(entityName));
264 }
265
266 override async findVirtual<T extends object>(
267 entityName: EntityName<T>,

Callers

nothing calls this directly

Calls 12

findVirtualMethod · 0.95
buildFilterByIdMethod · 0.95
buildFieldsMethod · 0.95
renameFieldsMethod · 0.95
buildQueryOptionsMethod · 0.95
isPrimaryKeyMethod · 0.80
asArrayMethod · 0.80
rethrowMethod · 0.80
findMethod · 0.65
getConnectionMethod · 0.65
mapResultMethod · 0.65
mapMethod · 0.45

Tested by

no test coverage detected