Skip to content

Commit

Permalink
fix(set): support optional "GET" parameter (#1264)
Browse files Browse the repository at this point in the history
  • Loading branch information
the5thbeatle committed Mar 28, 2023
1 parent fa27101 commit f8b6671
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/commands/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export function set(key, value, ...options) {
if (nx && this.data.has(key)) return null
if (xx && !this.data.has(key)) return null

let result = 'OK'
if (options.indexOf('GET') !== -1) {
result = this.data.has(key) ? this.data.get(key) : null
}

this.data.set(key, value)

const expireOptions = new Map(createGroupedArray(filteredOptions, 2))
Expand All @@ -31,7 +36,7 @@ export function set(key, value, ...options) {
this.expires.delete(key)
}

return 'OK'
return result
}

export function setBuffer(...args) {
Expand Down
18 changes: 18 additions & 0 deletions test/integration/commands/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,23 @@ runTwinSuite('set', (command, equals) => {
expect(await redis[command]('foo', 1, 'NX')).toBe(null)
redis.disconnect()
})

it('should return null if GET is specified and the key does not exist', async () => {
const redis = new Redis()

expect(await redis[command]('foo', 1, 'GET')).toBe(null)
redis.disconnect()
})

it('should return previous value if GET is specified and the key already exists', async () => {
const redis = new Redis({
data: {
foo: 'bar',
},
})

expect(equals(await redis[command]('foo', 1, 'GET'), 'bar')).toBe(true)
redis.disconnect()
})
})
})

0 comments on commit f8b6671

Please sign in to comment.