|
| 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