1- export type AqlQuery = {
1+ import { ArangoCollection , isArangoCollection } from "./collection" ;
2+
3+ export interface AqlQuery {
24 query : string ;
35 bindVars : { [ key : string ] : any } ;
4- } ;
6+ }
57
6- export type AqlLiteral = {
8+ export interface AqlLiteral {
79 toAQL : ( ) => string ;
8- } ;
10+ }
11+
12+ export type AqlValue =
13+ | string
14+ | number
15+ | boolean
16+ | ArangoCollection
17+ | AqlLiteral ;
918
1019export function isAqlQuery ( query : any ) : query is AqlQuery {
1120 return Boolean ( query && query . query && query . bindVars ) ;
@@ -15,21 +24,24 @@ export function isAqlLiteral(literal: any): literal is AqlLiteral {
1524 return Boolean ( literal && typeof literal . toAQL === "function" ) ;
1625}
1726
18- export function aql ( strings : TemplateStringsArray , ...args : any [ ] ) : AqlQuery {
27+ export function aql (
28+ strings : TemplateStringsArray ,
29+ ...args : AqlValue [ ]
30+ ) : AqlQuery {
1931 const bindVars : AqlQuery [ "bindVars" ] = { } ;
2032 const bindVals = [ ] ;
2133 let query = strings [ 0 ] ;
2234 for ( let i = 0 ; i < args . length ; i ++ ) {
2335 const rawValue = args [ i ] ;
2436 let value = rawValue ;
25- if ( rawValue && typeof rawValue . toAQL === "function" ) {
37+ if ( isAqlLiteral ( rawValue ) ) {
2638 query += `${ rawValue . toAQL ( ) } ${ strings [ i + 1 ] } ` ;
2739 continue ;
2840 }
2941 const index = bindVals . indexOf ( rawValue ) ;
3042 const isKnown = index !== - 1 ;
3143 let name = `value${ isKnown ? index : bindVals . length } ` ;
32- if ( rawValue && rawValue . isArangoCollection ) {
44+ if ( isArangoCollection ( rawValue ) ) {
3345 name = `@${ name } ` ;
3446 value = rawValue . name ;
3547 }
@@ -42,4 +54,10 @@ export function aql(strings: TemplateStringsArray, ...args: any[]): AqlQuery {
4254 return { query, bindVars } ;
4355}
4456
45- aql . literal = ( value : any ) => ( { toAQL ( ) { return value ; } } ) ;
57+ export namespace aql {
58+ export const literal = ( value : any ) : AqlLiteral => ( {
59+ toAQL ( ) {
60+ return String ( value ) ;
61+ }
62+ } ) ;
63+ }
0 commit comments