From c8a52889560c01ba803aea883ebbec66c7d8e030 Mon Sep 17 00:00:00 2001 From: Pavel Pashov Date: Wed, 4 Jun 2025 17:45:17 +0300 Subject: [PATCH 1/7] Asynchronous command result handling --- content/develop/clients/nodejs/migration.md | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/content/develop/clients/nodejs/migration.md b/content/develop/clients/nodejs/migration.md index b89fbb19ad..f530ba5939 100644 --- a/content/develop/clients/nodejs/migration.md +++ b/content/develop/clients/nodejs/migration.md @@ -43,7 +43,7 @@ each feature. | :-- | :-- | :-- | | [Command case](#command-case) | Lowercase only (eg, `hset`) | Uppercase or camel case (eg, `HSET` or `hSet`) | | [Command argument handling](#command-argument-handling) | Argument objects flattened and items passed directly | Argument objects parsed to generate correct argument list | -| [Asynchronous command result handling](#async-result) | Callbacks and Promises | Promises only | +| [Asynchronous command result handling](#async-result) | Callbacks and Promises | Promises and Callbacks (via Legacy Mode) | | [Arbitrary command execution](#arbitrary-command-execution) | Uses the `call()` method | Uses the `sendCommand()` method | ### Techniques @@ -189,7 +189,22 @@ client.get('mykey').then( `node-redis` supports only `Promise` objects for results, so you must always use a `then()` handler or the [`await`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await) -operator to receive them. +operator to receive them, however callbacks are still supported via the legacy mode: + +```js +// Promise +await client.set('mykey', 'myvalue'); + +// Callback +const legacyClient = client.legacy(); +legacyClient.set("mykey", "myvalue", (err, result) => { + if (err) { + console.error(err); + } else { + console.log(result); + } +}); +``` ### Arbitrary command execution From d08158932a5a0ab09871eb2ff56c9a5a9f7ae217 Mon Sep 17 00:00:00 2001 From: Pavel Pashov Date: Wed, 4 Jun 2025 17:46:52 +0300 Subject: [PATCH 2/7] Removed redundant await --- content/develop/clients/nodejs/migration.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/develop/clients/nodejs/migration.md b/content/develop/clients/nodejs/migration.md index f530ba5939..f88c4e0268 100644 --- a/content/develop/clients/nodejs/migration.md +++ b/content/develop/clients/nodejs/migration.md @@ -85,7 +85,7 @@ to make the connection: ```js import { createClient } from 'redis'; -const client = await createClient(); +const client = createClient(); await client.connect(); // Requires explicit connection. ``` From 51007e337a705ebb141da6f8237e3f932f3c6342 Mon Sep 17 00:00:00 2001 From: Pavel Pashov Date: Wed, 4 Jun 2025 17:48:30 +0300 Subject: [PATCH 3/7] Command argument handling --- content/develop/clients/nodejs/migration.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/content/develop/clients/nodejs/migration.md b/content/develop/clients/nodejs/migration.md index f88c4e0268..bdfe0dd793 100644 --- a/content/develop/clients/nodejs/migration.md +++ b/content/develop/clients/nodejs/migration.md @@ -137,15 +137,13 @@ objects are flattened into sequential key-value pairs: ```js // These commands are all equivalent. -client.hset('user' { +await client.hSet('user' { name: 'Bob', age: 20, description: 'I am a programmer', }); -client.hset('user', ['name', 'Bob', 'age', 20, 'description', 'I am a programmer']); - -client.hset('user', 'name', 'Bob', 'age', 20, 'description', 'I am a programmer'); +await client.hSet('user', ['name', 'Bob', 'age', 20, 'description', 'I am a programmer']); ``` `node-redis` uses predefined formats for command arguments. These include specific From 2fb97e29c7753801015f6503d7ccf9acd46d37ad Mon Sep 17 00:00:00 2001 From: Pavel Pashov Date: Wed, 4 Jun 2025 17:49:22 +0300 Subject: [PATCH 4/7] SETNX command details --- content/develop/clients/nodejs/migration.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/content/develop/clients/nodejs/migration.md b/content/develop/clients/nodejs/migration.md index bdfe0dd793..094cd7675b 100644 --- a/content/develop/clients/nodejs/migration.md +++ b/content/develop/clients/nodejs/migration.md @@ -352,9 +352,9 @@ command with an explicit method: client.setnx('bike:1', 'bike'); ``` -`node-redis` doesn't provide a `SETNX` method but implements the same -functionality with the `NX` option to the [`SET`]({{< relref "/commands/set" >}}) -command: +`node-redis` provides a `SETNX` method but due to the command being deprecated the same +functionality can be achieved with the `NX` option to the [`SET`]({{< relref "/commands/set" >}}) +command instead: ```js await client.set('bike:1', 'bike', {'NX': true}); From ca2b877f4805a050772452ac3d8d2289d4f22080 Mon Sep 17 00:00:00 2001 From: Pavel Pashov Date: Thu, 5 Jun 2025 10:48:10 +0300 Subject: [PATCH 5/7] Revert "Command argument handling" This reverts commit 51007e337a705ebb141da6f8237e3f932f3c6342. --- content/develop/clients/nodejs/migration.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/content/develop/clients/nodejs/migration.md b/content/develop/clients/nodejs/migration.md index 094cd7675b..44bed5f722 100644 --- a/content/develop/clients/nodejs/migration.md +++ b/content/develop/clients/nodejs/migration.md @@ -137,13 +137,15 @@ objects are flattened into sequential key-value pairs: ```js // These commands are all equivalent. -await client.hSet('user' { +client.hset('user' { name: 'Bob', age: 20, description: 'I am a programmer', }); -await client.hSet('user', ['name', 'Bob', 'age', 20, 'description', 'I am a programmer']); +client.hset('user', ['name', 'Bob', 'age', 20, 'description', 'I am a programmer']); + +client.hset('user', 'name', 'Bob', 'age', 20, 'description', 'I am a programmer'); ``` `node-redis` uses predefined formats for command arguments. These include specific From 4901cd4a272adba679f9fcfeb3de07a9ea1fb400 Mon Sep 17 00:00:00 2001 From: Pavel Pashov Date: Thu, 5 Jun 2025 10:50:03 +0300 Subject: [PATCH 6/7] Command argument handling - added a comma --- content/develop/clients/nodejs/migration.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/develop/clients/nodejs/migration.md b/content/develop/clients/nodejs/migration.md index 44bed5f722..c71c85aee0 100644 --- a/content/develop/clients/nodejs/migration.md +++ b/content/develop/clients/nodejs/migration.md @@ -137,7 +137,7 @@ objects are flattened into sequential key-value pairs: ```js // These commands are all equivalent. -client.hset('user' { +client.hset('user', { name: 'Bob', age: 20, description: 'I am a programmer', From cea3609694385cc564c6d4ac57df796189a6aad8 Mon Sep 17 00:00:00 2001 From: Pavel Pashov <60297174+PavelPashov@users.noreply.github.com> Date: Thu, 5 Jun 2025 11:25:45 +0300 Subject: [PATCH 7/7] Apply suggestions from code review Co-authored-by: andy-stark-redis <164213578+andy-stark-redis@users.noreply.github.com> --- content/develop/clients/nodejs/migration.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/content/develop/clients/nodejs/migration.md b/content/develop/clients/nodejs/migration.md index c71c85aee0..a0f36f1eab 100644 --- a/content/develop/clients/nodejs/migration.md +++ b/content/develop/clients/nodejs/migration.md @@ -43,7 +43,7 @@ each feature. | :-- | :-- | :-- | | [Command case](#command-case) | Lowercase only (eg, `hset`) | Uppercase or camel case (eg, `HSET` or `hSet`) | | [Command argument handling](#command-argument-handling) | Argument objects flattened and items passed directly | Argument objects parsed to generate correct argument list | -| [Asynchronous command result handling](#async-result) | Callbacks and Promises | Promises and Callbacks (via Legacy Mode) | +| [Asynchronous command result handling](#async-result) | Callbacks and Promises | Promises (but supports callbacks via Legacy Mode) | | [Arbitrary command execution](#arbitrary-command-execution) | Uses the `call()` method | Uses the `sendCommand()` method | ### Techniques @@ -189,7 +189,7 @@ client.get('mykey').then( `node-redis` supports only `Promise` objects for results, so you must always use a `then()` handler or the [`await`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await) -operator to receive them, however callbacks are still supported via the legacy mode: +operator to receive them. However, you can still use callbacks with the legacy mode if you need them: ```js // Promise @@ -354,9 +354,8 @@ command with an explicit method: client.setnx('bike:1', 'bike'); ``` -`node-redis` provides a `SETNX` method but due to the command being deprecated the same -functionality can be achieved with the `NX` option to the [`SET`]({{< relref "/commands/set" >}}) -command instead: +`node-redis` provides a `SETNX` method but this command is deprecated. Use the `NX` option to the [`SET`]({{< relref "/commands/set" >}}) +command to get the same functionality as `SETNX`: ```js await client.set('bike:1', 'bike', {'NX': true});