1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-04 15:02:09 +03:00

docs: Update doctests (#3020)

* move all doctests from emb-examples branch

* fix readme

* add package-lock.json

* --wip-- [skip ci]

* fix: replace client.quit() with client.close() as quit is deprecated

- doctests/cmds-hash.js
- doctests/cmds-list.js
- doctests/cmds-servermgmt.js
- doctests/cmds-set.js

* fix: replace client.quit() with client.close() as quit is deprecated

- doctests/cmds-sorted-set.js
- doctests/cmds-string.js
- doctests/dt-bitfield.js
- doctests/dt-bitmap.js

* fix: replace client.quit() with client.close() as quit is deprecated

- dt-bloom.js: replace client.quit() with client.close()
- dt-cms.js: replace client.quit() with client.close()
- dt-cuckoo.js: replace client.quit() with client.close() and update expected output comments to reflect v5 boolean returns
- dt-geo.js: replace client.quit() with client.close()

* fix(doctests): correct pfAdd return values and replace quit with close

- Fix dt-hll.js: pfAdd returns 1 instead of true in comments and assertions
- Fix dt-hash.js and dt-hll.js: replace deprecated client.quit() with client.close()

* fix(doctests): correct API usage and return values in json and list examples

- Fix dt-json.js: use options object for json.type, json.strLen, json.del, json.arrPop, json.objLen, json.objKeys
- Fix dt-json.js: correct json.del return value from [1] to 1
- Fix dt-list.js: correct client initialization, return values (null, OK, 1), and error type
- Replace deprecated client.quit() with client.close() in both files

* fix(doctests): update dt-set.js and dt-ss.js for v5 compliance

- Updated boolean return values to numbers for SISMEMBER and SMISMEMBER commands
- Fixed client lifecycle to use client.close() instead of client.quit()
- Removed unnecessary await from createClient()
- Added order-independent assertions for set operations
- Removed debug statement

* fix(doctests): update deprecated methods and imports for v5 compliance

- Fix dt-string.js: remove await from client creation and replace client.quit() with client.close()
- Fix dt-tdigest.js: replace deprecated client.quit() with client.close()
- Fix dt-topk.js: replace client.quit() with client.close() and fix output comment from [1, 0] to [true, false]
- Fix query-agg.js: update @redis/search imports to use new constant names and replace client.disconnect() with client.close()

* fix(doctests): update imports and replace deprecated disconnect with close

- Replace SchemaFieldTypes/VectorAlgorithms with SCHEMA_FIELD_TYPE/SCHEMA_VECTOR_FIELD_ALGORITHM
- Replace client.disconnect() with client.close() for consistent deprecation handling
- Update query-combined.js, query-em.js, query-ft.js, and query-geo.js

* fix(doctests): update imports and replace deprecated methods in remaining files

- Update imports to use SCHEMA_FIELD_TYPE and SCHEMA_VECTOR_FIELD_ALGORITHM constants
- Replace deprecated disconnect() and quit() methods with close()
- Fix assertion in search-quickstart.js to use correct bicycle ID

* fix(doctests): update cmds-generic.js and cmds-cnxmgmt.js for v5 compliance

- Replace deprecated client.quit() with client.close()
- Update sScanIterator to use collection-yielding behavior (value -> values)
- Fix HSCAN API changes: tuples renamed to entries
- Fix cursor type issues: use string '0' instead of number 0 for hScan
- Fix infinite loop in scan cleanup by using do-while pattern

* fix(doctests): update dt-streams.js object shapes and parameters for v5 compliance

- Update stream result objects from tuple format to proper object format with id/message properties
- Change xRead/xReadGroup results from nested arrays to objects with name/messages structure
- Update xAutoClaim results to use nextId, messages, and deletedMessages properties
- Add missing properties to xInfo* results (max-deleted-entry-id, entries-added, recorded-first-entry-id, entries-read, lag, inactive)
- Modernize parameter names (count -> COUNT, block -> BLOCK, etc.)
- Update MAXLEN/APPROXIMATE options to new TRIM object structure
- Fix error message format for XADD duplicate ID error
- Update boolean return values (True -> OK)

---------

Co-authored-by: Nikolay Karadzhov <nkaradzhov89@gmail.com>
This commit is contained in:
Pavel Pashov
2025-07-23 18:16:08 +03:00
committed by GitHub
parent ddd2cc5185
commit 0541b32f34
40 changed files with 8899 additions and 6 deletions

32
doctests/README.md Normal file
View File

@@ -0,0 +1,32 @@
# Command examples for redis.io
## Setup
To set up the examples folder so that you can run an example / develop one of your own:
```
$ git clone https://github.com/redis/node-redis.git
$ cd node-redis
$ npm install -ws && npm run build
$ cd doctests
$ npm install
```
## How to add examples
Create regular node file in the current folder with meaningful name. It makes sense prefix example files with
command category (e.g. string, set, list, hash, etc) to make navigation in the folder easier.
### Special markup
See https://github.com/redis-stack/redis-stack-website#readme for more details.
## How to test the examples
Just include necessary assertions in the example file and run
```bash
sh doctests/run_examples.sh
```
to test all examples in the current folder.
See `tests.js` for more details.

49
doctests/cmds-cnxmgmt.js Normal file
View File

@@ -0,0 +1,49 @@
// EXAMPLE: cmds_cnxmgmt
// REMOVE_START
import assert from "node:assert";
// REMOVE_END
// HIDE_START
import { createClient } from 'redis';
const client = createClient();
await client.connect().catch(console.error);
// HIDE_END
// STEP_START auth1
// REMOVE_START
await client.sendCommand(['CONFIG', 'SET', 'requirepass', 'temp_pass']);
// REMOVE_END
const res1 = await client.auth({ password: 'temp_pass' });
console.log(res1); // OK
const res2 = await client.auth({ username: 'default', password: 'temp_pass' });
console.log(res2); // OK
// REMOVE_START
assert.equal(res1, "OK");
assert.equal(res2, "OK");
await client.sendCommand(['CONFIG', 'SET', 'requirepass', '']);
// REMOVE_END
// STEP_END
// STEP_START auth2
// REMOVE_START
await client.sendCommand([
'ACL', 'SETUSER', 'test-user',
'on', '>strong_password', '+acl'
]);
// REMOVE_END
const res3 = await client.auth({ username: 'test-user', password: 'strong_password' });
console.log(res3); // OK
// REMOVE_START
assert.equal(res3, "OK");
await client.auth({ username: 'default', password: '' })
await client.sendCommand(['ACL', 'DELUSER', 'test-user']);
// REMOVE_END
// STEP_END
// HIDE_START
await client.close();
// HIDE_END

195
doctests/cmds-generic.js Normal file
View File

@@ -0,0 +1,195 @@
// EXAMPLE: cmds_generic
// REMOVE_START
import assert from "node:assert";
// REMOVE_END
// HIDE_START
import { createClient } from 'redis';
const client = createClient();
await client.connect().catch(console.error);
// HIDE_END
// STEP_START del
const delRes1 = await client.set('key1', 'Hello');
console.log(delRes1); // OK
const delRes2 = await client.set('key2', 'World');
console.log(delRes2); // OK
const delRes3 = await client.del(['key1', 'key2', 'key3']);
console.log(delRes3); // 2
// REMOVE_START
assert.equal(delRes3, 2);
// REMOVE_END
// STEP_END
// STEP_START expire
const expireRes1 = await client.set('mykey', 'Hello');
console.log(expireRes1); // OK
const expireRes2 = await client.expire('mykey', 10);
console.log(expireRes2); // 1
const expireRes3 = await client.ttl('mykey');
console.log(expireRes3); // 10
// REMOVE_START
assert.equal(expireRes3, 10);
// REMOVE_END
const expireRes4 = await client.set('mykey', 'Hello World');
console.log(expireRes4); // OK
const expireRes5 = await client.ttl('mykey');
console.log(expireRes5); // -1
// REMOVE_START
assert.equal(expireRes5, -1);
// REMOVE_END
const expireRes6 = await client.expire('mykey', 10, "XX");
console.log(expireRes6); // 0
// REMOVE_START
assert.equal(expireRes6, 0)
// REMOVE_END
const expireRes7 = await client.ttl('mykey');
console.log(expireRes7); // -1
// REMOVE_START
assert.equal(expireRes7, -1);
// REMOVE_END
const expireRes8 = await client.expire('mykey', 10, "NX");
console.log(expireRes8); // 1
// REMOVE_START
assert.equal(expireRes8, 1);
// REMOVE_END
const expireRes9 = await client.ttl('mykey');
console.log(expireRes9); // 10
// REMOVE_START
assert.equal(expireRes9, 10);
await client.del('mykey');
// REMOVE_END
// STEP_END
// STEP_START ttl
const ttlRes1 = await client.set('mykey', 'Hello');
console.log(ttlRes1); // OK
const ttlRes2 = await client.expire('mykey', 10);
console.log(ttlRes2); // 1
const ttlRes3 = await client.ttl('mykey');
console.log(ttlRes3); // 10
// REMOVE_START
assert.equal(ttlRes3, 10);
await client.del('mykey');
// REMOVE_END
// STEP_END
// STEP_START scan1
const scan1Res1 = await client.sAdd('myset', ['1', '2', '3', 'foo', 'foobar', 'feelsgood']);
console.log(scan1Res1); // 6
let scan1Res2 = [];
for await (const values of client.sScanIterator('myset', { MATCH: 'f*' })) {
scan1Res2 = scan1Res2.concat(values);
}
console.log(scan1Res2); // ['foo', 'foobar', 'feelsgood']
// REMOVE_START
console.assert(scan1Res2.sort().toString() === ['foo', 'foobar', 'feelsgood'].sort().toString());
await client.del('myset');
// REMOVE_END
// STEP_END
// STEP_START scan2
// REMOVE_START
for (let i = 1; i <= 1000; i++) {
await client.set(`key:${i}`, i);
}
// REMOVE_END
let cursor = '0';
let scanResult;
scanResult = await client.scan(cursor, { MATCH: '*11*' });
console.log(scanResult.cursor, scanResult.keys);
scanResult = await client.scan(scanResult.cursor, { MATCH: '*11*' });
console.log(scanResult.cursor, scanResult.keys);
scanResult = await client.scan(scanResult.cursor, { MATCH: '*11*' });
console.log(scanResult.cursor, scanResult.keys);
scanResult = await client.scan(scanResult.cursor, { MATCH: '*11*' });
console.log(scanResult.cursor, scanResult.keys);
scanResult = await client.scan(scanResult.cursor, { MATCH: '*11*', COUNT: 1000 });
console.log(scanResult.cursor, scanResult.keys);
// REMOVE_START
console.assert(scanResult.keys.length === 18);
cursor = '0';
const prefix = 'key:*';
do {
scanResult = await client.scan(cursor, { MATCH: prefix, COUNT: 1000 });
console.log(scanResult.cursor, scanResult.keys);
cursor = scanResult.cursor;
const keys = scanResult.keys;
if (keys.length) {
await client.del(keys);
}
} while (cursor !== '0');
// REMOVE_END
// STEP_END
// STEP_START scan3
const scan3Res1 = await client.geoAdd('geokey', { longitude: 0, latitude: 0, member: 'value' });
console.log(scan3Res1); // 1
const scan3Res2 = await client.zAdd('zkey', [{ score: 1000, value: 'value' }]);
console.log(scan3Res2); // 1
const scan3Res3 = await client.type('geokey');
console.log(scan3Res3); // zset
// REMOVE_START
console.assert(scan3Res3 === 'zset');
// REMOVE_END
const scan3Res4 = await client.type('zkey');
console.log(scan3Res4); // zset
// REMOVE_START
console.assert(scan3Res4 === 'zset');
// REMOVE_END
const scan3Res5 = await client.scan('0', { TYPE: 'zset' });
console.log(scan3Res5.keys); // ['zkey', 'geokey']
// REMOVE_START
console.assert(scan3Res5.keys.sort().toString() === ['zkey', 'geokey'].sort().toString());
await client.del(['geokey', 'zkey']);
// REMOVE_END
// STEP_END
// STEP_START scan4
const scan4Res1 = await client.hSet('myhash', { a: 1, b: 2 });
console.log(scan4Res1); // 2
const scan4Res2 = await client.hScan('myhash', '0');
console.log(scan4Res2.entries); // [{field: 'a', value: '1'}, {field: 'b', value: '2'}]
// REMOVE_START
assert.deepEqual(scan4Res2.entries, [
{ field: 'a', value: '1' },
{ field: 'b', value: '2' }
]);
// REMOVE_END
const scan4Res3 = await client.hScan('myhash', '0', { COUNT: 10 });
const items = scan4Res3.entries.map((item) => item.field)
console.log(items); // ['a', 'b']
// REMOVE_START
assert.deepEqual(items, ['a', 'b'])
await client.del('myhash');
// REMOVE_END
// STEP_END
// HIDE_START
await client.close();
// HIDE_END

109
doctests/cmds-hash.js Normal file
View File

@@ -0,0 +1,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) // null
// 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.close();
// HIDE_END

129
doctests/cmds-list.js Normal file
View File

@@ -0,0 +1,129 @@
// EXAMPLE: cmds_list
// HIDE_START
import assert from 'node:assert';
import { createClient } from 'redis';
const client = createClient();
await client.connect().catch(console.error);
// HIDE_END
// STEP_START lpush
const res1 = await client.lPush('mylist', 'world');
console.log(res1); // 1
const res2 = await client.lPush('mylist', 'hello');
console.log(res2); // 2
const res3 = await client.lRange('mylist', 0, -1);
console.log(res3); // [ 'hello', 'world' ]
// REMOVE_START
assert.deepEqual(res3, [ 'hello', 'world' ]);
await client.del('mylist');
// REMOVE_END
// STEP_END
// STEP_START lrange
const res4 = await client.rPush('mylist', 'one');
console.log(res4); // 1
const res5 = await client.rPush('mylist', 'two');
console.log(res5); // 2
const res6 = await client.rPush('mylist', 'three');
console.log(res6); // 3
const res7 = await client.lRange('mylist', 0, 0);
console.log(res7); // [ 'one' ]
const res8 = await client.lRange('mylist', -3, 2);
console.log(res8); // [ 'one', 'two', 'three' ]
const res9 = await client.lRange('mylist', -100, 100);
console.log(res9); // [ 'one', 'two', 'three' ]
const res10 = await client.lRange('mylist', 5, 10);
console.log(res10); // []
// REMOVE_START
assert.deepEqual(res7, [ 'one' ]);
assert.deepEqual(res8, [ 'one', 'two', 'three' ]);
assert.deepEqual(res9, [ 'one', 'two', 'three' ]);
assert.deepEqual(res10, []);
await client.del('mylist');
// REMOVE_END
// STEP_END
// STEP_START llen
const res11 = await client.lPush('mylist', 'World');
console.log(res11); // 1
const res12 = await client.lPush('mylist', 'Hello');
console.log(res12); // 2
const res13 = await client.lLen('mylist');
console.log(res13); // 2
// REMOVE_START
assert.equal(res13, 2);
await client.del('mylist');
// REMOVE_END
// STEP_END
// STEP_START rpush
const res14 = await client.rPush('mylist', 'hello');
console.log(res14); // 1
const res15 = await client.rPush('mylist', 'world');
console.log(res15); // 2
const res16 = await client.lRange('mylist', 0, -1);
console.log(res16); // [ 'hello', 'world' ]
// REMOVE_START
assert.deepEqual(res16, [ 'hello', 'world' ]);
await client.del('mylist');
// REMOVE_END
// STEP_END
// STEP_START lpop
const res17 = await client.rPush('mylist', ["one", "two", "three", "four", "five"]);
console.log(res17); // 5
const res18 = await client.lPop('mylist');
console.log(res18); // 'one'
const res19 = await client.lPopCount('mylist', 2);
console.log(res19); // [ 'two', 'three' ]
const res20 = await client.lRange('mylist', 0, -1);
console.log(res20); // [ 'four', 'five' ]
// REMOVE_START
assert.deepEqual(res20, [ 'four', 'five' ]);
await client.del('mylist');
// REMOVE_END
// STEP_END
// STEP_START rpop
const res21 = await client.rPush('mylist', ["one", "two", "three", "four", "five"]);
console.log(res21); // 5
const res22 = await client.rPop('mylist');
console.log(res22); // 'five'
const res23 = await client.rPopCount('mylist', 2);
console.log(res23); // [ 'four', 'three' ]
const res24 = await client.lRange('mylist', 0, -1);
console.log(res24); // [ 'one', 'two' ]
// REMOVE_START
assert.deepEqual(res24, [ 'one', 'two' ]);
await client.del('mylist');
// REMOVE_END
// STEP_END
// HIDE_START
await client.close();
// HIDE_END

View File

