Repository navigation
Expand file tree
/
Copy pathhimport.js
More file actions
73 lines (61 loc) 路 1.73 KB
/
Copy pathhimport.js
File metadata and controls
73 lines (61 loc) 路 1.73 KB
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
const Redis = require("..");
// Requires Redis 8.10 or newer.
// -----------------------------------------------------------------------------
// 1. Managed fieldsets: configure once and use for the client's lifetime
// -----------------------------------------------------------------------------
async function managedFieldsetsExample() {
const redis = new Redis({
himportFieldsets: [
{
name: "user-profile",
fields: ["name", "email", "age"],
},
],
});
try {
await redis.himport(
"SET",
"himport:managed:user:1",
"user-profile",
"Ada",
"ada@example.com",
"37"
);
console.log("Managed:", await redis.hgetall("himport:managed:user:1"));
} finally {
await redis.del("himport:managed:user:1");
redis.disconnect();
}
}
// -----------------------------------------------------------------------------
// 2. Manual batches: explicitly prepare, use, and discard a fieldset
// -----------------------------------------------------------------------------
async function manualBatchExample() {
const redis = new Redis();
try {
await redis.himport("PREPARE", "batch-user", "name", "email");
try {
await redis.himport(
"SET",
"himport:batch:user:1",
"batch-user",
"Grace",
"grace@example.com"
);
console.log("Manual:", await redis.hgetall("himport:batch:user:1"));
} finally {
await redis.himport("DISCARD", "batch-user");
}
} finally {
await redis.del("himport:batch:user:1");
redis.disconnect();
}
}
async function main() {
await managedFieldsetsExample();
await manualBatchExample();
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});