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

Method find

packages/mongodb/src/MongoDriver.ts:89–164  ·  view source on GitHub ↗
(
    entityName: EntityName<T>,
    where: FilterQuery<T>,
    options: FindOptions<T, P, F, E> = {},
  )

Source from the content-addressed store, hash-verified

87 }
88
89 async find<T extends object, P extends string = never, F extends string = never, E extends string = never>(
90 entityName: EntityName<T>,
91 where: FilterQuery<T>,
92 options: FindOptions<T, P, F, E> = {},
93 ): Promise<EntityData<T>[]> {
94 if (this.metadata.find(entityName)?.virtual) {
95 return this.findVirtual(entityName, where, options);
96 }
97
98 const { first, last, before, after } = options as FindByCursorOptions<T>;
99 const fields = this.buildFields(
100 entityName,
101 (options.populate as unknown as PopulateOptions<T>[]) || [],
102 options.fields,
103 options.exclude as any[],
104 );
105 where = this.renameFields(entityName, where as T, true);
106 const isCursorPagination = [first, last, before, after].some(v => v != null);
107
108 if (isCursorPagination) {
109 const andWhere = (cond1: FilterQuery<T>, cond2: FilterQuery<T>): FilterQuery<T> => {
110 if (Utils.isEmpty(cond1)) {
111 return cond2;
112 }
113
114 if (Utils.isEmpty(cond2)) {
115 return cond1;
116 }
117
118 return { $and: [cond1, cond2] } as FilterQuery<T>;
119 };
120 const meta = this.metadata.find<T>(entityName)!;
121 const { orderBy: newOrderBy, where: newWhere } = this.processCursorOptions(meta, options, options.orderBy!);
122 const newWhereConverted = this.renameFields(entityName, newWhere as T, true);
123 const orderBy = Utils.asArray(newOrderBy).map(order => this.renameFields(entityName, order as T, true));
124 const res = await this.rethrow(
125 this.getConnection('read').find(entityName, andWhere(where, newWhereConverted), {
126 orderBy,
127 limit: options.limit,
128 offset: options.offset,
129 fields,
130 ctx: options.ctx,
131 loggerContext: options.logging,
132 ...this.buildQueryOptions(options),
133 }),
134 );
135
136 if (isCursorPagination && !first && !!last) {
137 res.reverse();
138 }
139
140 return res.map(r => this.mapResult<T>(r, this.metadata.find<T>(entityName))!);
141 }
142
143 const orderBy = Utils.asArray(options.orderBy).map(orderBy => this.renameFields(entityName, orderBy as T, true));
144 const partitionLimit = (options as Dictionary)._partitionLimit as
145 | { partitionBy: string; limit: number; offset?: number }
146 | undefined;

Callers

nothing calls this directly

Calls 11

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

Tested by

no test coverage detected