@adonis-agora/filter
Version:
Server-side query filtering/sorting/pagination for AdonisJS — Spatie-style input, a Lucid adapter, and field allow-listing. Part of the Agora ecosystem.
72 lines • 3.76 kB
TypeScript
import type { QueryBuilderLike } from './lucid_adapter.js';
/** A recorded query-builder call: method name + its scalar args. */
export interface RecordedCall {
method: string;
args: unknown[];
}
/**
* A recording stand-in for a Lucid query builder, published under
* `@adonis-agora/filter/testing` so consumers can unit-test their own filter
* definitions without a database. It satisfies {@link QueryBuilderLike}, so any
* of the adapter/runner helpers (or {@link applyFilterFromRequest}) can be
* driven against it and the resulting translation asserted.
*
* Nested `where`/`orWhere` closures run against child recorders, and a dotted
* relation-path filter's `whereHas` records the relation name plus runs its
* nested callback against a child. {@link flatten} collects every leaf call
* (across all groups and relation subqueries) so tests can assert a translation
* appeared without caring about the exact grouping nesting.
*/
export declare class MockQueryBuilder implements QueryBuilderLike {
calls: RecordedCall[];
children: MockQueryBuilder[];
private record;
private group;
where(a: any, b?: any, c?: any): this;
orWhere(cb: (qb: QueryBuilderLike) => void): this;
/**
* Record a relation subquery: the `whereHas` call is captured with the
* relation name (so `find('whereHas')?.args[0]` is the relation), and the
* nested callback runs against a child recorder whose calls surface via
* {@link flatten}.
*/
whereHas(relation: string, cb: (qb: QueryBuilderLike) => void): this;
whereNot(column: string, value: unknown): this;
whereIn(column: string, values: unknown[]): this;
whereNotIn(column: string, values: unknown[]): this;
whereNull(column: string): this;
whereNotNull(column: string): this;
whereBetween(column: string, range: [unknown, unknown]): this;
whereNotBetween(column: string, range: [unknown, unknown]): this;
whereILike(column: string, value: string): this;
orWhereILike(column: string, value: string): this;
orderBy(column: string, direction: 'asc' | 'desc'): this;
/** Record a raw predicate: `args` are `[sql, bindings]` (bindings default `[]`). */
whereRaw(sql: string, bindings?: readonly unknown[]): this;
/** Record a raw ordering: `args` are `[sql, bindings]` (bindings default `[]`). */
orderByRaw(sql: string, bindings?: readonly unknown[]): this;
limit(count: number): this;
/** Record a row skip: `args` are `[n]`. Only the group-by-count aggregation pages this way. */
offset(n: number): this;
/** Record a projection: `args` are the select expressions (aggregates included). */
select(...columns: string[]): this;
/** Record an aggregation: `args` are `[column]` (e.g. `'* AS count'`). */
count(column: string): this;
/** Record a grouping: `args` are the group columns. */
groupBy(...columns: string[]): this;
/** Record a DISTINCT projection: `args` are the distinct column names. */
distinct(...columns: string[]): this;
/**
* Every recorded call across this builder and all nested groups and relation
* subqueries (excludes the anonymous group markers, but keeps `whereHas` since
* its recorded arg is the relation name, not a marker).
*/
flatten(): RecordedCall[];
/** Find the first flattened call matching `method`. */
find(method: string): RecordedCall | undefined;
/** Every flattened call matching `method` (handy for asserting relation subqueries). */
findAll(method: string): RecordedCall[];
}
/** Factory for a fresh {@link MockQueryBuilder} — the recording Lucid stand-in. */
export declare function makeMockQueryBuilder(): MockQueryBuilder;
//# sourceMappingURL=testing.d.ts.map