Skip to content

Commit

Permalink
Get returns records as null when they don't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed May 21, 2012
1 parent 431f035 commit e9af1b5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
22 changes: 13 additions & 9 deletions lib/Records.coffee
Expand Up @@ -299,10 +299,10 @@ module.exports = class Records extends Schema
`get(records, [options], callback)`
-----------------------------------
Retrieve one or multiple records. If options is an array, it is considered
to be the list of properties to retrieve. By default, unless the `force`
option is defined, only the properties not yet defined in the provided
records are fetch from Redis.
Retrieve one or multiple records. Records that doesn't exists are returned as null. If
options is an array, it is considered to be the list of properties to retrieve. By default,
unless the `force` option is defined, only the properties not yet defined in the provided
records are fetched from Redis.
`options` All options are optional. Options properties include:
Expand Down Expand Up @@ -337,21 +337,25 @@ module.exports = class Records extends Schema
records.forEach (record, i) ->
# An error would have been thrown by id if record was null and accept_null wasn't provided
return unless record?
if record[identifier] is null
recordId = record[identifier]
if recordId is null
records[i] = null
else if options.properties?.length
options.properties.forEach (property) ->
unless options.force or record[property]
recordId = record[identifier]
cmds.push ['hget', "#{db}:#{name}:#{recordId}", property, (err, value)->
cmds.push ['hget', "#{db}:#{name}:#{recordId}", property, (err, value) ->
record[property] = value
]
else
recordId = record[identifier]
cmds.push ['hgetall', "#{db}:#{name}:#{recordId}", (err, values)->
cmds.push ['hgetall', "#{db}:#{name}:#{recordId}", (err, values) ->
for property, value of values
record[property] = value
]
# Check if the record really exists
cmds.push ['exists', "#{db}:#{name}:#{recordId}", (err, exists) ->
records[i] = null unless exists
]
# No need to go further
if cmds.length is 0
return callback null, if isArray then records else records[0]
multi = redis.multi cmds
Expand Down
10 changes: 8 additions & 2 deletions test/get.coffee
Expand Up @@ -24,7 +24,7 @@ describe 'get', ->
after (next) ->
client.quit next

it 'Test get # identifier', (next) ->
it 'should use a provided identifier', (next) ->
Users.create
username: 'my_username'
email: 'my@email.com'
Expand All @@ -42,6 +42,12 @@ describe 'get', ->
user.user_id.should.eql userId
Users.clear next

it 'should faild with a missing identifier', (next) ->
Users.get -1, (err, user) ->
should.not.exist err
should.not.exist user
Users.clear next

it 'Test get # unique property', (next) ->
Users.create
username: 'my_username'
Expand All @@ -59,7 +65,7 @@ describe 'get', ->
should.not.exist user
Users.clear next

it 'Test get # properties', (next) ->
it 'should only return the provided properties', (next) ->
Users.create
username: 'my_username'
email: 'my@email.com'
Expand Down

0 comments on commit e9af1b5

Please sign in to comment.