@@ -4,8 +4,8 @@ import * as DELETE from './DELETE';
44import * as EXPLAIN from './EXPLAIN' ;
55import * as LIST from './LIST' ;
66import * as PROFILE from './PROFILE' ;
7- import * as QUERY_RO from './QUERY_RO' ;
87import * as QUERY from './QUERY' ;
8+ import * as RO_QUERY from './RO_QUERY' ;
99import * as SLOWLOG from './SLOWLOG' ;
1010import { RedisCommandArgument , RedisCommandArguments } from '@redis/client/dist/lib/commands' ;
1111
@@ -22,28 +22,93 @@ export default {
2222 list : LIST ,
2323 PROFILE ,
2424 profile : PROFILE ,
25- QUERY_RO ,
26- queryRo : QUERY_RO ,
2725 QUERY ,
2826 query : QUERY ,
27+ RO_QUERY ,
28+ roQuery : RO_QUERY ,
2929 SLOWLOG ,
3030 slowLog : SLOWLOG
3131} ;
3232
33+ type QueryParam = null | string | number | boolean | QueryParams | Array < QueryParam > ;
34+
35+ type QueryParams = {
36+ [ key : string ] : QueryParam ;
37+ } ;
38+
39+ export interface QueryOptions {
40+ params ?: QueryParams ;
41+ TIMEOUT ?: number ;
42+ }
43+
44+ export type QueryOptionsBackwardCompatible = QueryOptions | number ;
45+
3346export function pushQueryArguments (
3447 args : RedisCommandArguments ,
3548 graph : RedisCommandArgument ,
3649 query : RedisCommandArgument ,
37- timeout ?: number
50+ options ?: QueryOptionsBackwardCompatible ,
51+ compact ?: boolean
3852) : RedisCommandArguments {
39- args . push (
40- graph ,
41- query
42- ) ;
53+ args . push ( graph ) ;
4354
44- if ( timeout !== undefined ) {
45- args . push ( timeout . toString ( ) ) ;
55+ if ( typeof options === 'number' ) {
56+ args . push ( query ) ;
57+ pushTimeout ( args , options ) ;
58+ } else {
59+ args . push (
60+ options ?. params ?
61+ `CYPHER ${ queryParamsToString ( options . params ) } ${ query } ` :
62+ query
63+ ) ;
64+
65+ if ( options ?. TIMEOUT !== undefined ) {
66+ pushTimeout ( args , options . TIMEOUT ) ;
67+ }
68+ }
69+
70+ if ( compact ) {
71+ args . push ( '--compact' ) ;
4672 }
4773
4874 return args ;
49- }
75+ }
76+
77+ function pushTimeout ( args : RedisCommandArguments , timeout : number ) : void {
78+ args . push ( 'TIMEOUT' , timeout . toString ( ) ) ;
79+ }
80+
81+ function queryParamsToString ( params : QueryParams ) : string {
82+ const parts = [ ] ;
83+ for ( const [ key , value ] of Object . entries ( params ) ) {
84+ parts . push ( `${ key } =${ queryParamToString ( value ) } ` ) ;
85+ }
86+ return parts . join ( ' ' ) ;
87+ }
88+
89+ function queryParamToString ( param : QueryParam ) : string {
90+ if ( param === null ) {
91+ return 'null' ;
92+ }
93+
94+ switch ( typeof param ) {
95+ case 'string' :
96+ return `"${ param . replace ( / [ " \\ ] / g, '\\$&' ) } "` ;
97+
98+ case 'number' :
99+ case 'boolean' :
100+ return param . toString ( ) ;
101+ }
102+
103+ if ( Array . isArray ( param ) ) {
104+ return `[${ param . map ( queryParamToString ) . join ( ',' ) } ]` ;
105+ } else if ( typeof param === 'object' ) {
106+ const body = [ ] ;
107+ for ( const [ key , value ] of Object . entries ( param ) ) {
108+ body . push ( `${ key } :${ queryParamToString ( value ) } ` ) ;
109+ }
110+ return `{${ body . join ( ',' ) } }` ;
111+ } else {
112+ throw new TypeError ( `Unexpected param type ${ typeof param } ${ param } ` )
113+ }
114+ }
0 commit comments