Skip to content

Commit 9ae6928

Browse files
suggest type compatible imports
Signed-off-by: rawpixel-vincent <[email protected]>
1 parent 1b9a911 commit 9ae6928

File tree

1 file changed

+59
-25
lines changed

1 file changed

+59
-25
lines changed

README.md

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ npm install --save-dev @types/node
5454
// if your project is a TypeScript project,
5555
// Note that `import Valkey from "iovalkey"` is still supported,
5656
// but will be deprecated in the next major version.
57-
const Valkey = require("iovalkey");
57+
58+
// you can also use const iovalkey = require("iovalkey");
59+
// const { Valkey, Cluster } = iovalkey
60+
61+
const { Valkey, Cluster } = require("iovalkey");
5862

5963
// Create a Valkey instance.
6064
// By default, it will connect to localhost:6379.
@@ -110,6 +114,7 @@ a connection to Valkey will be created at the same time.
110114
You can specify which Valkey to connect to by:
111115

112116
```javascript
117+
const { Valkey } = require("iovalkey");
113118
new Valkey(); // Connect to 127.0.0.1:6379
114119
new Valkey(6380); // 127.0.0.1:6380
115120
new Valkey(6379, "192.168.1.1"); // 192.168.1.1:6379
@@ -126,6 +131,7 @@ new Valkey({
126131
You can also specify connection options as a [`redis://` URL](http://www.iana.org/assignments/uri-schemes/prov/redis) or [`rediss://` URL](https://www.iana.org/assignments/uri-schemes/prov/rediss) when using [TLS encryption](#tls-options):
127132

128133
```javascript
134+
const { Valkey } = require("iovalkey");
129135
// Connect to 127.0.0.1:6380, db 4, using password "authpassword":
130136
new Valkey("redis://:[email protected]:6380/4");
131137

@@ -144,7 +150,7 @@ By leveraging Node.js's built-in events module, iovalkey makes pub/sub very stra
144150
```javascript
145151
// publisher.js
146152

147-
const Valkey = require("iovalkey");
153+
const { Valkey } = require("iovalkey");
148154
const valkey = new Valkey();
149155

