Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add `object` option to get

  • Loading branch information...
commit 92b4adffcf230597a1f3d8a991945fac8486c619 1 parent 4947bca
@wdavidw authored
Showing with 26 additions and 5 deletions.
  1. +8 −1 lib/Records.coffee
  2. +18 −4 test/get.coffee
View
9 lib/Records.coffee
@@ -316,6 +316,7 @@ module.exports = class Records extends Schema
* `properties` Array of properties to fetch, all properties if not defined.
* `force` Force the retrieval of properties even if already present in the record objects.
* `accept_null` Skip objects if they are provided as null.
+ * `object` If `true`, return an object where keys are the identifier and value are the fetched records
`callback` Called on success or failure. Received parameters are:
@@ -364,7 +365,13 @@ module.exports = class Records extends Schema
multi.exec (err, values) =>
return callback err if err
@unserialize records
- callback null, if isArray then records else records[0]
+ if options.object
+ recordsByIds = {}
+ for record in records
+ recordsByIds[record[identifier]] = record
+ callback null, recordsByIds
+ else
+ callback null, if isArray then records else records[0]
###
`id(records, [options], callback)`
View
22 test/get.coffee
@@ -26,7 +26,7 @@ describe 'get', ->
it 'Test get # identifier', (next) ->
Users.create
- username: 'my_username',
+ username: 'my_username'
email: 'my@email.com'
, (err, user) ->
userId = user.user_id
@@ -44,7 +44,7 @@ describe 'get', ->
it 'Test get # unique property', (next) ->
Users.create
- username: 'my_username',
+ username: 'my_username'
email: 'my@email.com'
, (err, user) ->
userId = user.user_id
@@ -61,8 +61,8 @@ describe 'get', ->
it 'Test get # properties', (next) ->
Users.create
- username: 'my_username',
- email: 'my@email.com',
+ username: 'my_username'
+ email: 'my@email.com'
, (err, user) ->
userId = user.user_id
Users.get userId, ['username'], (err, user) ->
@@ -96,6 +96,20 @@ describe 'get', ->
should.not.exist users[2]
Users.clear next
+ it 'should return an object where keys are the identifiers with option `object`', (next) ->
+ Users.create [
+ username: 'username_1'
+ ,
+ username: 'username_2'
+ ,
+ username: 'username_3'
+ ], identifiers: true, (err, ids) ->
+ Users.get ids, object: true, (err, users) ->
+ Object.keys(users).length.should.eql 3
+ for id, user of users then id.should.eql "#{user.user_id}"
+ Users.clear next
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.