1
0
mirror of https://github.com/redis/node-redis.git synced 2025-07-31 05:44:24 +03:00
Files
node-redis/doctests/cmds-sorted-set.js
Pavel Pashov 0541b32f34 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>
2025-07-23 18:16:08 +03:00

116 lines
3.0 KiB
JavaScript

// 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