diff --git a/examples/README.md b/examples/README.md index 19e9df31f90..ce5b5a0f35e 100644 --- a/examples/README.md +++ b/examples/README.md @@ -21,7 +21,7 @@ This folder contains example scripts showing how to use Node Redis in different | `search-hashes.js` | Uses [RediSearch](https://redisearch.io) to index and search data in hashes. | | `search-json.js` | Uses [RediSearch](https://redisearch.io/) and [RedisJSON](https://redisjson.io/) to index and search JSON data. | | `search-knn.js` | Uses [RediSearch vector similarity]([https://redisearch.io/](https://redis.io/docs/stack/search/reference/vectors/)) to index and run KNN queries. | -| `set-scan.js` | An example script that shows how to use the SSCAN iterator functionality. | +| `set.js` | An example script that shows how to use the set data structure within nodejs application. | | `sorted-set.js` | Add members with scores to a Sorted Set and retrieve them using the ZSCAN iteractor functionality. | | `stream-producer.js` | Adds entries to a [Redis Stream](https://redis.io/topics/streams-intro) using the `XADD` command. | | `stream-consumer.js` | Reads entries from a [Redis Stream](https://redis.io/topics/streams-intro) using the blocking `XREAD` command. | diff --git a/examples/set-scan.js b/examples/set-scan.js deleted file mode 100644 index 73f6c443444..00000000000 --- a/examples/set-scan.js +++ /dev/null @@ -1,15 +0,0 @@ -// An example script that shows how to use the SSCAN iterator functionality to retrieve the contents of a Redis set. -// Create the set in redis-cli with this command: -// sadd setName a b c d e f g h i j k l m n o p q - -import { createClient } from 'redis'; - -const client = createClient(); -await client.connect(); - -const setName = 'setName'; -for await (const member of client.sScanIterator(setName)) { - console.log(member); -} - -await client.quit(); diff --git a/examples/set.js b/examples/set.js new file mode 100644 index 00000000000..d0ceeb0c383 --- /dev/null +++ b/examples/set.js @@ -0,0 +1,39 @@ +// An example explaining how to add values to a set and how to retrive them + +import { createClient } from 'redis'; + +const client = createClient(); +await client.connect(); + +const setName = "user1:favorites"; + +// if you try to add any data of type other than string you will get an error saying `Invalid argument type` +// so before adding make sure the value is of type string or convert it using toString() method +// https://redis.io/commands/sadd/ +await client.SADD(setName, "1"); + +// retrieve values of the set defined +// https://redis.io/commands/smembers/ +const favorites = await client.SMEMBERS(setName); +for (const favorite of favorites) { + console.log(favorite); +} + +// alternate way to retrieve data from set +for await (const member of client.sScanIterator(setName)) { + console.log(member); +} + +// another alternate way to retrieve values from the set +// https://redis.io/commands/sscan/ +let iCursor = 0; +do { + const { cursor, members } = await client.SSCAN(setName, iCursor); + members.forEach((member) => { + console.log(member); + }); + iCursor = cursor; +} while (iCursor !== 0) + + +await client.quit();