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 class
  • QueryBuilder - Internal query builder
  • QueryOptions - TypeScript type for query options
  • LeanQuery - For lean queries (plain objects)
  • JoinQuery - For join operations
  • Subquery - For subqueries
  • Operators - Query operators ($eq, $ne, $gt, etc.)

Query Methods

  • new Query<T>(database: Database, collectionName: string, options?: QueryOptions) - Create query
  • query.where(conditions: Record<string, any>): this - Add where conditions
  • query.select(fields: string[]): this - Select specific fields
  • query.sort(fields: Record<string, 1 | -1> | Array<{field: string; direction: 1 | -1}>): this - Sort results
  • query.limit(n: number): this - Limit number of results
  • query.skip(n: number): this - Skip results
  • query.lean(): LeanQuery<T> - Return plain objects
  • query.all(): Promise<T[]> - Execute and get all results
  • query.first(): Promise<T | null> - Get first result
  • query.buildAQL(): { query: string; bindVars: Record<string, any> } - Build AQL query

QueryBuilder Methods

  • QueryBuilder.where(conditions: Record<string, any>): this
  • QueryBuilder.select(fields: string[]): this
  • QueryBuilder.sort(fields: Record<string, 1 | -1>): this
  • QueryBuilder.limit(n: number): this
  • QueryBuilder.skip(n: number): this
  • QueryBuilder.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.