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