@@ -0,0 +1,45 @@
// EXAMPLE: cmds_servermgmt
// REMOVE_START
import assert from 'node:assert';
// REMOVE_END
// HIDE_START
import { createClient } from 'redis';
const client = createClient();
await client.connect().catch(console.error);
// HIDE_END
// STEP_START flushall
// REMOVE_START
await client.set('foo', '1');
await client.set('bar', '2');
await client.set('baz', '3');
// REMOVE_END
const res1 = await client.flushAll('SYNC'); // or ASYNC
console.log(res1); // OK
const res2 = await client.keys('*');
console.log(res2); // []
// REMOVE_START
assert.equal(res1, 'OK');
assert.deepEqual(res2, []);
// REMOVE_END
// STEP_END
// STEP_START info
const res3 = await client.info();
console.log(res3)
// # Server
// redis_version:7.4.0
// redis_git_sha1:c9d29f6a
// redis_git_dirty:0
// redis_build_id:4c367a16e3f9616
// redis_mode:standalone
// ...
// STEP_END
// HIDE_START
await client.close();
// HIDE_END

44
doctests/cmds-set.js Normal file
View File

@@ -0,0 +1,44 @@
// EXAMPLE: cmds_set
// REMOVE_START
import assert from 'node:assert';
// REMOVE_END
// HIDE_START
import { createClient } from 'redis';
const client = createClient();
await client.connect().catch(console.error);
// HIDE_END
// STEP_START sadd
const res1 = await client.sAdd('myset', ['Hello', 'World']);
console.log(res1); // 2
const res2 = await client.sAdd('myset', ['World']);
console.log(res2); // 0
const res3 = await client.sMembers('myset')
console.log(res3); // ['Hello', 'World']
// REMOVE_START
assert.deepEqual(res3, ['Hello', 'World']);
await client.del('myset');
// REMOVE_END
// STEP_END
// STEP_START smembers
const res4 = await client.sAdd('myset', ['Hello', 'World']);
console.log(res4); // 2
const res5 = await client.sMembers('myset')
console.log(res5); // ['Hello', 'World']
// REMOVE_START
assert.deepEqual(res5, ['Hello', 'World']);
await client.del('myset');
// REMOVE_END
// STEP_END
// HIDE_START
await client.close();
// HIDE_END

115
doctests/cmds-sorted-set.js Normal file
View File

@@ -0,0 +1,115 @@
// EXAMPLE: cmds_sorted_set
// REMOVE_START
import assert from "node: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().catch(console.error);
// HIDE_END
// STEP_START zadd
const val1 = await client.zAdd("myzset", [{ value: 'one', score: 1 }]);
console.log(val1);
// returns 1
const val2 = await client.zAdd("myzset", [{ value: 'uno', score: 1 }]);
console.log(val2);
// returns 1
const val3 = await client.zAdd("myzset", [{ value: 'two', score: 2 }, { value: 'three', score: 3 }]);
console.log(val3);
// returns 2
const val4 = await client.zRangeWithScores("myzset", 0, -1);
console.log(val4);
// returns [{value: 'one', score: 1}, {value: 'uno', score: 1}, {value: 'two', score: 2}, {value: 'three', score: 3} ]
// REMOVE_START
assert.equal(val1, 1);
assert.equal(val2, 1);
assert.equal(val3, 2);
assert.deepEqual(val4, [
{ value: 'one', score: 1 },
{ value: 'uno', score: 1 },
{ value: 'two', score: 2 },
{ value: 'three', score: 3 }
]);
await client.del('myzset');
// REMOVE_END
// STEP_END
// STEP_START zrange1
const val5 = await client.zAdd("myzset", [
{ value: 'one', score: 1 },
{ value: 'two', score: 2 },
{ value: 'three', score: 3 }
]);
console.log(val5);
// returns 3
const val6 = await client.zRange('myzset', 0, -1);
console.log(val6);
// returns ['one', 'two', 'three']
// REMOVE_START
console.assert(JSON.stringify(val6) === JSON.stringify(['one', 'two', 'three']));
// REMOVE_END
const val7 = await client.zRange('myzset', 2, 3);
console.log(val7);
// returns ['three']
// REMOVE_START
console.assert(JSON.stringify(val7) === JSON.stringify(['three']));
// REMOVE_END
const val8 = await client.zRange('myzset', -2, -1);
console.log(val8);
// returns ['two', 'three']
// REMOVE_START
console.assert(JSON.stringify(val8) === JSON.stringify(['two', 'three']));
await client.del('myzset');
// REMOVE_END
// STEP_END
// STEP_START zrange2
const val9 = await client.zAdd("myzset", [
{ value: 'one', score: 1 },
{ value: 'two', score: 2 },
{ value: 'three', score: 3 }
]);
console.log(val9);
// returns 3
const val10 = await client.zRangeWithScores('myzset', 0, 1);
console.log(val10);
// returns [{value: 'one', score: 1}, {value: 'two', score: 2}]
// REMOVE_START
console.assert(JSON.stringify(val10) === JSON.stringify([{value: 'one', score: 1}, {value: 'two', score: 2}]));
await client.del('myzset');
// REMOVE_END
// STEP_END
// STEP_START zrange3
const val11 = await client.zAdd("myzset", [
{ value: 'one', score: 1 },
{ value: 'two', score: 2 },
{ value: 'three', score: 3 }
]);
console.log(val11);
// returns 3
const val12 = await client.zRange('myzset', 2, 3, { BY: 'SCORE', LIMIT: { offset: 1, count: 1 } });
console.log(val12);
// >>> ['three']
// REMOVE_START
console.assert(JSON.stringify(val12) === JSON.stringify(['three']));
await client.del('myzset');
// REMOVE_END
// STEP_END
// HIDE_START
await client.close();
// HIDE_END

27
doctests/cmds-string.js Normal file
View File

@@ -0,0 +1,27 @@
// EXAMPLE: cmds_string
// REMOVE_START
import assert from "node: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().catch(console.error);
// HIDE_END
// STEP_START incr
await client.set("mykey", "10");
const value1 = await client.incr("mykey");
console.log(value1);
// returns 11
// REMOVE_START
assert.equal(value1, 11);
await client.del('mykey');
// REMOVE_END
// STEP_END
// HIDE_START
await client.close();
// HIDE_END

View File

@@ -0,0 +1,92 @@
[
{
"pickup_zone": "POLYGON((-74.0610 40.7578, -73.9510 40.7578, -73.9510 40.6678, -74.0610 40.6678, -74.0610 40.7578))",
"store_location": "-74.0060,40.7128",
"brand": "Velorim",
"model": "Jigger",
"price": 270,
"description": "Small and powerful, the Jigger is the best ride for the smallest of tikes! This is the tiniest kids pedal bike on the market available without a coaster brake, the Jigger is the vehicle of choice for the rare tenacious little rider raring to go.",
"condition": "new"
},
{
"pickup_zone": "POLYGON((-118.2887 34.0972, -118.1987 34.0972, -118.1987 33.9872, -118.2887 33.9872, -118.2887 34.0972))",
"store_location": "-118.2437,34.0522",
"brand": "Bicyk",
"model": "Hillcraft",
"price": 1200,
"description": "Kids want to ride with as little weight as possible. Especially on an incline! They may be at the age when a 27.5\" wheel bike is just too clumsy coming off a 24\" bike. The Hillcraft 26 is just the solution they need!",
"condition": "used"
},
{
"pickup_zone": "POLYGON((-87.6848 41.9331, -87.5748 41.9331, -87.5748 41.8231, -87.6848 41.8231, -87.6848 41.9331))",
"store_location": "-87.6298,41.8781",
"brand": "Nord",
"model": "Chook air 5",
"price": 815,
"description": "The Chook Air 5 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. The lower top tube makes it easy to mount and dismount in any situation, giving your kids greater safety on the trails.",
"condition": "used"
},
{
"pickup_zone": "POLYGON((-80.2433 25.8067, -80.1333 25.8067, -80.1333 25.6967, -80.2433 25.6967, -80.2433 25.8067))",
"store_location": "-80.1918,25.7617",
"brand": "Eva",
"model": "Eva 291",
"price": 3400,
"description": "The sister company to Nord, Eva launched in 2005 as the first and only women-dedicated bicycle brand. Designed by women for women, allEva bikes are optimized for the feminine physique using analytics from a body metrics database. If you like 29ers, try the Eva 291. Its a brand new bike for 2022.. This full-suspension, cross-country ride has been designed for velocity. The 291 has 100mm of front and rear travel, a superlight aluminum frame and fast-rolling 29-inch wheels. Yippee!",
"condition": "used"
},
{
"pickup_zone": "POLYGON((-122.4644 37.8199, -122.3544 37.8199, -122.3544 37.7099, -122.4644 37.7099, -122.4644 37.8199))",
"store_location": "-122.4194,37.7749",
"brand": "Noka Bikes",
"model": "Kahuna",
"price": 3200,
"description": "Whether you want to try your hand at XC racing or are looking for a lively trail bike that's just as inspiring on the climbs as it is over rougher ground, the Wilder is one heck of a bike built specifically for short women. Both the frames and components have been tweaked to include a womens saddle, different bars and unique colourway.",
"condition": "used"
},
{
"pickup_zone": "POLYGON((-0.1778 51.5524, 0.0822 51.5524, 0.0822 51.4024, -0.1778 51.4024, -0.1778 51.5524))",
"store_location": "-0.1278,51.5074",
"brand": "Breakout",
"model": "XBN 2.1 Alloy",
"price": 810,
"description": "The XBN 2.1 Alloy is our entry-level road bike but thats not to say that its a basic machine. With an internal weld aluminium frame, a full carbon fork, and the slick-shifting Claris gears from Shimanos, this is a bike which doesnt break the bank and delivers craved performance.",
"condition": "new"
},
{
"pickup_zone": "POLYGON((2.1767 48.9016, 2.5267 48.9016, 2.5267 48.5516, 2.1767 48.5516, 2.1767 48.9016))",
"store_location": "2.3522,48.8566",
"brand": "ScramBikes",
"model": "WattBike",
"price": 2300,
"description": "The WattBike is the best e-bike for people who still feel young at heart. It has a Bafang 1000W mid-drive system and a 48V 17.5AH Samsung Lithium-Ion battery, allowing you to ride for more than 60 miles on one charge. Its great for tackling hilly terrain or if you just fancy a more leisurely ride. With three working modes, you can choose between E-bike, assisted bicycle, and normal bike modes.",
"condition": "new"
},
{
"pickup_zone": "POLYGON((13.3260 52.5700, 13.6550 52.5700, 13.6550 52.2700, 13.3260 52.2700, 13.3260 52.5700))",
"store_location": "13.4050,52.5200",
"brand": "Peaknetic",
"model": "Secto",
"price": 430,
"description": "If you struggle with stiff fingers or a kinked neck or back after a few minutes on the road, this lightweight, aluminum bike alleviates those issues and allows you to enjoy the ride. From the ergonomic grips to the lumbar-supporting seat position, the Roll Low-Entry offers incredible comfort. The rear-inclined seat tube facilitates stability by allowing you to put a foot on the ground to balance at a stop, and the low step-over frame makes it accessible for all ability and mobility levels. The saddle is very soft, with a wide back to support your hip joints and a cutout in the center to redistribute that pressure. Rim brakes deliver satisfactory braking control, and the wide tires provide a smooth, stable ride on paved roads and gravel. Rack and fender mounts facilitate setting up the Roll Low-Entry as your preferred commuter, and the BMX-like handlebar offers space for mounting a flashlight, bell, or phone holder.",
"condition": "new"
},
{
"pickup_zone": "POLYGON((1.9450 41.4301, 2.4018 41.4301, 2.4018 41.1987, 1.9450 41.1987, 1.9450 41.4301))",
"store_location": "2.1734, 41.3851",
"brand": "nHill",
"model": "Summit",
"price": 1200,
"description": "This budget mountain bike from nHill performs well both on bike paths and on the trail. The fork with 100mm of travel absorbs rough terrain. Fat Kenda Booster tires give you grip in corners and on wet trails. The Shimano Tourney drivetrain offered enough gears for finding a comfortable pace to ride uphill, and the Tektro hydraulic disc brakes break smoothly. Whether you want an affordable bike that you can take to work, but also take trail in mountains on the weekends or youre just after a stable, comfortable ride for the bike path, the Summit gives a good value for money.",
"condition": "new"
},
{
"pickup_zone": "POLYGON((12.4464 42.1028, 12.5464 42.1028, 12.5464 41.7028, 12.4464 41.7028, 12.4464 42.1028))",
"store_location": "12.4964,41.9028",
"model": "ThrillCycle",
"brand": "BikeShind",
"price": 815,
"description": "An artsy, retro-inspired bicycle thats as functional as it is pretty: The ThrillCycle steel frame offers a smooth ride. A 9-speed drivetrain has enough gears for coasting in the city, but we wouldnt suggest taking it to the mountains. Fenders protect you from mud, and a rear basket lets you transport groceries, flowers and books. The ThrillCycle comes with a limited lifetime warranty, so this little guy will last you long past graduation.",
"condition": "refurbished"
}
]

File diff suppressed because it is too large Load Diff

76
doctests/dt-bitfield.js Normal file
View File

@@ -0,0 +1,76 @@
// 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.close();
// REMOVE_END

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.close();
// REMOVE_END

46
doctests/dt-bloom.js Normal file
View File

@@ -0,0 +1,46 @@
// EXAMPLE: bf_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 bloom
const res1 = await client.bf.reserve('bikes:models', 0.01, 1000);
console.log(res1); // >>> OK
const res2 = await client.bf.add('bikes:models', 'Smoky Mountain Striker');
console.log(res2); // >>> true
const res3 = await client.bf.exists('bikes:models', 'Smoky Mountain Striker');
console.log(res3); // >>> true
const res4 = await client.bf.mAdd('bikes:models', [
'Rocky Mountain Racer',
'Cloudy City Cruiser',
'Windy City Wippet'
]);
console.log(res4); // >>> [true, true, true]
const res5 = await client.bf.mExists('bikes:models', [
'Rocky Mountain Racer',
'Cloudy City Cruiser',
'Windy City Wippet'
]);
console.log(res5); // >>> [true, true, true]
// STEP_END
// REMOVE_START
assert.equal(res1, 'OK')
assert.equal(res2, true)
assert.equal(res3, true)
assert.deepEqual(res4, [true, true, true])
assert.deepEqual(res5, [true, true, true])
await client.close();
// REMOVE_END

50
doctests/dt-cms.js Normal file
View File

@@ -0,0 +1,50 @@
// EXAMPLE: cms_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 cms
const res1 = await client.cms.initByProb('bikes:profit', 0.001, 0.002);
console.log(res1); // >>> OK
const res2 = await client.cms.incrBy('bikes:profit', {
item: 'Smoky Mountain Striker',
incrementBy: 100
});
console.log(res2); // >>> [100]
const res3 = await client.cms.incrBy('bikes:profit', [
{
item: 'Rocky Mountain Racer',
incrementBy: 200
},
{
item: 'Cloudy City Cruiser',
incrementBy: 150
}
]);
console.log(res3); // >>> [200, 150]
const res4 = await client.cms.query('bikes:profit', 'Smoky Mountain Striker');
console.log(res4); // >>> [100]
const res5 = await client.cms.info('bikes:profit');
console.log(res5.width, res5.depth, res5.count); // >>> 2000 9 450
// STEP_END
// REMOVE_START
assert.equal(res1, 'OK')
assert.deepEqual(res2, [100])
assert.deepEqual(res3, [200, 150])
assert.deepEqual(res4, [100])
assert.deepEqual(res5, { width: 2000, depth: 9, count: 450 })
await client.close();
// REMOVE_END

38
doctests/dt-cuckoo.js Normal file
View File

@@ -0,0 +1,38 @@
// EXAMPLE: cuckoo_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 cuckoo
const res1 = await client.cf.reserve('bikes:models', 1000000);
console.log(res1); // >>> OK
const res2 = await client.cf.add('bikes:models', 'Smoky Mountain Striker');
console.log(res2); // >>> true
const res3 = await client.cf.exists('bikes:models', 'Smoky Mountain Striker');
console.log(res3); // >>> true
const res4 = await client.cf.exists('bikes:models', 'Terrible Bike Name');
console.log(res4); // >>> false
const res5 = await client.cf.del('bikes:models', 'Smoky Mountain Striker');
console.log(res5); // >>> true
// STEP_END
// REMOVE_START
assert.equal(res1, 'OK')
assert.equal(res2, true)
assert.equal(res3, true)
assert.equal(res4, false)
assert.equal(res5, true)
await client.close();
// REMOVE_END

59
doctests/dt-geo.js Normal file
View File

