forked from valkey-io/valkey-glide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransaction.ts
More file actions
3556 lines (3353 loc) · 146 KB
/
Transaction.ts
File metadata and controls
3556 lines (3353 loc) · 146 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0
*/
import {
BaseClient, // eslint-disable-line @typescript-eslint/no-unused-vars
ReadFrom, // eslint-disable-line @typescript-eslint/no-unused-vars
} from "./BaseClient";
import {
AggregationType,
BaseScanOptions,
BitFieldGet,
BitFieldIncrBy, // eslint-disable-line @typescript-eslint/no-unused-vars
BitFieldOverflow, // eslint-disable-line @typescript-eslint/no-unused-vars
BitFieldSet, // eslint-disable-line @typescript-eslint/no-unused-vars
BitFieldSubCommands,
BitOffset, // eslint-disable-line @typescript-eslint/no-unused-vars
BitOffsetMultiplier, // eslint-disable-line @typescript-eslint/no-unused-vars
BitOffsetOptions,
BitmapIndexType,
BitwiseOperation,
CoordOrigin, // eslint-disable-line @typescript-eslint/no-unused-vars
ExpireOptions,
FlushMode,
FunctionListOptions,
FunctionListResponse, // eslint-disable-line @typescript-eslint/no-unused-vars
FunctionStatsResponse, // eslint-disable-line @typescript-eslint/no-unused-vars
GeoAddOptions,
GeoBoxShape, // eslint-disable-line @typescript-eslint/no-unused-vars
GeoCircleShape, // eslint-disable-line @typescript-eslint/no-unused-vars
GeoSearchResultOptions,
GeoSearchShape,
GeoUnit,
GeospatialData,
InfoOptions,
InsertPosition,
KeyWeight,
LPosOptions,
ListDirection,
LolwutOptions,
MemberOrigin, // eslint-disable-line @typescript-eslint/no-unused-vars
RangeByIndex,
RangeByLex,
RangeByScore,
ReturnTypeXinfoStream, // eslint-disable-line @typescript-eslint/no-unused-vars
ScoreBoundary,
ScoreFilter,
SearchOrigin,
SetOptions,
SortClusterOptions,
SortOptions,
StreamAddOptions,
StreamClaimOptions,
StreamGroupOptions,
StreamReadOptions,
StreamTrimOptions,
ZAddOptions,
createAppend,
createBLMPop,
createBLMove,
createBLPop,
createBRPop,
createBZMPop,
createBZPopMax,
createBZPopMin,
createBitCount,
createBitField,
createBitOp,
createBitPos,
createClientGetName,
createClientId,
createConfigGet,
createConfigResetStat,
createConfigRewrite,
createConfigSet,
createCopy,
createCustomCommand,
createDBSize,
createDecr,
createDecrBy,
createDel,
createEcho,
createExists,
createExpire,
createExpireAt,
createExpireTime,
createFCall,
createFCallReadOnly,
createFlushAll,
createFlushDB,
createFunctionDelete,
createFunctionFlush,
createFunctionList,
createFunctionLoad,
createFunctionStats,
createGeoAdd,
createGeoDist,
createGeoHash,
createGeoPos,
createGeoSearch,
createGeoSearchStore,
createGet,
createGetBit,
createGetDel,
createGetRange,
createHDel,
createHExists,
createHGet,
createHGetAll,
createHIncrBy,
createHIncrByFloat,
createHLen,
createHMGet,
createHRandField,
createHSet,
createHSetNX,
createHStrlen,
createHVals,
createIncr,
createIncrBy,
createIncrByFloat,
createInfo,
createLCS,
createLIndex,
createLInsert,
createLLen,
createLMPop,
createLMove,
createLPop,
createLPos,
createLPush,
createLPushX,
createLRange,
createLRem,
createLSet,
createLTrim,
createLastSave,
createLolwut,
createMGet,
createMSet,
createMSetNX,
createObjectEncoding,
createObjectFreq,
createObjectIdletime,
createObjectRefcount,
createPExpire,
createPExpireAt,
createPExpireTime,
createPTTL,
createPersist,
createPfAdd,
createPfCount,
createPfMerge,
createPing,
createPubSubChannels,
createPubSubNumPat,
createPubSubNumSub,
createPubSubShardNumSub,
createPublish,
createPubsubShardChannels,
createRPop,
createRPush,
createRPushX,
createRandomKey,
createRename,
createRenameNX,
createSAdd,
createSCard,
createSDiff,
createSDiffStore,
createSInter,
createSInterCard,
createSInterStore,
createSIsMember,
createSMIsMember,
createSMembers,
createSMove,
createSPop,
createSRandMember,
createSRem,
createSUnion,
createSUnionStore,
createSelect,
createSet,
createSetBit,
createSetRange,
createSort,
createSortReadOnly,
createStrlen,
createTTL,
createTime,
createTouch,
createType,
createUnlink,
createXAdd,
createXClaim,
createXDel,
createXInfoConsumers,
createXInfoStream,
createXLen,
createXRead,
createXTrim,
createXGroupCreate,
createXGroupDestroy,
createZAdd,
createZCard,
createZCount,
createZDiff,
createZDiffStore,
createZDiffWithScores,
createZIncrBy,
createZInterCard,
createZInterstore,
createZLexCount,
createZMPop,
createZMScore,
createZPopMax,
createZPopMin,
createZRandMember,
createZRange,
createZRangeWithScores,
createZRank,
createZRem,
createZRemRangeByLex,
createZRemRangeByRank,
createZRemRangeByScore,
createZRevRank,
createZRevRankWithScore,
createZScan,
createZScore,
createXPending,
GeoSearchStoreResultOptions,
StreamPendingOptions,
} from "./Commands";
import { command_request } from "./ProtobufMessage";
/**
* Base class encompassing shared commands for both standalone and cluster mode implementations in a transaction.
* Transactions allow the execution of a group of commands in a single step.
*
* Command Response:
* An array of command responses is returned by the client exec command, in the order they were given.
* Each element in the array represents a command given to the transaction.
* The response for each command depends on the executed Redis command.
* Specific response types are documented alongside each method.
*
* @example
* ```typescript
* const transaction = new BaseTransaction()
* .set("key", "value")
* .get("key");
* const result = await client.exec(transaction);
* console.log(result); // Output: ['OK', 'value']
* ```
*/
export class BaseTransaction<T extends BaseTransaction<T>> {
/**
* @internal
*/
readonly commands: command_request.Command[] = [];
/**
* Array of command indexes indicating commands that need to be converted into a `Set` within the transaction.
* @internal
*/
readonly setCommandsIndexes: number[] = [];
/**
* Adds a command to the transaction and returns the transaction instance.
* @param command - The command to add.
* @param shouldConvertToSet - Indicates if the command should be converted to a `Set`.
* @returns The updated transaction instance.
*/
protected addAndReturn(
command: command_request.Command,
shouldConvertToSet: boolean = false,
): T {
if (shouldConvertToSet) {
// The command's index within the transaction is saved for later conversion of its response to a Set type.
this.setCommandsIndexes.push(this.commands.length);
}
this.commands.push(command);
return this as unknown as T;
}
/** Get the value associated with the given key, or null if no such value exists.
* See https://valkey.io/commands/get/ for details.
*
* @param key - The key to retrieve from the database.
*
* Command Response - If `key` exists, returns the value of `key` as a string. Otherwise, return null.
*/
public get(key: string): T {
return this.addAndReturn(createGet(key));
}
/**
* Gets a string value associated with the given `key`and deletes the key.
*
* See https://valkey.io/commands/getdel/ for details.
*
* @param key - The key to retrieve from the database.
*
* Command Response - If `key` exists, returns the `value` of `key`. Otherwise, return `null`.
*/
public getdel(key: string): T {
return this.addAndReturn(createGetDel(key));
}
/**
* Returns the substring of the string value stored at `key`, determined by the offsets
* `start` and `end` (both are inclusive). Negative offsets can be used in order to provide
* an offset starting from the end of the string. So `-1` means the last character, `-2` the
* penultimate and so forth. If `key` does not exist, an empty string is returned. If `start`
* or `end` are out of range, returns the substring within the valid range of the string.
*
* See https://valkey.io/commands/getrange/ for details.
*
* @param key - The key of the string.
* @param start - The starting offset.
* @param end - The ending offset.
*
* Command Response - substring extracted from the value stored at `key`.
*/
public getrange(key: string, start: number, end: number): T {
return this.addAndReturn(createGetRange(key, start, end));
}
/** Set the given key with the given value. Return value is dependent on the passed options.
* See https://valkey.io/commands/set/ for details.
*
* @param key - The key to store.
* @param value - The value to store with the given key.
* @param options - The set options.
*
* Command Response - If the value is successfully set, return OK.
* If `value` isn't set because of `onlyIfExists` or `onlyIfDoesNotExist` conditions, return null.
* If `returnOldValue` is set, return the old value as a string.
*/
public set(key: string, value: string, options?: SetOptions): T {
return this.addAndReturn(createSet(key, value, options));
}
/** Ping the Redis server.
* See https://valkey.io/commands/ping/ for details.
*
* @param message - An optional message to include in the PING command.
* If not provided, the server will respond with "PONG".
* If provided, the server will respond with a copy of the message.
*
* Command Response - "PONG" if `message` is not provided, otherwise return a copy of `message`.
*/
public ping(message?: string): T {
return this.addAndReturn(createPing(message));
}
/** Get information and statistics about the Redis server.
* See https://valkey.io/commands/info/ for details.
*
* @param options - A list of InfoSection values specifying which sections of information to retrieve.
* When no parameter is provided, the default option is assumed.
*
* Command Response - a string containing the information for the sections requested.
*/
public info(options?: InfoOptions[]): T {
return this.addAndReturn(createInfo(options));
}
/** Remove the specified keys. A key is ignored if it does not exist.
* See https://valkey.io/commands/del/ for details.
*
* @param keys - A list of keys to be deleted from the database.
*
* Command Response - the number of keys that were removed.
*/
public del(keys: string[]): T {
return this.addAndReturn(createDel(keys));
}
/** Get the name of the connection on which the transaction is being executed.
* See https://valkey.io/commands/client-getname/ for more details.
*
* Command Response - the name of the client connection as a string if a name is set, or null if no name is assigned.
*/
public clientGetName(): T {
return this.addAndReturn(createClientGetName());
}
/** Rewrite the configuration file with the current configuration.
* See https://valkey.io/commands/select/ for details.
*
* Command Response - "OK" when the configuration was rewritten properly. Otherwise, the transaction fails with an error.
*/
public configRewrite(): T {
return this.addAndReturn(createConfigRewrite());
}
/** Resets the statistics reported by Redis using the INFO and LATENCY HISTOGRAM commands.
* See https://valkey.io/commands/config-resetstat/ for details.
*
* Command Response - always "OK".
*/
public configResetStat(): T {
return this.addAndReturn(createConfigResetStat());
}
/** Retrieve the values of multiple keys.
* See https://valkey.io/commands/mget/ for details.
*
* @param keys - A list of keys to retrieve values for.
*
* Command Response - A list of values corresponding to the provided keys. If a key is not found,
* its corresponding value in the list will be null.
*/
public mget(keys: string[]): T {
return this.addAndReturn(createMGet(keys));
}
/** Set multiple keys to multiple values in a single atomic operation.
* See https://valkey.io/commands/mset/ for details.
*
* @param keyValueMap - A key-value map consisting of keys and their respective values to set.
*
* Command Response - always "OK".
*/
public mset(keyValueMap: Record<string, string>): T {
return this.addAndReturn(createMSet(keyValueMap));
}
/**
* Sets multiple keys to values if the key does not exist. The operation is atomic, and if one or
* more keys already exist, the entire operation fails.
*
* See https://valkey.io/commands/msetnx/ for more details.
*
* @param keyValueMap - A key-value map consisting of keys and their respective values to set.
* Command Response - `true` if all keys were set. `false` if no key was set.
*/
public msetnx(keyValueMap: Record<string, string>): T {
return this.addAndReturn(createMSetNX(keyValueMap));
}
/** Increments the number stored at `key` by one. If `key` does not exist, it is set to 0 before performing the operation.
* See https://valkey.io/commands/incr/ for details.
*
* @param key - The key to increment its value.
*
* Command Response - the value of `key` after the increment.
*/
public incr(key: string): T {
return this.addAndReturn(createIncr(key));
}
/** Increments the number stored at `key` by `amount`. If `key` does not exist, it is set to 0 before performing the operation.
* See https://valkey.io/commands/incrby/ for details.
*
* @param key - The key to increment its value.
* @param amount - The amount to increment.
*
* Command Response - the value of `key` after the increment.
*/
public incrBy(key: string, amount: number): T {
return this.addAndReturn(createIncrBy(key, amount));
}
/** Increment the string representing a floating point number stored at `key` by `amount`.
* By using a negative amount value, the result is that the value stored at `key` is decremented.
* If `key` does not exist, it is set to 0 before performing the operation.
* See https://valkey.io/commands/incrbyfloat/ for details.
*
* @param key - The key to increment its value.
* @param amount - The amount to increment.
*
* Command Response - the value of `key` after the increment.
*
*/
public incrByFloat(key: string, amount: number): T {
return this.addAndReturn(createIncrByFloat(key, amount));
}
/** Returns the current connection id.
* See https://valkey.io/commands/client-id/ for details.
*
* Command Response - the id of the client.
*/
public clientId(): T {
return this.addAndReturn(createClientId());
}
/** Decrements the number stored at `key` by one. If `key` does not exist, it is set to 0 before performing the operation.
* See https://valkey.io/commands/decr/ for details.
*
* @param key - The key to decrement its value.
*
* Command Response - the value of `key` after the decrement.
*/
public decr(key: string): T {
return this.addAndReturn(createDecr(key));
}
/** Decrements the number stored at `key` by `amount`. If `key` does not exist, it is set to 0 before performing the operation.
* See https://valkey.io/commands/decrby/ for details.
*
* @param key - The key to decrement its value.
* @param amount - The amount to decrement.
*
* Command Response - the value of `key` after the decrement.
*/
public decrBy(key: string, amount: number): T {
return this.addAndReturn(createDecrBy(key, amount));
}
/**
* Perform a bitwise operation between multiple keys (containing string values) and store the result in the
* `destination`.
*
* See https://valkey.io/commands/bitop/ for more details.
*
* @param operation - The bitwise operation to perform.
* @param destination - The key that will store the resulting string.
* @param keys - The list of keys to perform the bitwise operation on.
*
* Command Response - The size of the string stored in `destination`.
*/
public bitop(
operation: BitwiseOperation,
destination: string,
keys: string[],
): T {
return this.addAndReturn(createBitOp(operation, destination, keys));
}
/**
* Returns the bit value at `offset` in the string value stored at `key`. `offset` must be greater than or equal
* to zero.
*
* See https://valkey.io/commands/getbit/ for more details.
*
* @param key - The key of the string.
* @param offset - The index of the bit to return.
*
* Command Response - The bit at the given `offset` of the string. Returns `0` if the key is empty or if the
* `offset` exceeds the length of the string.
*/
public getbit(key: string, offset: number): T {
return this.addAndReturn(createGetBit(key, offset));
}
/**
* Sets or clears the bit at `offset` in the string value stored at `key`. The `offset` is a zero-based index, with
* `0` being the first element of the list, `1` being the next element, and so on. The `offset` must be less than
* `2^32` and greater than or equal to `0`. If a key is non-existent then the bit at `offset` is set to `value` and
* the preceding bits are set to `0`.
*
* See https://valkey.io/commands/setbit/ for more details.
*
* @param key - The key of the string.
* @param offset - The index of the bit to be set.
* @param value - The bit value to set at `offset`. The value must be `0` or `1`.
*
* Command Response - The bit value that was previously stored at `offset`.
*/
public setbit(key: string, offset: number, value: number): T {
return this.addAndReturn(createSetBit(key, offset, value));
}
/**
* Returns the position of the first bit matching the given `bit` value. The optional starting offset
* `start` is a zero-based index, with `0` being the first byte of the list, `1` being the next byte and so on.
* The offset can also be a negative number indicating an offset starting at the end of the list, with `-1` being
* the last byte of the list, `-2` being the penultimate, and so on.
*
* See https://valkey.io/commands/bitpos/ for more details.
*
* @param key - The key of the string.
* @param bit - The bit value to match. Must be `0` or `1`.
* @param start - (Optional) The starting offset. If not supplied, the search will start at the beginning of the string.
*
* Command Response - The position of the first occurrence of `bit` in the binary value of the string held at `key`.
* If `start` was provided, the search begins at the offset indicated by `start`.
*/
public bitpos(key: string, bit: number, start?: number): T {
return this.addAndReturn(createBitPos(key, bit, start));
}
/**
* Returns the position of the first bit matching the given `bit` value. The offsets are zero-based indexes, with
* `0` being the first element of the list, `1` being the next, and so on. These offsets can also be negative
* numbers indicating offsets starting at the end of the list, with `-1` being the last element of the list, `-2`
* being the penultimate, and so on.
*
* If you are using Valkey 7.0.0 or above, the optional `indexType` can also be provided to specify whether the
* `start` and `end` offsets specify BIT or BYTE offsets. If `indexType` is not provided, BYTE offsets
* are assumed. If BIT is specified, `start=0` and `end=2` means to look at the first three bits. If BYTE is
* specified, `start=0` and `end=2` means to look at the first three bytes.
*
* See https://valkey.io/commands/bitpos/ for more details.
*
* @param key - The key of the string.
* @param bit - The bit value to match. Must be `0` or `1`.
* @param start - The starting offset.
* @param end - The ending offset.
* @param indexType - (Optional) The index offset type. This option can only be specified if you are using Valkey
* version 7.0.0 or above. Could be either {@link BitmapIndexType.BYTE} or {@link BitmapIndexType.BIT}. If no
* index type is provided, the indexes will be assumed to be byte indexes.
*
* Command Response - The position of the first occurrence from the `start` to the `end` offsets of the `bit` in the
* binary value of the string held at `key`.
*/
public bitposInterval(
key: string,
bit: number,
start: number,
end: number,
indexType?: BitmapIndexType,
): T {
return this.addAndReturn(createBitPos(key, bit, start, end, indexType));
}
/**
* Reads or modifies the array of bits representing the string that is held at `key` based on the specified
* `subcommands`.
*
* See https://valkey.io/commands/bitfield/ for more details.
*
* @param key - The key of the string.
* @param subcommands - The subcommands to be performed on the binary value of the string at `key`, which could be
* any of the following:
*
* - {@link BitFieldGet}
* - {@link BitFieldSet}
* - {@link BitFieldIncrBy}
* - {@link BitFieldOverflow}
*
* Command Response - An array of results from the executed subcommands:
*
* - {@link BitFieldGet} returns the value in {@link BitOffset} or {@link BitOffsetMultiplier}.
* - {@link BitFieldSet} returns the old value in {@link BitOffset} or {@link BitOffsetMultiplier}.
* - {@link BitFieldIncrBy} returns the new value in {@link BitOffset} or {@link BitOffsetMultiplier}.
* - {@link BitFieldOverflow} determines the behavior of the {@link BitFieldSet} and {@link BitFieldIncrBy}
* subcommands when an overflow or underflow occurs. {@link BitFieldOverflow} does not return a value and
* does not contribute a value to the array response.
*/
public bitfield(key: string, subcommands: BitFieldSubCommands[]): T {
return this.addAndReturn(createBitField(key, subcommands));
}
/**
* Reads the array of bits representing the string that is held at `key` based on the specified `subcommands`.
*
* See https://valkey.io/commands/bitfield_ro/ for more details.
*
* @param key - The key of the string.
* @param subcommands - The {@link BitFieldGet} subcommands to be performed.
*
* Command Response - An array of results from the {@link BitFieldGet} subcommands.
*
* since Valkey version 6.0.0.
*/
public bitfieldReadOnly(key: string, subcommands: BitFieldGet[]): T {
return this.addAndReturn(createBitField(key, subcommands, true));
}
/** Reads the configuration parameters of a running Redis server.
* See https://valkey.io/commands/config-get/ for details.
*
* @param parameters - A list of configuration parameter names to retrieve values for.
*
* Command Response - A map of values corresponding to the configuration parameters.
*
*/
public configGet(parameters: string[]): T {
return this.addAndReturn(createConfigGet(parameters));
}
/** Set configuration parameters to the specified values.
* See https://valkey.io/commands/config-set/ for details.
*
* @param parameters - A List of keyValuePairs consisting of configuration parameters and their respective values to set.
*
* Command Response - "OK" when the configuration was set properly. Otherwise, the transaction fails with an error.
*/
public configSet(parameters: Record<string, string>): T {
return this.addAndReturn(createConfigSet(parameters));
}
/** Retrieve the value associated with `field` in the hash stored at `key`.
* See https://valkey.io/commands/hget/ for details.
*
* @param key - The key of the hash.
* @param field - The field in the hash stored at `key` to retrieve from the database.
*
* Command Response - the value associated with `field`, or null when `field` is not present in the hash or `key` does not exist.
*/
public hget(key: string, field: string): T {
return this.addAndReturn(createHGet(key, field));
}
/** Sets the specified fields to their respective values in the hash stored at `key`.
* See https://valkey.io/commands/hset/ for details.
*
* @param key - The key of the hash.
* @param fieldValueMap - A field-value map consisting of fields and their corresponding values
* to be set in the hash stored at the specified key.
*
* Command Response - The number of fields that were added.
*/
public hset(key: string, fieldValueMap: Record<string, string>): T {
return this.addAndReturn(createHSet(key, fieldValueMap));
}
/** Sets `field` in the hash stored at `key` to `value`, only if `field` does not yet exist.
* If `key` does not exist, a new key holding a hash is created.
* If `field` already exists, this operation has no effect.
* See https://valkey.io/commands/hsetnx/ for more details.
*
* @param key - The key of the hash.
* @param field - The field to set the value for.
* @param value - The value to set.
*
* Command Response - `true` if the field was set, `false` if the field already existed and was not set.
*/
public hsetnx(key: string, field: string, value: string): T {
return this.addAndReturn(createHSetNX(key, field, value));
}
/** Removes the specified fields from the hash stored at `key`.
* Specified fields that do not exist within this hash are ignored.
* See https://valkey.io/commands/hdel/ for details.
*
* @param key - The key of the hash.
* @param fields - The fields to remove from the hash stored at `key`.
*
* Command Response - the number of fields that were removed from the hash, not including specified but non existing fields.
* If `key` does not exist, it is treated as an empty hash and it returns 0.
*/
public hdel(key: string, fields: string[]): T {
return this.addAndReturn(createHDel(key, fields));
}
/** Returns the values associated with the specified fields in the hash stored at `key`.
* See https://valkey.io/commands/hmget/ for details.
*
* @param key - The key of the hash.
* @param fields - The fields in the hash stored at `key` to retrieve from the database.
*
* Command Response - a list of values associated with the given fields, in the same order as they are requested.
* For every field that does not exist in the hash, a null value is returned.
* If `key` does not exist, it is treated as an empty hash and it returns a list of null values.
*/
public hmget(key: string, fields: string[]): T {
return this.addAndReturn(createHMGet(key, fields));
}
/** Returns if `field` is an existing field in the hash stored at `key`.
* See https://valkey.io/commands/hexists/ for details.
*
* @param key - The key of the hash.
* @param field - The field to check in the hash stored at `key`.
*
* Command Response - `true` if the hash contains `field`. If the hash does not contain `field`, or if `key` does not exist,
* the command response will be `false`.
*/
public hexists(key: string, field: string): T {
return this.addAndReturn(createHExists(key, field));
}
/** Returns all fields and values of the hash stored at `key`.
* See https://valkey.io/commands/hgetall/ for details.
*
* @param key - The key of the hash.
*
* Command Response - a map of fields and their values stored in the hash. Every field name in the map is followed by its value.
* If `key` does not exist, it returns an empty map.
*/
public hgetall(key: string): T {
return this.addAndReturn(createHGetAll(key));
}
/** Increments the number stored at `field` in the hash stored at `key` by `increment`.
* By using a negative increment value, the value stored at `field` in the hash stored at `key` is decremented.
* If `field` or `key` does not exist, it is set to 0 before performing the operation.
* See https://valkey.io/commands/hincrby/ for details.
*
* @param key - The key of the hash.
* @param amount - The amount to increment.
* @param field - The field in the hash stored at `key` to increment its value.
*
* Command Response - the value of `field` in the hash stored at `key` after the increment.
*/
public hincrBy(key: string, field: string, amount: number): T {
return this.addAndReturn(createHIncrBy(key, field, amount));
}
/** Increment the string representing a floating point number stored at `field` in the hash stored at `key` by `increment`.
* By using a negative increment value, the value stored at `field` in the hash stored at `key` is decremented.
* If `field` or `key` does not exist, it is set to 0 before performing the operation.
* See https://valkey.io/commands/hincrbyfloat/ for details.
*
* @param key - The key of the hash.
* @param amount - The amount to increment.
* @param field - The field in the hash stored at `key` to increment its value.
*
* Command Response - the value of `field` in the hash stored at `key` after the increment.
*/
public hincrByFloat(key: string, field: string, amount: number): T {
return this.addAndReturn(createHIncrByFloat(key, field, amount));
}
/** Returns the number of fields contained in the hash stored at `key`.
* See https://valkey.io/commands/hlen/ for more details.
*
* @param key - The key of the hash.
*
* Command Response - The number of fields in the hash, or 0 when the key does not exist.
*/
public hlen(key: string): T {
return this.addAndReturn(createHLen(key));
}
/** Returns all values in the hash stored at key.
* See https://valkey.io/commands/hvals/ for more details.
*
* @param key - The key of the hash.
*
* Command Response - a list of values in the hash, or an empty list when the key does not exist.
*/
public hvals(key: string): T {
return this.addAndReturn(createHVals(key));
}
/**
* Returns the string length of the value associated with `field` in the hash stored at `key`.
*
* See https://valkey.io/commands/hstrlen/ for details.
*
* @param key - The key of the hash.
* @param field - The field in the hash.
*
* Command Response - The string length or `0` if `field` or `key` does not exist.
*/
public hstrlen(key: string, field: string): T {
return this.addAndReturn(createHStrlen(key, field));
}
/**
* Returns a random field name from the hash value stored at `key`.
*
* See https://valkey.io/commands/hrandfield/ for more details.
*
* since Valkey version 6.2.0.
*
* @param key - The key of the hash.
*
* Command Response - A random field name from the hash stored at `key`, or `null` when
* the key does not exist.
*/
public hrandfield(key: string): T {
return this.addAndReturn(createHRandField(key));
}
/**
* Retrieves up to `count` random field names from the hash value stored at `key`.
*
* See https://valkey.io/commands/hrandfield/ for more details.
*
* since Valkey version 6.2.0.
*
* @param key - The key of the hash.
* @param count - The number of field names to return.
*
* If `count` is positive, returns unique elements. If negative, allows for duplicates.
*
* Command Response - An `array` of random field names from the hash stored at `key`,
* or an `empty array` when the key does not exist.
*/
public hrandfieldCount(key: string, count: number): T {
return this.addAndReturn(createHRandField(key, count));
}
/**
* Retrieves up to `count` random field names along with their values from the hash
* value stored at `key`.
*
* See https://valkey.io/commands/hrandfield/ for more details.
*
* since Valkey version 6.2.0.
*
* @param key - The key of the hash.
* @param count - The number of field names to return.
*
* If `count` is positive, returns unique elements. If negative, allows for duplicates.
*
* Command Response - A 2D `array` of `[fieldName, value]` `arrays`, where `fieldName` is a random
* field name from the hash and `value` is the associated value of the field name.
* If the hash does not exist or is empty, the response will be an empty `array`.
*/
public hrandfieldWithValues(key: string, count: number): T {
return this.addAndReturn(createHRandField(key, count, true));
}
/** Inserts all the specified values at the head of the list stored at `key`.
* `elements` are inserted one after the other to the head of the list, from the leftmost element to the rightmost element.
* If `key` does not exist, it is created as empty list before performing the push operations.
* See https://valkey.io/commands/lpush/ for details.
*
* @param key - The key of the list.
* @param elements - The elements to insert at the head of the list stored at `key`.
*
* Command Response - the length of the list after the push operations.
*/
public lpush(key: string, elements: string[]): T {
return this.addAndReturn(createLPush(key, elements));
}
/**
* Inserts specified values at the head of the `list`, only if `key` already
* exists and holds a list.
*
* See https://valkey.io/commands/lpushx/ for details.
*
* @param key - The key of the list.
* @param elements - The elements to insert at the head of the list stored at `key`.
*
* Command Response - The length of the list after the push operation.
*/
public lpushx(key: string, elements: string[]): T {
return this.addAndReturn(createLPushX(key, elements));
}
/** Removes and returns the first elements of the list stored at `key`.
* The command pops a single element from the beginning of the list.
* See https://valkey.io/commands/lpop/ for details.
*
* @param key - The key of the list.
*
* Command Response - The value of the first element.
* If `key` does not exist null will be returned.
*/
public lpop(key: string): T {
return this.addAndReturn(createLPop(key));
}
/** Removes and returns up to `count` elements of the list stored at `key`, depending on the list's length.
* See https://valkey.io/commands/lpop/ for details.
*
* @param key - The key of the list.
* @param count - The count of the elements to pop from the list.
*
* Command Response - A list of the popped elements will be returned depending on the list's length.
* If `key` does not exist null will be returned.
*/
public lpopCount(key: string, count: number): T {
return this.addAndReturn(createLPop(key, count));
}
/** Returns the specified elements of the list stored at `key`.
* The offsets `start` and `end` are zero-based indexes, with 0 being the first element of the list, 1 being the next element and so on.
* These offsets can also be negative numbers indicating offsets starting at the end of the list,
* with -1 being the last element of the list, -2 being the penultimate, and so on.
* See https://valkey.io/commands/lrange/ for details.
*
* @param key - The key of the list.
* @param start - The starting point of the range.
* @param end - The end of the range.
*
* Command Response - list of elements in the specified range.
* If `start` exceeds the end of the list, or if `start` is greater than `end`, an empty list will be returned.
* If `end` exceeds the actual end of the list, the range will stop at the actual end of the list.
* If `key` does not exist an empty list will be returned.
*/
public lrange(key: string, start: number, end: number): T {
return this.addAndReturn(createLRange(key, start, end));
}
/** Returns the length of the list stored at `key`.
* See https://valkey.io/commands/llen/ for details.
*
* @param key - The key of the list.
*
* Command Response - the length of the list at `key`.
* If `key` does not exist, it is interpreted as an empty list and 0 is returned.
*/
public llen(key: string): T {
return this.addAndReturn(createLLen(key));
}
/**
* Atomically pops and removes the left/right-most element to the list stored at `source`
* depending on `whereFrom`, and pushes the element at the first/last element of the list
* stored at `destination` depending on `whereTo`, see {@link ListDirection}.
*
* See https://valkey.io/commands/lmove/ for details.
*
* @param source - The key to the source list.
* @param destination - The key to the destination list.
* @param whereFrom - The {@link ListDirection} to remove the element from.
* @param whereTo - The {@link ListDirection} to add the element to.
*