Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Count the values of an index property

  • Loading branch information...
commit 9230c2bd81901a7838bb211eb50149593913691e 1 parent 2a4febd
@wdavidw authored
Showing with 39 additions and 5 deletions.
  1. +18 −4 lib/Records.coffee
  2. +21 −1 test/count.coffee
View
22 lib/Records.coffee
@@ -133,13 +133,27 @@ module.exports = class Records extends Schema
-----------------
Count the number of records present in the database.
+ `count(property, value, callback)`
+ ----------------------------------
+ Count the number of values for an indexed property.
+
###
count: (callback) ->
{redis} = @
- {db, name, identifier} = @data
- @redis.scard "#{db}:#{name}_#{identifier}", (err, count) ->
- return callback err if err
- callback null, count
+ {db, name, identifier, index} = @data
+ if arguments.length is 3
+ property = callback
+ value = arguments[1]
+ callback = arguments[2]
+ return callback new Error "Property is not indexed" unless index[property]
+ value = @hash value
+ @redis.scard "#{db}:#{name}_#{property}:#{value}", (err, count) ->
+ return callback err if err
+ callback null, count
+ else
+ @redis.scard "#{db}:#{name}_#{identifier}", (err, count) ->
+ return callback err if err
+ callback null, count
###
`create(records, [options], callback)`
View
22 test/count.coffee
@@ -35,4 +35,24 @@ describe 'count', ->
Users.count (err, count) ->
should.not.exist err
count.should.eql 2
- next()
+ next()
+
+ it 'should count the index elements of a property', (next) ->
+ Users.create [
+ username: 'username_1',
+ email: 'my@email.com',
+ password: 'my_password'
+ ,
+ username: 'username_2',
+ email: 'my_2@email.com',
+ password: 'my_password'
+ ,
+ username: 'username_3',
+ email: 'my@email.com',
+ password: 'my_password'
+ ], (err, user) ->
+ Users.count 'email', 'my@email.com', (err, count) ->
+ should.not.exist err
+ count.should.eql 2
+ next()
+
Please sign in to comment.
Something went wrong with that request. Please try again.