@@ -0,0 +1,59 @@
// EXAMPLE: geo_tutorial
// HIDE_START
import assert from 'assert';
import { createClient } from 'redis';
const client = createClient();
await client.connect();
// HIDE_END
// REMOVE_START
await client.del('bikes:rentable')
// REMOVE_END
// STEP_START geoAdd
const res1 = await client.geoAdd('bikes:rentable', {
longitude: -122.27652,
latitude: 37.805186,
member: 'station:1'
});
console.log(res1) // 1
const res2 = await client.geoAdd('bikes:rentable', {
longitude: -122.2674626,
latitude: 37.8062344,
member: 'station:2'
});
console.log(res2) // 1
const res3 = await client.geoAdd('bikes:rentable', {
longitude: -122.2469854,
latitude: 37.8104049,
member: 'station:3'
})
console.log(res3) // 1
// STEP_END
// REMOVE_START
assert.equal(res1, 1);
assert.equal(res2, 1);
assert.equal(res3, 1);
// REMOVE_END
// STEP_START geoSearch
const res4 = await client.geoSearch(
'bikes:rentable', {
longitude: -122.27652,
latitude: 37.805186,
},
{ radius: 5,
unit: 'km'
}
);
console.log(res4) // ['station:1', 'station:2', 'station:3']
// STEP_END
// REMOVE_START
assert.deepEqual(res4, ['station:1', 'station:2', 'station:3']);
// REMOVE_END
await client.close()

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.close();
// REMOVE_END

38
doctests/dt-hll.js Normal file
View File

@@ -0,0 +1,38 @@
// EXAMPLE: hll_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 pfadd
const res1 = await client.pfAdd('bikes', ['Hyperion', 'Deimos', 'Phoebe', 'Quaoar']);
console.log(res1); // >>> 1
const res2 = await client.pfCount('bikes');
console.log(res2); // >>> 4
const res3 = await client.pfAdd('commuter_bikes', ['Salacia', 'Mimas', 'Quaoar']);
console.log(res3); // >>> 1
const res4 = await client.pfMerge('all_bikes', ['bikes', 'commuter_bikes']);
console.log(res4); // >>> OK
const res5 = await client.pfCount('all_bikes');
console.log(res5); // >>> 6
// STEP_END
// REMOVE_START
assert.equal(res1, 1)
assert.equal(res2, 4)
assert.equal(res3, 1)
assert.equal(res4, 'OK')
assert.equal(res5, 6)
await client.close();
// 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", { path: "$" });
console.log(res2); // ['"Hyperion"']
const res3 = await client.json.type("bike", { path: "$" });
console.log(res3); // [ 'string' ]
// STEP_END
// REMOVE_START
assert.deepEqual(res2, ['"Hyperion"']);
// REMOVE_END
// STEP_START str
const res4 = await client.json.strLen("bike", { path: "$" });
console.log(res4) // [10]
const res5 = await client.json.strAppend("bike", '" (Enduro bikes)"');
console.log(res5) // 27
const res6 = await client.json.get("bike", { path: "$" });
console.log(res6) // ['"Hyperion"" (Enduro bikes)"']
// STEP_END
// REMOVE_START
assert.deepEqual(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", { path: "$" });
console.log(res12); // [[ 'Deimos', { crashes: 0 }, null ]]
const res13 = await client.json.get("newbike", { path: "$[1].crashes" });
console.log(res13); // [0]
const res14 = await client.json.del("newbike", { path: "$.[-1]"} );
console.log(res14); // 1
const res15 = await client.json.get("newbike", { path: "$" });
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", { path: "$" });
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", { path: "$" });
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", { path: "$" });
console.log(res22); // [[ 'Prickett' ]]
const res23 = await client.json.arrPop("riders", { path: "$" });
console.log(res23); // [ 'Prickett' ]
const res24 = await client.json.arrPop("riders", { path: "$" });
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", { path: "$" });
console.log(res26); // [3]
const res27 = await client.json.objKeys("bike:1", { path: "$" });
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',
{ path: '$.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',
{ path: "$..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.close();
// REMOVE_END

329
doctests/dt-list.js Normal file
View File

@@ -0,0 +1,329 @@
// EXAMPLE: list_tutorial
// HIDE_START
import assert from 'assert';
import { createClient } from 'redis';
const client = createClient();
await client.connect();
// HIDE_END
// REMOVE_START
await client.del('bikes:repairs');
await client.del('bikes:finished');
// REMOVE_END
// STEP_START queue
const res1 = await client.lPush('bikes:repairs', 'bike:1');
console.log(res1); // 1
const res2 = await client.lPush('bikes:repairs', 'bike:2');
console.log(res2); // 2
const res3 = await client.rPop('bikes:repairs');
console.log(res3); // bike:1
const res4 = await client.rPop('bikes:repairs');
console.log(res4); // bike:2
// STEP_END
// REMOVE_START
assert.equal(res1, 1);
assert.equal(res2, 2);
assert.equal(res3, 'bike:1');
assert.equal(res4, 'bike:2');
// REMOVE_END
// STEP_START stack
const res5 = await client.lPush('bikes:repairs', 'bike:1');
console.log(res5); // 1
const res6 = await client.lPush('bikes:repairs', 'bike:2');
console.log(res6); // 2
const res7 = await client.lPop('bikes:repairs');
console.log(res7); // bike:2
const res8 = await client.lPop('bikes:repairs');
console.log(res8); // bike:1
// STEP_END
// REMOVE_START
assert.equal(res5, 1);
assert.equal(res6, 2);
assert.equal(res7, 'bike:2');
assert.equal(res8, 'bike:1');
// REMOVE_END
// STEP_START lLen
const res9 = await client.lLen('bikes:repairs');
console.log(res9); // 0
// STEP_END
// REMOVE_START
assert.equal(res9, 0);
// REMOVE_END
// STEP_START lMove_lRange
const res10 = await client.lPush('bikes:repairs', 'bike:1');
console.log(res10); // 1
const res11 = await client.lPush('bikes:repairs', 'bike:2');
console.log(res11); // 2
const res12 = await client.lMove('bikes:repairs', 'bikes:finished', 'LEFT', 'LEFT');
console.log(res12); // 'bike:2'
const res13 = await client.lRange('bikes:repairs', 0, -1);
console.log(res13); // ['bike:1']
const res14 = await client.lRange('bikes:finished', 0, -1);
console.log(res14); // ['bike:2']
// STEP_END
// REMOVE_START
assert.equal(res10, 1);
assert.equal(res11, 2);
assert.equal(res12, 'bike:2');
assert.deepEqual(res13, ['bike:1']);
assert.deepEqual(res14, ['bike:2']);
await client.del('bikes:repairs');
// REMOVE_END
// STEP_START lPush_rPush
const res15 = await client.rPush('bikes:repairs', 'bike:1');
console.log(res15); // 1
const res16 = await client.rPush('bikes:repairs', 'bike:2');
console.log(res16); // 2
const res17 = await client.lPush('bikes:repairs', 'bike:important_bike');
console.log(res17); // 3
const res18 = await client.lRange('bikes:repairs', 0, -1);
console.log(res18); // ['bike:important_bike', 'bike:1', 'bike:2']
// STEP_END
// REMOVE_START
assert.equal(res15, 1);
assert.equal(res16, 2);
assert.equal(res17, 3);
assert.deepEqual(res18, ['bike:important_bike', 'bike:1', 'bike:2']);
await client.del('bikes:repairs');
// REMOVE_END
// STEP_START variadic
const res19 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']);
console.log(res19); // 3
const res20 = await client.lPush(
'bikes:repairs', ['bike:important_bike', 'bike:very_important_bike']
);
console.log(res20); // 5
const res21 = await client.lRange('bikes:repairs', 0, -1);
console.log(res21); // ['bike:very_important_bike', 'bike:important_bike', 'bike:1', 'bike:2', 'bike:3']
// STEP_END
// REMOVE_START
assert.equal(res19, 3);
assert.equal(res20, 5);
assert.deepEqual(res21, [
'bike:very_important_bike',
'bike:important_bike',
'bike:1',
'bike:2',
'bike:3',
]);
await client.del('bikes:repairs');
// REMOVE_END
// STEP_START lPop_rPop
const res22 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']);
console.log(res22); // 3
const res23 = await client.rPop('bikes:repairs');
console.log(res23); // 'bike:3'
const res24 = await client.lPop('bikes:repairs');
console.log(res24); // 'bike:1'
const res25 = await client.rPop('bikes:repairs');
console.log(res25); // 'bike:2'
const res26 = await client.rPop('bikes:repairs');
console.log(res26); // null
// STEP_END
// REMOVE_START
assert.deepEqual(res22, 3);
assert.equal(res23, 'bike:3');
assert.equal(res24, 'bike:1');
assert.equal(res25, 'bike:2');
assert.equal(res26, null);
// REMOVE_END
// STEP_START lTrim
const res27 = await client.lPush(
'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']
);
console.log(res27); // 5
const res28 = await client.lTrim('bikes:repairs', 0, 2);
console.log(res28); // OK
const res29 = await client.lRange('bikes:repairs', 0, -1);
console.log(res29); // ['bike:5', 'bike:4', 'bike:3']
// STEP_END
// REMOVE_START
assert.equal(res27, 5);
assert.equal(res28, 'OK');
assert.deepEqual(res29, ['bike:5', 'bike:4', 'bike:3']);
await client.del('bikes:repairs');
// REMOVE_END
// STEP_START lTrim_end_of_list
const res27eol = await client.rPush(
'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']
);
console.log(res27eol); // 5
const res28eol = await client.lTrim('bikes:repairs', -3, -1);
console.log(res28eol); // 'OK'
const res29eol = await client.lRange('bikes:repairs', 0, -1);
console.log(res29eol); // ['bike:3', 'bike:4', 'bike:5']
// STEP_END
// REMOVE_START
assert.equal(res27eol, 5);
assert.equal(res28eol, 'OK');
assert.deepEqual(res29eol, ['bike:3', 'bike:4', 'bike:5']);
await client.del('bikes:repairs');
// REMOVE_END
// STEP_START brPop
const res31 = await client.rPush('bikes:repairs', ['bike:1', 'bike:2']);
console.log(res31); // 2
const res32 = await client.brPop('bikes:repairs', 1);
console.log(res32); // { key: 'bikes:repairs', element: 'bike:2' }
const res33 = await client.brPop('bikes:repairs', 1);
console.log(res33); // { key: 'bikes:repairs', element: 'bike:1' }
const res34 = await client.brPop('bikes:repairs', 1);
console.log(res34); // null
// STEP_END
// REMOVE_START
assert.equal(res31, 2);
assert.deepEqual(res32, { key: 'bikes:repairs', element: 'bike:2' });
assert.deepEqual(res33, { key: 'bikes:repairs', element: 'bike:1' });
assert.equal(res34, null);
await client.del('bikes:repairs');
await client.del('new_bikes');
// REMOVE_END
// STEP_START rule_1
const res35 = await client.del('new_bikes');
console.log(res35); // 0
const res36 = await client.lPush('new_bikes', ['bike:1', 'bike:2', 'bike:3']);
console.log(res36); // 3
// STEP_END
// REMOVE_START
assert.equal(res35, 0);
assert.equal(res36, 3);
await client.del('new_bikes');
// REMOVE_END
// STEP_START rule_1.1
const res37 = await client.set('new_bikes', 'bike:1');
console.log(res37); // 'OK'
const res38 = await client.type('new_bikes');
console.log(res38); // 'string'
try {
const res39 = await client.lPush('new_bikes', 'bike:2', 'bike:3');
// redis.exceptions.ResponseError:
// [SimpleError: WRONGTYPE Operation against a key holding the wrong kind of value]
}
catch(e){
console.log(e);
}
// STEP_END
// REMOVE_START
assert.equal(res37, 'OK');
assert.equal(res38, 'string');
await client.del('new_bikes');
// REMOVE_END
// STEP_START rule_2
await client.lPush('bikes:repairs', ['bike:1', 'bike:2', 'bike:3']);
console.log(res36); // 3
const res40 = await client.exists('bikes:repairs')
console.log(res40); // 1
const res41 = await client.lPop('bikes:repairs');
console.log(res41); // 'bike:3'
const res42 = await client.lPop('bikes:repairs');
console.log(res42); // 'bike:2'
const res43 = await client.lPop('bikes:repairs');
console.log(res43); // 'bike:1'
const res44 = await client.exists('bikes:repairs');
console.log(res44); // 0
// STEP_END
// REMOVE_START
assert.equal(res40, 1);
assert.equal(res41, 'bike:3');
assert.equal(res42, 'bike:2');
assert.equal(res43, 'bike:1');
assert.equal(res44, 0);
await client.del('bikes:repairs');
// REMOVE_END
// STEP_START rule_3
const res45 = await client.del('bikes:repairs');
console.log(res45); // 0
const res46 = await client.lLen('bikes:repairs');
console.log(res46); // 0
const res47 = await client.lPop('bikes:repairs');
console.log(res47); // null
// STEP_END
// REMOVE_START
assert.equal(res45, 0);
assert.equal(res46, 0);
assert.equal(res47, null);
// REMOVE_END
// STEP_START lTrim.1
const res48 = await client.lPush(
'bikes:repairs', ['bike:1', 'bike:2', 'bike:3', 'bike:4', 'bike:5']
);
console.log(res48); // 5
const res49 = await client.lTrim('bikes:repairs', 0, 2);
console.log(res49); // 'OK'
const res50 = await client.lRange('bikes:repairs', 0, -1);
console.log(res50); // ['bike:5', 'bike:4', 'bike:3']
// STEP_END
// REMOVE_START
assert.equal(res48, 5);
assert.equal(res49, 'OK');
assert.deepEqual(res50, ['bike:5', 'bike:4', 'bike:3']);
await client.del('bikes:repairs');
await client.close();
// REMOVE_END

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

@@ -0,0 +1,176 @@
// EXAMPLE: sets_tutorial
// HIDE_START
import assert from 'assert';
import { createClient } from 'redis';
const client = 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) // >>> 1
const res6 = await client.sIsMember('bikes:racing:usa', 'bike:2')
console.log(res6) // >>> 0
// STEP_END
// REMOVE_START
assert.equal(res5, 1)
assert.equal(res6, 0)
// 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) // >>> 1
const res12 = await client.smIsMember('bikes:racing:france', ['bike:2', 'bike:3', 'bike:4'])
console.log(res12) // >>> [1, 1, 0]
// STEP_END
// REMOVE_START
assert.equal(res11, 1)
assert.deepEqual(res12, [1, 1, 0])
// 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.sort(), ['bike:2', 'bike:3'].sort())
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.sort(), ['bike:2', 'bike:3'].sort())
await client.del('bikes:racing:france')
await client.del('bikes:racing:usa')
await client.del('bikes:racing:italy')
// REMOVE_END
// 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)
await client.close()
// none of the other results are deterministic
// REMOVE_END

162
doctests/dt-ss.js Normal file
View File

