import { Document } from 'arangojs/documents'; import { DocumentCollection } from 'arangojs/collection'; import { AqlLiteral, GeneratedAqlQuery } from 'arangojs/aql'; import { Database } from 'arangojs'; /** * A `CountFn` function returns the number of documents in a single collection matching search `terms` given. * * `count()` provides the standard implementation. */ export declare type CountFn = (terms?: Terms>>, inject?: Inject) => Promise; /** * Recursively renders all of a complex object's properties required, non-null, and non-undefined. * * Note: this type recurses through objects only, not arrays. */ export declare type DeepNonNullable = NonNullable extends object ? { [P in keyof T]-?: DeepNonNullable; } : T; /** Query sort direction. */ export declare type Direction = 'ASC' | 'DESC'; /** * Simple search filter type in which any number of conditions can be specified for a presumed parameter. * * Multiple operators can be used in combination. * By default they will be combined into an `AND` filter. * Specify `mode: "OR"` to switch to an `OR` filter. * * Note: `LIKE`, `NOT LIKE`, and regular expression operators are only available if `T` extends `string`. */ export declare type Filter = { mode?: 'AND' | 'OR'; eq?: T | null; gt?: T; gte?: T; in?: T[]; like?: T & string; lt?: T; lte?: T; neq?: T | null; nin?: T[]; nlike?: T & string; nreg?: T & string; reg?: T & string; }; /** * A `FindFn` function returns the first document in a single collection matching search `terms` given. * A `sort` can be specified to control the result. * * `find()` provides the standard implementation. */ export declare type FindFn = (terms?: Terms>>, sort?: Sort>[] | Sort>, inject?: Inject) => Promise | undefined>; /** * An `Inject` object allows modifying a search query with user-defined AQL literal expressions. * This can be useful for complex requirements, such as joining data or setting additional variables with `LET`. * * All injected strings are implicitly converted to AQL literals in the query, so should be manually escaped as needed. * * Note that while `CountFn` and `FindFn` do not use all the same parameters as `SearchFn`, all injections are still * supported so a single injection provider can be used for all query types. */ export declare type Inject = { /** Injected before FILTER statements. */ beforeFilter?: string; /** Injected after FILTER statements, before SORT statements. */ beforeSort?: string; /** Injected after SORT statements, before LIMIT statement. */ beforeLimit?: string; /** Injected after all other statements. */ after?: string; }; /** * Query limit. * Always a tuple, but the second value can be omitted. */ export declare type Limit = [number, number?]; /** * Searchable data type. * Essentially, any object is valid. */ export declare type Searchable = Record; /** * Formulate search terms based on a complex data type. * Nested object types are preserved, while scalar properties are converted to `Filter` representations. */ export declare type Terms = { [P in keyof T]?: NonNullable extends object ? Terms : Filter>; }; /** * A `SearchFn` function matches documents in a single collection and returns a `SearchResult` based on the given * `terms`, `limit`, and `sort`. * * A fourth `inject` argument can be given to add user-defined sections to the query, allowing complex requirements * to be added around the core query pattern. */ export declare type SearchFn = (terms?: Terms>>, limit?: Limit, sort?: Sort>[] | Sort>, inject?: Inject) => Promise>; /** * A `SimpleSearchFn` function matches documents in a single collection and returns a `SearchResult` based on the given * `terms`, `limit`, and `sort`. * * This type can be implemented by user code as an alternative to `SearchFn` to prevent further injections. */ export declare type SimpleSearchFn = (terms?: Terms>>, limit?: Limit, sort?: Sort>[] | Sort>) => Promise>; /** * Search results are a tuple of three values: * 1. The **total** number of matching documents in the searched collection, ignoring limit * 2. The documents matched within the searched collection, respecting limit * 3. The AQL query object for the latter (for debugging purposes) */ export declare type SearchResult = [number, Document[], GeneratedAqlQuery]; /** * Query sort order as a tuple of key and direction. * * The sort key can be specified as an AQL literal, in which case it will be used exactly as given. * This can be useful in conjunction with query injections. */ export declare type Sort = [AqlLiteral | keyof T, Direction]; /** Format scalar or scalar array data for use in AQL. */ export declare const formatData: (data: T | T[]) => string; /** Format scalar data for use in AQL. */ export declare const formatValue: (data: T) => string; /** Map of search operator properties to AQL equivalents. */ export declare const operatorMap: Record, 'mode'>, string>; /** Search operators. */ export declare const operators: string[]; /** * Parse a search limit to a string AQL limit. * * Note: `LIMIT` is not prepended. */ export declare const parseLimit: (l: Limit) => string | number; /** * Parse a search filter to a string of AQL filters. * * Note: `FILTER` is not prepended. */ export declare const parseFilter: (param: string, search: Filter) => string; /** * Parse query sort(s) to an array of string AQL sorts. * * Note: `SORT` is not prepended. */ export declare const parseSort: (s: Sort | Sort[], parent: string) => string[]; /** * Parse search terms to a flat array of search filters. * The `parent` argument refers to the current document, and is prefixed to each filter. */ export declare const parseTerms: (s: Terms, parent: string) => string[]; /** * Build and execute a count query that matches documents in a single collection. * Returns the total number of matches. * * This example resembles the generated AQL query: * * ```aql * FOR {i} IN {c} {FILTER ...} COLLECT WITH COUNT INTO {n} RETURN {n} * ``` */ export declare const count: (db: Database, c: DocumentCollection, i?: string, n?: string) => CountFn; /** * Build and execute a find query that returns the first matching document in a single collection. * * This example resembles the generated AQL query: * * ```aql * FOR {i} IN {collection} {FILTER ...} {SORT ...} LIMIT 1 RETURN {i} * ``` */ export declare const find: (db: Database, c: DocumentCollection, i?: string) => FindFn; /** * Build and execute a search query across a single collection. * Returns a `SearchResult` tuple containing the total number of matches (ignoring limit), all matching documents * (respecting limit), and the AQL query. * * This example resembles the generated AQL query: * * ```aql * FOR {i} IN {collection} {FILTER ...} {SORT ...} {LIMIT ...} RETURN {i} * ``` */ export declare const search: (db: Database, c: DocumentCollection, i?: string, n?: string) => SearchFn; declare const _default: { count: (db: Database, c: DocumentCollection, i?: string, n?: string) => CountFn; find: (db: Database, c: DocumentCollection, i?: string) => FindFn; formatData: (data: T_2 | T_2[]) => string; formatValue: (data: T_3) => string; operatorMap: Record<"eq" | "gt" | "gte" | "in" | "like" | "lt" | "lte" | "neq" | "nin" | "nlike" | "nreg" | "reg", string>; operators: string[]; parseFilter: (param: string, search: Filter) => string; parseLimit: (l: Limit) => string | number; parseSort: (s: Sort | Sort[], parent: string) => string[]; parseTerms: (s: Terms, parent: string) => string[]; search: (db: Database, c: DocumentCollection, i?: string, n?: string) => SearchFn; }; export default _default;