Skip to content

Commit 123024c

Browse files
committed
chore(examples): fix examples for v5
1 parent bd5c230 commit 123024c

20 files changed

+133
-75
lines changed

examples/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ We'd love to see more examples here. If you have an idea that you'd like to see
4040

4141
To set up the examples folder so that you can run an example / develop one of your own:
4242

43-
```
44-
$ git clone https://github.com/redis/node-redis.git
45-
$ cd node-redis
46-
$ npm install -ws && npm run build-all
47-
$ cd examples
48-
$ npm install
43+
```bash
44+
git clone https://github.com/redis/node-redis.git
45+
cd node-redis
46+
npm install -ws && npm run build
47+
cd examples
48+
npm install
4949
```
5050

5151
### Coding Guidelines for Examples
@@ -91,5 +91,5 @@ await client.connect();
9191

9292
// Add your example code here...
9393

94-
client.destroy();
94+
client.close();
9595
```

examples/blocking-list-pop.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44
// The script will be blocked until the LPUSH command is executed.
55
// After which we log the list and quit the client.
66

7-
import { createClient, commandOptions } from 'redis';
7+
import { createClientPool } from 'redis';
88

9-
const client = createClient();
9+
const client = createClientPool();
1010

1111
await client.connect();
1212

1313
const keyName = 'keyName';
1414

1515
const blpopPromise = client.blPop(
16-
commandOptions({ isolated: true }),
1716
keyName,
1817
0
1918
);

examples/command-with-modifiers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Define a custom script that shows example of SET command
22
// with several modifiers.
33

4-
import { createClient } from '../packages/client';
4+
import { createClient } from 'redis';
55

66
const client = createClient();
77

examples/connect-to-cluster.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// This is an example script to connect to a running cluster.
22
// After connecting to the cluster the code sets and get a value.
33

4-
// To setup this cluster you can follow the guide here:
4+
// To setup this cluster you can follow the guide here:
55
// https://redis.io/docs/manual/scaling/
66
// In this guide the ports which are being used are 7000 - 7005
77

@@ -29,5 +29,4 @@ await cluster.connect();
2929
await cluster.set('hello', 'cluster');
3030
const value = await cluster.get('hello');
3131
console.log(value);
32-
33-
await cluster.quit();
32+
await cluster.close();

examples/dump-and-restore.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ const client = await createClient({
1212
console.log('Redis Client Error', err);
1313
}).connect();
1414

15+
// Make sure the source key exists
16+
await client.set('source', 'value');
17+
18+
// Make sure destination doesnt exist
19+
await client.del('destination');
20+
1521
// DUMP a specific key into a local variable
1622
const dump = await client.dump('source');
1723

examples/get-server-time.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ const client = createClient();
66
await client.connect();
77

88
const serverTime = await client.time();
9-
// 2022-02-25T12:57:40.000Z { microseconds: 351346 }
9+
// In v5, TIME returns [unixTimestamp: string, microseconds: string] instead of Date
10+
// Example: ['1708956789', '123456']
1011
console.log(serverTime);
1112

12-
client.destroy();
13+
// Convert to JavaScript Date if needed
14+
const [seconds, microseconds] = serverTime;
15+
const date = new Date(parseInt(seconds) * 1000 + parseInt(microseconds) / 1000);
16+
console.log('Converted to Date:', date);
17+
18+
client.close();

examples/hyperloglog.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const client = createClient();
99
await client.connect();
1010

1111
// Use `pfAdd` to add an element to a Hyperloglog, creating the Hyperloglog if necessary.
12-
// await client.pfAdd(key, value)
12+
// await client.pfAdd(key, value) // returns 1 or 0
1313

1414
// To get a count, the `pfCount` method is used.
1515
// await client.pfCount(key)
@@ -48,4 +48,4 @@ try {
4848
console.error(e);
4949
}
5050

51-
client.destroy();
51+
client.close();

examples/lua-multi-incr.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ const client = createClient({
1212
'redis.pcall("INCRBY", KEYS[1], ARGV[1]),' +
1313
'redis.pcall("INCRBY", KEYS[2], ARGV[1])' +
1414
'}',
15-
transformArguments(key1, key2, increment) {
16-
return [key1, key2, increment.toString()];
17-
},
15+
parseCommand(parser, key1, key2, increment) {
16+
parser.pushKey(key1);
17+
parser.pushKey(key2);
18+
parser.push(increment.toString());
19+
},
1820
}),
1921
},
2022
});

examples/managing-json.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ results = await client.json.get('noderedis:jsondata', {
5757
});
5858

5959
// Goldie is 3 years old now.
60-
console.log(`Goldie is ${JSON.stringify(results[0])} years old now.`);
60+
console.log(`Goldie is ${JSON.parse(results)[0]} years old now.`);
6161

6262
// Add a new pet...
6363
await client.json.arrAppend('noderedis:jsondata', '$.pets', {
@@ -68,9 +68,14 @@ await client.json.arrAppend('noderedis:jsondata', '$.pets', {
6868
});
6969

7070
// How many pets do we have now?
71-
const numPets = await client.json.arrLen('noderedis:jsondata', '$.pets');
71+
const numPets = await client.json.arrLen('noderedis:jsondata', { path: '$.pets' });
7272

7373
// We now have 4 pets.
7474
console.log(`We now have ${numPets} pets.`);
7575

76-
client.destroy();
76+
const rex = { name: 'Rex', species: 'dog', age: 3, isMammal: true }
77+
78+
const index = await client.json.arrIndex( 'noderedis:jsondata', '$.pets', rex);
79+
console.log(`Rex is at index ${index}`);
80+
81+
client.close();

examples/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"name": "node-redis-examples",
33
"version": "1.0.0",
4-
"description": "node-redis 4 example script",
4+
"description": "node-redis 5 example script",
55
"main": "index.js",
66
"private": true,
77
"type": "module",
88
"dependencies": {
9-
"redis": "../packages/client"
9+
"redis": "../packages/redis"
1010
}
1111
}
1212

0 commit comments

Comments
 (0)