@@ -0,0 +1,162 @@
// EXAMPLE: ss_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 zadd
const res1 = await client.zAdd('racer_scores', { score: 10, value: 'Norem' });
console.log(res1); // >>> 1
const res2 = await client.zAdd('racer_scores', { score: 12, value: 'Castilla' });
console.log(res2); // >>> 1
const res3 = await client.zAdd('racer_scores', [
{ score: 8, value: 'Sam-Bodden' },
{ score: 10, value: 'Royce' },
{ score: 6, value: 'Ford' },
{ score: 14, value: 'Prickett' },
{ score: 12, value: 'Castilla' }
]);
console.log(res3); // >>> 4
// STEP_END
// REMOVE_START
assert.equal(res1, 1)
assert.equal(res2, 1)
assert.equal(res3, 4)
// REMOVE_END
// REMOVE_START
const count = await client.zCard('racer_scores');
console.assert(count === 6);
// REMOVE_END
// STEP_START zrange
const res4 = await client.zRange('racer_scores', 0, -1);
console.log(res4); // >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce', 'Castilla', 'Prickett']
// STEP_END
// REMOVE_START
assert.deepEqual(res4, ['Ford', 'Sam-Bodden', 'Norem', 'Royce', 'Castilla', 'Prickett'])
// REMOVE_END
// STEP_START zrange_withscores
const res6 = await client.zRangeWithScores('racer_scores', 0, -1);
console.log(res6);
// >>> [
// { value: 'Ford', score: 6 }, { value: 'Sam-Bodden', score: 8 },
// { value: 'Norem', score: 10 }, { value: 'Royce', score: 10 },
// { value: 'Castilla', score: 12 }, { value: 'Prickett', score: 14 }
// ]
// STEP_END
// REMOVE_START
assert.deepEqual(res6, [ { value: 'Ford', score: 6 }, { value: 'Sam-Bodden', score: 8 }, { value: 'Norem', score: 10 }, { value: 'Royce', score: 10 }, { value: 'Castilla', score: 12 }, { value: 'Prickett', score: 14 } ]
)
// REMOVE_END
// STEP_START zrangebyscore
const res7 = await client.zRangeByScore('racer_scores', '-inf', 10);
console.log(res7); // >>> ['Ford', 'Sam-Bodden', 'Norem', 'Royce']
// STEP_END
// REMOVE_START
assert.deepEqual(res7, ['Ford', 'Sam-Bodden', 'Norem', 'Royce'])
// REMOVE_END
// STEP_START zremrangebyscore
const res8 = await client.zRem('racer_scores', 'Castilla');
console.log(res8); // >>> 1
const res9 = await client.zRemRangeByScore('racer_scores', '-inf', 9);
console.log(res9); // >>> 2
// REMOVE_START
assert.equal(res8, 1)
assert.equal(res9, 2)
// REMOVE_END
const res10 = await client.zRange('racer_scores', 0, -1);
console.log(res10); // >>> ['Norem', 'Royce', 'Prickett']
// STEP_END
// REMOVE_START
assert.deepEqual(res10, ['Norem', 'Royce', 'Prickett'])
// REMOVE_END
// REMOVE_START
const count2 = await client.zCard('racer_scores');
console.assert(count2 === 3);
// REMOVE_END
// STEP_START zrank
const res11 = await client.zRank('racer_scores', 'Norem');
console.log(res11); // >>> 0
const res12 = await client.zRevRank('racer_scores', 'Norem');
console.log(res12); // >>> 2
// STEP_END
// STEP_START zadd_lex
const res13 = await client.zAdd('racer_scores', [
{ score: 0, value: 'Norem' },
{ score: 0, value: 'Sam-Bodden' },
{ score: 0, value: 'Royce' },
{ score: 0, value: 'Ford' },
{ score: 0, value: 'Prickett' },
{ score: 0, value: 'Castilla' }
]);
console.log(res13); // >>> 3
// REMOVE_START
assert.equal(count2, 3)
assert.equal(res11, 0)
assert.equal(res12, 2)
assert.equal(res13, 3)
// REMOVE_END
const res14 = await client.zRange('racer_scores', 0, -1);
console.log(res14); // >>> ['Castilla', 'Ford', 'Norem', 'Prickett', 'Royce', 'Sam-Bodden']
const res15 = await client.zRangeByLex('racer_scores', '[A', '[L');
console.log(res15); // >>> ['Castilla', 'Ford']
// STEP_END
// REMOVE_START
assert.deepEqual(res14, ['Castilla', 'Ford', 'Norem', 'Prickett', 'Royce', 'Sam-Bodden'])
assert.deepEqual(res15, ['Castilla', 'Ford'])
// REMOVE_END
// STEP_START leaderboard
const res16 = await client.zAdd('racer_scores', { score: 100, value: 'Wood' });
console.log(res16); // >>> 1
const res17 = await client.zAdd('racer_scores', { score: 100, value: 'Henshaw' });
console.log(res17); // >>> 1
const res18 = await client.zAdd('racer_scores', { score: 150, value: 'Henshaw' }, { nx: true });
console.log(res18); // >>> 0
const res19 = await client.zIncrBy('racer_scores', 50, 'Wood');
console.log(res19); // >>> 150.0
const res20 = await client.zIncrBy('racer_scores', 50, 'Henshaw');
console.log(res20); // >>> 200.0
// STEP_END
// REMOVE_START
assert.equal(res16, 1)
assert.equal(res17, 1)
assert.equal(res18, 0)
assert.equal(res19, 150.0)
assert.equal(res20, 200.0)
await client.close();
// REMOVE_END

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

@@ -0,0 +1,366 @@
// 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', '+', {COUNT: 2});
console.log(res4); // >>> [{ id: '1692629576966-0', message: { rider: 'Castilla', speed: '30.2', position: '1', location_id: '1' } }, { id: '1692629594113-0', message: { 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); // >>> [{ name: 'race:france', messages: [{ id: '1692629576966-0', message: { rider: 'Castilla', speed: '30.2', position: '1', location_id: '1' } }, { id: '1692629594113-0', message: { rider: 'Norem', speed: '28.8', position: '3', location_id: '1' } }, { id: '1692629613374-0', message: { 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); // >>> [SimpleError: ERR The ID specified in XADD is equal or smaller than the target stream top item]
}
// 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); // >>> [{ id: '1692629576966-0', message: { rider: 'Castilla', speed: '30.2', position: '1', location_id: '1' } }, { id: '1692629594113-0', message: { rider: 'Norem', speed: '28.8', position: '3', location_id: '1' } }, { id: '1692629613374-0', message: { rider: 'Prickett', speed: '29.7', position: '2', location_id: '1' } }, { id: '1692629676124-0', message: { 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); // >>> [{ id: '1692629576966-0', message: { rider: 'Castilla', speed: '30.2', position: '1', location_id: '1' } }]
// STEP_END
// STEP_START xRange_step_1
const res13 = await client.xRange('race:france', '-', '+', {COUNT: 2});
console.log(res13); // >>> [{ id: '1692629576966-0', message: { rider: 'Castilla', speed: '30.2', position: '1', location_id: '1' } }, { id: '1692629594113-0', message: { 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', '+', {COUNT: 2});
console.log(res14); // >>> [{ id: '1692629613374-0', message: { rider: 'Prickett', speed: '29.7', position: '2', location_id: '1' } }, { id: '1692629676124-0', message: { 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', '+', {COUNT: 2});
console.log(res15); // >>> []
// STEP_END
// STEP_START xrevrange
const res16 = await client.xRevRange('race:france', '+', '-', {COUNT: 1});
console.log(
res16
); // >>> [{ id: '1692629676124-0', message: { 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); // >>> [{ name: 'race:france', messages: [{ id: '1692629576966-0', message: { rider: 'Castilla', speed: '30.2', position: '1', location_id: '1' } }, { id: '1692629594113-0', message: { 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); // >>> OK
// STEP_END
// STEP_START xgroup_create_mkstream
const res19 = await client.xGroupCreate('race:italy', 'italy_riders', '$', {
MKSTREAM: true
});
console.log(res19); // >>> OK
// 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); // >>> [{ name: 'race:italy', messages: [{ id: '1692629925771-0', message: { 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); // >>> [{ name: 'race:italy', messages: [{ id: '1692629925771-0', message: { 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); // >>> [{ name: 'race:italy', messages: [] }]
// STEP_END
// STEP_START xgroup_read_bob
const res24 = await client.xReadGroup(
'italy_riders',
'Bob', {
key: 'race:italy',
id: '>'
}, {
COUNT: 2
}
);
console.log(res24); // >>> [{ name: 'race:italy', messages: [{ id: '1692629925789-0', message: { rider: 'Royce' } }, { id: '1692629925790-0', message: { rider: 'Sam-Bodden' } }] }]
// STEP_END
// STEP_START xpending
const res25 = await client.xPending('race:italy', 'italy_riders');
console.log(res25); // >>> {'pending': 2, 'firstId': '1692629925789-0', 'lastId': '1692629925790-0', 'consumers': [{'name': 'Bob', 'deliveriesCounter': 2}]}
// STEP_END
// STEP_START xpending_plus_minus
const res26 = await client.xPendingRange('race:italy', 'italy_riders', '-', '+', 10);
console.log(res26); // >>> [{'id': '1692629925789-0', 'consumer': 'Bob', 'millisecondsSinceLastDelivery': 31084, 'deliveriesCounter:': 1}, {'id': '1692629925790-0', 'consumer': 'Bob', 'millisecondsSinceLastDelivery': 31084, 'deliveriesCounter': 1}]
// STEP_END
// STEP_START xRange_pending
const res27 = await client.xRange('race:italy', '1692629925789-0', '1692629925789-0');
console.log(res27); // >>> [{ id: '1692629925789-0', message: { rider: 'Royce' } }]
// STEP_END
// STEP_START xclaim
const res28 = await client.xClaim(
'race:italy', 'italy_riders', 'Alice', 60000, ['1692629925789-0']
);
console.log(res28); // >>> [{ id: '1692629925789-0', message: { rider: 'Royce' } }]
// STEP_END
// STEP_START xautoclaim
const res29 = await client.xAutoClaim('race:italy', 'italy_riders', 'Alice', 1, '0-0', {
COUNT: 1
});
console.log(res29); // >>> { nextId: '1692629925790-0', messages: [{ id: '1692629925789-0', message: { rider: 'Royce' } }], deletedMessages: [] }
// STEP_END
// STEP_START xautoclaim_cursor
const res30 = await client.xAutoClaim(
'race:italy', 'italy_riders', 'Alice', 1, '(1692629925789-0',
{
COUNT: 1
}
);
console.log(res30); // >>> { nextId: '0-0', messages: [{ id: '1692629925790-0', message: { rider: 'Sam-Bodden' } }], deletedMessages: [] }
// 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', 'max-deleted-entry-id': '0-0', 'entries-added': 5, 'recorded-first-entry-id': '1692629925771-0', groups: 1, 'first-entry': { id: '1692629925771-0', message: { rider: 'Castilla' } }, 'last-entry': { id: '1692629926436-0', message: { rider: 'Norem' } } }
// STEP_END
// STEP_START xinfo_groups
const res32 = await client.xInfoGroups('race:italy');
console.log(res32); // >>> [{ name: 'italy_riders', consumers: 2, pending: 3, 'last-delivered-id': '1692629925790-0', 'entries-read': 3, lag: 2 }]
// STEP_END
// STEP_START xinfo_consumers
const res33 = await client.xInfoConsumers('race:italy', 'italy_riders');
console.log(res33); // >>> [{ name: 'Alice', pending: 3, idle: 170582, inactive: 170582 }, { name: 'Bob', pending: 0, idle: 489404, inactive: 489404 }]
// STEP_END
// STEP_START maxlen
await client.xAdd('race:italy', '*', {
'rider': 'Jones'
}, {
TRIM: {
strategy: 'MAXLEN',
strategyModifier: '~',
threshold: 2
}
});
await client.xAdd('race:italy', '*', {
'rider': 'Wood'
}, {
TRIM: {
strategy: 'MAXLEN',
strategyModifier: '~',
threshold: 2
}
});
await client.xAdd('race:italy', '*', {
'rider': 'Henshaw'
}, {
TRIM: {
strategy: 'MAXLEN',
strategyModifier: '~',
threshold: 2
}
});
const res34 = await client.xLen('race:italy');
console.log(res34); // >>> 8
const res35 = await client.xRange('race:italy', '-', '+');
console.log(res35); // >>> [{ id: '1692629925771-0', message: { rider: 'Castilla' } }, { id: '1692629925789-0', message: { rider: 'Royce' } }, { id: '1692629925790-0', message: { rider: 'Sam-Bodden' } }, { id: '1692629925791-0', message: { rider: 'Prickett' } }, { id: '1692629926436-0', message: { rider: 'Norem' } }, { id: '1692630612602-0', message: { rider: 'Jones' } }, { id: '1692630641947-0', message: { rider: 'Wood' } }, { id: '1692630648281-0', message: { rider: 'Henshaw' } }]
await client.xAdd('race:italy', '*', {
'rider': 'Smith'
}, {
TRIM: {
strategy: 'MAXLEN',
strategyModifier: '=',
threshold: 2
}
});
const res36 = await client.xRange('race:italy', '-', '+');
console.log(res36); // >>> [{ id: '1692630648281-0', message: { rider: 'Henshaw' } }, { id: '1692631018238-0', message: { rider: 'Smith' } }]
// STEP_END
// STEP_START xTrim
const res37 = await client.xTrim('race:italy', 'MAXLEN', 10, {
strategyModifier: '=',
});
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); // >>> [{ id: '1692630648281-0', message: { rider: 'Henshaw' } }, { id: '1692631018238-0', message: { 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); // >>> [{ id: '1692630648281-0', message: { 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: set_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 set_get
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 setnx_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.close();
// REMOVE_END

85
doctests/dt-tdigest.js Normal file
View File

@@ -0,0 +1,85 @@
// EXAMPLE: tdigest_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 tdig_start
const res1 = await client.tDigest.create('bikes:sales', 100);
console.log(res1); // >>> OK
const res2 = await client.tDigest.add('bikes:sales', [21]);
console.log(res2); // >>> OK
const res3 = await client.tDigest.add('bikes:sales', [150, 95, 75, 34]);
console.log(res3); // >>> OK
// STEP_END
// REMOVE_START
assert.equal(res1, 'OK')
assert.equal(res2, 'OK')
assert.equal(res3, 'OK')
// REMOVE_END
// STEP_START tdig_cdf
const res4 = await client.tDigest.create('racer_ages');
console.log(res4); // >>> OK
const res5 = await client.tDigest.add('racer_ages', [
45.88, 44.2, 58.03, 19.76, 39.84, 69.28, 50.97, 25.41, 19.27, 85.71, 42.63
]);
console.log(res5); // >>> OK
const res6 = await client.tDigest.rank('racer_ages', [50]);
console.log(res6); // >>> [7]
const res7 = await client.tDigest.rank('racer_ages', [50, 40]);
console.log(res7); // >>> [7, 4]
// STEP_END
// REMOVE_START
assert.equal(res4, 'OK')
assert.equal(res5, 'OK')
assert.deepEqual(res6, [7])
assert.deepEqual(res7, [7, 4])
// REMOVE_END
// STEP_START tdig_quant
const res8 = await client.tDigest.quantile('racer_ages', [0.5]);
console.log(res8); // >>> [44.2]
const res9 = await client.tDigest.byRank('racer_ages', [4]);
console.log(res9); // >>> [42.63]
// STEP_END
// STEP_START tdig_min
const res10 = await client.tDigest.min('racer_ages');
console.log(res10); // >>> 19.27
const res11 = await client.tDigest.max('racer_ages');
console.log(res11); // >>> 85.71
// STEP_END
// REMOVE_START
assert.deepEqual(res8, [44.2])
assert.deepEqual(res9, [42.63])
assert.equal(res10, 19.27)
assert.equal(res11, 85.71)
// REMOVE_END
// STEP_START tdig_reset
const res12 = await client.tDigest.reset('racer_ages');
console.log(res12); // >>> OK
// STEP_END
// REMOVE_START
assert.equal(res12, 'OK')
await client.close();
// REMOVE_END

48
doctests/dt-topk.js Normal file
View File

@@ -0,0 +1,48 @@
// EXAMPLE: topk_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 topk
const res1 = await client.topK.reserve('bikes:keywords', 5, {
width: 2000,
depth: 7,
decay: 0.925
});
console.log(res1); // >>> OK
const res2 = await client.topK.add('bikes:keywords', [
'store',
'seat',
'handlebars',
'handles',
'pedals',
'tires',
'store',
'seat'
]);
console.log(res2); // >>> [null, null, null, null, null, 'handlebars', null, null]
const res3 = await client.topK.list('bikes:keywords');
console.log(res3); // >>> ['store', 'seat', 'pedals', 'tires', 'handles']
const res4 = await client.topK.query('bikes:keywords', ['store', 'handlebars']);
console.log(res4); // >>> [true, false]
// STEP_END
// REMOVE_START
assert.equal(res1, 'OK')
assert.deepEqual(res2, [null, null, null, null, null, 'handlebars', null, null])
assert.deepEqual(res3, ['store', 'seat', 'pedals', 'tires', 'handles'])
assert.deepEqual(res4, [1, 0])
await client.close();
// REMOVE_END

889
doctests/package-lock.json generated Normal file
View File

