You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-07-31 05:44:24 +03:00
* 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>
177 lines
5.3 KiB
JavaScript
177 lines
5.3 KiB
JavaScript
// 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
|