@@ -88,25 +88,28 @@ export async function deleteKeysViaCli(keyData: KeyData): Promise<void> {
88
88
* @param keyArguments The arguments of key
89
89
*/
90
90
export async function populateDBWithHashes ( host : string , port : string , keyArguments : AddKeyArguments ) : Promise < void > {
91
- const dbConf = { port : Number . parseInt ( port ) , host, username : 'default' } ;
92
- const client = createClient ( dbConf ) ;
91
+ const url = `redis://default@ ${ host } : ${ port } ` ;
92
+ const client = createClient ( { url } ) ;
93
93
94
- await client . on ( 'error' , async function ( error : string ) {
95
- throw new Error ( error ) ;
94
+ client . on ( 'error' , ( error : Error ) => {
95
+ console . error ( 'Redis Client Error' , error ) ;
96
96
} ) ;
97
- await client . on ( 'connect' , async function ( ) {
97
+
98
+ try {
99
+ await client . connect ( ) ;
100
+
98
101
if ( keyArguments . keysCount ) {
99
102
for ( let i = 0 ; i < keyArguments . keysCount ; i ++ ) {
100
103
const keyName = `${ keyArguments . keyNameStartWith } ${ Common . generateWord ( 20 ) } ` ;
101
- await client . hset ( [ keyName , 'field1' , 'Hello' ] , async ( error : string ) => {
102
- if ( error ) {
103
- throw error ;
104
- }
105
- } ) ;
104
+ await client . hSet ( keyName , 'field1' , 'Hello' ) ;
106
105
}
107
106
}
108
- await client . quit ( ) ;
109
- } ) ;
107
+
108
+ } catch ( error ) {
109
+ console . error ( 'Error during Redis operations:' , error ) ;
110
+ } finally {
111
+ await client . disconnect ( ) ;
112
+ }
110
113
}
111
114
112
115
/**
@@ -116,28 +119,36 @@ export async function populateDBWithHashes(host: string, port: string, keyArgume
116
119
* @param keyArguments The arguments of key and its fields
117
120
*/
118
121
export async function populateHashWithFields ( host : string , port : string , keyArguments : AddKeyArguments ) : Promise < void > {
119
- const dbConf = { port : Number . parseInt ( port ) , host, username : 'default' } ;
120
- const client = createClient ( dbConf ) ;
121
- const fields : string [ ] = [ ] ;
122
+ const url = `redis://default@ ${ host } : ${ port } ` ;
123
+ const client = createClient ( { url } ) ;
124
+ const fields : Record < string , string > = { } ;
122
125
123
- await client . on ( 'error' , async function ( error : string ) {
124
- throw new Error ( error ) ;
126
+ client . on ( 'error' , ( error : Error ) => {
127
+ console . error ( 'Redis Client Error' , error ) ;
125
128
} ) ;
126
- await client . on ( 'connect' , async function ( ) {
129
+
130
+ try {
131
+ await client . connect ( ) ;
132
+
127
133
if ( keyArguments . fieldsCount ) {
128
134
for ( let i = 0 ; i < keyArguments . fieldsCount ; i ++ ) {
129
135
const field = `${ keyArguments . fieldStartWith } ${ Common . generateWord ( 10 ) } ` ;
130
136
const fieldValue = `${ keyArguments . fieldValueStartWith } ${ Common . generateWord ( 10 ) } ` ;
131
- fields . push ( field , fieldValue ) ;
137
+ fields [ field ] = fieldValue ;
132
138
}
133
139
}
134
- await client . hset ( keyArguments . keyName , fields , async ( error : string ) => {
135
- if ( error ) {
136
- throw error ;
137
- }
138
- } ) ;
139
- await client . quit ( ) ;
140
- } ) ;
140
+
141
+ if ( keyArguments . keyName ) {
142
+ await client . hSet ( keyArguments . keyName , fields ) ;
143
+ } else {
144
+ throw new Error ( 'keyName is required to populate the hash.' ) ;
145
+ }
146
+
147
+ } catch ( error ) {
148
+ console . error ( 'Error setting hash fields:' , error ) ;
149
+ } finally {
150
+ await client . disconnect ( ) ;
151
+ }
141
152
}
142
153
143
154
/**
@@ -147,27 +158,35 @@ export async function populateHashWithFields(host: string, port: string, keyArgu
147
158
* @param keyArguments The arguments of key and its members
148
159
*/
149
160
export async function populateListWithElements ( host : string , port : string , keyArguments : AddKeyArguments ) : Promise < void > {
150
- const dbConf = { port : Number . parseInt ( port ) , host, username : 'default' } ;
151
- const client = createClient ( dbConf ) ;
161
+ const url = `redis://default@ ${ host } : ${ port } ` ;
162
+ const client = createClient ( { url } ) ;
152
163
const elements : string [ ] = [ ] ;
153
164
154
- await client . on ( 'error' , async function ( error : string ) {
155
- throw new Error ( error ) ;
165
+ client . on ( 'error' , ( error : Error ) => {
166
+ console . error ( 'Redis Client Error' , error ) ;
156
167
} ) ;
157
- await client . on ( 'connect' , async function ( ) {
168
+
169
+ try {
170
+ await client . connect ( ) ;
171
+
158
172
if ( keyArguments . elementsCount ) {
159
173
for ( let i = 0 ; i < keyArguments . elementsCount ; i ++ ) {
160
174
const element = `${ keyArguments . elementStartWith } ${ Common . generateWord ( 10 ) } ` ;
161
175
elements . push ( element ) ;
162
176
}
163
177
}
164
- await client . lpush ( keyArguments . keyName , elements , async ( error : string ) => {
165
- if ( error ) {
166
- throw error ;
167
- }
168
- } ) ;
169
- await client . quit ( ) ;
170
- } ) ;
178
+
179
+ if ( keyArguments . keyName ) {
180
+ await client . lPush ( keyArguments . keyName , elements ) ;
181
+ } else {
182
+ throw new Error ( 'keyName is required to populate the list.' ) ;
183
+ }
184
+
185
+ } catch ( error ) {
186
+ console . error ( 'Error pushing elements to list:' , error ) ;
187
+ } finally {
188
+ await client . disconnect ( ) ;
189
+ }
171
190
}
172
191
173
192
/**
@@ -177,27 +196,35 @@ export async function populateListWithElements(host: string, port: string, keyAr
177
196
* @param keyArguments The arguments of key and its members
178
197
*/
179
198
export async function populateSetWithMembers ( host : string , port : string , keyArguments : AddKeyArguments ) : Promise < void > {
180
- const dbConf = { port : Number . parseInt ( port ) , host, username : 'default' } ;
181
- const client = createClient ( dbConf ) ;
199
+ const url = `redis://default@ ${ host } : ${ port } ` ;
200
+ const client = createClient ( { url } ) ;
182
201
const members : string [ ] = [ ] ;
183
202
184
- await client . on ( 'error' , async function ( error : string ) {
185
- throw new Error ( error ) ;
203
+ client . on ( 'error' , ( error : Error ) => {
204
+ console . error ( 'Redis Client Error' , error ) ;
186
205
} ) ;
187
- await client . on ( 'connect' , async function ( ) {
206
+
207
+ try {
208
+ await client . connect ( ) ;
209
+
188
210
if ( keyArguments . membersCount ) {
189
211
for ( let i = 0 ; i < keyArguments . membersCount ; i ++ ) {
190
212
const member = `${ keyArguments . memberStartWith } ${ Common . generateWord ( 10 ) } ` ;
191
213
members . push ( member ) ;
192
214
}
193
215
}
194
- await client . sadd ( keyArguments . keyName , members , async ( error : string ) => {
195
- if ( error ) {
196
- throw error ;
197
- }
198
- } ) ;
199
- await client . quit ( ) ;
200
- } ) ;
216
+
217
+ if ( keyArguments . keyName ) {
218
+ await client . sAdd ( keyArguments . keyName , members ) ;
219
+ } else {
220
+ throw new Error ( 'keyName is required to populate the set.' ) ;
221
+ }
222
+
223
+ } catch ( error ) {
224
+ console . error ( 'Error adding members to set:' , error ) ;
225
+ } finally {
226
+ await client . disconnect ( ) ;
227
+ }
201
228
}
202
229
203
230
/**
@@ -207,30 +234,38 @@ export async function populateSetWithMembers(host: string, port: string, keyArgu
207
234
* @param keyArguments The arguments of key and its members
208
235
*/
209
236
export async function populateZSetWithMembers ( host : string , port : string , keyArguments : AddKeyArguments ) : Promise < void > {
210
- const dbConf = { port : Number . parseInt ( port ) , host, username : 'default' } ;
211
- let minScoreValue : - 10 ;
212
- let maxScoreValue : 10 ;
213
- const client = createClient ( dbConf ) ;
214
- const members : string [ ] = [ ] ;
237
+ const url = `redis://default@ ${ host } : ${ port } ` ;
238
+ const client = createClient ( { url } ) ;
239
+ const minScoreValue = - 10 ;
240
+ const maxScoreValue = 10 ;
241
+ const members : { score : number ; value : string } [ ] = [ ] ;
215
242
216
- await client . on ( 'error' , async function ( error : string ) {
217
- throw new Error ( error ) ;
243
+ client . on ( 'error' , ( error : Error ) => {
244
+ console . error ( 'Redis Client Error' , error ) ;
218
245
} ) ;
219
- await client . on ( 'connect' , async function ( ) {
246
+
247
+ try {
248
+ await client . connect ( ) ;
249
+
220
250
if ( keyArguments . membersCount ) {
221
251
for ( let i = 0 ; i < keyArguments . membersCount ; i ++ ) {
222
252
const memberName = `${ keyArguments . memberStartWith } ${ Common . generateWord ( 10 ) } ` ;
223
- const scoreValue = random ( minScoreValue , maxScoreValue ) . toString ( 2 ) ;
224
- members . push ( scoreValue , memberName ) ;
253
+ const scoreValue = random ( minScoreValue , maxScoreValue ) ;
254
+ members . push ( { score : scoreValue , value : memberName } ) ;
225
255
}
226
256
}
227
- await client . zadd ( keyArguments . keyName , members , async ( error : string ) => {
228
- if ( error ) {
229
- throw error ;
230
- }
231
- } ) ;
232
- await client . quit ( ) ;
233
- } ) ;
257
+
258
+ if ( keyArguments . keyName ) {
259
+ await client . zAdd ( keyArguments . keyName , members ) ;
260
+ } else {
261
+ throw new Error ( 'keyName is required to populate the sorted set.' ) ;
262
+ }
263
+
264
+ } catch ( error ) {
265
+ console . error ( 'Error adding members to sorted set:' , error ) ;
266
+ } finally {
267
+ await client . disconnect ( ) ;
268
+ }
234
269
}
235
270
236
271
/**
@@ -239,20 +274,23 @@ export async function populateZSetWithMembers(host: string, port: string, keyArg
239
274
* @param port The port of database
240
275
*/
241
276
export async function deleteAllKeysFromDB ( host : string , port : string ) : Promise < void > {
242
- const dbConf = { port : Number . parseInt ( port ) , host, username : 'default' } ;
243
- const client = createClient ( dbConf ) ;
277
+ const url = `redis://default@ ${ host } : ${ port } ` ;
278
+ const client = createClient ( { url } ) ;
244
279
245
- await client . on ( 'error' , async function ( error : string ) {
246
- throw new Error ( error ) ;
247
- } ) ;
248
- await client . on ( 'connect' , async function ( ) {
249
- await client . flushall ( ( error : string ) => {
250
- if ( error ) {
251
- throw error ;
252
- }
253
- } ) ;
254
- await client . quit ( ) ;
280
+ client . on ( 'error' , ( error : Error ) => {
281
+ console . error ( 'Redis Client Error' , error ) ;
255
282
} ) ;
283
+
284
+ try {
285
+ await client . connect ( ) ;
286
+
287
+ await client . flushAll ( ) ;
288
+
289
+ } catch ( error ) {
290
+ console . error ( 'Error flushing database:' , error ) ;
291
+ } finally {
292
+ await client . disconnect ( ) ;
293
+ }
256
294
}
257
295
258
296
/**
0 commit comments