@@ -0,0 +1,889 @@
{
"name": "node-redis-doctests",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "node-redis-doctests",
"version": "1.0.0",
"dependencies": {
"@xenova/transformers": "^2.17.2",
"redis": "file:/packages/redis"
}
},
"..": {
"name": "redis-monorepo",
"extraneous": true,
"workspaces": [
"./packages/client",
"./packages/test-utils",
"./packages/bloom",
"./packages/json",
"./packages/search",
"./packages/time-series",
"./packages/entraid",
"./packages/redis"
],
"devDependencies": {
"@istanbuljs/nyc-config-typescript": "^1.0.2",
"@release-it/bumper": "^7.0.5",
"@types/mocha": "^10.0.6",
"@types/node": "^20.11.16",
"gh-pages": "^6.1.1",
"mocha": "^10.2.0",
"nyc": "^15.1.0",
"release-it": "^19.0.2",
"ts-node": "^10.9.2",
"tsx": "^4.7.0",
"typedoc": "^0.25.7",
"typescript": "^5.3.3"
}
},
"../../../../../packages/redis": {},
"node_modules/@huggingface/jinja": {
"version": "0.2.2",
"resolved": "https://registry.npmjs.org/@huggingface/jinja/-/jinja-0.2.2.tgz",
"integrity": "sha512-/KPde26khDUIPkTGU82jdtTW9UAuvUTumCAbFs/7giR0SxsvZC4hru51PBvpijH6BVkHcROcvZM/lpy5h1jRRA==",
"license": "MIT",
"engines": {
"node": ">=18"
}
},
"node_modules/@protobufjs/aspromise": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/@protobufjs/aspromise/-/aspromise-1.1.2.tgz",
"integrity": "sha512-j+gKExEuLmKwvz3OgROXtrJ2UG2x8Ch2YZUxahh+s1F2HZ+wAceUNLkvy6zKCPVRkU++ZWQrdxsUeQXmcg4uoQ==",
"license": "BSD-3-Clause"
},
"node_modules/@protobufjs/base64": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/@protobufjs/base64/-/base64-1.1.2.tgz",
"integrity": "sha512-AZkcAA5vnN/v4PDqKyMR5lx7hZttPDgClv83E//FMNhR2TMcLUhfRUBHCmSl0oi9zMgDDqRUJkSxO3wm85+XLg==",
"license": "BSD-3-Clause"
},
"node_modules/@protobufjs/codegen": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/@protobufjs/codegen/-/codegen-2.0.4.tgz",
"integrity": "sha512-YyFaikqM5sH0ziFZCN3xDC7zeGaB/d0IUb9CATugHWbd1FRFwWwt4ld4OYMPWu5a3Xe01mGAULCdqhMlPl29Jg==",
"license": "BSD-3-Clause"
},
"node_modules/@protobufjs/eventemitter": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/@protobufjs/eventemitter/-/eventemitter-1.1.0.tgz",
"integrity": "sha512-j9ednRT81vYJ9OfVuXG6ERSTdEL1xVsNgqpkxMsbIabzSo3goCjDIveeGv5d03om39ML71RdmrGNjG5SReBP/Q==",
"license": "BSD-3-Clause"
},
"node_modules/@protobufjs/fetch": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/@protobufjs/fetch/-/fetch-1.1.0.tgz",
"integrity": "sha512-lljVXpqXebpsijW71PZaCYeIcE5on1w5DlQy5WH6GLbFryLUrBD4932W/E2BSpfRJWseIL4v/KPgBFxDOIdKpQ==",
"license": "BSD-3-Clause",
"dependencies": {
"@protobufjs/aspromise": "^1.1.1",
"@protobufjs/inquire": "^1.1.0"
}
},
"node_modules/@protobufjs/float": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/@protobufjs/float/-/float-1.0.2.tgz",
"integrity": "sha512-Ddb+kVXlXst9d+R9PfTIxh1EdNkgoRe5tOX6t01f1lYWOvJnSPDBlG241QLzcyPdoNTsblLUdujGSE4RzrTZGQ==",
"license": "BSD-3-Clause"
},
"node_modules/@protobufjs/inquire": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/@protobufjs/inquire/-/inquire-1.1.0.tgz",
"integrity": "sha512-kdSefcPdruJiFMVSbn801t4vFK7KB/5gd2fYvrxhuJYg8ILrmn9SKSX2tZdV6V+ksulWqS7aXjBcRXl3wHoD9Q==",
"license": "BSD-3-Clause"
},
"node_modules/@protobufjs/path": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/@protobufjs/path/-/path-1.1.2.tgz",
"integrity": "sha512-6JOcJ5Tm08dOHAbdR3GrvP+yUUfkjG5ePsHYczMFLq3ZmMkAD98cDgcT2iA1lJ9NVwFd4tH/iSSoe44YWkltEA==",
"license": "BSD-3-Clause"
},
"node_modules/@protobufjs/pool": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/@protobufjs/pool/-/pool-1.1.0.tgz",
"integrity": "sha512-0kELaGSIDBKvcgS4zkjz1PeddatrjYcmMWOlAuAPwAeccUrPHdUqo/J6LiymHHEiJT5NrF1UVwxY14f+fy4WQw==",
"license": "BSD-3-Clause"
},
"node_modules/@protobufjs/utf8": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/@protobufjs/utf8/-/utf8-1.1.0.tgz",
"integrity": "sha512-Vvn3zZrhQZkkBE8LSuW3em98c0FwgO4nxzv6OdSxPKJIEKY2bGbHn+mhGIPerzI4twdxaP8/0+06HBpwf345Lw==",
"license": "BSD-3-Clause"
},
"node_modules/@types/long": {
"version": "4.0.2",
"resolved": "https://registry.npmjs.org/@types/long/-/long-4.0.2.tgz",
"integrity": "sha512-MqTGEo5bj5t157U6fA/BiDynNkn0YknVdh48CMPkTSpFTVmvao5UQmm7uEF6xBEo7qIMAlY/JSleYaE6VOdpaA==",
"license": "MIT"
},
"node_modules/@types/node": {
"version": "24.0.10",
"resolved": "https://registry.npmjs.org/@types/node/-/node-24.0.10.tgz",
"integrity": "sha512-ENHwaH+JIRTDIEEbDK6QSQntAYGtbvdDXnMXnZaZ6k13Du1dPMmprkEHIL7ok2Wl2aZevetwTAb5S+7yIF+enA==",
"license": "MIT",
"dependencies": {
"undici-types": "~7.8.0"
}
},
"node_modules/@xenova/transformers": {
"version": "2.17.2",
"resolved": "https://registry.npmjs.org/@xenova/transformers/-/transformers-2.17.2.tgz",
"integrity": "sha512-lZmHqzrVIkSvZdKZEx7IYY51TK0WDrC8eR0c5IMnBsO8di8are1zzw8BlLhyO2TklZKLN5UffNGs1IJwT6oOqQ==",
"license": "Apache-2.0",
"dependencies": {
"@huggingface/jinja": "^0.2.2",
"onnxruntime-web": "1.14.0",
"sharp": "^0.32.0"
},
"optionalDependencies": {
"onnxruntime-node": "1.14.0"
}
},
"node_modules/b4a": {
"version": "1.6.7",
"resolved": "https://registry.npmjs.org/b4a/-/b4a-1.6.7.tgz",
"integrity": "sha512-OnAYlL5b7LEkALw87fUVafQw5rVR9RjwGd4KUwNQ6DrrNmaVaUCgLipfVlzrPQ4tWOR9P0IXGNOx50jYCCdSJg==",
"license": "Apache-2.0"
},
"node_modules/bare-events": {
"version": "2.5.4",
"resolved": "https://registry.npmjs.org/bare-events/-/bare-events-2.5.4.tgz",
"integrity": "sha512-+gFfDkR8pj4/TrWCGUGWmJIkBwuxPS5F+a5yWjOHQt2hHvNZd5YLzadjmDUtFmMM4y429bnKLa8bYBMHcYdnQA==",
"license": "Apache-2.0",
"optional": true
},
"node_modules/bare-fs": {
"version": "4.1.6",
"resolved": "https://registry.npmjs.org/bare-fs/-/bare-fs-4.1.6.tgz",
"integrity": "sha512-25RsLF33BqooOEFNdMcEhMpJy8EoR88zSMrnOQOaM3USnOK2VmaJ1uaQEwPA6AQjrv1lXChScosN6CzbwbO9OQ==",
"license": "Apache-2.0",
"optional": true,
"dependencies": {
"bare-events": "^2.5.4",
"bare-path": "^3.0.0",
"bare-stream": "^2.6.4"
},
"engines": {
"bare": ">=1.16.0"
},
"peerDependencies": {
"bare-buffer": "*"
},
"peerDependenciesMeta": {
"bare-buffer": {
"optional": true
}
}
},
"node_modules/bare-os": {
"version": "3.6.1",
"resolved": "https://registry.npmjs.org/bare-os/-/bare-os-3.6.1.tgz",
"integrity": "sha512-uaIjxokhFidJP+bmmvKSgiMzj2sV5GPHaZVAIktcxcpCyBFFWO+YlikVAdhmUo2vYFvFhOXIAlldqV29L8126g==",
"license": "Apache-2.0",
"optional": true,
"engines": {
"bare": ">=1.14.0"
}
},
"node_modules/bare-path": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/bare-path/-/bare-path-3.0.0.tgz",
"integrity": "sha512-tyfW2cQcB5NN8Saijrhqn0Zh7AnFNsnczRcuWODH0eYAXBsJ5gVxAUuNr7tsHSC6IZ77cA0SitzT+s47kot8Mw==",
"license": "Apache-2.0",
"optional": true,
"dependencies": {
"bare-os": "^3.0.1"
}
},
"node_modules/bare-stream": {
"version": "2.6.5",
"resolved": "https://registry.npmjs.org/bare-stream/-/bare-stream-2.6.5.tgz",
"integrity": "sha512-jSmxKJNJmHySi6hC42zlZnq00rga4jjxcgNZjY9N5WlOe/iOoGRtdwGsHzQv2RlH2KOYMwGUXhf2zXd32BA9RA==",
"license": "Apache-2.0",
"optional": true,
"dependencies": {
"streamx": "^2.21.0"
},
"peerDependencies": {
"bare-buffer": "*",
"bare-events": "*"
},
"peerDependenciesMeta": {
"bare-buffer": {
"optional": true
},
"bare-events": {
"optional": true
}
}
},
"node_modules/base64-js": {
"version": "1.5.1",
"resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz",
"integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
],
"license": "MIT"
},
"node_modules/bl": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/bl/-/bl-4.1.0.tgz",
"integrity": "sha512-1W07cM9gS6DcLperZfFSj+bWLtaPGSOHWhPiGzXmvVJbRLdG82sH/Kn8EtW1VqWVA54AKf2h5k5BbnIbwF3h6w==",
"license": "MIT",
"dependencies": {
"buffer": "^5.5.0",
"inherits": "^2.0.4",
"readable-stream": "^3.4.0"
}
},
"node_modules/buffer": {
"version": "5.7.1",
"resolved": "https://registry.npmjs.org/buffer/-/buffer-5.7.1.tgz",
"integrity": "sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
],
"license": "MIT",
"dependencies": {
"base64-js": "^1.3.1",
"ieee754": "^1.1.13"
}
},
"node_modules/chownr": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/chownr/-/chownr-1.1.4.tgz",
"integrity": "sha512-jJ0bqzaylmJtVnNgzTeSOs8DPavpbYgEr/b0YL8/2GO3xJEhInFmhKMUnEJQjZumK7KXGFhUy89PrsJWlakBVg==",
"license": "ISC"
},
"node_modules/color": {
"version": "4.2.3",
"resolved": "https://registry.npmjs.org/color/-/color-4.2.3.tgz",
"integrity": "sha512-1rXeuUUiGGrykh+CeBdu5Ie7OJwinCgQY0bc7GCRxy5xVHy+moaqkpL/jqQq0MtQOeYcrqEz4abc5f0KtU7W4A==",
"license": "MIT",
"dependencies": {
"color-convert": "^2.0.1",
"color-string": "^1.9.0"
},
"engines": {
"node": ">=12.5.0"
}
},
"node_modules/color-convert": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
"integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==",
"license": "MIT",
"dependencies": {
"color-name": "~1.1.4"
},
"engines": {
"node": ">=7.0.0"
}
},
"node_modules/color-name": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
"integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==",
"license": "MIT"
},
"node_modules/color-string": {
"version": "1.9.1",
"resolved": "https://registry.npmjs.org/color-string/-/color-string-1.9.1.tgz",
"integrity": "sha512-shrVawQFojnZv6xM40anx4CkoDP+fZsw/ZerEMsW/pyzsRbElpsL/DBVW7q3ExxwusdNXI3lXpuhEZkzs8p5Eg==",
"license": "MIT",
"dependencies": {
"color-name": "^1.0.0",
"simple-swizzle": "^0.2.2"
}
},
"node_modules/decompress-response": {
"version": "6.0.0",
"resolved": "https://registry.npmjs.org/decompress-response/-/decompress-response-6.0.0.tgz",
"integrity": "sha512-aW35yZM6Bb/4oJlZncMH2LCoZtJXTRxES17vE3hoRiowU2kWHaJKFkSBDnDR+cm9J+9QhXmREyIfv0pji9ejCQ==",
"license": "MIT",
"dependencies": {
"mimic-response": "^3.1.0"
},
"engines": {
"node": ">=10"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/deep-extend": {
"version": "0.6.0",
"resolved": "https://registry.npmjs.org/deep-extend/-/deep-extend-0.6.0.tgz",
"integrity": "sha512-LOHxIOaPYdHlJRtCQfDIVZtfw/ufM8+rVj649RIHzcm/vGwQRXFt6OPqIFWsm2XEMrNIEtWR64sY1LEKD2vAOA==",
"license": "MIT",
"engines": {
"node": ">=4.0.0"
}
},
"node_modules/detect-libc": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.0.4.tgz",
"integrity": "sha512-3UDv+G9CsCKO1WKMGw9fwq/SWJYbI0c5Y7LU1AXYoDdbhE2AHQ6N6Nb34sG8Fj7T5APy8qXDCKuuIHd1BR0tVA==",
"license": "Apache-2.0",
"engines": {
"node": ">=8"
}
},
"node_modules/end-of-stream": {
"version": "1.4.5",
"resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.5.tgz",
"integrity": "sha512-ooEGc6HP26xXq/N+GCGOT0JKCLDGrq2bQUZrQ7gyrJiZANJ/8YDTxTpQBXGMn+WbIQXNVpyWymm7KYVICQnyOg==",
"license": "MIT",
"dependencies": {
"once": "^1.4.0"
}
},
"node_modules/expand-template": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/expand-template/-/expand-template-2.0.3.tgz",
"integrity": "sha512-XYfuKMvj4O35f/pOXLObndIRvyQ+/+6AhODh+OKWj9S9498pHHn/IMszH+gt0fBCRWMNfk1ZSp5x3AifmnI2vg==",
"license": "(MIT OR WTFPL)",
"engines": {
"node": ">=6"
}
},
"node_modules/fast-fifo": {
"version": "1.3.2",
"resolved": "https://registry.npmjs.org/fast-fifo/-/fast-fifo-1.3.2.tgz",
"integrity": "sha512-/d9sfos4yxzpwkDkuN7k2SqFKtYNmCTzgfEpz82x34IM9/zc8KGxQoXg1liNC/izpRM/MBdt44Nmx41ZWqk+FQ==",
"license": "MIT"
},
"node_modules/flatbuffers": {
"version": "1.12.0",
"resolved": "https://registry.npmjs.org/flatbuffers/-/flatbuffers-1.12.0.tgz",
"integrity": "sha512-c7CZADjRcl6j0PlvFy0ZqXQ67qSEZfrVPynmnL+2zPc+NtMvrF8Y0QceMo7QqnSPc7+uWjUIAbvCQ5WIKlMVdQ==",
"license": "SEE LICENSE IN LICENSE.txt"
},
"node_modules/fs-constants": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/fs-constants/-/fs-constants-1.0.0.tgz",
"integrity": "sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==",
"license": "MIT"
},
"node_modules/github-from-package": {
"version": "0.0.0",
"resolved": "https://registry.npmjs.org/github-from-package/-/github-from-package-0.0.0.tgz",
"integrity": "sha512-SyHy3T1v2NUXn29OsWdxmK6RwHD+vkj3v8en8AOBZ1wBQ/hCAQ5bAQTD02kW4W9tUp/3Qh6J8r9EvntiyCmOOw==",
"license": "MIT"
},
"node_modules/guid-typescript": {
"version": "1.0.9",
"resolved": "https://registry.npmjs.org/guid-typescript/-/guid-typescript-1.0.9.tgz",
"integrity": "sha512-Y8T4vYhEfwJOTbouREvG+3XDsjr8E3kIr7uf+JZ0BYloFsttiHU0WfvANVsR7TxNUJa/WpCnw/Ino/p+DeBhBQ==",
"license": "ISC"
},
"node_modules/ieee754": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz",
"integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
],
"license": "BSD-3-Clause"
},
"node_modules/inherits": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz",
"integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==",
"license": "ISC"
},
"node_modules/ini": {
"version": "1.3.8",
"resolved": "https://registry.npmjs.org/ini/-/ini-1.3.8.tgz",
"integrity": "sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew==",
"license": "ISC"
},
"node_modules/is-arrayish": {
"version": "0.3.2",
"resolved": "https://registry.npmjs.org/is-arrayish/-/is-arrayish-0.3.2.tgz",
"integrity": "sha512-eVRqCvVlZbuw3GrM63ovNSNAeA1K16kaR/LRY/92w0zxQ5/1YzwblUX652i4Xs9RwAGjW9d9y6X88t8OaAJfWQ==",
"license": "MIT"
},
"node_modules/long": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/long/-/long-4.0.0.tgz",
"integrity": "sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA==",
"license": "Apache-2.0"
},
"node_modules/mimic-response": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/mimic-response/-/mimic-response-3.1.0.tgz",
"integrity": "sha512-z0yWI+4FDrrweS8Zmt4Ej5HdJmky15+L2e6Wgn3+iK5fWzb6T3fhNFq2+MeTRb064c6Wr4N/wv0DzQTjNzHNGQ==",
"license": "MIT",
"engines": {
"node": ">=10"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/minimist": {
"version": "1.2.8",
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz",
"integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==",
"license": "MIT",
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/mkdirp-classic": {
"version": "0.5.3",
"resolved": "https://registry.npmjs.org/mkdirp-classic/-/mkdirp-classic-0.5.3.tgz",
"integrity": "sha512-gKLcREMhtuZRwRAfqP3RFW+TK4JqApVBtOIftVgjuABpAtpxhPGaDcfvbhNvD0B8iD1oUr/txX35NjcaY6Ns/A==",
"license": "MIT"
},
"node_modules/napi-build-utils": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/napi-build-utils/-/napi-build-utils-2.0.0.tgz",
"integrity": "sha512-GEbrYkbfF7MoNaoh2iGG84Mnf/WZfB0GdGEsM8wz7Expx/LlWf5U8t9nvJKXSp3qr5IsEbK04cBGhol/KwOsWA==",
"license": "MIT"
},
"node_modules/node-abi": {
"version": "3.75.0",
"resolved": "https://registry.npmjs.org/node-abi/-/node-abi-3.75.0.tgz",
"integrity": "sha512-OhYaY5sDsIka7H7AtijtI9jwGYLyl29eQn/W623DiN/MIv5sUqc4g7BIDThX+gb7di9f6xK02nkp8sdfFWZLTg==",
"license": "MIT",
"dependencies": {
"semver": "^7.3.5"
},
"engines": {
"node": ">=10"
}
},
"node_modules/node-addon-api": {
"version": "6.1.0",
"resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-6.1.0.tgz",
"integrity": "sha512-+eawOlIgy680F0kBzPUNFhMZGtJ1YmqM6l4+Crf4IkImjYrO/mqPwRMh352g23uIaQKFItcQ64I7KMaJxHgAVA==",
"license": "MIT"
},
"node_modules/once": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz",
"integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==",
"license": "ISC",
"dependencies": {
"wrappy": "1"
}
},
"node_modules/onnx-proto": {
"version": "4.0.4",
"resolved": "https://registry.npmjs.org/onnx-proto/-/onnx-proto-4.0.4.tgz",
"integrity": "sha512-aldMOB3HRoo6q/phyB6QRQxSt895HNNw82BNyZ2CMh4bjeKv7g/c+VpAFtJuEMVfYLMbRx61hbuqnKceLeDcDA==",
"license": "MIT",
"dependencies": {
"protobufjs": "^6.8.8"
}
},
"node_modules/onnxruntime-common": {
"version": "1.14.0",
"resolved": "https://registry.npmjs.org/onnxruntime-common/-/onnxruntime-common-1.14.0.tgz",
"integrity": "sha512-3LJpegM2iMNRX2wUmtYfeX/ytfOzNwAWKSq1HbRrKc9+uqG/FsEA0bbKZl1btQeZaXhC26l44NWpNUeXPII7Ew==",
"license": "MIT"
},
"node_modules/onnxruntime-node": {
"version": "1.14.0",
"resolved": "https://registry.npmjs.org/onnxruntime-node/-/onnxruntime-node-1.14.0.tgz",
"integrity": "sha512-5ba7TWomIV/9b6NH/1x/8QEeowsb+jBEvFzU6z0T4mNsFwdPqXeFUM7uxC6QeSRkEbWu3qEB0VMjrvzN/0S9+w==",
"license": "MIT",
"optional": true,
"os": [
"win32",
"darwin",
"linux"
],
"dependencies": {
"onnxruntime-common": "~1.14.0"
}
},
"node_modules/onnxruntime-web": {
"version": "1.14.0",
"resolved": "https://registry.npmjs.org/onnxruntime-web/-/onnxruntime-web-1.14.0.tgz",
"integrity": "sha512-Kcqf43UMfW8mCydVGcX9OMXI2VN17c0p6XvR7IPSZzBf/6lteBzXHvcEVWDPmCKuGombl997HgLqj91F11DzXw==",
"license": "MIT",
"dependencies": {
"flatbuffers": "^1.12.0",
"guid-typescript": "^1.0.9",
"long": "^4.0.0",
"onnx-proto": "^4.0.4",
"onnxruntime-common": "~1.14.0",
"platform": "^1.3.6"
}
},
"node_modules/platform": {
"version": "1.3.6",
"resolved": "https://registry.npmjs.org/platform/-/platform-1.3.6.tgz",
"integrity": "sha512-fnWVljUchTro6RiCFvCXBbNhJc2NijN7oIQxbwsyL0buWJPG85v81ehlHI9fXrJsMNgTofEoWIQeClKpgxFLrg==",
"license": "MIT"
},
"node_modules/prebuild-install": {
"version": "7.1.3",
"resolved": "https://registry.npmjs.org/prebuild-install/-/prebuild-install-7.1.3.tgz",
"integrity": "sha512-8Mf2cbV7x1cXPUILADGI3wuhfqWvtiLA1iclTDbFRZkgRQS0NqsPZphna9V+HyTEadheuPmjaJMsbzKQFOzLug==",
"license": "MIT",
"dependencies": {
"detect-libc": "^2.0.0",
"expand-template": "^2.0.3",
"github-from-package": "0.0.0",
"minimist": "^1.2.3",
"mkdirp-classic": "^0.5.3",
"napi-build-utils": "^2.0.0",
"node-abi": "^3.3.0",
"pump": "^3.0.0",
"rc": "^1.2.7",
"simple-get": "^4.0.0",
"tar-fs": "^2.0.0",
"tunnel-agent": "^0.6.0"
},
"bin": {
"prebuild-install": "bin.js"
},
"engines": {
"node": ">=10"
}
},
"node_modules/prebuild-install/node_modules/tar-fs": {
"version": "2.1.3",
"resolved": "https://registry.npmjs.org/tar-fs/-/tar-fs-2.1.3.tgz",
"integrity": "sha512-090nwYJDmlhwFwEW3QQl+vaNnxsO2yVsd45eTKRBzSzu+hlb1w2K9inVq5b0ngXuLVqQ4ApvsUHHnu/zQNkWAg==",
"license": "MIT",
"dependencies": {
"chownr": "^1.1.1",
"mkdirp-classic": "^0.5.2",
"pump": "^3.0.0",
"tar-stream": "^2.1.4"
}
},
"node_modules/prebuild-install/node_modules/tar-stream": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/tar-stream/-/tar-stream-2.2.0.tgz",
"integrity": "sha512-ujeqbceABgwMZxEJnk2HDY2DlnUZ+9oEcb1KzTVfYHio0UE6dG71n60d8D2I4qNvleWrrXpmjpt7vZeF1LnMZQ==",
"license": "MIT",
"dependencies": {
"bl": "^4.0.3",
"end-of-stream": "^1.4.1",
"fs-constants": "^1.0.0",
"inherits": "^2.0.3",
"readable-stream": "^3.1.1"
},
"engines": {
"node": ">=6"
}
},
"node_modules/protobufjs": {
"version": "6.11.4",
"resolved": "https://registry.npmjs.org/protobufjs/-/protobufjs-6.11.4.tgz",
"integrity": "sha512-5kQWPaJHi1WoCpjTGszzQ32PG2F4+wRY6BmAT4Vfw56Q2FZ4YZzK20xUYQH4YkfehY1e6QSICrJquM6xXZNcrw==",
"hasInstallScript": true,
"license": "BSD-3-Clause",
"dependencies": {
"@protobufjs/aspromise": "^1.1.2",
"@protobufjs/base64": "^1.1.2",
"@protobufjs/codegen": "^2.0.4",
"@protobufjs/eventemitter": "^1.1.0",
"@protobufjs/fetch": "^1.1.0",
"@protobufjs/float": "^1.0.2",
"@protobufjs/inquire": "^1.1.0",
"@protobufjs/path": "^1.1.2",
"@protobufjs/pool": "^1.1.0",
"@protobufjs/utf8": "^1.1.0",
"@types/long": "^4.0.1",
"@types/node": ">=13.7.0",
"long": "^4.0.0"
},
"bin": {
"pbjs": "bin/pbjs",
"pbts": "bin/pbts"
}
},
"node_modules/pump": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/pump/-/pump-3.0.3.tgz",
"integrity": "sha512-todwxLMY7/heScKmntwQG8CXVkWUOdYxIvY2s0VWAAMh/nd8SoYiRaKjlr7+iCs984f2P8zvrfWcDDYVb73NfA==",
"license": "MIT",
"dependencies": {
"end-of-stream": "^1.1.0",
"once": "^1.3.1"
}
},
"node_modules/rc": {
"version": "1.2.8",
"resolved": "https://registry.npmjs.org/rc/-/rc-1.2.8.tgz",
"integrity": "sha512-y3bGgqKj3QBdxLbLkomlohkvsA8gdAiUQlSBJnBhfn+BPxg4bc62d8TcBW15wavDfgexCgccckhcZvywyQYPOw==",
"license": "(BSD-2-Clause OR MIT OR Apache-2.0)",
"dependencies": {
"deep-extend": "^0.6.0",
"ini": "~1.3.0",
"minimist": "^1.2.0",
"strip-json-comments": "~2.0.1"
},
"bin": {
"rc": "cli.js"
}
},
"node_modules/readable-stream": {
"version": "3.6.2",
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.2.tgz",
"integrity": "sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==",
"license": "MIT",
"dependencies": {
"inherits": "^2.0.3",
"string_decoder": "^1.1.1",
"util-deprecate": "^1.0.1"
},
"engines": {
"node": ">= 6"
}
},
"node_modules/redis": {
"resolved": "../../../../../packages/redis",
"link": true
},
"node_modules/safe-buffer": {
"version": "5.2.1",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz",
"integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
],
"license": "MIT"
},
"node_modules/semver": {
"version": "7.7.2",
"resolved": "https://registry.npmjs.org/semver/-/semver-7.7.2.tgz",
"integrity": "sha512-RF0Fw+rO5AMf9MAyaRXI4AV0Ulj5lMHqVxxdSgiVbixSCXoEmmX/jk0CuJw4+3SqroYO9VoUh+HcuJivvtJemA==",
"license": "ISC",
"bin": {
"semver": "bin/semver.js"
},
"engines": {
"node": ">=10"
}
},
"node_modules/sharp": {
"version": "0.32.6",
"resolved": "https://registry.npmjs.org/sharp/-/sharp-0.32.6.tgz",
"integrity": "sha512-KyLTWwgcR9Oe4d9HwCwNM2l7+J0dUQwn/yf7S0EnTtb0eVS4RxO0eUSvxPtzT4F3SY+C4K6fqdv/DO27sJ/v/w==",
"hasInstallScript": true,
"license": "Apache-2.0",
"dependencies": {
"color": "^4.2.3",
"detect-libc": "^2.0.2",
"node-addon-api": "^6.1.0",
"prebuild-install": "^7.1.1",
"semver": "^7.5.4",
"simple-get": "^4.0.1",
"tar-fs": "^3.0.4",
"tunnel-agent": "^0.6.0"
},
"engines": {
"node": ">=14.15.0"
},
"funding": {
"url": "https://opencollective.com/libvips"
}
},
"node_modules/simple-concat": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/simple-concat/-/simple-concat-1.0.1.tgz",
"integrity": "sha512-cSFtAPtRhljv69IK0hTVZQ+OfE9nePi/rtJmw5UjHeVyVroEqJXP1sFztKUy1qU+xvz3u/sfYJLa947b7nAN2Q==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
],
"license": "MIT"
},
"node_modules/simple-get": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/simple-get/-/simple-get-4.0.1.tgz",
"integrity": "sha512-brv7p5WgH0jmQJr1ZDDfKDOSeWWg+OVypG99A/5vYGPqJ6pxiaHLy8nxtFjBA7oMa01ebA9gfh1uMCFqOuXxvA==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
],
"license": "MIT",
"dependencies": {
"decompress-response": "^6.0.0",
"once": "^1.3.1",
"simple-concat": "^1.0.0"
}
},
"node_modules/simple-swizzle": {
"version": "0.2.2",
"resolved": "https://registry.npmjs.org/simple-swizzle/-/simple-swizzle-0.2.2.tgz",
"integrity": "sha512-JA//kQgZtbuY83m+xT+tXJkmJncGMTFT+C+g2h2R9uxkYIrE2yy9sgmcLhCnw57/WSD+Eh3J97FPEDFnbXnDUg==",
"license": "MIT",
"dependencies": {
"is-arrayish": "^0.3.1"
}
},
"node_modules/streamx": {
"version": "2.22.1",
"resolved": "https://registry.npmjs.org/streamx/-/streamx-2.22.1.tgz",
"integrity": "sha512-znKXEBxfatz2GBNK02kRnCXjV+AA4kjZIUxeWSr3UGirZMJfTE9uiwKHobnbgxWyL/JWro8tTq+vOqAK1/qbSA==",
"license": "MIT",
"dependencies": {
"fast-fifo": "^1.3.2",
"text-decoder": "^1.1.0"
},
"optionalDependencies": {
"bare-events": "^2.2.0"
}
},
"node_modules/string_decoder": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz",
"integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==",
"license": "MIT",
"dependencies": {
"safe-buffer": "~5.2.0"
}
},
"node_modules/strip-json-comments": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-2.0.1.tgz",
"integrity": "sha512-4gB8na07fecVVkOI6Rs4e7T6NOTki5EmL7TUduTs6bu3EdnSycntVJ4re8kgZA+wx9IueI2Y11bfbgwtzuE0KQ==",
"license": "MIT",
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/tar-fs": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/tar-fs/-/tar-fs-3.1.0.tgz",
"integrity": "sha512-5Mty5y/sOF1YWj1J6GiBodjlDc05CUR8PKXrsnFAiSG0xA+GHeWLovaZPYUDXkH/1iKRf2+M5+OrRgzC7O9b7w==",
"license": "MIT",
"dependencies": {
"pump": "^3.0.0",
"tar-stream": "^3.1.5"
},
"optionalDependencies": {
"bare-fs": "^4.0.1",
"bare-path": "^3.0.0"
}
},
"node_modules/tar-stream": {
"version": "3.1.7",
"resolved": "https://registry.npmjs.org/tar-stream/-/tar-stream-3.1.7.tgz",
"integrity": "sha512-qJj60CXt7IU1Ffyc3NJMjh6EkuCFej46zUqJ4J7pqYlThyd9bO0XBTmcOIhSzZJVWfsLks0+nle/j538YAW9RQ==",
"license": "MIT",
"dependencies": {
"b4a": "^1.6.4",
"fast-fifo": "^1.2.0",
"streamx": "^2.15.0"
}
},
"node_modules/text-decoder": {
"version": "1.2.3",
"resolved": "https://registry.npmjs.org/text-decoder/-/text-decoder-1.2.3.tgz",
"integrity": "sha512-3/o9z3X0X0fTupwsYvR03pJ/DjWuqqrfwBgTQzdWDiQSm9KitAyz/9WqsT2JQW7KV2m+bC2ol/zqpW37NHxLaA==",
"license": "Apache-2.0",
"dependencies": {
"b4a": "^1.6.4"
}
},
"node_modules/tunnel-agent": {
"version": "0.6.0",
"resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.6.0.tgz",
"integrity": "sha512-McnNiV1l8RYeY8tBgEpuodCC1mLUdbSN+CYBL7kJsJNInOP8UjDDEwdk6Mw60vdLLrr5NHKZhMAOSrR2NZuQ+w==",
"license": "Apache-2.0",
"dependencies": {
"safe-buffer": "^5.0.1"
},
"engines": {
"node": "*"
}
},
"node_modules/undici-types": {
"version": "7.8.0",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-7.8.0.tgz",
"integrity": "sha512-9UJ2xGDvQ43tYyVMpuHlsgApydB8ZKfVYTsLDhXkFL/6gfkp+U8xTGdh8pMJv1SpZna0zxG1DwsKZsreLbXBxw==",
"license": "MIT"
},
"node_modules/util-deprecate": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",
"integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==",
"license": "MIT"
},
"node_modules/wrappy": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
"integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==",
"license": "ISC"
}
}
}

