1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-01 16:46:54 +03:00
Files
node-redis/doctests/dt-bitfield.js
2024-07-03 22:29:13 +03:00

77 lines
1.3 KiB
JavaScript

// EXAMPLE: bitfield_tutorial
// HIDE_START
import assert from 'assert';
import { createClient } from 'redis';
const client = createClient();
await client.connect();
// HIDE_END
// REMOVE_START
await client.flushDb();
// REMOVE_END
// STEP_START bf
let res1 = await client.bitField("bike:1:stats", [{
operation: 'SET',
encoding: 'u32',
offset: '#0',
value: 1000
}]);
console.log(res1); // >>> [0]
let res2 = await client.bitField('bike:1:stats', [
{
operation: 'INCRBY',
encoding: 'u32',
offset: '#0',
increment: -50
},
{
operation: 'INCRBY',
encoding: 'u32',
offset: '#1',
increment: 1
}
]);
console.log(res2); // >>> [950, 1]
let res3 = await client.bitField('bike:1:stats', [
{
operation: 'INCRBY',
encoding: 'u32',
offset: '#0',
increment: 500
},
{
operation: 'INCRBY',
encoding: 'u32',
offset: '#1',
increment: 1
}
]);
console.log(res3); // >>> [1450, 2]
let res4 = await client.bitField('bike:1:stats', [
{
operation: 'GET',
encoding: 'u32',
offset: '#0'
},
{
operation: 'GET',
encoding: 'u32',
offset: '#1'
}
]);
console.log(res4); // >>> [1450, 2]
// STEP_END
// REMOVE_START
assert.deepEqual(res1, [0])
assert.deepEqual(res2, [950, 1])
assert.deepEqual(res3, [1450, 2])
assert.deepEqual(res4, [1450, 2])
await client.quit();
// REMOVE_END