@@ -9,20 +9,157 @@ categories:
99- oss
1010- kubernetes
1111- clients
12- description : Learn the differences between ioredis and node-redis
12+ description : Learn the differences between ` ioredis` and ` node-redis`
1313linkTitle : Migrate from ioredis
1414title : Migrate from ioredis
1515weight : 6
1616---
1717
1818Redis previously recommended the [ ` ioredis ` ] ( https://github.com/redis/ioredis )
19- client library for development with [ ` Node.js ` ] ( https://nodejs.org/en ) ,
19+ client library for development with [ Node.js] ( https://nodejs.org/en ) ,
2020but this library is now deprecated in favor of
2121[ ` node-redis ` ] ({{< relref "/develop/clients/nodejs" >}}). This guide
22- outlines the main similarities and differences between the two libraries that
23- you should be aware of if you are an ` ioredis ` user and you want to start a new
24- Node.js project or migrate an existing ` ioredis ` project to ` node-redis `
22+ outlines the main similarities and differences between the two libraries.
23+ 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 `
25+
26+ The table below summarizes how ` ioredis ` and ` node-redis ` implement some
27+ key features of Redis. See the following sections for more information about
28+ each feature.
2529
2630| Feature | ` ioredis ` | ` node-redis ` |
2731| :-- | :-- | :-- |
28- | Handling asynchronous command results | Callbacks and Promises | Promises only |
32+ | [ Command case] ( #command-case ) | Lowercase only (eg, ` hset ` ) | Uppercase or camel case (eg, ` HSET ` or ` hSet ` ) |
33+ | [ Command argument handling] ( #command-argument-handling ) | Argument objects flattened and items passed directly | Argument objects parsed to generate correct argument list |
34+ | [ Asynchronous command result handling] ( #async-result ) | Callbacks and Promises | Promises only |
35+ | [ Pipelining] ( #pipelining ) | Automatic, or with ` pipeline() ` command | Automatic, or with ` multi() ` command |
36+
37+ ## Command case
38+
39+ Command methods in ` ioredis ` are always lowercase. With ` node-redis ` , you can
40+ use uppercase or camel case versions of the method names.
41+
42+ ``` js
43+ // ioredis
44+ redis .hset (" key" , " field" , " value" );
45+
46+ // node-redis
47+ redis .HSET (" key" , " field" , " value" );
48+
49+ // ...or
50+ redis .hSet (" key" , " field" , " value" );
51+ ```
52+
53+ ## Command argument handling
54+
55+ ` ioredis ` parses command arguments to strings and then passes them to
56+ the server in order, like [ ` redis-cli ` ] ({{< relref "/develop/tools/cli" >}})
57+ commands.
58+
59+ ``` js
60+ // Equivalent to the command line `SET key 100 EX 10`.
61+ redis .set (" key" , 100 , " EX" , 10 );
62+ ```
63+
64+ Arrays passed as arguments are flattened into individual elements and
65+ objects are flattened into sequential key-value pairs:
66+
67+ ``` js
68+ // These commands are all equivalent.
69+ redis .hset (" user" {
70+ name: " Bob" ,
71+ age: 20 ,
72+ description: " I am a programmer" ,
73+ });
74+
75+ redis .hset (" user" , [" name" , " Bob" , " age" , 20 , " description" , " I am a programmer" ]);
76+
77+ redis .hset (" user" , " name" , " Bob" , " age" , 20 , " description" , " I am a programmer" );
78+ ```
79+
80+ ` node-redis ` uses predefined formats for command arguments. These include specific
81+ classes for commmand options that generally don't correspond to the syntax
82+ of the CLI command. Internally, ` node-redis ` constructs the correct command using
83+ the method arguments you pass:
84+
85+ ``` js
86+ // Equivalent to the command line `SET key 100 EX 10`.
87+ redis .set (" bike:5" , " bike" , {EX : 10 });
88+ ```
89+
90+ ## Asynchronous command result handling {#async-result}
91+
92+ All commands for both ` ioredis ` and ` node-redis ` are executed
93+ asynchronously. ` ioredis ` supports both callbacks and
94+ [ ` Promise ` ] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise )
95+ return values to respond to command results:
96+
97+ ``` js
98+ // Callback
99+ redis .get (" mykey" , (err , result ) => {
100+ if (err) {
101+ console .error (err);
102+ } else {
103+ console .log (result);
104+ }
105+ });
106+
107+ // Promise
108+ redis .get (" mykey" ).then (
109+ (result ) => {
110+ console .log (result);
111+ },
112+ (err ) => {
113+ console .error (err);
114+ }
115+ );
116+ ```
117+
118+ ` node-redis ` supports only ` Promise ` objects for results, so
119+ you must always use a ` then() ` handler or the
120+ [ ` await ` ] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await )
121+ operator to receive them.
122+
123+ ## Pipelining
124+
125+ Both ` ioredis ` and ` node-redis ` will pipeline commands automatically if
126+ they are executed in the same "tick" of the
127+ [ event loop] ( https://nodejs.org/en/learn/asynchronous-work/event-loop-timers-and-nexttick#what-is-the-event-loop )
128+ (see
129+ [ Pipelines and transactions] ({{< relref "/develop/clients/nodejs/transpipe" >}})
130+ for more information).
131+
132+ You can also create a pipeline with explicit commands in both clients.
133+ For ` ioredis ` , you use the ` pipeline() ` command with a chain of
134+ commands, ending with ` exec() ` to run the pipeline:
135+
136+ ``` js
137+ // ioredis example
138+ redis
139+ .pipeline ()
140+ .set (" foo" , " 1" )
141+ .get (" foo" )
142+ .set (" foo" , " 2" )
143+ .incr (" foo" )
144+ .get (" foo" )
145+ .exec (function (err , results ) {
146+ // Handle results or errors.
147+ });
148+ ```
149+
150+ For ` node-redis ` , the approach is similar, except that you call the ` multi() `
151+ command to start the pipeline and ` execAsPipeline() ` to run it:
152+
153+ ``` js
154+ redis .multi ()
155+ .set (' seat:3' , ' #3' )
156+ .set (' seat:4' , ' #4' )
157+ .set (' seat:5' , ' #5' )
158+ .execAsPipeline ()
159+ .then ((results ) => {
160+ // Handle array of results.
161+ },
162+ (err ) => {
163+ // Handle errors.
164+ });
165+ ```
0 commit comments