Skip to content

Commit c1f649f

Browse files
DOC-4942 added remaining commands plus fixes
1 parent 674e586 commit c1f649f

File tree

1 file changed

+96
-38
lines changed

1 file changed

+96
-38
lines changed

content/develop/clients/nodejs/migration.md

Lines changed: 96 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ categories:
99
- oss
1010
- kubernetes
1111
- clients
12-
description: Learn the differences between `ioredis` and `node-redis`
12+
description: Discover the differences between `ioredis` and `node-redis`.
1313
linkTitle: Migrate from ioredis
1414
title: Migrate from ioredis
1515
weight: 6
@@ -21,11 +21,11 @@ but this library is now deprecated in favor of
2121
[`node-redis`]({{< relref "/develop/clients/nodejs" >}}). This guide
2222
outlines the main similarities and differences between the two libraries.
2323
You may find this information useful if you are an `ioredis` user and you want to
24-
start a new Node.js project or migrate an existing `ioredis` project to `node-redis`
24+
start a new Node.js project or migrate an existing `ioredis` project to `node-redis`.
2525

2626
## Comparison of `ioredis` and `node-redis`
2727

28-
The table below summarizes how `ioredis` and `node-redis` implement some
28+
The tables below summarize how `ioredis` and `node-redis` implement some
2929
key features of Redis. See the following sections for more information about
3030
each feature.
3131