12
doctests/package.json Normal file
View File

@@ -0,0 +1,12 @@
{
"name": "node-redis-doctests",
"version": "1.0.0",
"description": "Code examples for redis.io",
"main": "index.js",
"private": true,
"type": "module",
"dependencies": {
"redis": "file:/packages/redis",
"@xenova/transformers": "^2.17.2"
}
}

139
doctests/query-agg.js Normal file
View File

@@ -0,0 +1,139 @@
// EXAMPLE: query_agg
// HIDE_START
import assert from 'node:assert';
import fs from 'node:fs';
import { createClient } from 'redis';
import { SCHEMA_FIELD_TYPE, FT_AGGREGATE_STEPS, FT_AGGREGATE_GROUP_BY_REDUCERS } from '@redis/search';
const client = createClient();
await client.connect().catch(console.error);
// create index
await client.ft.create('idx:bicycle', {
'$.condition': {
type: SCHEMA_FIELD_TYPE.TAG,
AS: 'condition'
},
'$.price': {
type: SCHEMA_FIELD_TYPE.NUMERIC,
AS: 'price'
}
}, {
ON: 'JSON',
PREFIX: 'bicycle:'
})
// load data
const bicycles = JSON.parse(fs.readFileSync('data/query_em.json', 'utf8'));
await Promise.all(
bicycles.map((bicycle, bid) => {
return client.json.set(`bicycle:${bid}`, '$', bicycle);
})
);
// HIDE_END
// STEP_START agg1
const res1 = await client.ft.aggregate('idx:bicycle', '@condition:{new}', {
LOAD: ['__key', 'price'],
APPLY: {
expression: '@price - (@price * 0.1)',
AS: 'discounted'
}
});
console.log(res1.results.length); // >>> 5
console.log(res1.results); // >>>
//[
// [Object: null prototype] { __key: 'bicycle:0', price: '270' },
// [Object: null prototype] { __key: 'bicycle:5', price: '810' },
// [Object: null prototype] { __key: 'bicycle:6', price: '2300' },
// [Object: null prototype] { __key: 'bicycle:7', price: '430' },
// [Object: null prototype] { __key: 'bicycle:8', price: '1200' }
//]
// REMOVE_START
assert.strictEqual(res1.results.length, 5);
// REMOVE_END
// STEP_END
// STEP_START agg2
const res2 = await client.ft.aggregate('idx:bicycle', '*', {
LOAD: ['@price'],
STEPS: [{
type: FT_AGGREGATE_STEPS.APPLY,
expression: '@price<1000',
AS: 'price_category'
},{
type: FT_AGGREGATE_STEPS.GROUPBY,
properties: '@condition',
REDUCE:[{
type: FT_AGGREGATE_GROUP_BY_REDUCERS.SUM,
property: '@price_category',
AS: 'num_affordable'
}]
}]
});
console.log(res2.results.length); // >>> 3
console.log(res2.results); // >>>
//[[Object: null prototype] { condition: 'refurbished', num_affordable: '1' },
// [Object: null prototype] { condition: 'used', num_affordable: '1' },
// [Object: null prototype] { condition: 'new', num_affordable: '3' }
//]
// REMOVE_START
assert.strictEqual(res2.results.length, 3);
// REMOVE_END
// STEP_END
// STEP_START agg3
const res3 = await client.ft.aggregate('idx:bicycle', '*', {
STEPS: [{
type: FT_AGGREGATE_STEPS.APPLY,
expression: "'bicycle'",
AS: 'type'
}, {
type: FT_AGGREGATE_STEPS.GROUPBY,
properties: '@type',
REDUCE: [{
type: FT_AGGREGATE_GROUP_BY_REDUCERS.COUNT,
property: null,
AS: 'num_total'
}]
}]
});
console.log(res3.results.length); // >>> 1
console.log(res3.results); // >>>
//[ [Object: null prototype] { type: 'bicycle', num_total: '10' } ]
// REMOVE_START
assert.strictEqual(res3.results.length, 1);
// REMOVE_END
// STEP_END
// STEP_START agg4
const res4 = await client.ft.aggregate('idx:bicycle', '*', {
LOAD: ['__key'],
STEPS: [{
type: FT_AGGREGATE_STEPS.GROUPBY,
properties: '@condition',
REDUCE: [{
type: FT_AGGREGATE_GROUP_BY_REDUCERS.TOLIST,
property: '__key',
AS: 'bicycles'
}]
}]
});
console.log(res4.results.length); // >>> 3
console.log(res4.results); // >>>
//[[Object: null prototype] {condition: 'refurbished', bicycles: [ 'bicycle:9' ]},
// [Object: null prototype] {condition: 'used', bicycles: [ 'bicycle:1', 'bicycle:2', 'bicycle:3', 'bicycle:4' ]},
// [Object: null prototype] {condition: 'new', bicycles: [ 'bicycle:5', 'bicycle:6', 'bicycle:7', 'bicycle:0', 'bicycle:8' ]}]
// REMOVE_START
assert.strictEqual(res4.results.length, 3);
// REMOVE_END
// STEP_END
// REMOVE_START
// destroy index and data
await client.ft.dropIndex('idx:bicycle', { DD: true });
await client.close();
// REMOVE_END

