Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

List may now return identifiers; Unserialize now accept the identifie…

…rs option
  • Loading branch information...
commit 9e746d6ba813a8b6e2520d922d9b56d34331bcc2 1 parent b315ae5
@wdavidw authored
Showing with 29 additions and 15 deletions.
  1. +13 −14 lib/Records.coffee
  2. +5 −1 lib/Schema.coffee
  3. +11 −0 test/list.coffee
View
27 lib/Records.coffee
@@ -255,9 +255,6 @@ module.exports = class Records extends Schema
for result in results
return callback new Error 'Corrupted user database ' if result[0] is not "0"
@unserialize records, options
- if options.identifiers
- records = for record in records
- record[identifier]
callback null, if isArray then records else records[0]
###
@@ -492,13 +489,14 @@ module.exports = class Records extends Schema
`options` Options properties include:
- * `where` Hash of property/value used to filter the query.
- * `operation` Redis operation in case of multiple `where` properties, default to `union`.
- * `sort` Name of the property by which records should be ordered.
* `direction` One of `asc` or `desc`, default to `asc`.
- * `properties` Array of properties to be returned.
+ * `identifiers` Return an array of identifiers instead of the record objects.
* `milliseconds` Convert date value to milliseconds timestamps instead of `Date` objects.
+ * `properties` Array of properties to be returned.
+ * `operation` Redis operation in case of multiple `where` properties, default to `union`.
* `seconds` Convert date value to seconds timestamps instead of `Date` objects.
+ * `sort` Name of the property by which records should be ordered.
+ * `where` Hash of property/value used to filter the query.
`callback` Called on success or failure. Received parameters are:
@@ -530,7 +528,9 @@ module.exports = class Records extends Schema
callback = options
options = {}
{redis, hash} = @
- {db, name, properties, identifier, index} = @data
+ {db, name, identifier, index} = @data
+ options.properties = options.properties or Object.keys @data.properties
+ options.properties = [identifier] if options.identifiers
args = []
multi = @redis.multi()
# Index
@@ -566,7 +566,7 @@ module.exports = class Records extends Schema
args.push 'by'
args.push "#{db}:#{name}:*->" + options.sort
# Properties to return
- for property of properties
+ for property in options.properties
args.push 'get'
args.push "#{db}:#{name}:*->" + property
# Sorting property is a string
@@ -577,10 +577,9 @@ module.exports = class Records extends Schema
args.push (err, values) =>
return callback err if err
return callback null, [] unless values.length
- keys = Object.keys properties
- result = for i in [0 ... values.length] by keys.length
+ result = for i in [0 ... values.length] by options.properties.length
record = {}
- for property, j in keys
+ for property, j in options.properties
record[property] = values[i + j]
@unserialize record, options
callback null, result
@@ -639,8 +638,8 @@ module.exports = class Records extends Schema
* `err` Error object if any.
* `records` Records with their newly created identifier.
- Records are not validated, it is the responsability of the client program calling `create` to either
- call `validate` before calling `create` or to passs the `validate` options.
+ Records are not validated, it is the responsability of the client program to either
+ call `validate` before calling `update` or to passs the `validate` options.
Updating a single record:
View
6 lib/Schema.coffee
@@ -260,15 +260,17 @@ module.exports = class Schema
`options` Options include:
+ * `identifiers` Return an array of identifiers instead of the record objects.
* `properties` Array of properties to be returned.
* `milliseconds` Convert date value to milliseconds timestamps instead of `Date` objects.
* `seconds` Convert date value to seconds timestamps instead of `Date` objects.
###
unserialize: (records, options = {}) ->
- {properties} = @data
+ {identifier, properties} = @data
isArray = Array.isArray records
records = [records] unless isArray
+ options.properties = [identifier] if options.identifiers
for record, i in records
continue unless record?
# Convert the record
@@ -289,9 +291,11 @@ module.exports = class Schema
else if options.seconds
record[property] = Math.round( value / 1000 )
else record[property] = new Date value
+ records[i] = record[identifier] if options.identifiers
# By convension, this has to be an identifier but we can't check it
else if typeof record is 'number' or typeof record is 'string'
records[i] = parseInt record
+
if isArray then records else records[0]
###
View
11 test/list.coffee
@@ -96,3 +96,14 @@ describe 'list', ->
users[0].username.should.eql 'username_2'
users[1].username.should.eql 'username_1'
Users.clear next
+
+ it 'should return an array of identifiers', (next) ->
+ Users.create [
+ { username: 'username_1', email: '1@email.com', password: 'my_password' }
+ { username: 'username_2', email: '2@email.com', password: 'my_password' }
+ ], (err, users) ->
+ Users.list { identifiers: true }, (err, ids) ->
+ should.not.exist err
+ ids.length.should.eql 2
+ for id in ids then id.should.be.a 'number'
+ Users.clear next
Please sign in to comment.
Something went wrong with that request. Please try again.