/** * db4ai Query Builder * * Provides a fluent API for building type-safe queries with excellent * TypeScript inference and ergonomic convenience methods. * * @packageDocumentation */ /** * Query operators for filtering documents. * * @typeParam V - The value type for the field being filtered * * @example * ```typescript * const filter: QueryOperators = { * $gte: 18, * $lt: 65 * }; * ``` */ export interface QueryOperators { /** Equal to */ $eq?: V; /** Not equal to */ $ne?: V; /** Greater than */ $gt?: V; /** Greater than or equal to */ $gte?: V; /** Less than */ $lt?: V; /** Less than or equal to */ $lte?: V; /** Value is in array */ $in?: V[]; /** Value is not in array */ $nin?: V[]; /** String contains substring (case-sensitive) */ $contains?: string; /** String starts with prefix */ $startsWith?: string; /** String ends with suffix */ $endsWith?: string; /** Field exists */ $exists?: boolean; /** Matches regular expression */ $regex?: string; /** Case-insensitive regex match */ $iregex?: string; } /** * Where clause for filtering documents. * * @typeParam T - The document type * * @example * ```typescript * const where: WhereClause = { * status: { $eq: 'active' }, * age: { $gte: 18 }, * $or: [ * { role: 'admin' }, * { permissions: { $contains: 'write' } } * ] * }; * ``` */ export type WhereClause = { [K in keyof T]?: T[K] | QueryOperators; } & { /** Nested field path access (e.g., 'profile.settings.theme') */ [path: string]: unknown | QueryOperators; } & { /** All conditions must match */ $and?: WhereClause[]; /** Any condition must match */ $or?: WhereClause[]; /** Condition must not match */ $not?: WhereClause; }; /** * Select clause for field projection. * * @typeParam T - The document type * * @example * ```typescript * const select: SelectClause = { * id: true, * name: true, * email: true * }; * ``` */ export type SelectClause = { [K in keyof T]?: boolean; }; /** * Include clause for related documents. * * @example * ```typescript * const include: IncludeClause = { * posts: true, * comments: { * include: { author: true }, * limit: 10 * } * }; * ``` */ export interface IncludeClause { [relation: string]: boolean | IncludeOptions; } /** * Options for including related documents. */ export interface IncludeOptions { /** Nested includes */ include?: IncludeClause; /** Filter related documents */ where?: Record; /** Sort related documents */ orderBy?: Record; /** Limit number of related documents */ limit?: number; /** Skip related documents */ offset?: number; } /** * Order by clause for sorting. * * @typeParam T - The document type * * @example * ```typescript * const orderBy: OrderByClause = { * createdAt: 'desc', * name: 'asc' * }; * ``` */ export type OrderByClause = { [K in keyof T]?: 'asc' | 'desc'; }; /** * Built query object. * * @typeParam T - The document type */ export interface Query { /** Filter conditions */ where?: WhereClause; /** Field selection */ select?: SelectClause; /** Related documents to include */ include?: IncludeClause; /** Sort order */ orderBy?: OrderByClause; /** Maximum number of results */ limit?: number; /** Number of results to skip */ offset?: number; } /** * Query builder interface for fluent query construction. * * @typeParam T - The document type * * @example * ```typescript * const query = createQueryBuilder() * .where({ status: 'active' }) * .where({ age: { $gte: 18 } }) * .orderBy({ createdAt: 'desc' }) * .limit(10) * .build(); * ``` */ export interface QueryBuilder { /** * Add a where condition. Multiple calls are combined with AND. * * @param condition - The filter condition * @returns The builder for chaining * * @example * ```typescript * builder.where({ status: 'active' }) * builder.where({ age: { $gte: 18 } }) * ``` */ where(condition: WhereClause): QueryBuilder; /** * Add an AND condition explicitly. * * @param conditions - Array of conditions that must all match * @returns The builder for chaining * * @example * ```typescript * builder.and([ * { status: 'active' }, * { role: 'admin' } * ]) * ``` */ and(conditions: WhereClause[]): QueryBuilder; /** * Add an OR condition. * * @param conditions - Array of conditions where at least one must match * @returns The builder for chaining * * @example * ```typescript * builder.or([ * { role: 'admin' }, * { permissions: { $contains: 'write' } } * ]) * ``` */ or(conditions: WhereClause[]): QueryBuilder; /** * Filter by exact field equality. * Convenience method for simple equality checks. * * @param field - The field name * @param value - The value to match * @returns The builder for chaining * * @example * ```typescript * builder.eq('status', 'active') * ``` */ eq(field: K, value: T[K]): QueryBuilder; /** * Filter by field not equal to value. * * @param field - The field name * @param value - The value that must not match * @returns The builder for chaining */ ne(field: K, value: T[K]): QueryBuilder; /** * Filter by field greater than value. * * @param field - The field name * @param value - The minimum value (exclusive) * @returns The builder for chaining */ gt(field: K, value: T[K]): QueryBuilder; /** * Filter by field greater than or equal to value. * * @param field - The field name * @param value - The minimum value (inclusive) * @returns The builder for chaining */ gte(field: K, value: T[K]): QueryBuilder; /** * Filter by field less than value. * * @param field - The field name * @param value - The maximum value (exclusive) * @returns The builder for chaining */ lt(field: K, value: T[K]): QueryBuilder; /** * Filter by field less than or equal to value. * * @param field - The field name * @param value - The maximum value (inclusive) * @returns The builder for chaining */ lte(field: K, value: T[K]): QueryBuilder; /** * Filter by field value in array. * * @param field - The field name * @param values - The allowed values * @returns The builder for chaining * * @example * ```typescript * builder.in('status', ['active', 'pending']) * ``` */ in(field: K, values: T[K][]): QueryBuilder; /** * Filter by field value not in array. * * @param field - The field name * @param values - The disallowed values * @returns The builder for chaining */ notIn(field: K, values: T[K][]): QueryBuilder; /** * Filter by string field containing substring. * * @param field - The field name * @param substring - The substring to search for * @returns The builder for chaining * * @example * ```typescript * builder.contains('name', 'john') * ``` */ contains(field: K, substring: string): QueryBuilder; /** * Filter by string field starting with prefix. * * @param field - The field name * @param prefix - The prefix to match * @returns The builder for chaining */ startsWith(field: K, prefix: string): QueryBuilder; /** * Filter by string field ending with suffix. * * @param field - The field name * @param suffix - The suffix to match * @returns The builder for chaining */ endsWith(field: K, suffix: string): QueryBuilder; /** * Filter by field between two values (inclusive). * * @param field - The field name * @param min - The minimum value * @param max - The maximum value * @returns The builder for chaining * * @example * ```typescript * builder.between('age', 18, 65) * ``` */ between(field: K, min: T[K], max: T[K]): QueryBuilder; /** * Select specific fields to include in results. * * @param fields - Field selection object * @returns The builder for chaining * * @example * ```typescript * builder.select({ id: true, name: true, email: true }) * ``` */ select(fields: SelectClause): QueryBuilder; /** * Select only specified field names. * Convenience method for positive selection. * * @param fields - Field names to include * @returns The builder for chaining * * @example * ```typescript * builder.only('id', 'name', 'email') * ``` */ only(...fields: K[]): QueryBuilder; /** * Exclude specified fields from results. * * @param fields - Field names to exclude * @returns The builder for chaining * * @example * ```typescript * builder.omit('password', 'secret') * ``` */ omit(...fields: K[]): QueryBuilder; /** * Include related documents. * * @param relations - Relations to include * @returns The builder for chaining * * @example * ```typescript * builder.include({ * posts: true, * comments: { limit: 10 } * }) * ``` */ include(relations: IncludeClause): QueryBuilder; /** * Include a single relation. * Convenience method for simple includes. * * @param relation - The relation name * @param options - Optional include options * @returns The builder for chaining * * @example * ```typescript * builder.with('posts') * builder.with('comments', { limit: 10 }) * ``` */ with(relation: string, options?: IncludeOptions | boolean): QueryBuilder; /** * Set the sort order. * * @param order - Sort specification * @returns The builder for chaining * * @example * ```typescript * builder.orderBy({ createdAt: 'desc', name: 'asc' }) * ``` */ orderBy(order: OrderByClause): QueryBuilder; /** * Sort by a single field ascending. * * @param field - The field to sort by * @returns The builder for chaining * * @example * ```typescript * builder.asc('name') * ``` */ asc(field: K): QueryBuilder; /** * Sort by a single field descending. * * @param field - The field to sort by * @returns The builder for chaining * * @example * ```typescript * builder.desc('createdAt') * ``` */ desc(field: K): QueryBuilder; /** * Limit the number of results. * * @param count - Maximum number of results * @returns The builder for chaining */ limit(count: number): QueryBuilder; /** * Skip a number of results. * * @param count - Number of results to skip * @returns The builder for chaining */ offset(count: number): QueryBuilder; /** * Alias for offset. * * @param count - Number of results to skip * @returns The builder for chaining */ skip(count: number): QueryBuilder; /** * Set pagination with page number and size. * * @param page - Page number (1-based) * @param pageSize - Number of items per page * @returns The builder for chaining * * @example * ```typescript * builder.page(1, 20) // First 20 items * builder.page(2, 20) // Items 21-40 * ``` */ page(page: number, pageSize: number): QueryBuilder; /** * Get the first N results. * Convenience method for limit(n). * * @param count - Number of results * @returns The builder for chaining */ first(count: number): QueryBuilder; /** * Get a single result. * Equivalent to limit(1). * * @returns The builder for chaining */ one(): QueryBuilder; /** * Build and return the query object. * * @returns The built query * * @example * ```typescript * const query = builder.build(); * const results = await collection.find(query.where); * ``` */ build(): Query; /** * Clone the current builder state. * * @returns A new builder with the same state * * @example * ```typescript * const base = query().where({ status: 'active' }); * const admins = base.clone().where({ role: 'admin' }); * const users = base.clone().where({ role: 'user' }); * ``` */ clone(): QueryBuilder; /** * Reset the builder to initial state. * * @returns The builder for chaining */ reset(): QueryBuilder; } /** * Creates a new query builder instance. * * @typeParam T - The document type * @returns A new query builder * * @example Basic usage * ```typescript * const query = createQueryBuilder() * .where({ status: 'active' }) * .orderBy({ createdAt: 'desc' }) * .limit(10) * .build(); * ``` * * @example Convenience methods * ```typescript * const query = createQueryBuilder() * .eq('status', 'active') * .gte('age', 18) * .only('id', 'name', 'email') * .desc('createdAt') * .page(1, 20) * .build(); * ``` * * @example Complex filtering * ```typescript * const query = createQueryBuilder() * .where({ status: 'active' }) * .or([ * { role: 'admin' }, * { permissions: { $contains: 'write' } } * ]) * .between('age', 18, 65) * .build(); * ``` */ export declare function createQueryBuilder(): QueryBuilder; /** * Shorthand for createQueryBuilder. * * @typeParam T - The document type * @returns A new query builder * * @example * ```typescript * import { query } from '@db4/db4ai'; * * const q = query() * .eq('status', 'active') * .limit(10) * .build(); * ``` */ export declare function query(): QueryBuilder; /** * Static query helper functions for common query patterns. */ export declare const Q: { /** * Create an equality condition. * * @example * ```typescript * Q.eq('status', 'active') * // { status: { $eq: 'active' } } * ``` */ eq: (field: K, value: T[K]) => WhereClause; /** * Create a not-equal condition. */ ne: (field: K, value: T[K]) => WhereClause; /** * Create a greater-than condition. */ gt: (field: K, value: T[K]) => WhereClause; /** * Create a greater-than-or-equal condition. */ gte: (field: K, value: T[K]) => WhereClause; /** * Create a less-than condition. */ lt: (field: K, value: T[K]) => WhereClause; /** * Create a less-than-or-equal condition. */ lte: (field: K, value: T[K]) => WhereClause; /** * Create an in-array condition. */ in: (field: K, values: T[K][]) => WhereClause; /** * Create a not-in-array condition. */ nin: (field: K, values: T[K][]) => WhereClause; /** * Create a contains condition. */ contains: (field: K, value: string) => WhereClause; /** * Create a starts-with condition. */ startsWith: (field: K, value: string) => WhereClause; /** * Create an ends-with condition. */ endsWith: (field: K, value: string) => WhereClause; /** * Create a between condition (inclusive). */ between: (field: K, min: T[K], max: T[K]) => WhereClause; /** * Create an AND condition. */ and: (...conditions: WhereClause[]) => WhereClause; /** * Create an OR condition. */ or: (...conditions: WhereClause[]) => WhereClause; /** * Create a NOT condition. */ not: (condition: WhereClause) => WhereClause; /** * Check if a field exists. */ exists: (field: K) => WhereClause; /** * Check if a field does not exist. */ notExists: (field: K) => WhereClause; }; //# sourceMappingURL=query.d.ts.map