-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathcmds-hash.js
109 lines (90 loc) · 2.11 KB
/
cmds-hash.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// EXAMPLE: cmds_hash
// HIDE_START
import assert from 'node:assert';
import { createClient } from 'redis';
const client = createClient();
await client.connect().catch(console.error);
// HIDE_END
// STEP_START hset
const res1 = await client.hSet('myhash', 'field1', 'Hello')
console.log(res1) // 1
const res2 = await client.hGet('myhash', 'field1')
console.log(res2) // Hello
const res3 = await client.hSet(
'myhash',
{
'field2': 'Hi',
'field3': 'World'
}
)
console.log(res3) // 2
const res4 = await client.hGet('myhash', 'field2')
console.log(res4) // Hi
const res5 = await client.hGet('myhash', 'field3')
console.log(res5) // World
const res6 = await client.hGetAll('myhash')
console.log(res6)
// REMOVE_START
assert.equal(res1, 1);
assert.equal(res2, 'Hello');
assert.equal(res3, 2);
assert.equal(res4, 'Hi');
assert.equal(res5, 'World');
assert.deepEqual(res6, {
field1: 'Hello',
field2: 'Hi',
field3: 'World'
});
await client.del('myhash')
// REMOVE_END
// STEP_END
// STEP_START hget
const res7 = await client.hSet('myhash', 'field1', 'foo')
console.log(res7) // 1
const res8 = await client.hGet('myhash', 'field1')
console.log(res8) // foo
const res9 = await client.hGet('myhash', 'field2')
console.log(res9) // foo
// REMOVE_START
assert.equal(res7, 1);
assert.equal(res8, 'foo');
assert.equal(res9, null);
await client.del('myhash')
// REMOVE_END
// STEP_END
// STEP_START hgetall
const res10 = await client.hSet(
'myhash',
{
'field1': 'Hello',
'field2': 'World'
}
)
const res11 = await client.hGetAll('myhash')
console.log(res11) // [Object: null prototype] { field1: 'Hello', field2: 'World' }
// REMOVE_START
assert.deepEqual(res11, {
field1: 'Hello',
field2: 'World'
});
await client.del('myhash')
// REMOVE_END
// STEP_END
// STEP_START hvals
const res12 = await client.hSet(
'myhash',
{
'field1': 'Hello',
'field2': 'World'
}
)
const res13 = await client.hVals('myhash')
console.log(res13) // [ 'Hello', 'World' ]
// REMOVE_START
assert.deepEqual(res13, [ 'Hello', 'World' ]);
await client.del('myhash')
// REMOVE_END
// STEP_END
// HIDE_START
await client.quit();
// HIDE_END