index.d.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import { Document } from 'arangojs/documents';
  2. import { DocumentCollection } from 'arangojs/collection';
  3. import { AqlLiteral, GeneratedAqlQuery } from 'arangojs/aql';
  4. import { Database } from 'arangojs';
  5. /**
  6. * A `CountFn` function returns the number of documents in a single collection matching search `terms` given.
  7. *
  8. * `count()` provides the standard implementation.
  9. */
  10. export declare type CountFn<T extends Searchable, S extends Searchable = T> = (terms?: Terms<Document<DeepNonNullable<S>>>, inject?: Inject) => Promise<number>;
  11. /**
  12. * Recursively renders all of a complex object's properties required, non-null, and non-undefined.
  13. *
  14. * Note: this type recurses through objects only, not arrays.
  15. */
  16. export declare type DeepNonNullable<T> = NonNullable<T> extends object ? {
  17. [P in keyof T]-?: DeepNonNullable<T[P]>;
  18. } : T;
  19. /** Query sort direction. */
  20. export declare type Direction = 'ASC' | 'DESC';
  21. /**
  22. * Simple search filter type in which any number of conditions can be specified for a presumed parameter.
  23. *
  24. * Multiple operators can be used in combination.
  25. * By default they will be combined into an `AND` filter.
  26. * Specify `mode: "OR"` to switch to an `OR` filter.
  27. *
  28. * Note: `LIKE`, `NOT LIKE`, and regular expression operators are only available if `T` extends `string`.
  29. */
  30. export declare type Filter<T> = {
  31. mode?: 'AND' | 'OR';
  32. eq?: T | null;
  33. gt?: T;
  34. gte?: T;
  35. in?: T[];
  36. like?: T & string;
  37. lt?: T;
  38. lte?: T;
  39. neq?: T | null;
  40. nin?: T[];
  41. nlike?: T & string;
  42. nreg?: T & string;
  43. reg?: T & string;
  44. };
  45. /**
  46. * A `FindFn` function returns the first document in a single collection matching search `terms` given.
  47. * A `sort` can be specified to control the result.
  48. *
  49. * `find()` provides the standard implementation.
  50. */
  51. export declare type FindFn<T extends Searchable, S extends Searchable = T> = (terms?: Terms<Document<DeepNonNullable<S>>>, sort?: Sort<Document<T>>[] | Sort<Document<T>>, inject?: Inject) => Promise<Document<T> | undefined>;
  52. /**
  53. * An `Inject` object allows modifying a search query with user-defined AQL literal expressions.
  54. * This can be useful for complex requirements, such as joining data or setting additional variables with `LET`.
  55. *
  56. * All injected strings are implicitly converted to AQL literals in the query, so should be manually escaped as needed.
  57. *
  58. * Note that while `CountFn` and `FindFn` do not use all the same parameters as `SearchFn`, all injections are still
  59. * supported so a single injection provider can be used for all query types.
  60. */
  61. export declare type Inject = {
  62. /** Injected before FILTER statements. */
  63. beforeFilter?: string;
  64. /** Injected after FILTER statements, before SORT statements. */
  65. beforeSort?: string;
  66. /** Injected after SORT statements, before LIMIT statement. */
  67. beforeLimit?: string;
  68. /** Injected after all other statements. */
  69. after?: string;
  70. };
  71. /**
  72. * Query limit.
  73. * Always a tuple, but the second value can be omitted.
  74. */
  75. export declare type Limit = [number, number?];
  76. /**
  77. * Searchable data type.
  78. * Essentially, any object is valid.
  79. */
  80. export declare type Searchable = Record<string, unknown>;
  81. /**
  82. * Formulate search terms based on a complex data type.
  83. * Nested object types are preserved, while scalar properties are converted to `Filter` representations.
  84. */
  85. export declare type Terms<T> = {
  86. [P in keyof T]?: NonNullable<NonNullable<T[P]> extends object ? Terms<T[P]> : Filter<T[P]>>;
  87. };
  88. /**
  89. * A `SearchFn` function matches documents in a single collection and returns a `SearchResult` based on the given
  90. * `terms`, `limit`, and `sort`.
  91. *
  92. * A fourth `inject` argument can be given to add user-defined sections to the query, allowing complex requirements
  93. * to be added around the core query pattern.
  94. */
  95. export declare type SearchFn<T extends Searchable, S extends Searchable = T> = (terms?: Terms<Document<DeepNonNullable<S>>>, limit?: Limit, sort?: Sort<Document<S>>[] | Sort<Document<S>>, inject?: Inject) => Promise<SearchResult<T>>;
  96. /**
  97. * A `SimpleSearchFn` function matches documents in a single collection and returns a `SearchResult` based on the given
  98. * `terms`, `limit`, and `sort`.
  99. *
  100. * This type can be implemented by user code as an alternative to `SearchFn` to prevent further injections.
  101. */
  102. export declare type SimpleSearchFn<T extends Searchable, S extends Searchable = T> = (terms?: Terms<Document<DeepNonNullable<S>>>, limit?: Limit, sort?: Sort<Document<S>>[] | Sort<Document<S>>) => Promise<SearchResult<T>>;
  103. /**
  104. * Search results are a tuple of three values:
  105. * 1. The **total** number of matching documents in the searched collection, ignoring limit
  106. * 2. The documents matched within the searched collection, respecting limit
  107. * 3. The AQL query object for the latter (for debugging purposes)
  108. */
  109. export declare type SearchResult<T extends Searchable> = [number, Document<T>[], GeneratedAqlQuery];
  110. /**
  111. * Query sort order as a tuple of key and direction.
  112. *
  113. * The sort key can be specified as an AQL literal, in which case it will be used exactly as given.
  114. * This can be useful in conjunction with query injections.
  115. */
  116. export declare type Sort<T> = [AqlLiteral | keyof T, Direction];
  117. /** Format scalar or scalar array data for use in AQL. */
  118. export declare const formatData: <T>(data: T | T[]) => string;
  119. /** Format scalar data for use in AQL. */
  120. export declare const formatValue: <T>(data: T) => string;
  121. /** Map of search operator properties to AQL equivalents. */
  122. export declare const operatorMap: Record<keyof Omit<Filter<unknown>, 'mode'>, string>;
  123. /** Search operators. */
  124. export declare const operators: string[];
  125. /**
  126. * Parse a search limit to a string AQL limit.
  127. *
  128. * Note: `LIMIT` is not prepended.
  129. */
  130. export declare const parseLimit: (l: Limit) => string | number;
  131. /**
  132. * Parse a search filter to a string of AQL filters.
  133. *
  134. * Note: `FILTER` is not prepended.
  135. */
  136. export declare const parseFilter: <T>(param: string, search: Filter<T>) => string;
  137. /**
  138. * Parse query sort(s) to an array of string AQL sorts.
  139. *
  140. * Note: `SORT` is not prepended.
  141. */
  142. export declare const parseSort: <T>(s: Sort<T> | Sort<T>[], parent: string) => string[];
  143. /**
  144. * Parse search terms to a flat array of search filters.
  145. * The `parent` argument refers to the current document, and is prefixed to each filter.
  146. */
  147. export declare const parseTerms: <T>(s: Terms<T>, parent: string) => string[];
  148. /**
  149. * Build and execute a count query that matches documents in a single collection.
  150. * Returns the total number of matches.
  151. *
  152. * This example resembles the generated AQL query:
  153. *
  154. * ```aql
  155. * FOR {i} IN {c} {FILTER ...} COLLECT WITH COUNT INTO {n} RETURN {n}
  156. * ```
  157. */
  158. export declare const count: <T extends Searchable, S extends Searchable = T>(db: Database, c: DocumentCollection<T>, i?: string, n?: string) => CountFn<T, S>;
  159. /**
  160. * Build and execute a find query that returns the first matching document in a single collection.
  161. *
  162. * This example resembles the generated AQL query:
  163. *
  164. * ```aql
  165. * FOR {i} IN {collection} {FILTER ...} {SORT ...} LIMIT 1 RETURN {i}
  166. * ```
  167. */
  168. export declare const find: <T extends Searchable, S extends Searchable = T>(db: Database, c: DocumentCollection<T>, i?: string) => FindFn<T, S>;
  169. /**
  170. * Build and execute a search query across a single collection.
  171. * Returns a `SearchResult` tuple containing the total number of matches (ignoring limit), all matching documents
  172. * (respecting limit), and the AQL query.
  173. *
  174. * This example resembles the generated AQL query:
  175. *
  176. * ```aql
  177. * FOR {i} IN {collection} {FILTER ...} {SORT ...} {LIMIT ...} RETURN {i}
  178. * ```
  179. */
  180. export declare const search: <T extends Searchable, S extends Searchable = T>(db: Database, c: DocumentCollection<T>, i?: string, n?: string) => SearchFn<T, S>;
  181. declare const _default: {
  182. count: <T extends Searchable, S extends Searchable = T>(db: Database, c: DocumentCollection<T>, i?: string, n?: string) => CountFn<T, S>;
  183. find: <T_1 extends Searchable, S_1 extends Searchable = T_1>(db: Database, c: DocumentCollection<T_1>, i?: string) => FindFn<T_1, S_1>;
  184. formatData: <T_2>(data: T_2 | T_2[]) => string;
  185. formatValue: <T_3>(data: T_3) => string;
  186. operatorMap: Record<"eq" | "gt" | "gte" | "in" | "like" | "lt" | "lte" | "neq" | "nin" | "nlike" | "nreg" | "reg", string>;
  187. operators: string[];
  188. parseFilter: <T_4>(param: string, search: Filter<T_4>) => string;
  189. parseLimit: (l: Limit) => string | number;
  190. parseSort: <T_5>(s: Sort<T_5> | Sort<T_5>[], parent: string) => string[];
  191. parseTerms: <T_6>(s: Terms<T_6>, parent: string) => string[];
  192. search: <T_7 extends Searchable, S_2 extends Searchable = T_7>(db: Database, c: DocumentCollection<T_7>, i?: string, n?: string) => SearchFn<T_7, S_2>;
  193. };
  194. export default _default;