191
doctests/query-combined.js Normal file
View File

@@ -0,0 +1,191 @@
// EXAMPLE: query_combined
// HIDE_START
import assert from 'node:assert';
import fs from 'node:fs';
import { createClient } from 'redis';
import { SCHEMA_FIELD_TYPE, SCHEMA_VECTOR_FIELD_ALGORITHM } from '@redis/search';
import { pipeline } from '@xenova/transformers';
function float32Buffer(arr) {
const floatArray = new Float32Array(arr);
const float32Buffer = Buffer.from(floatArray.buffer);
return float32Buffer;
}
async function embedText(sentence) {
let modelName = 'Xenova/all-MiniLM-L6-v2';
let pipe = await pipeline('feature-extraction', modelName);
let vectorOutput = await pipe(sentence, {
pooling: 'mean',
normalize: true,
});
if (vectorOutput == null) {
throw new Error('vectorOutput is undefined');
}
const embedding = Object.values(vectorOutput.data);
return embedding;
}
let vector_query = float32Buffer(await embedText('That is a very happy person'));
const client = createClient();
await client.connect().catch(console.error);
// create index
await client.ft.create('idx:bicycle', {
'$.description': {
type: SCHEMA_FIELD_TYPE.TEXT,
AS: 'description'
},
'$.condition': {
type: SCHEMA_FIELD_TYPE.TAG,
AS: 'condition'
},
'$.price': {
type: SCHEMA_FIELD_TYPE.NUMERIC,
AS: 'price'
},
'$.description_embeddings': {
type: SCHEMA_FIELD_TYPE.VECTOR,
TYPE: 'FLOAT32',
ALGORITHM: SCHEMA_VECTOR_FIELD_ALGORITHM.FLAT,
DIM: 384,
DISTANCE_METRIC: 'COSINE',
AS: 'vector',
}
}, {
ON: 'JSON',
PREFIX: 'bicycle:'
});
// load data
const bicycles = JSON.parse(fs.readFileSync('data/query_vector.json', 'utf8'));
await Promise.all(
bicycles.map((bicycle, bid) => {
return client.json.set(`bicycle:${bid}`, '$', bicycle);
})
);
// HIDE_END
// STEP_START combined1
const res1 = await client.ft.search('idx:bicycle', '@price:[500 1000] @condition:{new}');
console.log(res1.total); // >>> 1
console.log(res1); // >>>
//{
// total: 1,
// documents: [ { id: 'bicycle:5', value: [Object: null prototype] } ]
//}
// REMOVE_START
assert.strictEqual(res1.total, 1);
// REMOVE_END
// STEP_END
// STEP_START combined2
const res2 = await client.ft.search('idx:bicycle', 'kids @price:[500 1000] @condition:{used}');
console.log(res2.total); // >>> 1
console.log(res2); // >>>
// {
// total: 1,
// documents: [ { id: 'bicycle:2', value: [Object: null prototype] } ]
// }
// REMOVE_START
assert.strictEqual(res2.total, 1);
// REMOVE_END
// STEP_END
// STEP_START combined3
const res3 = await client.ft.search('idx:bicycle', '(kids | small) @condition:{used}');
console.log(res3.total); // >>> 2
console.log(res3); // >>>
//{
// total: 2,
// documents: [
// { id: 'bicycle:2', value: [Object: null prototype] },
// { id: 'bicycle:1', value: [Object: null prototype] }
// ]
//}
// REMOVE_START
assert.strictEqual(res3.total, 2);
// REMOVE_END
// STEP_END
// STEP_START combined4
const res4 = await client.ft.search('idx:bicycle', '@description:(kids | small) @condition:{used}');
console.log(res4.total); // >>> 2
console.log(res4); // >>>
//{
// total: 2,
// documents: [
// { id: 'bicycle:2', value: [Object: null prototype] },
// { id: 'bicycle:1', value: [Object: null prototype] }
// ]
//}
// REMOVE_START
assert.strictEqual(res4.total, 2);
// REMOVE_END
// STEP_END
// STEP_START combined5
const res5 = await client.ft.search('idx:bicycle', '@description:(kids | small) @condition:{new | used}');
console.log(res5.total); // >>> 3
console.log(res5); // >>>
//{
// total: 3,
// documents: [
// { id: 'bicycle:1', value: [Object: null prototype] },
// { id: 'bicycle:0', value: [Object: null prototype] },
// { id: 'bicycle:2', value: [Object: null prototype] }
// ]
//}
// REMOVE_START
assert.strictEqual(res5.total, 3);
// REMOVE_END
// STEP_END
// STEP_START combined6
const res6 = await client.ft.search('idx:bicycle', '@price:[500 1000] -@condition:{new}');
console.log(res6.total); // >>> 2
console.log(res6); // >>>
//{
// total: 2,
// documents: [
// { id: 'bicycle:2', value: [Object: null prototype] },
// { id: 'bicycle:9', value: [Object: null prototype] }
// ]
//}
// REMOVE_START
assert.strictEqual(res6.total, 2);
// REMOVE_END
// STEP_END
// STEP_START combined7
const res7 = await client.ft.search('idx:bicycle',
'(@price:[500 1000] -@condition:{new})=>[KNN 3 @vector $query_vector]', {
PARAMS: { query_vector: vector_query },
DIALECT: 2
}
);
console.log(res7.total); // >>> 2
console.log(res7); // >>>
//{
// total: 2,
// documents: [
// { id: 'bicycle:2', value: [Object: null prototype] },
// { id: 'bicycle:9', value: [Object: null prototype] }
// ]
//}
// REMOVE_START
assert.strictEqual(res7.total, 2);
// REMOVE_END
// STEP_END
// REMOVE_START
// destroy index and data
await client.ft.dropIndex('idx:bicycle', { DD: true });
await client.close();
// REMOVE_END

121
doctests/query-em.js Normal file
View File

@@ -0,0 +1,121 @@
// EXAMPLE: query_em
// HIDE_START
import assert from 'node:assert';
import fs from 'node:fs';
import { createClient, SCHEMA_FIELD_TYPE } from 'redis';
const client = createClient();
await client.connect().catch(console.error);
// create index
await client.ft.create('idx:bicycle', {
'$.description': {
type: SCHEMA_FIELD_TYPE.TEXT,
AS: 'description'
},
'$.price': {
type: SCHEMA_FIELD_TYPE.NUMERIC,
AS: 'price'
},
'$.condition': {
type: SCHEMA_FIELD_TYPE.TAG,
AS: 'condition'
}
}, {
ON: 'JSON',
PREFIX: 'bicycle:'
})
// load data
const bicycles = JSON.parse(fs.readFileSync('data/query_em.json', 'utf8'));
await Promise.all(
bicycles.map((bicycle, bid) => {
return client.json.set(`bicycle:${bid}`, '$', bicycle);
})
);
// HIDE_END
// STEP_START em1
const res1 = await client.ft.search('idx:bicycle', '@price:[270 270]');
console.log(res1.total); // >>> 1
// REMOVE_START
assert.strictEqual(res1.total, 1);
// REMOVE_END
try {
const res2 = await client.ft.search('idx:bicycle', '@price:[270]');
console.log(res2.total); // >>> 1
assert.strictEqual(res2.total, 1);
} catch (err) {
console.log("'@price:[270]' syntax not yet supported.");
}
try {
const res3 = await client.ft.search('idx:bicycle', '@price==270');
console.log(res3.total); // >>> 1
assert.strictEqual(res3.total, 1);
} catch (err) {
console.log("'@price==270' syntax not yet supported.");
}
// FILTER is not supported
// const res4 = await client.ft.search('idx:bicycle', '*', {
// FILTER: {
// field: 'price',
// min: 270,
// max: 270,
// }
// });
// console.log(res4.total); // >>> 1
// REMOVE_START
// assert.strictEqual(res4.total, 10);
// REMOVE_END
// STEP_END
// STEP_START em2
const res5 = await client.ft.search('idx:bicycle', '@condition:{new}');
console.log(res5.total); // >>> 5
// REMOVE_START
assert.strictEqual(res5.total, 5);
// REMOVE_END
// STEP_END
// STEP_START em3
await client.ft.create('idx:email', {
'$.email': {
type: SCHEMA_FIELD_TYPE.TAG,
AS: 'email'
}
}, {
ON: 'JSON',
PREFIX: 'key:'
})
await client.json.set('key:1', '$', { email: 'test@redis.com' });
try {
const res6 = await client.ft.search('idx:email', 'test@redis.com', { DIALECT: 2 });
console.log(res6);
} catch (err) {
console.log("'test@redis.com' syntax not yet supported.");
}
// REMOVE_START
await client.ft.dropIndex('idx:email', { DD: true });
// REMOVE_END
// STEP_END
// STEP_START em4
const res7 = await client.ft.search('idx:bicycle', '@description:"rough terrain"');
console.log(res7.total); // >>> 1 (Result{1 total, docs: [Document {'id': 'bicycle:8'...)
// REMOVE_START
assert.strictEqual(res7.total, 1);
// REMOVE_END
// STEP_END
// REMOVE_START
// destroy index and data
await client.ft.dropIndex('idx:bicycle', { DD: true });
await client.close();
// REMOVE_END

84
doctests/query-ft.js Normal file
View File

@@ -0,0 +1,84 @@
// EXAMPLE: query_ft
// HIDE_START
import assert from 'node:assert';
import fs from 'node:fs';
import { createClient, SCHEMA_FIELD_TYPE } from 'redis';
const client = createClient();
await client.connect().catch(console.error);
// create index
await client.ft.create('idx:bicycle', {
'$.model': {
type: SCHEMA_FIELD_TYPE.TEXT,
AS: 'model'
},
'$.brand': {
type: SCHEMA_FIELD_TYPE.TEXT,
AS: 'brand'
},
'$.description': {
type: SCHEMA_FIELD_TYPE.TEXT,
AS: 'description'
}
}, {
ON: 'JSON',
PREFIX: 'bicycle:'
})
// load data
const bicycles = JSON.parse(fs.readFileSync('data/query_em.json', 'utf8'));
await Promise.all(
bicycles.map((bicycle, bid) => {
return client.json.set(`bicycle:${bid}`, '$', bicycle);
})
);
// HIDE_END
// STEP_START ft1
const res1 = await client.ft.search('idx:bicycle', '@description: kids');
console.log(res1.total); // >>> 2
// REMOVE_START
assert.strictEqual(res1.total, 2);
// REMOVE_END
// STEP_END
// STEP_START ft2
const res2 = await client.ft.search('idx:bicycle', '@model: ka*');
console.log(res2.total); // >>> 1
// REMOVE_START
assert.strictEqual(res2.total, 1);
// REMOVE_END
// STEP_END
// STEP_START ft3
const res3 = await client.ft.search('idx:bicycle', '@brand: *bikes');
console.log(res3.total); // >>> 2
// REMOVE_START
assert.strictEqual(res3.total, 2);
// REMOVE_END
// STEP_END
// STEP_START ft4
const res4 = await client.ft.search('idx:bicycle', '%optamized%');
console.log(res4); // >>> { total: 1, documents: [ { id: 'bicycle:3', value: [Object: null prototype] } ]}
// REMOVE_START
assert.strictEqual(res4.total, 1);
// REMOVE_END
// STEP_END
// STEP_START ft5
const res5 = await client.ft.search('idx:bicycle', '%%optamised%%');
console.log(res5); // >>> { total: 1, documents: [ { id: 'bicycle:3', value: [Object: null prototype] } ]}
// REMOVE_START
assert.strictEqual(res5.total, 1);
// REMOVE_END
// STEP_END
// REMOVE_START
// destroy index and data
await client.ft.dropIndex('idx:bicycle', { DD: true });
await client.close();
// REMOVE_END

82
doctests/query-geo.js Normal file
View File

@@ -0,0 +1,82 @@
// EXAMPLE: query_geo
// HIDE_START
import assert from 'node:assert';
import fs from 'node:fs';
import { createClient } from 'redis';
import { SCHEMA_FIELD_TYPE } from '@redis/search';
const client = createClient();
await client.connect().catch(console.error);
// create index
await client.ft.create('idx:bicycle', {
'$.store_location': {
type: SCHEMA_FIELD_TYPE.GEO,
AS: 'store_location'
},
'$.pickup_zone': {
type: SCHEMA_FIELD_TYPE.GEOSHAPE,
AS: 'pickup_zone'
}
}, {
ON: 'JSON',
PREFIX: 'bicycle:'
})
// load data
const bicycles = JSON.parse(fs.readFileSync('data/query_em.json', 'utf8'));
await Promise.all(
bicycles.map((bicycle, bid) => {
return client.json.set(`bicycle:${bid}`, '$', bicycle);
})
);
// HIDE_END
// STEP_START geo1
const res1= await client.ft.search('idx:bicycle', '@store_location:[-0.1778 51.5524 20 mi]');
console.log(res1.total); // >>> 1
console.log(res1); // >>> {total: 1, documents: [ { id: 'bicycle:5', value: [Object: null prototype] } ]}
// REMOVE_START
assert.strictEqual(res1.total, 1);
// REMOVE_END
// STEP_END
// STEP_START geo2
const params_dict_geo2 = { bike: 'POINT(-0.1278 51.5074)' };
const q_geo2 = '@pickup_zone:[CONTAINS $bike]';
const res2 = await client.ft.search('idx:bicycle', q_geo2, { PARAMS: params_dict_geo2, DIALECT: 3 });
console.log(res2.total); // >>> 1
console.log(res2); // >>> {total: 1, documents: [ { id: 'bicycle:5', value: [Object: null prototype] } ]}
// REMOVE_START
assert.strictEqual(res2.total, 1);
// REMOVE_END
// STEP_END
// STEP_START geo3
const params_dict_geo3 = { europe: 'POLYGON((-25 35, 40 35, 40 70, -25 70, -25 35))' };
const q_geo3 = '@pickup_zone:[WITHIN $europe]';
const res3 = await client.ft.search('idx:bicycle', q_geo3, { PARAMS: params_dict_geo3, DIALECT: 3 });
console.log(res3.total); // >>> 5
console.log(res3); // >>>
// {
// total: 5,
// documents: [
// { id: 'bicycle:5', value: [Object: null prototype] },
// { id: 'bicycle:6', value: [Object: null prototype] },
// { id: 'bicycle:7', value: [Object: null prototype] },
// { id: 'bicycle:8', value: [Object: null prototype] },
// { id: 'bicycle:9', value: [Object: null prototype] }
// ]
// }
// REMOVE_START
assert.strictEqual(res3.total, 5);
// REMOVE_END
// STEP_END
// REMOVE_START
// destroy index and data
await client.ft.dropIndex('idx:bicycle', { DD: true });
await client.close();
// REMOVE_END

