Permalink
Browse files

Fix clear method when there are no indexes; Add identifier option to …

…create
  • Loading branch information...
1 parent 1c60d9a commit 63cc88baa8a03e49382d1308935db1b2d7575a9a @wdavidw wdavidw committed Mar 5, 2012
Showing with 124 additions and 14 deletions.
  1. +59 −12 lib/Records.coffee
  2. +48 −0 test/clear.coffee
  3. +15 −0 test/create.coffee
  4. +2 −2 test/list.coffee
View
@@ -44,11 +44,12 @@ module.exports = class Records extends Schema
constructor: (ron, schema) ->
@redis = ron.redis
super ron, schema
-
###
- Return all records
- ------------------
+
+ `all(callback)` Return all records
+ ----------------------------------
Similar to the find method with far less options and a faster implementation.
+
###
all: (callback) ->
{redis} = @
@@ -62,10 +63,22 @@ module.exports = class Records extends Schema
callback null, records
###
- Clear all the records
- ---------------------
- Remove all the records and the references poiting to them. On
- success, the number of removed records is provided in the callback.
+
+ `clear(callback)` Clear all the records
+ ---------------------------------------
+ Remove all the records and the references poiting to them. This function
+ takes no other argument than the callback called on error or success.
+
+ `callback` Received parameters are:
+
+ * `err` Error object if any.
+ * `count` Number of removed records on success
+
+ Usage:
+ ron.get('users').clear (err, count) ->
+ return console.error "Failed: #{err.message}" if err
+ console.log "#{count} records removed"
+
###
clear: (callback) ->
{redis, hash} = @
@@ -76,7 +89,7 @@ module.exports = class Records extends Schema
# Grab index values for later removal
indexSort = []
indexProperties = Object.keys(index)
- if indexProperties
+ if indexProperties.length
indexSort.push "#{db}:#{name}_#{identifier}"
for property in indexProperties
indexSort.push 'get'
@@ -126,9 +139,29 @@ module.exports = class Records extends Schema
callback null, count
###
- Create a new record.
+ `create(records, [options], callback)` Create new records
+ ----------------------------------------------
+ Take a record object or multiple records and insert them. The records must no
+ exists in the database or an error will be returned in the callback. The records objects
+ passed in the function are returned in the callback with their new identifier property.
+
+ `records` Record object or array of record objects.
+
+ `options` Options properties include:
+
+ * `identifiers` Return only the created identifiers instead of the records.
+
+ `callback` Called on success or failure. Received parameters are:
+
+ * `err` Error object if any.
+ * `records` Records with their newly created identifier.
+
+
###
- create: (records, callback) ->
+ create: (records, options, callback) ->
+ if arguments.length is 2
+ callback = options
+ options = {}
{redis, hash} = @
{db, name, properties, identifier, index, unique} = @data
isArray = Array.isArray records
@@ -176,6 +209,9 @@ module.exports = class Records extends Schema
for result in results
return callback new Error 'Corrupted user database ' if result[0] is not "0"
# @type records
+ if options.identifiers
+ records = for record in records
+ record[identifier]
callback null, if isArray then records else records[0]
###
@@ -187,7 +223,6 @@ module.exports = class Records extends Schema
###
exists: (records, callback) ->
{redis} = @
- $ = @
{db, name, identifier, unique} = @data
isArray = Array.isArray records
records = [records] if ! isArray
@@ -319,7 +354,19 @@ module.exports = class Records extends Schema
return callback err if err
@type records
callback null, if isArray then records else records[0]
-
+ ###
+ `list(options, callback)` List records
+ --------------------------------------
+ List records with filtering and sorting.
+
+ Using the `union` operation:
+ Users.list
+ group: ['admin', 'redis']
+ operation: 'union'
+ direction: 'desc'
+ , (err, users) ->
+ console.log users
+ ###
list: (options, callback) ->
if typeof options is 'function'
callback = options
View
@@ -0,0 +1,48 @@
+
+should = require 'should'
+
+try config = require '../conf/test' catch e
+ron = require '../index'
+
+describe 'all', ->
+
+ client = Users = null
+
+ before (next) ->
+ client = ron config
+ Users = client.get
+ name: 'users'
+ properties:
+ user_id: identifier: true
+ username: unique: true
+ email: index: true
+ next()
+
+ beforeEach (next) ->
+ Users.clear next
+
+ after (next) ->
+ client.quit next
+
+ it 'shall clear nothing if there are no record', (next) ->
+ Users.all (err, users) ->
+ users.length.should.equal 0
+ Users.clear (err, count) ->
+ should.not.exist err
+ count.should.eql 0
+ next()
+
+ it 'shall clear simple records', (next) ->
+ Records = client.get { name: 'records', properties: id_records: identifier: true }
+ Records.all (err, users) ->
+ users.length.should.equal 0
+ Records.clear (err, count) ->
+ should.not.exist err
+ count.should.eql 0
+ Records.create {}, (err, count) ->
+ should.not.exist err
+ Records.clear (err, count) ->
+ should.not.exist err
+ count.should.eql 1
+ next()
+
View
@@ -83,5 +83,20 @@ describe 'create', ->
err.message.should.eql 'Record 1 already exists'
Users.clear next
+ it 'should only return the newly created identifiers', (next) ->
+ Users.create [
+ username: 'my_username_1',
+ email: 'my_first@email.com',
+ password: 'my_password'
+ ,
+ username: 'my_username_2',
+ email: 'my_second@email.com',
+ password: 'my_password'
+ ], identifiers: true, (err, ids) ->
+ should.not.exist err
+ ids.length.should.equal 2
+ for id in ids then id.should.be.a 'number'
+ Users.clear next
+
View
@@ -25,13 +25,13 @@ describe 'list', ->
after (next) ->
client.quit next
- it 'Test list # result empty', (next) ->
+ it 'should be empty if there are no record', (next) ->
Users.list { }, (err, users) ->
should.not.exist err
users.length.should.eql 0
next()
- it 'Test list # sort', (next) ->
+ it 'should sort record according to one property', (next) ->
Users.create [
{ username: 'username_1', email: '1@email.com', password: 'my_password' }
{ username: 'username_2', email: '2@email.com', password: 'my_password' }

0 comments on commit 63cc88b

Please sign in to comment.