/** * Query-related type definitions */ import { SortOrder, RelationshipType } from './enums'; import { ITransaction } from './transaction.interface'; /** * Options for querying data */ export interface IQueryOptions { /** Table or collection name */ table: string; /** Columns to select (omit for all columns) */ select?: string[]; /** Filter conditions */ where?: IWhereClause; /** Sort configuration */ orderBy?: IOrderBy | IOrderBy[]; /** Maximum records to return */ limit?: number; /** Records to skip (for pagination) */ offset?: number; /** Relationship includes */ include?: IInclude | IInclude[]; /** Environment (if not using connection context) */ env?: string; /** Product tag (if not using connection context) */ product?: string; /** Database tag (if not using connection context) */ database?: string; /** Transaction to use */ transaction?: ITransaction; /** Session token in format: session_tag:jwt_token */ session?: string; /** Cache tag for result caching */ cache?: string; } /** * Filter conditions for queries * Supports simple equality, comparison operators, and logical operators */ export interface IWhereClause { [key: string]: any | IComparisonOperator | ILogicalOperator; } /** * Comparison operators for filtering * Uses lowercase naming convention to match Mongoose/MongoDB style for less adoption friction */ export interface IComparisonOperator { /** Equal to */ $eq?: any; /** Not equal to */ $ne?: any; /** Greater than */ $gt?: any; /** Greater than or equal */ $gte?: any; /** Less than */ $lt?: any; /** Less than or equal */ $lte?: any; /** In array of values */ $in?: any[]; /** Not in array of values */ $nin?: any[]; /** Pattern match (SQL LIKE) */ $like?: string; /** Case-insensitive pattern match (PostgreSQL) */ $ilike?: string; /** Between two values [min, max] */ $between?: [any, any]; /** Is null */ $isNull?: boolean; /** Is not null */ $isNotNull?: boolean; /** Contains (for arrays/JSONB) */ $contains?: any; /** Contained by (for arrays/JSONB) */ $containedBy?: any; /** Overlaps (for arrays) */ $overlaps?: any[]; /** Regex match */ $regex?: string | RegExp; /** Exists check */ $exists?: boolean; } /** * Logical operators for combining conditions * Uses lowercase naming convention to match Mongoose/MongoDB style */ export interface ILogicalOperator { /** All conditions must match */ $and?: IWhereClause[]; /** Any condition can match */ $or?: IWhereClause[]; /** Negate condition */ $not?: IWhereClause; /** None of the conditions should match */ $nor?: IWhereClause[]; } /** * Sort configuration */ export interface IOrderBy { /** Column name to sort by */ column: string; /** Sort order (ASC or DESC) */ order: SortOrder | 'ASC' | 'DESC'; /** Nulls first or last (PostgreSQL) */ nulls?: 'FIRST' | 'LAST'; } /** * Relationship include configuration */ export interface IInclude { /** Name of the relation (used as key in result) */ relation: string; /** Relationship type */ type: RelationshipType | 'one-to-one' | 'one-to-many' | 'many-to-one' | 'many-to-many'; /** Related table name (optional, defaults to relation name) */ table?: string; /** Foreign key column in current table (for many-to-one) or related table (for one-to-many) */ foreignKey?: string; /** Primary key column (defaults to 'id') */ primaryKey?: string; /** Junction table name (for many-to-many) */ through?: string; /** Foreign key in junction table pointing to current table */ throughForeignKey?: string; /** Foreign key in junction table pointing to related table */ throughRelatedKey?: string; /** Columns to select from related table */ select?: string[]; /** Filter conditions for related records */ where?: IWhereClause; /** Sort configuration for related records */ orderBy?: IOrderBy | IOrderBy[]; /** Maximum related records to return */ limit?: number; /** Nested includes */ include?: IInclude | IInclude[]; } /** * Result of a query operation */ export interface IQueryResult { /** Array of matching records */ data: T[]; /** Total count (useful for pagination) */ count: number; /** Column names (for raw queries) */ fields?: string[]; } /** * Options for raw query execution */ export interface IRawQueryOptions { table: string; /** SQL query string (for SQL databases) or query object (for MongoDB) */ query: string | object; /** Query parameters (for parameterized queries) */ params?: any[]; /** Collection name (for MongoDB) */ collection?: string; /** Environment (if not using connection context) */ env?: string; /** Product tag */ product?: string; /** Database tag */ database?: string; /** Transaction to use */ transaction?: ITransaction; /** Cache tag for result caching */ cache?: string; } /** * Result of a raw query */ export interface IRawQueryResult { /** Query results */ data: T[]; /** Number of affected/returned rows */ count: number; /** Column/field names */ fields?: string[]; /** Rows affected (for INSERT/UPDATE/DELETE) */ rowsAffected?: number; } /** * Internal representation of a built query * Used by adapters to execute database-specific queries */ export interface IBuiltQuery { /** Original query options */ options: IQueryOptions; /** SQL string (for SQL databases) */ sql?: string; /** Query parameters */ params?: any[]; /** MongoDB query (for MongoDB) */ mongoQuery?: object; /** MongoDB projection */ mongoProjection?: object; /** MongoDB sort */ mongoSort?: object; }