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

Method count

packages/sql/src/query/QueryBuilder.ts:975–989  ·  view source on GitHub ↗

* Creates a COUNT query to count matching rows. * * @example * ```ts * const count = await em.createQueryBuilder(User) * .count() * .where({ isActive: true }) * .execute('get'); * ```

(field?: F | F[], distinct = false)

Source from the content-addressed store, hash-verified

973 * ```
974 */
975 count<F extends Field<Entity, RootAlias, Context>>(field?: F | F[], distinct = false): CountQueryBuilder<Entity> {
976 if (field) {
977 this.#state.fields = Utils.asArray(field as string);
978 } else if (distinct || this.hasToManyJoins()) {
979 this.#state.fields = this.mainAlias.meta.primaryKeys;
980 } else {
981 this.#state.fields = [raw('*')];
982 }
983
984 if (distinct) {
985 this.#state.flags.add(QueryFlag.DISTINCT);
986 }
987
988 return this.init(QueryType.COUNT) as unknown as CountQueryBuilder<Entity>;
989 }
990
991 /**
992 * Adds a JOIN clause to the query for an entity relation.

Callers

nothing calls this directly

Calls 5

hasToManyJoinsMethod · 0.95
initMethod · 0.95
rawFunction · 0.90
asArrayMethod · 0.80
addMethod · 0.80

Tested by

no test coverage detected