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

Function createQueryBuilderFromOptions

packages/sql/src/AbstractSqlDriver.ts:173–251  ·  view source on GitHub ↗
(
    meta: EntityMetadata<T>,
    where: FilterQuery<T>,
    options: FindOptions<T, any, any, any> = {},
  )

Source from the content-addressed store, hash-verified

171 }
172
173 private async createQueryBuilderFromOptions<T extends object>(
174 meta: EntityMetadata<T>,
175 where: FilterQuery<T>,
176 options: FindOptions<T, any, any, any> = {},
177 ): Promise<AnyQueryBuilder<T>> {
178 const connectionType = this.resolveConnectionType({ ctx: options.ctx, connectionType: options.connectionType });
179 const populate = this.autoJoinOneToOneOwner(
180 meta,
181 options.populate as unknown as PopulateOptions<T>[],
182 options.fields,
183 );
184 const joinedProps = this.joinedProps(meta, populate, options);
185 const schema = this.getSchemaName(meta, options);
186 const qb = this.createQueryBuilder(
187 meta.class,
188 options.ctx,
189 connectionType,
190 false,
191 options.logging,
192 undefined,
193 options.em as any,
194 )
195 .withSchema(schema)
196 .cache(false);
197 qb.setAbortOptions(pickAbortOptions(options));
198 const fields = this.buildFields(meta, populate, joinedProps, qb, qb.alias, options, schema);
199 const orderBy = this.buildOrderBy(qb, meta, populate, options);
200 const populateWhere = this.buildPopulateWhere(meta, joinedProps, options);
201 Utils.asArray(options.flags).forEach(flag => qb.setFlag(flag));
202
203 if (Utils.isPrimaryKey(where, meta.compositePK)) {
204 where = { [Utils.getPrimaryKeyHash(meta.primaryKeys)]: where } as ObjectQuery<T>;
205 }
206
207 this.validateSqlOptions(options);
208
209 const { first, last, before, after } = options as FindByCursorOptions<T>;
210 const isCursorPagination = [first, last, before, after].some(v => v != null);
211 qb.state.resolvedPopulateWhere = (options as Dictionary)._populateWhere;
212 qb.select(fields as any)
213 // only add populateWhere if we are populate-joining, as this will be used to add `on` conditions
214 .populate(
215 populate,
216 joinedProps.length > 0 ? populateWhere : undefined,
217 joinedProps.length > 0 ? options.populateFilter : undefined,
218 )
219 .where(where as any)
220 .groupBy(options.groupBy as any)
221 .having(options.having as any)
222 .indexHint(options.indexHint as string)
223 .collation(options.collation as string)
224 .comment(options.comments)
225 .hintComment(options.hintComments);
226
227 if (isCursorPagination) {
228 const { orderBy: newOrderBy, where } = this.processCursorOptions(meta, options, orderBy);
229 (qb.andWhere as any)(where).orderBy(newOrderBy);
230 } else {

Callers

nothing calls this directly

Calls 15

pickAbortOptionsFunction · 0.85
cacheMethod · 0.80
setAbortOptionsMethod · 0.80
asArrayMethod · 0.80
isPrimaryKeyMethod · 0.80
getPrimaryKeyHashMethod · 0.80
indexHintMethod · 0.80
applyJoinedFiltersMethod · 0.80
setPartitionLimitMethod · 0.80
getSchemaNameMethod · 0.65
withSchemaMethod · 0.65
setFlagMethod · 0.65

Tested by

no test coverage detected