150156
setInterval(() => {
@@ -161,7 +167,7 @@ setInterval(() => {
161167
```javascript
162168
// subscriber.js
163169

164-
const Valkey = require("iovalkey");
170+
const { Valkey } = require("iovalkey");
165171
const valkey = new Valkey();
166172

167173
valkey.subscribe("my-channel-1", "my-channel-2", (err, count) => {
@@ -195,7 +201,7 @@ It's worth noticing that a connection (aka a `Valkey` instance) can't play both
195201
If you want to do pub/sub in the same file/process, you should create a separate connection:
196202

197203
```javascript
198-
const Valkey = require("iovalkey");
204+
const { Valkey } = require("iovalkey");
199205
const sub = new Valkey();
200206
const pub = new Valkey();
201207

@@ -224,7 +230,8 @@ valkey.on("pmessageBuffer", (pattern, channel, message) => {});
224230
Valkey v5 introduces a new data type called streams. It doubles as a communication channel for building streaming architectures and as a log-like data structure for persisting data. With iovalkey, the usage can be pretty straightforward. Say we have a producer publishes messages to a stream with `valkey.xadd("mystream", "*", "randomValue", Math.random())` (You may find the [official documentation of Streams](https://valkey.io/topics/streams-intro/) as a starter to understand the parameters used), to consume the messages, we'll have a consumer with the following code:
225231

226232
```javascript
227-
const Valkey = require("iovalkey");
233+
const { Valkey } = require("iovalkey");
234+
228235
const valkey = new Valkey();
229236

230237
const processMessage = (message) => {
@@ -449,6 +456,8 @@ care of script caching and to detect when to use `EVAL` and when to use `EVALSHA
449456
iovalkey exposes a `defineCommand` method to make scripting much easier to use:
450457

451458
```javascript
459+
const { Valkey } = require("iovalkey");
460+
452461
const valkey = new Valkey();
453462

454463
// This will define a command myecho:
@@ -495,6 +504,8 @@ valkey.echoDynamicKeyNumber(2, "k1", "k2", "a1", "a2", (err, result) => {
495504
Besides `defineCommand()`, you can also define custom commands with the `scripts` constructor option:
496505

497506
```javascript
507+
const { Valkey } = require("iovalkey");
508+
498509
const valkey = new Valkey({
499510
scripts: {
500511
myecho: {
@@ -519,6 +530,8 @@ namespaces.
519530
and this feature also won't apply to the replies of commands even if they are key names ([#325](https://github.com/mcollina/iovalkey/issues/325)).
520531

521532
```javascript
533+
const { Valkey } = require("iovalkey");
534+
522535
const fooValkey = new Valkey({ keyPrefix: "foo:" });
523536
fooValkey.set("bar", "baz"); // Actually sends SET foo:bar baz
524537

@@ -549,15 +562,17 @@ iovalkey has a flexible system for transforming arguments and replies. There are
549562
of transformers, argument transformer and reply transformer:
550563

551564
```javascript
552-
const Valkey = require("iovalkey");
565+
const { Command } = require("iovalkey");
566+
567+
const valkey = new Valkey();
553568

554569
// Here's the built-in argument transformer converting
555570
// hmset('key', { k1: 'v1', k2: 'v2' })
556571
// or
557572
// hmset('key', new Map([['k1', 'v1'], ['k2', 'v2']]))
558573
// into
559574
// hmset('key', 'k1', 'v1', 'k2', 'v2')
560-
Valkey.Command.setArgumentTransformer("hmset", (args) => {
575+
Command.setArgumentTransformer("hmset", (args) => {
561576
if (args.length === 2) {
562577
if (args[1] instanceof Map) {
563578
// utils is a internal module of iovalkey
@@ -574,7 +589,7 @@ Valkey.Command.setArgumentTransformer("hmset", (args) => {
574589
// ['k1', 'v1', 'k2', 'v2']
575590
// into
576591
// { k1: 'v1', 'k2': 'v2' }
577-
Valkey.Command.setReplyTransformer("hgetall", (result) => {
592+
Command.setReplyTransformer("hgetall", (result) => {
578593
if (Array.isArray(result)) {
579594
const obj = {};
580595
for (let i = 0; i < result.length; i += 2) {
@@ -591,6 +606,10 @@ a reply transformer for `hgetall`. Transformers for `hmset` and `hgetall` were m
591606
above, and the transformer for `mset` is similar to the one for `hmset`:
592607

593608
```javascript
609+
const { Valkey } = require("iovalkey");
610+
611+
const valkey = new Valkey();
612+
594613
valkey.mset({ k1: "v1", k2: "v2" });
595614
valkey.get("k1", (err, result) => {
596615
// result === 'v1';
@@ -610,7 +629,11 @@ valkey.get("k3", (err, result) => {
610629
Another useful example of a reply transformer is one that changes `hgetall` to return array of arrays instead of objects which avoids an unwanted conversation of hash keys to strings when dealing with binary hash keys:
611630

612631
```javascript
613-
Valkey.Command.setReplyTransformer("hgetall", (result) => {
632+
const { Valkey, Command } = require("iovalkey");
633+
634+
const valkey = new Valkey();
635+
636+
Command.setReplyTransformer("hgetall", (result) => {
614637
const arr = [];
615638
for (let i = 0; i < result.length; i += 2) {
616639
arr.push([result[i], result[i + 1]]);
@@ -935,9 +958,9 @@ Valkey Cluster provides a way to run a Valkey installation where data is automat
935958
You can connect to a Valkey Cluster like this:
936959

937960
```javascript
938-
const Valkey = require("iovalkey");
961+
const { Cluster } = require("iovalkey");
939962

940-
const cluster = new Valkey.Cluster([
963+
const cluster = new Cluster([
941964
{
942965
port: 6380,
943966
host: "127.0.0.1",
@@ -1015,7 +1038,8 @@ A typical valkey cluster contains three or more masters and several slaves for e
10151038
For example:
10161039

10171040
```javascript
1018-
const cluster = new Valkey.Cluster(
1041+
const { Cluster } = require("iovalkey");
1042+
const cluster = new Cluster(
10191043
[
10201044
/* nodes */
10211045
],
@@ -1062,7 +1086,9 @@ Sometimes the cluster is hosted within a internal network that can only be acces
10621086
You can specify nat mapping rules via `natMap` option:
10631087

10641088
```javascript
1065-
const cluster = new Valkey.Cluster(
1089+
const { Cluster } = require("iovalkey");
1090+
1091+
const cluster = new Cluster(
10661092
[
10671093
{
10681094
host: "203.0.113.73",
@@ -1081,7 +1107,9 @@ const cluster = new Valkey.Cluster(
10811107

10821108
Or you can specify this parameter through function:
10831109
```javascript
1084-
const cluster = new Redis.Cluster(
1110+
const { Cluster } = require("iovalkey");
1111+
1112+
const cluster = new Cluster(
10851113
[
10861114
{
10871115
host: "203.0.113.73",
@@ -1134,11 +1162,13 @@ When any commands in a pipeline receives a `MOVED` or `ASK` error, iovalkey will
11341162
Pub/Sub in cluster mode works exactly as the same as in standalone mode. Internally, when a node of the cluster receives a message, it will broadcast the message to the other nodes. iovalkey makes sure that each message will only be received once by strictly subscribing one node at the same time.
11351163

11361164
```javascript
1165+
const { Cluster } = require("iovalkey");
1166+
11371167
const nodes = [
11381168
/* nodes */
11391169
];
1140-
const pub = new Valkey.Cluster(nodes);
1141-
const sub = new Valkey.Cluster(nodes);
1170+
const pub = new Cluster(nodes);
1171+
const sub = new Cluster(nodes);
11421172
sub.on("message", (channel, message) => {
11431173
console.log(channel, message);
11441174
});
@@ -1167,8 +1197,8 @@ sub.subscribe("news", () => {
11671197
Setting the `password` option to access password-protected clusters:
11681198

11691199
```javascript
1170-
const Valkey = require("iovalkey");
1171-
const cluster = new Valkey.Cluster(nodes, {
1200+
const { Cluster } = require("iovalkey");
1201+
const cluster = new Cluster(nodes, {
11721202
redisOptions: {
11731203
password: "your-cluster-password",
11741204
},
@@ -1178,8 +1208,8 @@ const cluster = new Valkey.Cluster(nodes, {
11781208
If some of nodes in the cluster using a different password, you should specify them in the first parameter:
11791209

11801210
```javascript
1181-
const Valkey = require("iovalkey");
1182-
const cluster = new Valkey.Cluster(
1211+
const { Cluster } = require("iovalkey");
1212+
const cluster = new Cluster(
11831213
[
11841214
// Use password "password-for-30001" for 30001
11851215
{ port: 30001, password: "password-for-30001" },
@@ -1202,7 +1232,9 @@ this, you may encounter errors with invalid certificates. To resolve this
12021232
issue, construct the `Cluster` with the `dnsLookup` option as follows:
12031233

12041234
```javascript
1205-
const cluster = new Valkey.Cluster(
1235+
const { Cluster } = require("iovalkey");
1236+
1237+
const cluster = new Cluster(
12061238
[
12071239
{
12081240
host: "clustercfg.myCluster.abcdefg.xyz.cache.amazonaws.com",
@@ -1245,7 +1277,8 @@ Note that the same slot limitation within a single command still holds, as it is
12451277
This sample code uses iovalkey with automatic pipeline enabled.
12461278

12471279
```javascript
1248-
const Valkey = require("./built");
1280+
const { Valkey } = require("iovalkey");
1281+
12491282
const http = require("http");
12501283

12511284
const db = new Valkey({ enableAutoPipelining: true });
@@ -1309,11 +1342,12 @@ And here's the same test for a cluster of 3 masters and 3 replicas:
13091342
All the errors returned by the Valkey server are instances of `ReplyError`, which can be accessed via `Valkey`:
13101343

13111344
```javascript
1312-
const Valkey = require("iovalkey");
1345+
const { Valkey, ReplyError } = require("iovalkey");
1346+
13131347
const valkey = new Valkey();
13141348
// This command causes a reply error since the SET command requires two arguments.
13151349
valkey.set("foo", (err) => {
1316-
err instanceof Valkey.ReplyError;
1350+
err instanceof ReplyError;
13171351
});
13181352
```
13191353

@@ -1336,7 +1370,7 @@ iovalkey provides an option `showFriendlyErrorStack` to solve the problem. When
13361370
`showFriendlyErrorStack`, iovalkey will optimize the error stack for you:
13371371

13381372
```javascript
1339-
const Valkey = require("iovalkey");
1373+
const { Valkey } = require("iovalkey");
13401374
const valkey = new Valkey({ showFriendlyErrorStack: true });
13411375
valkey.set("foo");
13421376
```

0 commit comments

Comments
 (0)