@@ -4,8 +4,8 @@ import * as DELETE from './DELETE';
4
4
import * as EXPLAIN from './EXPLAIN' ;
5
5
import * as LIST from './LIST' ;
6
6
import * as PROFILE from './PROFILE' ;
7
- import * as QUERY_RO from './QUERY_RO' ;
8
7
import * as QUERY from './QUERY' ;
8
+ import * as RO_QUERY from './RO_QUERY' ;
9
9
import * as SLOWLOG from './SLOWLOG' ;
10
10
import { RedisCommandArgument , RedisCommandArguments } from '@redis/client/dist/lib/commands' ;
11
11
@@ -22,28 +22,93 @@ export default {
22
22
list : LIST ,
23
23
PROFILE ,
24
24
profile : PROFILE ,
25
- QUERY_RO ,
26
- queryRo : QUERY_RO ,
27
25
QUERY ,
28
26
query : QUERY ,
27
+ RO_QUERY ,
28
+ roQuery : RO_QUERY ,
29
29
SLOWLOG ,
30
30
slowLog : SLOWLOG
31
31
} ;
32
32
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
+
33
46
export function pushQueryArguments (
34
47
args : RedisCommandArguments ,
35
48
graph : RedisCommandArgument ,
36
49
query : RedisCommandArgument ,
37
- timeout ?: number
50
+ options ?: QueryOptionsBackwardCompatible ,
51
+ compact ?: boolean
38
52
) : RedisCommandArguments {
39
- args . push (
40
- graph ,
41
- query
42
- ) ;
53
+ args . push ( graph ) ;
43
54
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' ) ;
46
72
}
47
73
48
74
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