You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-06 02:15:48 +03:00
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>
This commit is contained in:
@@ -27,7 +27,7 @@ This folder contains example scripts showing how to use Node Redis in different
|
|||||||
| `time-series.js` | Create, populate and query timeseries data with [Redis Timeseries](https://redistimeseries.io). |
|
| `time-series.js` | Create, populate and query timeseries data with [Redis Timeseries](https://redistimeseries.io). |
|
||||||
| `topk.js` | Use the [RedisBloom](https://redisbloom.io) TopK to track the most frequently seen items. |
|
| `topk.js` | Use the [RedisBloom](https://redisbloom.io) TopK to track the most frequently seen items. |
|
||||||
| `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. |
|
| `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. |
|
||||||
| `tranaaction-with-arbitrary-commands.js` | Mix and match supported commands with arbitrary command strings in a Redis transaction. |
|
| `transaction-with-arbitrary-commands.js` | Mix and match supported commands with arbitrary command strings in a Redis transaction. |
|
||||||
| `transaction-with-watch.js` | An Example of [Redis transaction](https://redis.io/docs/manual/transactions) with `WATCH` command on isolated connection with optimistic locking. |
|
| `transaction-with-watch.js` | An Example of [Redis transaction](https://redis.io/docs/manual/transactions) with `WATCH` command on isolated connection with optimistic locking. |
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
40
examples/transaction-with-arbitrary-commands.js
Normal file
40
examples/transaction-with-arbitrary-commands.js
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
// How to mix and match supported commands that have named functions with
|
||||||
|
// commands sent as arbitrary strings in the same transaction context.
|
||||||
|
// Use this when working with new Redis commands that haven't been added to
|
||||||
|
// node-redis yet, or when working with commands that have been added to Redis
|
||||||
|
// by modules other than those directly supported by node-redis.
|
||||||
|
|
||||||
|
import { createClient } from 'redis';
|
||||||
|
|
||||||
|
const client = createClient();
|
||||||
|
|
||||||
|
await client.connect();
|
||||||
|
|
||||||
|
// Build some data fixtures.
|
||||||
|
await Promise.all([
|
||||||
|
client.hSet('hash1', { name: 'hash1', number: 1}),
|
||||||
|
client.hSet('hash2', { name: 'hash2', number: 1}),
|
||||||
|
client.hSet('hash3', { name: 'hash3', number: 3})
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Outside of a transaction, use sendCommand to send arbitrary commands.
|
||||||
|
await client.sendCommand(['hset', 'hash2', 'number', '3']);
|
||||||
|
|
||||||
|
// In a transaction context, use addCommand to send arbitrary commands.
|
||||||
|
// addCommand can be mixed and matched with named command functions as
|
||||||
|
// shown.
|
||||||
|
const responses = await client
|
||||||
|
.multi()
|
||||||
|
.hGetAll('hash2')
|
||||||
|
.addCommand(['hset', 'hash3', 'number', '4'])
|
||||||
|
.hGet('hash3', 'number')
|
||||||
|
.exec();
|
||||||
|
|
||||||
|
// responses will be:
|
||||||
|
// [ [Object: null prototype] { name: 'hash2', number: '3' }, 0, '4' ]
|
||||||
|
console.log(responses);
|
||||||
|
|
||||||
|
// Clean up fixtures.
|
||||||
|
await client.del(['hash1', 'hash2', 'hash3']);
|
||||||
|
|
||||||
|
await client.quit();
|
Reference in New Issue
Block a user