Query Module
The Query module provides a chainable query builder for constructing and executing AQL queries. It includes query caching for performance optimization.
What is it?
Query is a class that provides a fluent, chainable API for building AQL queries. It supports filtering, sorting, pagination, projection, and includes automatic query caching to improve performance for repeated queries.
Why do you need it?
- Chainable API: Build complex queries using method chaining
- Query Caching: Automatically caches compiled queries for performance
- Type Safety: Full TypeScript support with type inference
- AQL Generation: Automatically generates optimized AQL queries
- Flexible Filtering: Support for complex where conditions
How it works
Query uses a QueryBuilder internally to construct AQL queries. When you chain methods (where, sort, limit, etc.), it builds up a query structure. The query is compiled to AQL when executed, and the compiled structure is cached for reuse. This eliminates the overhead of parsing and compiling repeated queries.
Exports
Query- Main Query classQueryBuilder- Internal query builderQueryOptions- TypeScript type for query optionsLeanQuery- For lean queries (plain objects)JoinQuery- For join operationsSubquery- For subqueriesOperators- Query operators ($eq, $ne, $gt, etc.)
Query Methods
new Query<T>(database: Database, collectionName: string, options?: QueryOptions)- Create queryquery.where(conditions: Record<string, any>): this- Add where conditionsquery.select(fields: string[]): this- Select specific fieldsquery.sort(fields: Record<string, 1 | -1> | Array<{field: string; direction: 1 | -1}>): this- Sort resultsquery.limit(n: number): this- Limit number of resultsquery.skip(n: number): this- Skip resultsquery.lean(): LeanQuery<T>- Return plain objectsquery.all(): Promise<T[]>- Execute and get all resultsquery.first(): Promise<T | null>- Get first resultquery.buildAQL(): { query: string; bindVars: Record<string, any> }- Build AQL query
QueryBuilder Methods
QueryBuilder.where(conditions: Record<string, any>): thisQueryBuilder.select(fields: string[]): thisQueryBuilder.sort(fields: Record<string, 1 | -1>): thisQueryBuilder.limit(n: number): thisQueryBuilder.skip(n: number): thisQueryBuilder.buildAQL(): { query: string; bindVars: Record<string, any> }
TypeScript Types
interface QueryOptions {
where?: Record;
select?: string[];
sort?: Record;
limit?: number;
skip?: number;
}
interface OperatorMap {
$eq: (value: any) => any;
$ne: (value: any) => any;
$gt: (value: any) => any;
$gte: (value: any) => any;
$lt: (value: any) => any;
$lte: (value: any) => any;
$in: (values: any[]) => any;
$nin: (values: any[]) => any;
$between: (min: any, max: any) => any;
}
📖 For usage examples and best practices: See Queries Guide for detailed query documentation, examples, and best practices.