1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-07 13:22:56 +03:00

DOC-3914: re-issue TCE PRs previously issued by justincastilla (#2781)

This commit is contained in:
David Dougherty
2024-07-03 12:29:04 -07:00
committed by GitHub
parent 70f3606a3d
commit e7347f8a51
8 changed files with 1154 additions and 146 deletions

39
doctests/dt-bitmap.js Normal file
View File

@@ -0,0 +1,39 @@
// EXAMPLE: bitmap_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 ping
const res1 = await client.setBit("pings:2024-01-01-00:00", 123, 1)
console.log(res1) // >>> 0
const res2 = await client.getBit("pings:2024-01-01-00:00", 123)
console.log(res2) // >>> 1
const res3 = await client.getBit("pings:2024-01-01-00:00", 456)
console.log(res3) // >>> 0
// STEP_END
// REMOVE_START
assert.equal(res1, 0)
// REMOVE_END
// STEP_START bitcount
// HIDE_START
await client.setBit("pings:2024-01-01-00:00", 123, 1)
// HIDE_END
const res4 = await client.bitCount("pings:2024-01-01-00:00")
console.log(res4) // >>> 1
// STEP_END
// REMOVE_START
assert.equal(res4, 1)
await client.quit();
// REMOVE_END

98
doctests/dt-hash.js Normal file
View File

@@ -0,0 +1,98 @@
// EXAMPLE: hash_tutorial
// HIDE_START
import assert from 'assert';
import { createClient } from 'redis';
const client = createClient();
await client.connect();
// HIDE_END
// STEP_START set_get_all
const res1 = await client.hSet(
'bike:1',
{
'model': 'Deimos',
'brand': 'Ergonom',
'type': 'Enduro bikes',
'price': 4972,
}
)
console.log(res1) // 4
const res2 = await client.hGet('bike:1', 'model')
console.log(res2) // 'Deimos'
const res3 = await client.hGet('bike:1', 'price')
console.log(res3) // '4972'
const res4 = await client.hGetAll('bike:1')
console.log(res4)
/*
{
brand: 'Ergonom',
model: 'Deimos',
price: '4972',
type: 'Enduro bikes'
}
*/
// STEP_END
// REMOVE_START
assert.equal(res1, 4);
assert.equal(res2, 'Deimos');
assert.equal(res3, '4972');
assert.deepEqual(res4, {
model: 'Deimos',
brand: 'Ergonom',
type: 'Enduro bikes',
price: '4972'
});
// REMOVE_END
// STEP_START hmGet
const res5 = await client.hmGet('bike:1', ['model', 'price'])
console.log(res5) // ['Deimos', '4972']
// STEP_END
// REMOVE_START
assert.deepEqual(Object.values(res5), ['Deimos', '4972'])
// REMOVE_END
// STEP_START hIncrBy
const res6 = await client.hIncrBy('bike:1', 'price', 100)
console.log(res6) // 5072
const res7 = await client.hIncrBy('bike:1', 'price', -100)
console.log(res7) // 4972
// STEP_END
// REMOVE_START
assert.equal(res6, 5072)
assert.equal(res7, 4972)
// REMOVE_END
// STEP_START hIncrBy_hGet_hMget
const res11 = await client.hIncrBy('bike:1:stats', 'rides', 1)
console.log(res11) // 1
const res12 = await client.hIncrBy('bike:1:stats', 'rides', 1)
console.log(res12) // 2
const res13 = await client.hIncrBy('bike:1:stats', 'rides', 1)
console.log(res13) // 3
const res14 = await client.hIncrBy('bike:1:stats', 'crashes', 1)
console.log(res14) // 1
const res15 = await client.hIncrBy('bike:1:stats', 'owners', 1)
console.log(res15) // 1
const res16 = await client.hGet('bike:1:stats', 'rides')
console.log(res16) // 3
const res17 = await client.hmGet('bike:1:stats', ['crashes', 'owners'])
console.log(res17) // ['1', '1']
// STEP_END
// REMOVE_START
assert.equal(res11, 1);
assert.equal(res12, 2);
assert.equal(res13, 3);
assert.equal(res14, 1);
assert.equal(res15, 1);
assert.equal(res16, '3');
assert.deepEqual(res17, ['1', '1']);
await client.quit();
// REMOVE_END

425
doctests/dt-json.js Normal file
View File

@@ -0,0 +1,425 @@
// EXAMPLE: json_tutorial
// HIDE_START
import assert from 'assert';
import {
createClient
} from 'redis';
const client = await createClient();
await client.connect();
// HIDE_END
// REMOVE_START
await client.flushDb();
// REMOVE_END
// STEP_START set_get
const res1 = await client.json.set("bike", "$", '"Hyperion"');
console.log(res1); // OK
const res2 = await client.json.get("bike", "$");
console.log(res2); // "Hyperion"
const res3 = await client.json.type("bike", "$");
console.log(res3); // [ 'string' ]
// STEP_END
// REMOVE_START
assert.equal(res2, '"Hyperion"');
// REMOVE_END
// STEP_START str
const res4 = await client.json.strLen("bike", "$");
console.log(res4) // [10]
const res5 = await client.json.strAppend("bike", '" (Enduro bikes)"');
console.log(res5) // 27
const res6 = await client.json.get("bike", "$");
console.log(res6) // ['"Hyperion"" (Enduro bikes)"']
// STEP_END
// REMOVE_START
assert.equal(res6, ['"Hyperion"" (Enduro bikes)"']);
// REMOVE_END
// STEP_START num
const res7 = await client.json.set("crashes", "$", 0);
console.log(res7) // OK
const res8 = await client.json.numIncrBy("crashes", "$", 1);
console.log(res8) // [1]
const res9 = await client.json.numIncrBy("crashes", "$", 1.5);
console.log(res9) // [2.5]
const res10 = await client.json.numIncrBy("crashes", "$", -0.75);
console.log(res10) // [1.75]
// STEP_END
// REMOVE_START
assert.deepEqual(res10, [1.75])
// REMOVE_END
// STEP_START arr
const res11 = await client.json.set("newbike", "$", ["Deimos", {"crashes": 0 }, null]);
console.log(res11); // OK
const res12 = await client.json.get("newbike", "$");
console.log(res12); // [ 'Deimos', { crashes: 0 }, null ]
const res13 = await client.json.get("newbike", "$[1].crashes");
console.log(res13); // [ 'Deimos', { crashes: 0 }, null ]
const res14 = await client.json.del("newbike", "$.[-1]");
console.log(res14); // [1]
const res15 = await client.json.get("newbike", "$");
console.log(res15); // [ 'Deimos', { crashes: 0 } ]
// STEP_END
// REMOVE_START
assert.deepEqual(res15, ["Deimos", {
"crashes": 0
}]);
// REMOVE_END
// STEP_START arr2
const res16 = await client.json.set("riders", "$", []);
console.log(res16); // OK
const res17 = await client.json.arrAppend("riders", "$", "Norem");
console.log(res17); // [1]
const res18 = await client.json.get("riders", "$");
console.log(res18); // [ 'Norem' ]
const res19 = await client.json.arrInsert("riders", "$", 1, "Prickett", "Royse", "Castilla");
console.log(res19); // [4]
const res20 = await client.json.get("riders", "$");
console.log(res20); // [ 'Norem', 'Prickett', 'Royse', 'Castilla' ]
const res21 = await client.json.arrTrim("riders", "$", 1, 1);
console.log(res21); // [1]
const res22 = await client.json.get("riders", "$");
console.log(res22); // [ 'Prickett' ]
const res23 = await client.json.arrPop("riders", "$");
console.log(res23); // [ 'Prickett' ]
const res24 = await client.json.arrPop("riders", "$");
console.log(res24); // [null]
// STEP_END
// REMOVE_START
assert.deepEqual(res24, [null]);
// REMOVE_END
// STEP_START obj
const res25 = await client.json.set(
"bike:1", "$", {
"model": "Deimos",
"brand": "Ergonom",
"price": 4972
}
);
console.log(res25); // OK
const res26 = await client.json.objLen("bike:1", "$");
console.log(res26); // [3]
const res27 = await client.json.objKeys("bike:1", "$");
console.log(res27); // [['model', 'brand', 'price']]
// STEP_END
// REMOVE_START
assert.deepEqual(res27, [
["model", "brand", "price"]
]);
// REMOVE_END
// STEP_START set_bikes
// HIDE_START
const inventoryJSON = {
"inventory": {
"mountain_bikes": [{
"id": "bike:1",
"model": "Phoebe",
"description": "This is a mid-travel trail slayer that is a fantastic daily driver or one bike quiver. The Shimano Claris 8-speed groupset gives plenty of gear range to tackle hills and there\u2019s room for mudguards and a rack too. This is the bike for the rider who wants trail manners with low fuss ownership.",
"price": 1920,
"specs": {
"material": "carbon",
"weight": 13.1
},
"colors": ["black", "silver"],
},
{
"id": "bike:2",
"model": "Quaoar",
"description": "Redesigned for the 2020 model year, this bike impressed our testers and is the best all-around trail bike we've ever tested. The Shimano gear system effectively does away with an external cassette, so is super low maintenance in terms of wear and teaawait client. All in all it's an impressive package for the price, making it very competitive.",
"price": 2072,
"specs": {
"material": "aluminium",
"weight": 7.9
},
"colors": ["black", "white"],
},
{
"id": "bike:3",
"model": "Weywot",
"description": "This bike gives kids aged six years and older a durable and uberlight mountain bike for their first experience on tracks and easy cruising through forests and fields. A set of powerful Shimano hydraulic disc brakes provide ample stopping ability. If you're after a budget option, this is one of the best bikes you could get.",
"price": 3264,
"specs": {
"material": "alloy",
"weight": 13.8
},
},
],
"commuter_bikes": [{
"id": "bike:4",
"model": "Salacia",
"description": "This bike is a great option for anyone who just wants a bike to get about on With a slick-shifting Claris gears from Shimano\u2019s, this is a bike which doesn\u2019t break the bank and delivers craved performance. It\u2019s for the rider who wants both efficiency and capability.",
"price": 1475,
"specs": {
"material": "aluminium",
"weight": 16.6
},
"colors": ["black", "silver"],
},
{
"id": "bike:5",
"model": "Mimas",
"description": "A real joy to ride, this bike got very high scores in last years Bike of the year report. The carefully crafted 50-34 tooth chainset and 11-32 tooth cassette give an easy-on-the-legs bottom gear for climbing, and the high-quality Vittoria Zaffiro tires give balance and grip.It includes a low-step frame , our memory foam seat, bump-resistant shocks and conveniently placed thumb throttle. Put it all together and you get a bike that helps redefine what can be done for this price.",
"price": 3941,
"specs": {
"material": "alloy",
"weight": 11.6
},
},
],
}
};
// HIDE_END
const res28 = await client.json.set("bikes:inventory", "$", inventoryJSON);
console.log(res28); // OK
// STEP_END
// STEP_START get_bikes
const res29 = await client.json.get("bikes:inventory", {
path: "$.inventory.*"
});
console.log(res29);
/*
[
[
{
id: 'bike:1',
model: 'Phoebe',
description: 'This is a mid-travel trail slayer that is a fantastic daily driver or one bike quiver. The Shimano Claris 8-speed groupset gives plenty of gear range to tackle hills and theres room for mudguards and a rack too. This is the bike for the rider who wants trail manners with low fuss ownership.',
price: 1920,
specs: [Object],
colors: [Array]
},
{
id: 'bike:2',
model: 'Quaoar',
description: "Redesigned for the 2020 model year, this bike impressed our testers and is the best all-around trail bike we've ever tested. The Shimano gear system effectively does away with an external cassette, so is super low maintenance in terms of wear and teaawait client. All in all it's an impressive package for the price, making it very competitive.",
price: 2072,
specs: [Object],
colors: [Array]
},
{
id: 'bike:3',
model: 'Weywot',
description: "This bike gives kids aged six years and older a durable and uberlight mountain bike for their first experience on tracks and easy cruising through forests and fields. A set of powerful Shimano hydraulic disc brakes provide ample stopping ability. If you're after a budget option, this is one of the best bikes you could get.",
price: 3264,
specs: [Object]
}
],
[
{
id: 'bike:4',
model: 'Salacia',
description: 'This bike is a great option for anyone who just wants a bike to get about on With a slick-shifting Claris gears from Shimanos, this is a bike which doesnt break the bank and delivers craved performance. Its for the rider who wants both efficiency and capability.',
price: 1475,
specs: [Object],
colors: [Array]
},
{
id: 'bike:5',
model: 'Mimas',
description: 'A real joy to ride, this bike got very high scores in last years Bike of the year report. The carefully crafted 50-34 tooth chainset and 11-32 tooth cassette give an easy-on-the-legs bottom gear for climbing, and the high-quality Vittoria Zaffiro tires give balance and grip.It includes a low-step frame , our memory foam seat, bump-resistant shocks and conveniently placed thumb throttle. Put it all together and you get a bike that helps redefine what can be done for this price.',
price: 3941,
specs: [Object]
}
]
]
*/
// STEP_END
// STEP_START get_mtnbikes
const res30 = await client.json.get("bikes:inventory", {
path: "$.inventory.mountain_bikes[*].model"
});
console.log(res30); // ['Phoebe', 'Quaoar', 'Weywot']
const res31 = await client.json.get("bikes:inventory", {
path: '$.inventory["mountain_bikes"][*].model'
});
console.log(res31); // ['Phoebe', 'Quaoar', 'Weywot']
const res32 = await client.json.get("bikes:inventory", {
path: "$..mountain_bikes[*].model"
});
console.log(res32); // ['Phoebe', 'Quaoar', 'Weywot']
// STEP_END
// REMOVE_START
assert.deepEqual(res30, ["Phoebe", "Quaoar", "Weywot"]);
assert.deepEqual(res31, ["Phoebe", "Quaoar", "Weywot"]);
assert.deepEqual(res32, ["Phoebe", "Quaoar", "Weywot"]);
// REMOVE_END
// STEP_START get_models
const res33 = await client.json.get("bikes:inventory", {
path: "$..model"
});
console.log(res33); // ['Phoebe', 'Quaoar', 'Weywot', 'Salacia', 'Mimas']
// STEP_END
// REMOVE_START
assert.deepEqual(res33, ["Phoebe", "Quaoar", "Weywot", "Salacia", "Mimas"]);
// REMOVE_END
// STEP_START get2mtnbikes
const res34 = await client.json.get("bikes:inventory", {
path: "$..mountain_bikes[0:2].model"
});
console.log(res34); // ['Phoebe', 'Quaoar']
// STEP_END
// REMOVE_START
assert.deepEqual(res34, ["Phoebe", "Quaoar"]);
// REMOVE_END
// STEP_START filter1
const res35 = await client.json.get("bikes:inventory", {
path: "$..mountain_bikes[?(@.price < 3000 && @.specs.weight < 10)]"
});
console.log(res35);
/*
[
{
id: 'bike:2',
model: 'Quaoar',
description: "Redesigned for the 2020 model year, this bike impressed our testers and is the best all-around trail bike we've ever tested. The Shimano gear system effectively does away with an external cassette, so is super low maintenance in terms of wear and teaawait client. All in all it's an impressive package for the price, making it very competitive.",
price: 2072,
specs: { material: 'aluminium', weight: 7.9 },
colors: [ 'black', 'white' ]
}
]
*/
// STEP_END
// STEP_START filter2
// names of bikes made from an alloy
const res36 = await client.json.get("bikes:inventory", {
path: "$..[?(@.specs.material == 'alloy')].model"
});
console.log(res36); // ['Weywot', 'Mimas']
// STEP_END
// REMOVE_START
assert.deepEqual(res36, ["Weywot", "Mimas"]);
// REMOVE_END
// STEP_START filter3
const res37 = await client.json.get("bikes:inventory", {
path: "$..[?(@.specs.material =~ '(?i)al')].model"
});
console.log(res37); // ['Quaoar', 'Weywot', 'Salacia', 'Mimas']
// STEP_END
// REMOVE_START
assert.deepEqual(res37, ["Quaoar", "Weywot", "Salacia", "Mimas"]);
// REMOVE_END
// STEP_START filter4
const res37a = await client.json.set(
'bikes:inventory',
'$.inventory.mountain_bikes[0].regex_pat',
'(?i)al'
);
const res37b = await client.json.set(
'bikes:inventory',
'$.inventory.mountain_bikes[1].regex_pat',
'(?i)al'
);
const res37c = await client.json.set(
'bikes:inventory',
'$.inventory.mountain_bikes[2].regex_pat',
'(?i)al'
);
const res37d = await client.json.get(
'bikes:inventory',
'$.inventory.mountain_bikes[?(@.specs.material =~ @.regex_pat)].model'
);
console.log(res37d); // ['Quaoar', 'Weywot']
// STEP_END
// STEP_START update_bikes
const res38 = await client.json.get("bikes:inventory", {
path: "$..price"
});
console.log(res38); // [1920, 2072, 3264, 1475, 3941]
const res39 = await client.json.numIncrBy("bikes:inventory", "$..price", -100);
console.log(res39); // [1820, 1972, 3164, 1375, 3841]
const res40 = await client.json.numIncrBy("bikes:inventory", "$..price", 100);
console.log(res40); // [1920, 2072, 3264, 1475, 3941]
// STEP_END
// REMOVE_START
assert.deepEqual(res40.sort(), [1475, 1920, 2072, 3264, 3941]);
// REMOVE_END
// STEP_START update_filters1
const res40a = await client.json.set(
'bikes:inventory',
'$.inventory.*[?(@.price<2000)].price',
1500
);
// Get all prices from the inventory
const res40b = await client.json.get(
'bikes:inventory',
'$..price'
);
console.log(res40b); // [1500, 2072, 3264, 1500, 3941]
// STEP_END
// STEP_START update_filters2
const res41 = await client.json.arrAppend(
"bikes:inventory", "$.inventory.*[?(@.price<2000)].colors", "pink"
);
console.log(res41); // [3, 3]
const res42 = await client.json.get("bikes:inventory", {
path: "$..[*].colors"
});
console.log(res42); // [['black', 'silver', 'pink'], ['black', 'white'], ['black', 'silver', 'pink']]
// STEP_END
// REMOVE_START
assert.deepEqual(res42, [
["black", "silver", "pink"],
["black", "white"],
["black", "silver", "pink"],
]);
await client.quit();
// REMOVE_END

178
doctests/dt-set.js Normal file
View File

@@ -0,0 +1,178 @@
// EXAMPLE: sets_tutorial
// HIDE_START
import assert from 'assert';
import { createClient } from 'redis';
const client = await createClient();
await client.connect();
// HIDE_END
// REMOVE_START
await client.del('bikes:racing:france')
await client.del('bikes:racing:usa')
// REMOVE_END
// STEP_START sAdd
const res1 = await client.sAdd('bikes:racing:france', 'bike:1')
console.log(res1) // >>> 1
const res2 = await client.sAdd('bikes:racing:france', 'bike:1')
console.log(res2) // >>> 0
const res3 = await client.sAdd('bikes:racing:france', ['bike:2', 'bike:3'])
console.log(res3) // >>> 2
const res4 = await client.sAdd('bikes:racing:usa', ['bike:1', 'bike:4'])
console.log(res4) // >>> 2
// STEP_END
// REMOVE_START
assert.equal(res1, 1)
assert.equal(res2, 0)
assert.equal(res3, 2)
assert.equal(res4, 2)
// REMOVE_END
// STEP_START sIsMember
// HIDE_START
await client.del('bikes:racing:france')
await client.del('bikes:racing:usa')
await client.sAdd('bikes:racing:france', 'bike:1', 'bike:2', 'bike:3')
await client.sAdd('bikes:racing:usa', 'bike:1', 'bike:4')
// HIDE_END
const res5 = await client.sIsMember('bikes:racing:usa', 'bike:1')
console.log(res5) // >>> true
const res6 = await client.sIsMember('bikes:racing:usa', 'bike:2')
console.log(res6) // >>> false
// STEP_END
// REMOVE_START
assert.equal(res5, true)
assert.equal(res6, false)
// REMOVE_END
// STEP_START sinster
// HIDE_START
await client.del('bikes:racing:france')
await client.del('bikes:racing:usa')
await client.sAdd('bikes:racing:france', 'bike:1', 'bike:2', 'bike:3')
await client.sAdd('bikes:racing:usa', 'bike:1', 'bike:4')
// HIDE_END
const res7 = await client.sInter('bikes:racing:france', 'bikes:racing:usa')
console.log(res7) // >>> {'bike:1'}
// STEP_END
// REMOVE_START
assert.deepEqual(res7, [ 'bike:1' ])
// REMOVE_END
// STEP_START sCard
// HIDE_START
await client.del('bikes:racing:france')
await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3'])
// HIDE_END
const res8 = await client.sCard('bikes:racing:france')
console.log(res8) // >>> 3
// STEP_END
// REMOVE_START
assert.equal(res8, 3)
await client.del('bikes:racing:france')
// REMOVE_END
// STEP_START sAdd_sMembers
const res9 = await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3'])
console.log(res9) // >>> 3
const res10 = await client.sMembers('bikes:racing:france')
console.log(res10) // >>> ['bike:1', 'bike:2', 'bike:3']
// STEP_END
// REMOVE_START
assert.equal(res9, 3)
assert.deepEqual(res10.sort(), ['bike:1', 'bike:2', 'bike:3'])
// REMOVE_END
// STEP_START smIsMember
const res11 = await client.sIsMember('bikes:racing:france', 'bike:1')
console.log(res11) // >>> true
const res12 = await client.smIsMember('bikes:racing:france', ['bike:2', 'bike:3', 'bike:4'])
console.log(res12) // >>> [true, true, false]
// STEP_END
// REMOVE_START
assert.equal(res11, true)
assert.deepEqual(res12, [true, true, false])
// REMOVE_END
// STEP_START sDiff
await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3'])
await client.sAdd('bikes:racing:usa', ['bike:1', 'bike:4'])
const res13 = await client.sDiff(['bikes:racing:france', 'bikes:racing:usa'])
console.log(res13) // >>> [ 'bike:2', 'bike:3' ]
// STEP_END
// REMOVE_START
assert.deepEqual(res13, ['bike:2', 'bike:3'])
await client.del('bikes:racing:france')
await client.del('bikes:racing:usa')
// REMOVE_END
// STEP_START multisets
await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3'])
await client.sAdd('bikes:racing:usa', ['bike:1', 'bike:4'])
await client.sAdd('bikes:racing:italy', ['bike:1', 'bike:2', 'bike:3', 'bike:4'])
const res14 = await client.sInter(
['bikes:racing:france', 'bikes:racing:usa', 'bikes:racing:italy']
)
console.log(res14) // >>> ['bike:1']
const res15 = await client.sUnion(
['bikes:racing:france', 'bikes:racing:usa', 'bikes:racing:italy']
)
console.log(res15) // >>> ['bike:1', 'bike:2', 'bike:3', 'bike:4']
const res16 = await client.sDiff(['bikes:racing:france', 'bikes:racing:usa', 'bikes:racing:italy'])
console.log(res16) // >>> []
const res17 = await client.sDiff(['bikes:racing:usa', 'bikes:racing:france'])
console.log(res17) // >>> ['bike:4']
const res18 = await client.sDiff(['bikes:racing:france', 'bikes:racing:usa'])
console.log(res18) // >>> ['bike:2', 'bike:3']
// STEP_END
// REMOVE_START
assert.deepEqual(res14, ['bike:1'])
assert.deepEqual(res15.sort(), ['bike:1', 'bike:2', 'bike:3', 'bike:4'])
assert.deepEqual(res16, [])
assert.deepEqual(res17, ['bike:4'])
assert.deepEqual(res18, ['bike:2', 'bike:3'])
await client.del('bikes:racing:france')
await client.del('bikes:racing:usa')
await client.del('bikes:racing:italy')
// REMOVE_END
debugger;
// STEP_START sRem
await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5'])
const res19 = await client.sRem('bikes:racing:france', 'bike:1')
console.log(res19) // >>> 1
const res20 = await client.sPop('bikes:racing:france')
console.log(res20) // >>> bike:3 or other random value
const res21 = await client.sMembers('bikes:racing:france')
console.log(res21) // >>> ['bike:2', 'bike:4', 'bike:5']; depends on previous result
const res22 = await client.sRandMember('bikes:racing:france')
console.log(res22) // >>> bike:4 or other random value
// STEP_END
// REMOVE_START
assert.equal(res19, 1)
client.quit()
// none of the other results are deterministic
// REMOVE_END

346
doctests/dt-streams.js Normal file
View File

@@ -0,0 +1,346 @@
// EXAMPLE: stream_tutorial
// HIDE_START
import assert from 'assert';
import {
createClient
} from 'redis';
const client = await createClient();
await client.connect();
// HIDE_END
// REMOVE_START
await client.flushDb();
// REMOVE_END
// STEP_START xAdd
const res1 = await client.xAdd(
'race:france', '*', {
'rider': 'Castilla',
'speed': '30.2',
'position': '1',
'location_id': '1'
}
);
console.log(res1); // >>> 1700073067968-0 N.B. actual values will differ from these examples
const res2 = await client.xAdd(
'race:france', '*', {
'rider': 'Norem',
'speed': '28.8',
'position': '3',
'location_id': '1'
},
);
console.log(res2); // >>> 1692629594113-0
const res3 = await client.xAdd(
'race:france', '*', {
'rider': 'Prickett',
'speed': '29.7',
'position': '2',
'location_id': '1'
},
);
console.log(res3); // >>> 1692629613374-0
// STEP_END
// REMOVE_START
assert.equal(await client.xLen('race:france'), 3);
// REMOVE_END
// STEP_START xRange
const res4 = await client.xRange('race:france', '1691765278160-0', '+', 2);
console.log(res4); // >>> [('1692629576966-0', {'rider': 'Castilla', 'speed': '30.2', 'position': '1', 'location_id': '1'}), ('1692629594113-0', {'rider': 'Norem', 'speed': '28.8', 'position': '3', 'location_id': '1'})]
// STEP_END
// STEP_START xread_block
const res5 = await client.xRead({
key: 'race:france',
id: '0-0'
}, {
count: 100,
block: 300
});
console.log(res5); // >>> [['race:france', [('1692629576966-0', {'rider': 'Castilla', 'speed': '30.2', 'position': '1', 'location_id': '1'}), ('1692629594113-0', {'rider': 'Norem', 'speed': '28.8', 'position': '3', 'location_id': '1'}), ('1692629613374-0', {'rider': 'Prickett', 'speed': '29.7', 'position': '2', 'location_id': '1'})]]]
// STEP_END
// STEP_START xAdd_2
const res6 = await client.xAdd(
'race:france', '*', {
'rider': 'Castilla',
'speed': '29.9',
'position': '1',
'location_id': '2'
}
);
console.log(res6); // >>> 1692629676124-0
// STEP_END
// STEP_START xlen
const res7 = await client.xLen('race:france');
console.log(res7); // >>> 4
// STEP_END
// STEP_START xAdd_id
const res8 = await client.xAdd('race:usa', '0-1', {
'racer': 'Castilla'
});
console.log(res8); // >>> 0-1
const res9 = await client.xAdd('race:usa', '0-2', {
'racer': 'Norem'
});
console.log(res9); // >>> 0-2
// STEP_END
// STEP_START xAdd_bad_id
try {
const res10 = await client.xAdd('race:usa', '0-1', {
'racer': 'Prickett'
});
console.log(res10); // >>> 0-1
} catch (error) {
console.error(error); // >>> WRONGID
}
// STEP_END
// STEP_START xadd_7
const res11a = await client.xAdd('race:usa', '0-*', { racer: 'Norem' });
console.log(res11a); // >>> 0-3
// STEP_END
// STEP_START xRange_all
const res11 = await client.xRange('race:france', '-', '+');
console.log(res11); // >>> [('1692629576966-0', {'rider': 'Castilla', 'speed': '30.2', 'position': '1', 'location_id': '1'}), ('1692629594113-0', {'rider': 'Norem', 'speed': '28.8', 'position': '3', 'location_id': '1'}), ('1692629613374-0', {'rider': 'Prickett', 'speed': '29.7', 'position': '2', 'location_id': '1'}), ('1692629676124-0', {'rider': 'Castilla', 'speed': '29.9', 'position': '1', 'location_id': '2'})]
// STEP_END
// STEP_START xRange_time
const res12 = await client.xRange('race:france', '1692629576965', '1692629576967');
console.log(res12); // >>> [('1692629576966-0', {'rider': 'Castilla', 'speed': '30.2', 'position': '1', 'location_id': '1'})]
// STEP_END
// STEP_START xRange_step_1
const res13 = await client.xRange('race:france', '-', '+', 2);
console.log(res13); // >>> [('1692629576966-0', {'rider': 'Castilla', 'speed': '30.2', 'position': '1', 'location_id': '1'}), ('1692629594113-0', {'rider': 'Norem', 'speed': '28.8', 'position': '3', 'location_id': '1'})]
// STEP_END
// STEP_START xRange_step_2
const res14 = await client.xRange('race:france', '(1692629594113-0', '+', 2);
console.log(res14); // >>> [('1692629613374-0', {'rider': 'Prickett', 'speed': '29.7', 'position': '2', 'location_id': '1'}), ('1692629676124-0', {'rider': 'Castilla', 'speed': '29.9', 'position': '1', 'location_id': '2'})]
// STEP_END
// STEP_START xRange_empty
const res15 = await client.xRange('race:france', '(1692629676124-0', '+', 2);
console.log(res15); // >>> []
// STEP_END
// STEP_START xrevrange
const res16 = await client.xRevRange('race:france', '+', '-', 1);
console.log(
res16
); // >>> [('1692629676124-0', {'rider': 'Castilla', 'speed': '29.9', 'position': '1', 'location_id': '2'})]
// STEP_END
// STEP_START xread
const res17 = await client.xRead({
key: 'race:france',
id: '0-0'
}, {
count: 2
});
console.log(res17); // >>> [['race:france', [('1692629576966-0', {'rider': 'Castilla', 'speed': '30.2', 'position': '1', 'location_id': '1'}), ('1692629594113-0', {'rider': 'Norem', 'speed': '28.8', 'position': '3', 'location_id': '1'})]]]
// STEP_END
// STEP_START xgroup_create
const res18 = await client.xGroupCreate('race:france', 'france_riders', '$');
console.log(res18); // >>> True
// STEP_END
// STEP_START xgroup_create_mkstream
const res19 = await client.xGroupCreate('race:italy', 'italy_riders', '$', {
'MKSTREAM': true
});
console.log(res19); // >>> True
// STEP_END
// STEP_START xgroup_read
await client.xAdd('race:italy', '*', {
'rider': 'Castilla'
});
await client.xAdd('race:italy', '*', {
'rider': 'Royce'
});
await client.xAdd('race:italy', '*', {
'rider': 'Sam-Bodden'
});
await client.xAdd('race:italy', '*', {
'rider': 'Prickett'
});
await client.xAdd('race:italy', '*', {
'rider': 'Norem'
});
const res20 = await client.xReadGroup(
'italy_riders',
'Alice', {
key: 'race:italy',
id: '>'
}, {
'COUNT': 1
}
);
console.log(res20); // >>> [['race:italy', [('1692629925771-0', {'rider': 'Castilla'})]]]
// STEP_END
// STEP_START xgroup_read_id
const res21 = await client.xReadGroup(
'italy_riders',
'Alice', {
key: 'race:italy',
id: '0'
}, {
'COUNT': 1
}
);
console.log(res21); // >>> [['race:italy', [('1692629925771-0', {'rider': 'Castilla'})]]]
// STEP_END
// STEP_START xack
const res22 = await client.xAck('race:italy', 'italy_riders', '1692629925771-0')
console.log(res22); // >>> 1
const res23 = await client.xReadGroup(
'italy_riders',
'Alice', {
key: 'race:italy',
id: '0'
}, {
'COUNT': 1
}
);
console.log(res23); // >>> [['race:italy', []]]
// STEP_END
// STEP_START xgroup_read_bob
const res24 = await client.xReadGroup(
'italy_riders',
'Bob', {
key: 'race:italy',
id: '>'
}, {
'COUNT': 2
}
);
console.log(res24); // >>> [['race:italy', [('1692629925789-0', {'rider': 'Royce'}), ('1692629925790-0', {'rider': 'Sam-Bodden'})]]]
// STEP_END
// STEP_START xpending
const res25 = await client.xPending('race:italy', 'italy_riders');
console.log(res25); // >>> {'pending': 2, 'min': '1692629925789-0', 'max': '1692629925790-0', 'consumers': [{'name': 'Bob', 'pending': 2}]}
// STEP_END
// STEP_START xpending_plus_minus
const res26 = await client.xPendingRange('race:italy', 'italy_riders', '-', '+', 10);
console.log(res26); // >>> [{'message_id': '1692629925789-0', 'consumer': 'Bob', 'time_since_delivered': 31084, 'times_delivered': 1}, {'message_id': '1692629925790-0', 'consumer': 'Bob', 'time_since_delivered': 31084, 'times_delivered': 1}]
// STEP_END
// STEP_START xRange_pending
const res27 = await client.xRange('race:italy', '1692629925789-0', '1692629925789-0');
console.log(res27); // >>> [('1692629925789-0', {'rider': 'Royce'})]
// STEP_END
// STEP_START xclaim
const res28 = await client.xClaim(
'race:italy', 'italy_riders', 'Alice', 60000, ['1692629925789-0']
);
console.log(res28); // >>> [('1692629925789-0', {'rider': 'Royce'})]
// STEP_END
// STEP_START xautoclaim
const res29 = await client.xAutoClaim('race:italy', 'italy_riders', 'Alice', 1, '0-0', 1);
console.log(res29); // >>> ['1692629925790-0', [('1692629925789-0', {'rider': 'Royce'})]]
// STEP_END
// STEP_START xautoclaim_cursor
const res30 = await client.xAutoClaim(
'race:italy', 'italy_riders', 'Alice', 1, '(1692629925789-0', 1
);
console.log(res30); // >>> ['0-0', [('1692629925790-0', {'rider': 'Sam-Bodden'})]]
// STEP_END
// STEP_START xinfo
const res31 = await client.xInfoStream('race:italy');
console.log(res31); // >>> {'length': 5, 'radix-tree-keys': 1, 'radix-tree-nodes': 2, 'last-generated-id': '1692629926436-0', 'groups': 1, 'first-entry': ('1692629925771-0', {'rider': 'Castilla'}), 'last-entry': ('1692629926436-0', {'rider': 'Norem'})}
// STEP_END
// STEP_START xinfo_groups
const res32 = await client.xInfoGroups('race:italy');
console.log(res32); // >>> [{'name': 'italy_riders', 'consumers': 2, 'pending': 2, 'last-delivered-id': '1692629925790-0'}]
// STEP_END
// STEP_START xinfo_consumers
const res33 = await client.xInfoConsumers('race:italy', 'italy_riders');
console.log(res33); // >>> [{'name': 'Alice', 'pending': 2, 'idle': 199332}, {'name': 'Bob', 'pending': 0, 'idle': 489170}]
// STEP_END
// STEP_START maxlen
await client.xAdd('race:italy', '*', {
'rider': 'Jones'
}, {
'MAXLEN': 2
});
await client.xAdd('race:italy', '*', {
'rider': 'Wood'
}, {
'MAXLEN': 2
});
await client.xAdd('race:italy', '*', {
'rider': 'Henshaw'
}, {
'MAXLEN': 2
});
const res34 = await client.xLen('race:italy');
console.log(res34); // >>> 8
const res35 = await client.xRange('race:italy', '-', '+');
console.log(res35); // >>> [('1692629925771-0', {'rider': 'Castilla'}), ('1692629925789-0', {'rider': 'Royce'}), ('1692629925790-0', {'rider': 'Sam-Bodden'}), ('1692629925791-0', {'rider': 'Prickett'}), ('1692629926436-0', {'rider': 'Norem'}), ('1692630612602-0', {'rider': 'Jones'}), ('1692630641947-0', {'rider': 'Wood'}), ('1692630648281-0', {'rider': 'Henshaw'})]
await client.xAdd('race:italy', '*', {
'rider': 'Smith'
}, {
'MAXLEN': 2,
'APPROXIMATE': false
});
const res36 = await client.xRange('race:italy', '-', '+');
console.log(res36); // >>> [('1692630648281-0', {'rider': 'Henshaw'}), ('1692631018238-0', {'rider': 'Smith'})]
// STEP_END
// STEP_START xTrim
const res37 = await client.xTrim('race:italy', 'MAXLEN', 10, {
'APPROXIMATE': false
});
console.log(res37); // >>> 0
// STEP_END
// STEP_START xTrim2
const res38 = await client.xTrim('race:italy', "MAXLEN", 10);
console.log(res38); // >>> 0
// STEP_END
// STEP_START xDel
const res39 = await client.xRange('race:italy', '-', '+');
console.log(res39); // >>> [('1692630648281-0', {'rider': 'Henshaw'}), ('1692631018238-0', {'rider': 'Smith'})]
const res40 = await client.xDel('race:italy', '1692631018238-0');
console.log(res40); // >>> 1
const res41 = await client.xRange('race:italy', '-', '+');
console.log(res41); // >>> [('1692630648281-0', {'rider': 'Henshaw'})]
// STEP_END
// REMOVE_START
await client.quit();
// REMOVE_END

68
doctests/dt-string.js Normal file
View File

@@ -0,0 +1,68 @@
// EXAMPLE: string_tutorial
// HIDE_START
import assert from 'assert';
import { createClient } from 'redis';
const client = await createClient();
await client.connect();
// HIDE_END
// REMOVE_START
await client.flushDb();
// REMOVE_END
// STEP_START set
const res1 = await client.set("bike:1", "Deimos");
console.log(res1); // OK
const res2 = await client.get("bike:1");
console.log(res2); // Deimos
// STEP_END
// REMOVE_START
assert.equal(res1, 'OK');
assert.equal(res2, 'Deimos');
// REMOVE_END
// STEP_START set_nx_xx
const res3 = await client.set("bike:1", "bike", {'NX': true});
console.log(res3); // null
console.log(await client.get("bike:1")); // Deimos
const res4 = await client.set("bike:1", "bike", {'XX': true});
console.log(res4); // OK
// STEP_END
// REMOVE_START
assert.equal(res3, null);
assert.equal(res4, 'OK');
// REMOVE_END
// STEP_START mset
const res5 = await client.mSet([
["bike:1", "Deimos"],
["bike:2", "Ares"],
["bike:3", "Vanth"]
]);
console.log(res5); // OK
const res6 = await client.mGet(["bike:1", "bike:2", "bike:3"]);
console.log(res6); // ['Deimos', 'Ares', 'Vanth']
// STEP_END
// REMOVE_START
assert.equal(res5, 'OK');
assert.deepEqual(res6, ["Deimos", "Ares", "Vanth"]);
// REMOVE_END
// STEP_START incr
await client.set("total_crashes", 0);
const res7 = await client.incr("total_crashes");
console.log(res7); // 1
const res8 = await client.incrBy("total_crashes", 10);
console.log(res8); // 11
// STEP_END
// REMOVE_START
assert.equal(res7, 1);
assert.equal(res8, 11);
await client.quit();
// REMOVE_END

View File

@@ -1,120 +0,0 @@
// EXAMPLE: hash_tutorial
// REMOVE_START
import assert from 'assert';
// REMOVE_END
// HIDE_START
import { createClient } from 'redis';
const client = createClient();
client.on('error', err => console.log('Redis Client Error', err));
await client.connect();
// HIDE_END
// STEP_START set_get_all
const fieldsAdded = await client.hSet(
'bike:1',
{
model: 'Deimos',
brand: 'Ergonom',
type: 'Enduro bikes',
price: 4972,
},
)
console.log(`Number of fields were added: ${fieldsAdded}`);
// Number of fields were added: 4
//REMOVE_START
assert.equal(fieldsAdded, 4);
//REMOVE_END
const model = await client.hGet('bike:1', 'model');
console.log(`Model: ${model}`);
// Model: Deimos
// REMOVE_START
assert.equal(model, 'Deimos');
// REMOVE_END
const price = await client.hGet('bike:1', 'price');
console.log(`Price: ${price}`);
// Price: 4972
// REMOVE_START
assert.equal(price, '4972');
// REMOVE_END
const bike = await client.hGetAll('bike:1');
console.log(bike);
// {
// model: 'Deimos',
// brand: 'Ergonom',
// type: 'Enduro bikes',
// price: '4972'
// }
// REMOVE_START
assert.equal(Object.keys(bike).length, 4);
// REMOVE_END
// STEP_END
// STEP_START hmget
const fields = await client.hmGet('bike:1', ['model', 'price']);
console.log(fields);
// [ 'Deimos', '4972' ]
// REMOVE_START
assert.equal(fields.length, 2);
// REMOVE_END
// STEP_END
// STEP_START hincrby
let newPrice = await client.hIncrBy('bike:1', 'price', 100);
console.log(newPrice);
// 5072
// REMOVE_START
assert.equal(newPrice, 5072);
// REMOVE_END
newPrice = await client.hIncrBy('bike:1', 'price', -100);
console.log(newPrice);
// 4972
// REMOVE_START
assert.equal(newPrice, 4972);
// REMOVE_END
// STEP_END
// STEP_START incrby_get_mget
let rides = await client.hIncrBy('bike:1:stats', 'rides', 1);
console.log(rides);
// 1
rides = await client.hIncrBy('bike:1:stats', 'rides', 1);
console.log(rides);
// 2
rides = await client.hIncrBy('bike:1:stats', 'rides', 1);
console.log(rides);
// 3
let crashes = await client.hIncrBy('bike:1:stats', 'crashes', 1);
console.log(crashes);
// 1
let owners = await client.hIncrBy('bike:1:stats', 'owners', 1);
console.log(owners);
// 1
rides = await client.hGet('bike:1:stats', 'rides');
console.log(`Total rides: ${rides}`);
// Total rides: 3
// REMOVE_START
assert.equal(rides, 3);
// REMOVE_END
const stats = await client.hmGet('bike:1:stats', ['crashes', 'owners']);
console.log(`Bike stats: crashes=${stats[0]}, owners=${stats[1]}`);
// Bike stats: crashes=1, owners=1
// REMOVE_START
assert.equal(stats.length, 2);
// REMOVE_END
// STEP_END
// HIDE_START
await client.quit();
// HIDE_END

View File

@@ -1,26 +0,0 @@
// EXAMPLE: set_and_get
// REMOVE_START
import assert from "assert";
// REMOVE_END
// HIDE_START
import { createClient } from 'redis';
const client = createClient();
client.on('error', err => console.log('Redis Client Error', err));
await client.connect();
// HIDE_END
await client.set('bike:1', 'Process 134');
const value = await client.get('bike:1');
console.log(value);
// returns 'Process 134'
//REMOVE_START
assert.equal(value, 'Process 134');
//REMOVE_END
// HIDE_START
await client.quit();
// HIDE_END