Skip to content

Commit d268364

Browse files
committed
Add hash tutorial
1 parent 0588b38 commit d268364

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

doctests/hash-tutorial.js

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// EXAMPLE: hash_tutorial
2+
// REMOVE_START
3+
import assert from 'assert';
4+
// REMOVE_END
5+
6+
// HIDE_START
7+
import { createClient } from 'redis';
8+
9+
const client = createClient();
10+
11+
client.on('error', err => console.log('Redis Client Error', err));
12+
13+
await client.connect();
14+
// HIDE_END
15+
16+
// STEP_START set_get_all
17+
const fieldsAdded = await client.hSet(
18+
'bike:1',
19+
{
20+
model: 'Deimos',
21+
brand: 'Ergonom',
22+
type: 'Enduro bikes',
23+
price: 4972,
24+
},
25+
)
26+
console.log(`Number of fields were added: ${fieldsAdded}`);
27+
// Number of fields were added: 4
28+
//REMOVE_START
29+
assert.equal(fieldsAdded, 4);
30+
//REMOVE_END
31+
32+
const model = await client.hGet('bike:1', 'model');
33+
console.log(`Model: ${model}`);
34+
// Model: Deimos
35+
// REMOVE_START
36+
assert.equal(model, 'Deimos');
37+
// REMOVE_END
38+
39+
const price = await client.hGet('bike:1', 'price');
40+
console.log(`Price: ${price}`);
41+
// Price: 4972
42+
// REMOVE_START
43+
assert.equal(price, '4972');
44+
// REMOVE_END
45+
46+
const bike = await client.hGetAll('bike:1');
47+
console.log(bike);
48+
// {
49+
// model: 'Deimos',
50+
// brand: 'Ergonom',
51+
// type: 'Enduro bikes',
52+
// price: '4972'
53+
// }
54+
// REMOVE_START
55+
assert.equal(Object.keys(bike).length, 4);
56+
// REMOVE_END
57+
// STEP_END
58+
59+
// STEP_START hmget
60+
const fields = await client.hmGet('bike:1', ['model', 'price']);
61+
console.log(fields);
62+
// [ 'Deimos', '4972' ]
63+
// REMOVE_START
64+
assert.equal(fields.length, 2);
65+
// REMOVE_END
66+
// STEP_END
67+
68+
// STEP_START hincrby
69+
let newPrice = await client.hIncrBy('bike:1', 'price', 100);
70+
console.log(newPrice);
71+
// 5072
72+
// REMOVE_START
73+
assert.equal(newPrice, 5072);
74+
// REMOVE_END
75+
newPrice = await client.hIncrBy('bike:1', 'price', -100);
76+
console.log(newPrice);
77+
// 4972
78+
// REMOVE_START
79+
assert.equal(newPrice, 4972);
80+
// REMOVE_END
81+
// STEP_END
82+
83+
// STEP_START incrby_get_mget
84+
let rides = await client.hIncrBy('bike:1:stats', 'rides', 1);
85+
console.log(rides);
86+
// 1
87+
88+
rides = await client.hIncrBy('bike:1:stats', 'rides', 1);
89+
console.log(rides);
90+
// 2
91+
92+
rides = await client.hIncrBy('bike:1:stats', 'rides', 1);
93+
console.log(rides);
94+
// 3
95+
96+
let crashes = await client.hIncrBy('bike:1:stats', 'crashes', 1);
97+
console.log(crashes);
98+
// 1
99+
100+
let owners = await client.hIncrBy('bike:1:stats', 'owners', 1);
101+
console.log(owners);
102+
// 1
103+
104+
rides = await client.hGet('bike:1:stats', 'rides');
105+
console.log(`Total rides: ${rides}`);
106+
// Total rides: 3
107+
// REMOVE_START
108+
assert.equal(rides, 3);
109+
// REMOVE_END
110+
const stats = await client.hmGet('bike:1:stats', ['crashes', 'owners']);
111+
console.log(`Bike stats: crashes=${stats[0]}, owners=${stats[1]}`);
112+
// Bike stats: crashes=1, owners=1
113+
// REMOVE_START
114+
assert.equal(stats.length, 2);
115+
// REMOVE_END
116+
// STEP_END
117+
118+
// HIDE_START
119+
await client.quit();
120+
// HIDE_END

0 commit comments

Comments
 (0)