Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | 131x 131x 27x 7x 2x 10x | import { counter } from '../model-operations.ts';
import type { Model } from '../model.ts';
import type { AggregateOptions, AnyModel, Attributes, ModelStatic } from '../options.ts';
import type { ModelConstructor } from './model-constructor.ts';
/**
* Aggregate statics of `Model`: `aggregate`, `max`, `min`, `sum`.
*
* @internal
*/
// oxlint-disable-next-line max-lines-per-function, typescript/explicit-module-boundary-types -- a mixin factory's body is its class (each method is still size-checked); its type can only be inferred.
export const ModelAggregates = <TBase extends ModelConstructor>(Base: TBase) => {
return class AggregatesCapability extends Base {
/**
* Runs one SQL aggregate function over a column (`SELECT fn(col) ...`), applying the default scope
* and the paranoid filter. The function name is checked against a safe name pattern, never
* concatenated unchecked.
*
* @param this - The model class.
* @param field - Attribute name or `'*'`.
* @param aggregateFunction - SQL aggregate function name, e.g. `'max'`.
* @param options - Options (`where`, `distinct`, `group`, `plain: false` to get rows, query options).
* @returns Aggregated value: a number for `count` and numeric attributes, otherwise parsed like the attribute; rows with `plain: false`.
* @example
* ```ts
* const avgPrice = await Product.aggregate('price', 'avg', { where: { active: true } });
* ```
*/
static aggregate<M extends AnyModel, T = unknown>(
this: ModelStatic<M>,
field: keyof Attributes<M> | '*',
aggregateFunction: string,
options: AggregateOptions<Attributes<M>> = {},
): Promise<T> {
return counter.aggregate(
this,
String(field),
aggregateFunction,
options as AggregateOptions,
) as Promise<T>;
}
/**
* Finds the largest value of an attribute with `max()`.
*
* @param this - The model class.
* @param field - Attribute name.
* @param options - Options, see {@link Model.aggregate}.
* @returns Maximum or `null` when there are no rows.
* @example
* ```ts
* const highest = await Product.max('price');
* ```
*/
static max<T = unknown, M extends AnyModel = AnyModel>(
this: ModelStatic<M>,
field: keyof Attributes<M>,
options?: AggregateOptions<Attributes<M>>,
): Promise<T> {
return (this as unknown as typeof Model).aggregate.call(
this,
field as never,
'max',
options as AggregateOptions,
) as Promise<T>;
}
/**
* Finds the smallest value of an attribute with `min()`.
*
* @param this - The model class.
* @param field - Attribute name.
* @param options - Options, see {@link Model.aggregate}.
* @returns Minimum or `null` when there are no rows.
* @example
* ```ts
* const lowest = await Product.min('price', { where: { active: true } });
* ```
*/
static min<T = unknown, M extends AnyModel = AnyModel>(
this: ModelStatic<M>,
field: keyof Attributes<M>,
options?: AggregateOptions<Attributes<M>>,
): Promise<T> {
return (this as unknown as typeof Model).aggregate.call(
this,
field as never,
'min',
options as AggregateOptions,
) as Promise<T>;
}
/**
* Adds up the values of an attribute with `sum()`.
*
* @param this - The model class.
* @param field - Attribute name.
* @param options - Options, see {@link Model.aggregate}.
* @returns Sum, or `null` when there are no rows (as returned by PostgreSQL).
* @example
* ```ts
* const total = await Invoice.sum('amount', { where: { paid: true } });
* ```
*/
static sum<M extends AnyModel>(
this: ModelStatic<M>,
field: keyof Attributes<M>,
options?: AggregateOptions<Attributes<M>>,
): Promise<number> {
return (this as unknown as typeof Model).aggregate.call(
this,
field as never,
'sum',
options as AggregateOptions,
) as Promise<number>;
}
};
};
|