Skip to content

added example usage of set datastructure #2534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
15 changes: 0 additions & 15 deletions examples/set-scan.js

This file was deleted.

39 changes: 39 additions & 0 deletions examples/set.js
Original file line number Diff line number Diff line change
@@ -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();
Loading