98
doctests/query-range.js Normal file
View File

@@ -0,0 +1,98 @@
// EXAMPLE: query_range
// HIDE_START
import assert from 'node:assert';
import fs from 'node:fs';
import { createClient, SCHEMA_FIELD_TYPE,} from 'redis';
const client = createClient();
await client.connect().catch(console.error);
// create index
await client.ft.create('idx:bicycle', {
'$.description': {
type: SCHEMA_FIELD_TYPE.TEXT,
AS: 'description'
},
'$.price': {
type: SCHEMA_FIELD_TYPE.NUMERIC,
AS: 'price'
},
'$.condition': {
type: SCHEMA_FIELD_TYPE.TAG,
AS: 'condition'
}
}, {
ON: 'JSON',
PREFIX: 'bicycle:'
})
// load data
const bicycles = JSON.parse(fs.readFileSync('data/query_em.json', 'utf8'));
await Promise.all(
bicycles.map((bicycle, bid) => {
return client.json.set(`bicycle:${bid}`, '$', bicycle);
})
);
// HIDE_END
// STEP_START range1
const res1 = await client.ft.search('idx:bicycle', '@price:[500 1000]');
console.log(res1.total); // >>> 3
// REMOVE_START
assert.strictEqual(res1.total, 3);
// REMOVE_END
// STEP_END
// STEP_START range2
// FILTER is not supported
// const res2 = await client.ft.search('idx:bicycle', '*', {
// FILTER: {
// field: 'price',
// min: 500,
// max: 1000,
// }
// });
// console.log(res2.total); // >>> 3
// REMOVE_START
// assert.strictEqual(res2.total, 3);
// REMOVE_END
// STEP_END
// STEP_START range3
// FILTER is not supported
// const res3 = await client.ft.search('idx:bicycle', '*', {
// FILTER: {
// field: 'price',
// min: '(1000',
// max: '+inf,
// }
// });
// console.log(res3.total); // >>> 5
// REMOVE_START
// assert.strictEqual(res3.total, 5);
// REMOVE_END
// STEP_END
// STEP_START range4
const res4 = await client.ft.search(
'idx:bicycle',
'@price:[-inf 2000]',
{
SORTBY: 'price',
LIMIT: { from: 0, size: 5 }
}
);
console.log(res4.total); // >>> 7
console.log(res4); // >>> { total: 7, documents: [ { id: 'bicycle:0', value: [Object: null prototype] }, { id: 'bicycle:7', value: [Object: null prototype] }, { id: 'bicycle:5', value: [Object: null prototype] }, { id: 'bicycle:2', value: [Object: null prototype] }, { id: 'bicycle:9', value: [Object: null prototype] } ] }
// REMOVE_START
assert.strictEqual(res4.total, 7);
// REMOVE_END
// STEP_END
// REMOVE_START
// destroy index and data
await client.ft.dropIndex('idx:bicycle', { DD: true });
await client.close();
// REMOVE_END

110
doctests/query-vector.js Normal file
View File

@@ -0,0 +1,110 @@
// EXAMPLE: query_vector
// HIDE_START
import assert from 'node:assert';
import fs from 'node:fs';
import { createClient } from 'redis';
import { SCHEMA_FIELD_TYPE, SCHEMA_VECTOR_FIELD_ALGORITHM } from '@redis/search';
import { pipeline } from '@xenova/transformers';
function float32Buffer(arr) {
const floatArray = new Float32Array(arr);
const float32Buffer = Buffer.from(floatArray.buffer);
return float32Buffer;
}
async function embedText(sentence) {
let modelName = 'Xenova/all-MiniLM-L6-v2';
let pipe = await pipeline('feature-extraction', modelName);
let vectorOutput = await pipe(sentence, {
pooling: 'mean',
normalize: true,
});
const embedding = Object.values(vectorOutput?.data);
return embedding;
}
const vector_query = float32Buffer(await embedText('That is a very happy person'));
const client = createClient();
await client.connect().catch(console.error);
// create index
await client.ft.create('idx:bicycle', {
'$.description': {
type: SCHEMA_FIELD_TYPE.TEXT,
AS: 'description'
},
'$.description_embeddings': {
type: SCHEMA_FIELD_TYPE.VECTOR,
TYPE: 'FLOAT32',
ALGORITHM: SCHEMA_VECTOR_FIELD_ALGORITHM.FLAT,
DIM: 384,
DISTANCE_METRIC: 'COSINE',
AS: 'vector'
}
}, {
ON: 'JSON',
PREFIX: 'bicycle:'
});
// load data
const bicycles = JSON.parse(fs.readFileSync('data/query_vector.json', 'utf8'));
await Promise.all(
bicycles.map((bicycle, bid) => {
return client.json.set(`bicycle:${bid}`, '$', bicycle);
})
);
// HIDE_END
// STEP_START vector1
const res1 = await client.ft.search('idx:bicycle',
'*=>[KNN 3 @vector $query_vector AS score]', {
PARAMS: { query_vector: vector_query },
RETURN: ['description'],
DIALECT: 2
}
);
console.log(res1.total); // >>> 3
console.log(res1); // >>>
//{
// total: 3,
// documents: [
// { id: 'bicycle:0', value: [Object: null prototype] {} },
// { id: 'bicycle:2', value: [Object: null prototype] {} },
// { id: 'bicycle:9', value: [Object: null prototype] {} }
// ]
//}
// REMOVE_START
assert.strictEqual(res1.total, 3);
// REMOVE_END
// STEP_END
// STEP_START vector2
const res2 = await client.ft.search('idx:bicycle',
'@vector:[VECTOR_RANGE 0.9 $query_vector]=>{$YIELD_DISTANCE_AS: vector_dist}', {
PARAMS: { query_vector: vector_query },
SORTBY: 'vector_dist',
RETURN: ['vector_dist', 'description'],
DIALECT: 2
}
);
console.log(res2.total); // >>> 1
console.log(res2); // >>>
//{
// total: 1,
// documents: [ { id: 'bicycle:0', value: [Object: null prototype] } ]
//}
// REMOVE_START
assert.strictEqual(res2.total, 1);
// REMOVE_END
// STEP_END
// REMOVE_START
// destroy index and data
await client.ft.dropIndex('idx:bicycle', { DD: true });
await client.close();
// REMOVE_END

15
doctests/run_examples.sh Executable file
View File

@@ -0,0 +1,15 @@
#!/bin/sh
basepath=`readlink -f $1`
if [ $? -ne 0 ]; then
basepath=`readlink -f $(dirname $0)`
fi
echo "No path specified, using ${basepath}"
set -e
cd ${basepath}
for i in `ls ${basepath}/*.js`; do
redis-cli flushdb
node $i
done

View File

@@ -0,0 +1,231 @@
// EXAMPLE: search_quickstart
// REMOVE_START
import assert from 'assert';
// REMOVE_END
// HIDE_START
import { createClient, SCHEMA_FIELD_TYPE } from 'redis';
// HIDE_END
// STEP_START connect
const client = createClient();
client.on('error', err => console.log('Redis Client Error', err));
await client.connect();
// STEP_END
// STEP_START data_sample
const bicycle1 = {
brand: 'Velorim',
model: 'Jigger',
price: 270,
description:
'Small and powerful, the Jigger is the best ' +
'ride for the smallest of tikes! This is the tiniest kids\u2019 ' +
'pedal bike on the market available without a coaster brake, the ' +
'Jigger is the vehicle of choice for the rare tenacious little' +
'rider raring to go.',
condition: 'new'
};
// STEP_END
const bicycles = [
bicycle1,
{
brand: 'Bicyk',
model: 'Hillcraft',
price: 1200,
description: 'Kids want to ride with as little weight as possible. Especially on an incline! They may be at the age when a 27.5\" wheel bike is just too clumsy coming off a 24\" bike. The Hillcraft 26 is just the solution they need!',
condition: 'used'
},
{
brand: 'Nord',
model: 'Chook air 5',
price: 815,
description: 'The Chook Air 5 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. The lower top tube makes it easy to mount and dismount in any situation, giving your kids greater safety on the trails.',
condition: 'used'
},
{
brand: 'Eva',
model: 'Eva 291',
price: 3400,
description: 'The sister company to Nord, Eva launched in 2005 as the first and only women-dedicated bicycle brand. Designed by women for women, allEva bikes are optimized for the feminine physique using analytics from a body metrics database. If you like 29ers, try the Eva 291. It\u2019s a brand new bike for 2022.. This full-suspension, cross-country ride has been designed for velocity. The 291 has 100mm of front and rear travel, a superlight aluminum frame and fast-rolling 29-inch wheels. Yippee!',
condition: 'used'
},
{
brand: 'Noka Bikes',
model: 'Kahuna',
price: 3200,
description: 'Whether you want to try your hand at XC racing or are looking for a lively trail bike that\'s just as inspiring on the climbs as it is over rougher ground, the Wilder is one heck of a bike built specifically for short women. Both the frames and components have been tweaked to include a women\u2019s saddle, different bars and unique colourway.',
condition: 'used'
},
{
brand: 'Breakout',
model: 'XBN 2.1 Alloy',
price: 810,
description: 'The XBN 2.1 Alloy is our entry-level road bike \u2013 but that\u2019s not to say that it\u2019s a basic machine. With an internal weld aluminium frame, a full carbon fork, and the slick-shifting Claris gears from Shimano\u2019s, this is a bike which doesn\u2019t break the bank and delivers craved performance.',
condition: 'new'
},
{
brand: 'ScramBikes',
model: 'WattBike',
price: 2300,
description: 'The WattBike is the best e-bike for people who still feel young at heart. It has a Bafang 1000W mid-drive system and a 48V 17.5AH Samsung Lithium-Ion battery, allowing you to ride for more than 60 miles on one charge. It\u2019s great for tackling hilly terrain or if you just fancy a more leisurely ride. With three working modes, you can choose between E-bike, assisted bicycle, and normal bike modes.',
condition: 'new'
},
{
brand: 'Peaknetic',
model: 'Secto',
price: 430,
description: 'If you struggle with stiff fingers or a kinked neck or back after a few minutes on the road, this lightweight, aluminum bike alleviates those issues and allows you to enjoy the ride. From the ergonomic grips to the lumbar-supporting seat position, the Roll Low-Entry offers incredible comfort. The rear-inclined seat tube facilitates stability by allowing you to put a foot on the ground to balance at a stop, and the low step-over frame makes it accessible for all ability and mobility levels. The saddle is very soft, with a wide back to support your hip joints and a cutout in the center to redistribute that pressure. Rim brakes deliver satisfactory braking control, and the wide tires provide a smooth, stable ride on paved roads and gravel. Rack and fender mounts facilitate setting up the Roll Low-Entry as your preferred commuter, and the BMX-like handlebar offers space for mounting a flashlight, bell, or phone holder.',
condition: 'new'
},
{
brand: 'nHill',
model: 'Summit',
price: 1200,
description: 'This budget mountain bike from nHill performs well both on bike paths and on the trail. The fork with 100mm of travel absorbs rough terrain. Fat Kenda Booster tires give you grip in corners and on wet trails. The Shimano Tourney drivetrain offered enough gears for finding a comfortable pace to ride uphill, and the Tektro hydraulic disc brakes break smoothly. Whether you want an affordable bike that you can take to work, but also take trail in mountains on the weekends or you\u2019re just after a stable, comfortable ride for the bike path, the Summit gives a good value for money.',
condition: 'new'
},
{
model: 'ThrillCycle',
brand: 'BikeShind',
price: 815,
description: 'An artsy, retro-inspired bicycle that\u2019s as functional as it is pretty: The ThrillCycle steel frame offers a smooth ride. A 9-speed drivetrain has enough gears for coasting in the city, but we wouldn\u2019t suggest taking it to the mountains. Fenders protect you from mud, and a rear basket lets you transport groceries, flowers and books. The ThrillCycle comes with a limited lifetime warranty, so this little guy will last you long past graduation.',
condition: 'refurbished'
}
];
// STEP_START create_index
const schema = {
'$.brand': {
type: SCHEMA_FIELD_TYPE.TEXT,
SORTABLE: true,
AS: 'brand'
},
'$.model': {
type: SCHEMA_FIELD_TYPE.TEXT,
AS: 'model'
},
'$.description': {
type: SCHEMA_FIELD_TYPE.TEXT,
AS: 'description'
},
'$.price': {
type: SCHEMA_FIELD_TYPE.NUMERIC,
AS: 'price'
},
'$.condition': {
type: SCHEMA_FIELD_TYPE.TAG,
AS: 'condition'
}
};
try {
await client.ft.create('idx:bicycle', schema, {
ON: 'JSON',
PREFIX: 'bicycle:'
});
} catch (e) {
if (e.message === 'Index already exists') {
console.log('Index exists already, skipped creation.');
} else {
// Something went wrong, perhaps RediSearch isn't installed...
console.error(e);
process.exit(1);
}
}
// STEP_END
// STEP_START add_documents
await Promise.all(
bicycles.map((bicycle, i) => client.json.set(`bicycle:${i}`, '$', bicycle))
);
// STEP_END
// STEP_START wildcard_query
let result = await client.ft.search('idx:bicycle', '*', {
LIMIT: {
from: 0,
size: 10
}
});
console.log(JSON.stringify(result, null, 2));
/*
{
"total": 10,
"documents": ...
}
*/
// STEP_END
// REMOVE_START
assert.equal(result.documents[0].id, 'bicycle:0');
// REMOVE_END
// STEP_START query_single_term
result = await client.ft.search(
'idx:bicycle',
'@model:Jigger',
{
LIMIT: {
from: 0,
size: 10
}
});
console.log(JSON.stringify(result, null, 2));
/*
{
"total": 1,
"documents": [{
"id": "bicycle:0",
"value": {
"brand": "Velorim",
"model": "Jigger",
"price": 270,
"description": "Small and powerful, the Jigger is the best ride for the smallest of tikes! This is the tiniest kids pedal bike on the market available without a coaster brake, the Jigger is the vehicle of choice for the rare tenacious little rider raring to go.",
"condition": "new"
}
}]
}
*/
// STEP_END
// REMOVE_START
assert.equal(result.documents[0].id, 'bicycle:0');
// REMOVE_END
// STEP_START query_exact_matching
result = await client.ft.search(
'idx:bicycle',
'@brand:"Noka Bikes"',
{
LIMIT: {
from: 0,
size: 10
}
}
);
console.log(JSON.stringify(result, null, 2));
/*
{
"total": 1,
"documents": [{
"id": "bicycle:4",
"value": {
"brand": "Noka Bikes",
"model": "Kahuna",
"price": 3200,
"description": "Whether you want to try your hand at XC racing or are looking for a lively trail bike that's just as inspiring on the climbs as it is over rougher ground, the Wilder is one heck of a bike built specifically for short women. Both the frames and components have been tweaked to include a womens saddle, different bars and unique colourway.",
"condition": "used"
}
}]
}
*/
// STEP_END
// REMOVE_START
assert.equal(result.documents[0].id, 'bicycle:4');
// REMOVE END
await client.close();

View File

@@ -0,0 +1,27 @@
// EXAMPLE: set_and_get
// REMOVE_START
import assert from "node: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().catch(console.error);
// 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');
await client.del('bike:1');
//REMOVE_END
// HIDE_START
await client.close();
// HIDE_END

View File

@@ -87,6 +87,7 @@ export default {
if (options?.TYPE) {
parser.push('TYPE', options.TYPE);
}
console.log('eeeeeeeeee', parser.redisArgs)
},
/**
* Transforms the SCAN reply into a structured object
@@ -95,6 +96,7 @@ export default {
* @returns Object with cursor and keys properties
*/
transformReply([cursor, keys]: [BlobStringReply, ArrayReply<BlobStringReply>]) {
console.log(cursor, keys)
return {
cursor,
keys