-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
55 lines (49 loc) · 1.61 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { aql, literal } from 'arangojs/aql'
import { Query } from './lib/structs'
import { buildSearch } from './search'
import { buildFilters } from './filter'
export { buildFilters } from './filter'
export { parseQuery } from './parse'
/** @returns an AQL query object. See @param query for details on required
* values. @param query.terms accepts either a string to be parsed or an array
* of terms.
*
* ! NOTE: v0.1.1 introduced a breaking change to adhere to the ArangoSearch
* spec. Please refer to
* <https://www.arangodb.com/docs/stable/aql/operations-limit.html> @param
* limit is an object with keys `start` @default 0, and `count` @default 20 */
export function buildAQL(
query: Query,
limit: any = { start: 0, count: 20 },
): any {
validateQuery(query)
collectKeys(query)
const SEARCH = buildSearch(query)
const FILTER = query.filters && buildFilters(query.filters)
/* FOR doc IN ${query.view} */
return aql`
FOR doc IN ${literal(query.view)}
${SEARCH}
${FILTER}
LIMIT ${limit.start}, ${limit.count}
RETURN doc`
}
function validateQuery(query: Query) {
if (!query.view.length)
throw new Error('query.view must be a valid ArangoSearch View name')
if (!query.collections.length)
throw new Error('query.collections must have at least one name')
}
function collectKeys(query: Query) {
/* unify query.key */
let _keys: string[]
if (typeof query.key == 'string') {
_keys = [query.key]
} else if (!query.key) {
_keys = ['text']
} else _keys = query.key
query.collections = query.collections.map((c) => {
if (!c.keys) c.keys = _keys
return c
})
}