-
Notifications
You must be signed in to change notification settings - Fork 1.9k
DOC-2553 - Adds code examples for the set tutorial #2582
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: emb-examples
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,176 @@ | ||||||
// EXAMPLE: sets_tutorial | ||||||
// HIDE_START | ||||||
import assert from 'assert'; | ||||||
import { createClient } from 'redis'; | ||||||
|
||||||
const client = await createClient(); | ||||||
await client.connect(); | ||||||
// HIDE_END | ||||||
// REMOVE_START | ||||||
await client.del('bikes:racing:france') | ||||||
await client.del('bikes:racing:usa') | ||||||
// REMOVE_END | ||||||
|
||||||
// STEP_START sAdd | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately, case matters for step names.
Suggested change
|
||||||
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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// HIDE_START | ||||||
await client.del('bikes:racing:france') | ||||||
await client.del('bikes:racing:usa') | ||||||
await client.sAdd('bikes:racing:france', 'bike:1', 'bike:2', 'bike:3') | ||||||
await client.sAdd('bikes:racing:usa', 'bike:1', 'bike:4') | ||||||
// HIDE_END | ||||||
const res5 = await client.sIsMember('bikes:racing:usa', 'bike:1') | ||||||
console.log(res5) // >>> true | ||||||
|
||||||
const res6 = await client.sIsMember('bikes:racing:usa', 'bike:2') | ||||||
console.log(res6) // >>> false | ||||||
Comment on lines
+40
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. returns 1/0 not true/false (at least in v5) |
||||||
// STEP_END | ||||||
|
||||||
// REMOVE_START | ||||||
assert.equal(res5, true) | ||||||
assert.equal(res6, false) | ||||||
// REMOVE_END | ||||||
|
||||||
// STEP_START sinster | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// 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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// 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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
const res11 = await client.sIsMember('bikes:racing:france', 'bike:1') | ||||||
console.log(res11) // >>> true | ||||||
|
||||||
const res12 = await client.smIsMember('bikes:racing:france', ['bike:2', 'bike:3', 'bike:4']) | ||||||
console.log(res12) // >>> [true, true, false] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in v5, we now return 1/1/0 not true/true/false |
||||||
// STEP_END | ||||||
|
||||||
// REMOVE_START | ||||||
assert.equal(res11, true) | ||||||
assert.deepEqual(res12, [true, true, false]) | ||||||
// REMOVE_END | ||||||
|
||||||
// STEP_START sDiff | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3']) | ||||||
await client.sAdd('bikes:racing:usa', ['bike:1', 'bike:4']) | ||||||
const res13 = await client.sDiff(['bikes:racing:france', 'bikes:racing:usa']) | ||||||
console.log(res13) // >>> [ 'bike:2', 'bike:3' ] | ||||||
// STEP_END | ||||||
|
||||||
// REMOVE_START | ||||||
assert.deepEqual(res13, ['bike:2', 'bike:3']) | ||||||
await client.del('bikes:racing:france') | ||||||
await client.del('bikes:racing:usa') | ||||||
// REMOVE_END | ||||||
|
||||||
// STEP_START multisets | ||||||
await client.sAdd('bikes:racing:france', ['bike:1', 'bike:2', 'bike:3']) | ||||||
await client.sAdd('bikes:racing:usa', ['bike:1', 'bike:4']) | ||||||
await client.sAdd('bikes:racing:italy', ['bike:1', 'bike:2', 'bike:3', 'bike:4']) | ||||||
|
||||||
const res14 = await client.sInter( | ||||||
['bikes:racing:france', 'bikes:racing:usa', 'bikes:racing:italy'] | ||||||
) | ||||||
console.log(res14) // >>> ['bike:1'] | ||||||
|
||||||
const res15 = await client.sUnion( | ||||||
['bikes:racing:france', 'bikes:racing:usa', 'bikes:racing:italy'] | ||||||
) | ||||||
console.log(res15) // >>> ['bike:1', 'bike:2', 'bike:3', 'bike:4'] | ||||||
|
||||||
const res16 = await client.sDiff(['bikes:racing:france', 'bikes:racing:usa', 'bikes:racing:italy']) | ||||||
console.log(res16) // >>> [] | ||||||
|
||||||
const res17 = await client.sDiff(['bikes:racing:usa', 'bikes:racing:france']) | ||||||
console.log(res17) // >>> ['bike:4'] | ||||||
|
||||||
const res18 = await client.sDiff(['bikes:racing:france', 'bikes:racing:usa']) | ||||||
console.log(res18) // >>> ['bike:2', 'bike:3'] | ||||||
// STEP_END | ||||||
|
||||||
// REMOVE_START | ||||||
assert.deepEqual(res14, ['bike:1']) | ||||||
assert.deepEqual(res15.sort(), ['bike:1', 'bike:2', 'bike:3', 'bike:4']) | ||||||
assert.deepEqual(res16, []) | ||||||
assert.deepEqual(res17, ['bike:4']) | ||||||
assert.deepEqual(res18, ['bike:2', 'bike:3']) | ||||||
await client.del('bikes:racing:france') | ||||||
await client.del('bikes:racing:usa') | ||||||
await client.del('bikes:racing:italy') | ||||||
// REMOVE_END | ||||||
|
||||||
// STEP_START sRem | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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 | ||||||
|
||||||
const res21 = await client.sMembers('bikes:racing:france') | ||||||
console.log(res21) // >>> ['bike:2', 'bike:4', 'bike:5'] | ||||||
|
||||||
const res22 = await client.sRandMember('bikes:racing:france') | ||||||
console.log(res22) // >>> bike:4 | ||||||
// STEP_END | ||||||
|
||||||
// REMOVE_START | ||||||
assert.equal(res19, 1) | ||||||
client.quit() | ||||||
// none of the other results are deterministic | ||||||
// REMOVE_END |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for consistency user flushdb