/** * Aggregation-related type definitions */ import { IWhereClause } from './query.interface'; import { ITransaction } from './transaction.interface'; /** * Base options for aggregation operations */ export interface IBaseAggregationOptions { /** Table or collection name */ table: string; /** Filter conditions */ where?: IWhereClause; /** Environment (if not using connection context) */ env?: string; /** Product tag */ product?: string; /** Database tag */ database?: string; /** Transaction to use */ transaction?: ITransaction; } /** * Options for count aggregation */ export interface ICountOptions extends IBaseAggregationOptions { /** Column to count (use '*' or omit for all rows) */ column?: string; /** Count only distinct values */ distinct?: boolean; } /** * Options for sum aggregation */ export interface ISumOptions extends IBaseAggregationOptions { /** Column to sum */ column: string; } /** * Options for average aggregation */ export interface IAvgOptions extends IBaseAggregationOptions { /** Column to average */ column: string; } /** * Options for min/max aggregation */ export interface IMinMaxOptions extends IBaseAggregationOptions { /** Column to find min/max */ column: string; } /** * Options for multiple aggregations in one query */ export interface IAggregateOptions extends IBaseAggregationOptions { /** Aggregation operations to perform */ operations: IAggregationOperations; } /** * Aggregation operations map * Key is the alias for the result, value is the operation */ export interface IAggregationOperations { [alias: string]: IAggregationOperation; } /** * Single aggregation operation * Uses lowercase naming convention to match Mongoose/MongoDB style */ export interface IAggregationOperation { /** Count - use '*' for all rows or column name */ $count?: string; /** Sum column values */ $sum?: string; /** Average column values */ $avg?: string; /** Minimum value */ $min?: string; /** Maximum value */ $max?: string; /** String aggregation (PostgreSQL: STRING_AGG, MySQL: GROUP_CONCAT) */ $stringAgg?: IStringAggOptions; /** Group concatenation (MySQL alias) */ $groupConcat?: IStringAggOptions; /** Array aggregation (PostgreSQL) */ $arrayAgg?: string; /** First value in group */ $first?: string; /** Last value in group */ $last?: string; } /** * String aggregation options */ export interface IStringAggOptions { /** Column to aggregate */ column: string; /** Separator between values */ separator?: string; /** Order by column */ orderBy?: string; /** Distinct values only */ distinct?: boolean; } /** * Result of aggregate operation */ export interface IAggregateResult { [alias: string]: any; } /** * Options for group by with aggregations */ export interface IGroupByOptions extends IBaseAggregationOptions { /** Columns to group by */ groupBy: string[]; /** Aggregation operations to perform */ operations: IAggregationOperations; /** Having clause (filter on aggregated values) */ having?: IHavingClause; /** Order by configuration */ orderBy?: IGroupByOrderBy[]; /** Maximum groups to return */ limit?: number; /** Groups to skip */ offset?: number; } /** * Having clause for filtering grouped results */ export interface IHavingClause { [alias: string]: IHavingCondition; } /** * Having condition * Uses lowercase naming convention to match Mongoose/MongoDB style */ export interface IHavingCondition { /** Greater than */ $gt?: number; /** Greater than or equal */ $gte?: number; /** Less than */ $lt?: number; /** Less than or equal */ $lte?: number; /** Equal to */ $eq?: any; /** Not equal to */ $ne?: any; /** Between values */ $between?: [number, number]; } /** * Order by for group by results */ export interface IGroupByOrderBy { /** Column or alias to order by */ column: string; /** Sort direction */ order: 'ASC' | 'DESC'; } /** * Result of group by operation */ export interface IGroupByResult { /** Group key values */ group: Record; /** Aggregation results */ aggregates: T; } /** * Internal representation of built aggregation query */ export interface IBuiltAggregation { /** Original options */ options: IAggregateOptions | IGroupByOptions; /** SQL string */ sql?: string; /** Query parameters */ params?: any[]; /** MongoDB aggregation pipeline */ pipeline?: object[]; }