@@ -35,7 +35,7 @@ each feature.
3535
| :-- | :-- | :-- |
3636
| [Initial connection](#initial-connection) | Happens when you create a client instance | Requires you to call a method on the client instance |
3737
| [Reconnection after a connection is lost](#reconnection) | Automatic by default | Manual by default |
38-
| [Connection events](#connection-events) | Emits `connect`, `ready`, `error`, and `close` events. | Emits `connect`, `ready`, `error`, `end`, and `reconnecting` events. |
38+
| [Connection events](#connection-events) | Emits `connect`, `ready`, `error`, and `close` events | Emits `connect`, `ready`, `error`, `end`, and `reconnecting` events |
3939

4040
### Command handling
4141

@@ -44,20 +44,23 @@ each feature.
4444
| [Command case](#command-case) | Lowercase only (eg, `hset`) | Uppercase or camel case (eg, `HSET` or `hSet`) |
4545
| [Command argument handling](#command-argument-handling) | Argument objects flattened and items passed directly | Argument objects parsed to generate correct argument list |
4646
| [Asynchronous command result handling](#async-result) | Callbacks and Promises | Promises only |
47+
| [Arbitrary command execution](#arbitrary-command-execution) | Uses the `call()` method | Uses the `sendCommand()` method |
4748

4849
### Techniques
4950

5051
| Feature | `ioredis` | `node-redis` |
5152
| :-- | :-- | :-- |
5253
| [Pipelining](#pipelining) | Automatic, or with `pipeline()` command | Automatic, or with `multi()` command |
53-
| [Scan iteration](#scan-iteration) | Uses `scanStream()`, etc. | Uses `scanIterator()`, etc |
54+
| [Scan iteration](#scan-iteration) | Uses `scanStream()`, etc | Uses `scanIterator()`, etc |
5455
| [Subscribing to channels](#subscribing-to-channels) | Uses `client.on('message', ...)` event | Uses `subscribe(...)` command |
5556

5657
### Specific commands
5758

5859
| Command | `ioredis` | `node-redis` |
5960
| :-- | :-- | :-- |
6061
| [`SETNX`](#setnx-command) | Supported explicitly | Supported as an option for `SET` |
62+
| [`HMSET`](#hmset-command) | Supported explicitly | Supported with standard `HSET` functionality |
63+
| [`CONFIG`](#config-command) | Supported explicitly | Supported with separate `configGet()`, `configSet()`, etc |co
6164

6265
## Details
6366

@@ -70,10 +73,10 @@ The sections below explain the points of comparison between `ioredis` and
7073
of the client object:
7174

7275
```js
73-
const Redis = require('ioredis');
76+
const client = require('ioredis');
7477

7578
// Connects to localhost:6379 on instantiation.
76-
const redis = new Redis();
79+
const client = new Redis();
7780
```
7881

7982
`node-redis` requires you to call the `connect()` method on the client object
@@ -100,7 +103,7 @@ for more information.
100103
The `connect`, `ready`, `error`, and `close` events that `ioredis` emits
101104
are equivalent to the `connect`, `ready`, `error`, and `end` events
102105
in `node-redis`, but `node-redis` also emits a `reconnecting` event.
103-
See []({{< relref "/develop/clients/nodejs/connect#connection-events" >}})
106+
See [Connection events]({{< relref "/develop/clients/nodejs/connect#connection-events" >}})
104107
for more information.
105108

106109
### Command case
@@ -110,13 +113,13 @@ use uppercase or camel case versions of the method names.
110113

111114
```js
112115
// ioredis
113-
redis.hset("key", "field", "value");
116+
client.hset('key', 'field', 'value');
114117

115118
// node-redis
116-
redis.HSET("key", "field", "value");
119+
client.HSET('key', 'field', 'value');
117120

118121
// ...or
119-
redis.hSet("key", "field", "value");
122+
client.hSet('key', 'field', 'value');
120123
```
121124

122125
### Command argument handling
@@ -126,23 +129,23 @@ the server, in a similar way to [`redis-cli`]({{< relref "/develop/tools/cli" >}
126129

127130
```js
128131
// Equivalent to the command line `SET key 100 EX 10`.
129-
redis.set("key", 100, "EX", 10);
132+
client.set('key', 100, 'EX', 10);
130133
```
131134

132135
Arrays passed as arguments are flattened into individual elements and
133136
objects are flattened into sequential key-value pairs:
134137

135138
```js
136139
// These commands are all equivalent.
137-
redis.hset("user" {
138-
name: "Bob",
140+
client.hset('user' {
141+
name: 'Bob',
139142
age: 20,
140-
description: "I am a programmer",
143+
description: 'I am a programmer',
141144
});
142145

143-
redis.hset("user", ["name", "Bob", "age", 20, "description", "I am a programmer"]);
146+
client.hset('user', ['name', 'Bob', 'age', 20, 'description', 'I am a programmer']);
144147

145-
redis.hset("user", "name", "Bob", "age", 20, "description", "I am a programmer");
148+
client.hset('user', 'name', 'Bob', 'age', 20, 'description', 'I am a programmer');
146149
```
147150

148151
`node-redis` uses predefined formats for command arguments. These include specific
@@ -151,8 +154,8 @@ of the CLI command. Internally, `node-redis` constructs the correct command usin
151154
the method arguments you pass:
152155

153156
```js
154-
// Equivalent to the command line `SET key 100 EX 10`.
155-
redis.set("bike:5", "bike", {EX: 10});
157+
// Equivalent to the command line `SET bike:5 bike EX 10`.
158+
client.set('bike:5', 'bike', {EX: 10});
156159
```
157160

158161
### Asynchronous command result handling {#async-result}
@@ -164,7 +167,7 @@ return values to respond to command results:
164167

165168
```js
166169
// Callback
167-
redis.get("mykey", (err, result) => {
170+
client.get('mykey', (err, result) => {
168171
if (err) {
169172
console.error(err);
170173
} else {
@@ -173,7 +176,7 @@ redis.get("mykey", (err, result) => {
173176
});
174177

175178
// Promise
176-
redis.get("mykey").then(
179+
client.get('mykey').then(
177180
(result) => {
178181
console.log(result);
179182
},
@@ -188,27 +191,54 @@ you must always use a `then()` handler or the
188191
[`await`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await)
189192
operator to receive them.
190193

194+
### Arbitrary command execution
195+
196+
`ioredis` lets you issue arbitrary commands in a similar format to
197+
[`redis-cli`]({{< relref "/develop/tools/cli" >}}) using the `call()`
198+
command:
199+
200+
```js
201+
await client.call('JSON.SET', 'doc', "$", '{"f1": {"a":1}, "f2":{"a":2}}');
202+
```
203+
204+
In `node-redis`, you can get the same effect outside a transaction using `sendCommand()`:
205+
206+
```js
207+
await client.sendCommand(['hset', 'hash2', 'number', '3']);
208+
```
209+
210+
Within a transaction, use `addCommand()` to include arbitrary commands. Note that
211+
you can freely mix `addCommand()` calls with standard commands in the same
212+
transaction:
213+
214+
```js
215+
const responses = await client.multi()
216+
.addCommand(['hset', 'hash3', 'number', '4'])
217+
.hGet('hash3', 'number')
218+
.exec();
219+
```
220+
191221
### Pipelining
192222

193223
Both `ioredis` and `node-redis` will pipeline commands automatically if
194224
they are executed in the same "tick" of the
195225
[event loop](https://nodejs.org/en/learn/asynchronous-work/event-loop-timers-and-nexttick#what-is-the-event-loop)
196226
(see
197-
[Pipelines and transactions]({{< relref "/develop/clients/nodejs/transpipe" >}})
227+
[Execute a pipeline]({{< relref "/develop/clients/nodejs/transpipe#execute-a-pipeline" >}})
198228
for more information).
199229

200230
You can also create a pipeline with explicit commands in both clients.
201-
For `ioredis`, you use the `pipeline()` command with a chain of
231+
With `ioredis`, you use the `pipeline()` command with a chain of
202232
commands, ending with `exec()` to run the pipeline:
203233

204234
```js
205235
// ioredis example
206-
redis.pipeline()
207-
.set("foo", "1")
208-
.get("foo")
209-
.set("foo", "2")
210-
.incr("foo")
211-
.get("foo")
236+
client.pipeline()
237+
.set('foo', '1')
238+
.get('foo')
239+
.set('foo', '2')
240+
.incr('foo')
241+
.get('foo')
212242
.exec(function (err, results) {
213243
// Handle results or errors.
214244
});
@@ -218,7 +248,7 @@ For `node-redis`, the approach is similar, except that you call the `multi()`
218248
command to start the pipeline and `execAsPipeline()` to run it:
219249

220250
```js
221-
redis.multi()
251+
client.multi()
222252
.set('seat:3', '#3')
223253
.set('seat:4', '#4')
224254
.set('seat:5', '#5')
@@ -238,19 +268,19 @@ from the set of keys returned by the [`SCAN`]({{< relref "/commands/scan" >}})
238268
command:
239269

240270
```js
241-
const redis = new Redis();
271+
const client = new Redis();
242272
// Create a readable stream (object mode)
243-
const stream = redis.scanStream();
244-
stream.on("data", (resultKeys) => {
273+
const stream = client.scanStream();
274+
stream.on('data', (resultKeys) => {
245275
// `resultKeys` is an array of strings representing key names.
246276
// Note that resultKeys may contain 0 keys, and that it will sometimes
247277
// contain duplicates due to SCAN's implementation in Redis.
248278
for (let i = 0; i < resultKeys.length; i++) {
249279
console.log(resultKeys[i]);
250280
}
251281
});
252-
stream.on("end", () => {
253-
console.log("all keys have been visited");
282+
stream.on('end', () => {
283+
console.log('all keys have been visited');
254284
});
255285
```
256286

@@ -268,6 +298,7 @@ other multi-key commands):
268298
```js
269299
for await (const keys of client.scanIterator()) {
270300
const values = await client.mGet(keys);
301+
// Process values...
271302
}
272303
```
273304

@@ -285,7 +316,7 @@ client.on('message', (channel, message) => {
285316
```
286317

287318
With `node-redis`, you use the `subscribe()` command to register the
288-
message callback. Also, when you use a connection to subscribe, the
319+
message callback. Also, when you use a connection to subscribe, that
289320
connection can't issue any other commands, so you must create a
290321
dedicated connection for the subscription. Use the `client.duplicate()`
291322
method to create a new connection with the same settings as the original:
@@ -305,13 +336,40 @@ await subscriber.subscribe('channel', (message) => {
305336
command with an explicit method:
306337

307338
```js
308-
client.setnx("bike:1", "bike");
339+
client.setnx('bike:1', 'bike');
309340
```
310341

311342
`node-redis` doesn't provide a `SETNX` method but implements the same
312343
functionality with the `NX` option to the [`SET`]({{< relref "/commands/set" >}})
313344
command:
314345

315346
```js
316-
await client.set("bike:1", "bike", {'NX': true});
347+
await client.set('bike:1', 'bike', {'NX': true});
348+
```
349+
350+
### `HMSET` command
351+
352+
The [`HMSET`]({{< relref "/commands/hmset" >}}) command has been deprecated
353+
since Redis v4.0.0, but it is still supported by `ioredis`. With `node-redis`
354+
you should use the [`HSET`]({{< relref "/commands/hset" >}}) command with
355+
multiple key-value pairs. See the [`HSET`]({{< relref "/commands/hset" >}})
356+
command page for more information.
357+
358+
### `CONFIG` command
359+
360+
`ioredis` supports a `config()` method to set or get server configuration
361+
options:
362+
363+
```js
364+
client.config('SET', 'notify-keyspace-events', 'KEA');
365+
```
366+
367+
`node-redis` doesn't have a `config()` method, but instead supports the
368+
standard commands [`configSet()`]({{< relref "/commands/config-set" >}}),
369+
[`configGet()`]({{< relref "/commands/config-get" >}}),
370+
[`configResetStat()`]({{< relref "/commands/config-resetstat" >}}), and
371+
[`configRewrite`]({{< relref "/commands/config-rewrite" >}}):
372+
373+
```js
374+
await client.configSet('maxclients', '2000');
317375
```

0 commit comments

Comments
 (0)