Skip to content

Commit 5204417

Browse files
committed
fix multi "generic" type, some docs
1 parent 3ca33b8 commit 5204417

File tree

4 files changed

+66
-13
lines changed

4 files changed

+66
-13
lines changed

docs/clustering.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ await cluster.close();
4040
## Auth with password and username
4141

4242
Specifying the password in the URL or a root node will only affect the connection to that specific node. In case you want to set the password for all the connections being created from a cluster instance, use the `defaults` option.
43+
4344
```javascript
4445
createCluster({
4546
rootNodes: [{

docs/transactions.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# [Transactions](https://redis.io/docs/interact/transactions/) ([`MULTI`](https://redis.io/commands/multi/)/[`EXEC`](https://redis.io/commands/exec/))
2+
3+
Start a [transaction](https://redis.io/docs/interact/transactions/) by calling `.multi()`, then chaining your commands. When you're done, call `.exec()` and you'll get an array back with your results:
4+
5+
```javascript
6+
const [setReply, getReply] = await client.multi()
7+
.set('key', 'value')
8+
.get('another-key')
9+
.exec();
10+
```
11+
12+
## `exec<'typed'>()`/`execTyped()`
13+
14+
A transaction invoked with `.exec<'typed'>`/`execTyped()` will return types appropriate to the commands in the transaction:
15+
16+
```javascript
17+
const multi = client.multi().ping();
18+
await multi.exec(); // Array<ReplyUnion>
19+
await multi.exec<'typed'>(); // [string]
20+
await multi.execTyped(); // [string]
21+
```
22+
23+
> :warning: this only works when all the commands are invoked in a single "call chain"
24+
25+
## [`WATCH`](https://redis.io/commands/watch/)
26+
27+
You can also [watch](https://redis.io/docs/interact/transactions/#optimistic-locking-using-check-and-set) keys by calling `.watch()`. Your transaction will abort if any of the watched keys change.
28+
29+
The `WATCH` state is stored on the connection (by the server). In case you need to run multiple `WATCH` & `MULTI` in parallel you'll need to use a [pool](./pool.md).

packages/client/lib/multi-command.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import { CommandArguments, RedisScript, TransformReply } from './RESP/types';
1+
import { CommandArguments, RedisScript, ReplyUnion, TransformReply } from './RESP/types';
22

3-
// TODO: enum?
43
export type MULTI_REPLY = {
54
GENERIC: 'generic';
65
TYPED: 'typed';
76
};
87

98
export type MultiReply = MULTI_REPLY[keyof MULTI_REPLY];
109

11-
export type MultiReplyType<T extends MultiReply, REPLIES> = T extends MULTI_REPLY['TYPED'] ? REPLIES : Array<unknown>;
10+
export type MultiReplyType<T extends MultiReply, REPLIES> = T extends MULTI_REPLY['TYPED'] ? REPLIES : Array<ReplyUnion>;
1211

1312
export interface RedisMultiQueuedCommand {
1413
args: CommandArguments;

packages/redis/README.md

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ createClient({
5555

5656
You can also use discrete parameters, UNIX sockets, and even TLS to connect. Details can be found in the [client configuration guide](../../docs/client-configuration.md).
5757

58-
To check if the the client is connected and ready to send commands, use `client.isReady` which returns a boolean. `client.isOpen` is also available. This returns `true` when the client's underlying socket is open, and `false` when it isn't (for example when the client is still connecting or reconnecting after a network error).
58+
To client exposes 2 `boolean`s that track the client state:
59+
1. `isOpen` - the client is either connecting or connected.
60+
2. `isReady` - the client is connected and ready to send
5961

6062
### Redis Commands
6163

@@ -97,19 +99,17 @@ await client.hVals('key'); // ['value1', 'value2']
9799
If you want to run commands and/or use arguments that Node Redis doesn't know about (yet!) use `.sendCommand()`:
98100

99101
```javascript
100-
await client.sendCommand(['SET', 'key', 'value', 'NX']); // 'OK'
101-
102+
await client.sendCommand(['SET', 'key', 'value', 'EX', '10', 'NX']); // 'OK'
102103
await client.sendCommand(['HGETALL', 'key']); // ['key1', 'field1', 'key2', 'field2']
103104
```
104105

105106
### Disconnecting
106107

107-
There are two functions that disconnect a client from the Redis server. In most scenarios you should use `.close()` to ensure that pending commands are sent to Redis before closing a connection.
108-
109-
> :warning: The `.quit()` and `.disconnect()` methods have been deprecated in v5. For more details, refer to the [v4-to-v5 guide](../../docs/v4-to-v5.md#quit-vs-disconnect).
110-
111108
#### `.close()`
112109

110+
Gracefully close a client's connection to Redis.
111+
Wait for commands in process, but reject any new commands.
112+
113113
```javascript
114114
const [ping, get] = await Promise.all([
115115
client.ping(),
@@ -124,11 +124,34 @@ try {
124124
}
125125
```
126126

127-
> :warning: `.close` is just like `.quit()` which was depreacted in Redis 7.2. See the [relevant section in the migration guide](../../docs/v4-to-v5.md#Quit-VS-Disconnect) for more information.
127+
> `.close()` is just like `.quit()` which was depreacted v5. See the [relevant section in the migration guide](../../docs/v4-to-v5.md#Quit-VS-Disconnect) for more information.
128128
129129
#### `.destroy()`
130130

131-
Forcibly close a client's connection to Redis immediately. Calling `destroy` will not send further pending commands to the Redis server, or wait for or parse outstanding responses.
131+
Forcibly close a client's connection to Redis.
132+
133+
```javascript
134+
try {
135+
const promise = Promise.all([
136+
client.ping(),
137+
client.get('key')
138+
]);
139+
140+
client.destroy();
141+
142+
await promise;
143+
} catch (err) {
144+
// DisconnectsClientError
145+
}
146+
147+
try {
148+
await client.get('key');
149+
} catch (err) {
150+
// ClientClosedError
151+
}
152+
```
153+
154+
> `.destroy()` is just like `.disconnect()` which was depreated in v5. See the [relevant section in the migration guide](../../docs/v4-to-v5.md#Quit-VS-Disconnect) for more information.
132155
133156
### Auto-Pipelining
134157

@@ -167,7 +190,7 @@ The Node Redis client class is an Nodejs EventEmitter and it emits an event each
167190
168191
### Links
169192

170-
- [Multi](../../docs/multi.md).
193+
- [Transactions (`MULTI`/`EXEC`)](../../docs/transactions.md).
171194
- [Pub/Sub](../../docs/pub-sub.md).
172195
- [Scan Iterators](../../docs/scan-iterators.md).
173196
- [Programmability](../../docs/programmability.md).
@@ -181,6 +204,7 @@ Node Redis is supported with the following versions of Redis:
181204

182205
| Version | Supported |
183206
|---------|--------------------|
207+
| 7.2.z | :heavy_check_mark: |
184208
| 7.0.z | :heavy_check_mark: |
185209
| 6.2.z | :heavy_check_mark: |
186210
| 6.0.z | :heavy_check_mark: |

0 commit comments

Comments
 (0)