Skip to content

Commit fa47b57

Browse files
Simon Prickettleibale
Simon Prickett
andauthored
Adds example for mixing arbitrary and supported commands in a transaction context. (#2315)
* Adds example for transactions with arbitrary commands. * Formatting. * Update transaction-with-arbitrary-commands.js Co-authored-by: Leibale Eidelman <me@leibale.com>
1 parent 63511e5 commit fa47b57

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

examples/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ This folder contains example scripts showing how to use Node Redis in different
2727
| `time-series.js` | Create, populate and query timeseries data with [Redis Timeseries](https://redistimeseries.io). |
2828
| `topk.js` | Use the [RedisBloom](https://redisbloom.io) TopK to track the most frequently seen items. |
2929
| `stream-consumer-group.js` | Reads entries from a [Redis Stream](https://redis.io/topics/streams-intro) as part of a consumer group using the blocking `XREADGROUP` command. |
30-
| `tranaaction-with-arbitrary-commands.js` | Mix and match supported commands with arbitrary command strings in a Redis transaction. |
30+
| `transaction-with-arbitrary-commands.js` | Mix and match supported commands with arbitrary command strings in a Redis transaction. |
3131
| `transaction-with-watch.js` | An Example of [Redis transaction](https://redis.io/docs/manual/transactions) with `WATCH` command on isolated connection with optimistic locking. |
3232

3333
## Contributing
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// How to mix and match supported commands that have named functions with
2+
// commands sent as arbitrary strings in the same transaction context.
3+
// Use this when working with new Redis commands that haven't been added to
4+
// node-redis yet, or when working with commands that have been added to Redis
5+
// by modules other than those directly supported by node-redis.
6+
7+
import { createClient } from 'redis';
8+
9+
const client = createClient();
10+
11+
await client.connect();
12+
13+
// Build some data fixtures.
14+
await Promise.all([
15+
client.hSet('hash1', { name: 'hash1', number: 1}),
16+
client.hSet('hash2', { name: 'hash2', number: 1}),
17+
client.hSet('hash3', { name: 'hash3', number: 3})
18+
]);
19+
20+
// Outside of a transaction, use sendCommand to send arbitrary commands.
21+
await client.sendCommand(['hset', 'hash2', 'number', '3']);
22+
23+
// In a transaction context, use addCommand to send arbitrary commands.
24+
// addCommand can be mixed and matched with named command functions as
25+
// shown.
26+
const responses = await client
27+
.multi()
28+
.hGetAll('hash2')
29+
.addCommand(['hset', 'hash3', 'number', '4'])
30+
.hGet('hash3', 'number')
31+
.exec();
32+
33+
// responses will be:
34+
// [ [Object: null prototype] { name: 'hash2', number: '3' }, 0, '4' ]
35+
console.log(responses);
36+
37+
// Clean up fixtures.
38+
await client.del(['hash1', 'hash2', 'hash3']);
39+
40+
await client.quit();

0 commit comments

Comments
 (0)