-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
docs: add more detail to the basic example #1043
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3735c8c
docs: add more detail to the basic example
barakplasma ff702fb
docs: review suggestions
barakplasma 36650de
docs: moved to config object
barakplasma 29e3c89
added more detail to basic example
barakplasma a9c93aa
fix: we shouldn't delete the key yet.
barakplasma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,47 @@ | ||
"use strict"; | ||
|
||
var Redis = require("ioredis"); | ||
var redis = new Redis(); | ||
const Redis = require("ioredis"); | ||
const redis = new Redis({ | ||
port: process.env.redisPort, | ||
host: process.env.redisEndpoint, | ||
password: process.env.redisPW | ||
}); | ||
|
||
// ioredis supports all Redis commands: | ||
redis.set("foo", "bar"); | ||
redis.set("foo", "bar"); // returns promise which resolves to string, "OK" | ||
|
||
// the format is: redis[SOME_REDIS_COMMAND_IN_LOWERCASE](ARGUMENTS_ARE_JOINED_INTO_COMMAND_STRING) | ||
// the js: ` redis.set("mykey", "Hello") ` is equivalent to the cli: ` redis> SET mykey "Hello" ` | ||
|
||
// ioredis supports the node.js callback style | ||
redis.get("foo", function(err, result) { | ||
if (err) { | ||
console.error(err); | ||
} else { | ||
console.log(result); | ||
console.log(result); // Promise resolves to "bar" | ||
} | ||
}); | ||
redis.del("foo"); | ||
|
||
// Or using a promise if the last argument isn't a function | ||
// Or ioredis returns a promise if the last argument isn't a function | ||
redis.get("foo").then(function(result) { | ||
console.log(result); | ||
}); | ||
|
||
redis.del("foo"); | ||
|
||
// Arguments to commands are flattened, so the following are the same: | ||
redis.sadd("set", 1, 3, 5, 7); | ||
redis.sadd("set", [1, 3, 5, 7]); | ||
redis.spop("set"); // Promise resolves to "5" or another item in the set | ||
|
||
// Most responses are strings, or arrays of strings | ||
redis.zadd("sortedSet", 1, "one", 2, "dos", 4, "quatro", 3, "three") | ||
redis.zrange("sortedSet", 0, 2, "WITHSCORES").then(res => console.log(res)); // Promise resolves to ["one", "1", "dos", "2", "three", "3"] as if the command was ` redis> ZRANGE sortedSet 0 2 WITHSCORES ` | ||
|
||
// Some responses have transformers to JS values | ||
redis.hset("myhash", "field1", "Hello"); | ||
redis.hgetall("myhash").then(res => console.log(res)); // Promise resolves to Object {field1: "Hello"} rather than a string, or array of strings | ||
|
||
// All arguments are passed directly to the redis server: | ||
redis.set("key", 100, "EX", 10); | ||
redis.set("key", 100, "EX", 10); // set's key to value 100 and expires it after 10 seconds | ||
|
||
// Change the server configuration | ||
redis.config("set", "notify-keyspace-events", "KEA"); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm considering putting all of this within an async function so that we can async/await some of the commands. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the late response! Yes I think that would be helpful but still we should keep some examples for callback-style. I'm going to merge this pr so feel free to create a new pr for the proposal. Thanks again for the contribution!