From 3735c8c6cd1b964d6dc8a3b5f3d526dcfe001586 Mon Sep 17 00:00:00 2001 From: Michael Salaverry Date: Mon, 13 Jan 2020 18:33:16 +0200 Subject: [PATCH 1/5] docs: add more detail to the basic example This change adds more details to the basic example. These details show a new user what to expect from the library. --- examples/basic_operations.js | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/examples/basic_operations.js b/examples/basic_operations.js index e69cac87..c8e2145d 100644 --- a/examples/basic_operations.js +++ b/examples/basic_operations.js @@ -1,20 +1,26 @@ "use strict"; -var Redis = require("ioredis"); -var redis = new Redis(); +const Redis = require("ioredis"); +const redis = new Redis(process.env.redisPort, 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); }); @@ -22,6 +28,15 @@ redis.get("foo").then(function(result) { // 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") +console.log(redis.zrange("sortedSet", 0, 2, "WITHSCORES")); // 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); From ff702fb19c05943e4369beb9d3ab605279b012c2 Mon Sep 17 00:00:00 2001 From: Michael Salaverry Date: Tue, 14 Jan 2020 10:34:14 +0200 Subject: [PATCH 2/5] docs: review suggestions --- examples/basic_operations.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/basic_operations.js b/examples/basic_operations.js index c8e2145d..8324a06c 100644 --- a/examples/basic_operations.js +++ b/examples/basic_operations.js @@ -4,7 +4,7 @@ const Redis = require("ioredis"); const redis = new Redis(process.env.redisPort, process.env.redisEndpoint, {password: process.env.redisPW}); // ioredis supports all Redis commands: -redis.set("foo", "bar"); // returns promise which resolves to string, "Ok" +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" ` @@ -32,7 +32,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") -console.log(redis.zrange("sortedSet", 0, 2, "WITHSCORES")); // Promise resolves to ["one", "1", "dos", "2", "three", "3"] as if the command was ` redis> ZRANGE sortedSet 0 2 WITHSCORES ` +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"); From 36650de658c20d3e31b8e291d4cba911b71b9e9d Mon Sep 17 00:00:00 2001 From: Michael Salaverry Date: Tue, 14 Jan 2020 10:36:29 +0200 Subject: [PATCH 3/5] docs: moved to config object --- examples/basic_operations.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/examples/basic_operations.js b/examples/basic_operations.js index 8324a06c..41cd7f05 100644 --- a/examples/basic_operations.js +++ b/examples/basic_operations.js @@ -1,7 +1,9 @@ -"use strict"; - const Redis = require("ioredis"); -const redis = new Redis(process.env.redisPort, process.env.redisEndpoint, {password: process.env.redisPW}); +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"); // returns promise which resolves to string, "OK" From 29e3c89d60acead68c01502073d9089efb562762 Mon Sep 17 00:00:00 2001 From: Michael Salaverry Date: Sun, 19 Jan 2020 15:53:52 +0200 Subject: [PATCH 4/5] added more detail to basic example --- README.md | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index c86b3c70..e180ad5a 100644 --- a/README.md +++ b/README.md @@ -76,23 +76,32 @@ $ npm install ioredis ## Basic Usage ```javascript -var Redis = require("ioredis"); -var redis = new Redis(); +const Redis = require("ioredis"); +const redis = new Redis(); // uses defaults unless given configuration object -redis.set("foo", "bar"); +// ioredis supports all Redis commands: +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) { - console.log(result); + if (err) { + console.error(err); + } else { + 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); + console.log(result); // Prints "bar" }); -// 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]); +// 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 ` // All arguments are passed directly to the redis server: redis.set("key", 100, "EX", 10); From a9c93aa18d6493d4ab04e8b5998e5d54fbb175d5 Mon Sep 17 00:00:00 2001 From: Michael Salaverry Date: Sun, 19 Jan 2020 15:56:49 +0200 Subject: [PATCH 5/5] fix: we shouldn't delete the key yet. --- examples/basic_operations.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/basic_operations.js b/examples/basic_operations.js index 41cd7f05..ec3876fb 100644 --- a/examples/basic_operations.js +++ b/examples/basic_operations.js @@ -20,13 +20,13 @@ redis.get("foo", function(err, result) { } }); -redis.del("foo"); - // 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]); @@ -41,7 +41,7 @@ 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");