Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Isolate id creation

  • Loading branch information...
commit 67f87da5e1780bf16d6725b39ebd2002536469d8 1 parent e8404e4
@wdavidw authored
Showing with 68 additions and 10 deletions.
  1. +29 −9 lib/Records.coffee
  2. +2 −1  lib/Schema.coffee
  3. +37 −0 test/id.coffee
View
38 lib/Records.coffee
@@ -223,17 +223,9 @@ module.exports = class Records extends Schema
# Get current date once if schema is temporal
date = new Date Date.now() if temporal?
# Generate new identifiers
- incr = 0
- for record in records then incr++ unless record[identifier]
- multi.incrby "#{db}:#{name}_incr", incr
- multi.exec (err, recordId) =>
- recordId = recordId - incr
- return callback err if err
+ @id records, (err, records) =>
multi = redis.multi()
for record, i in records
- recordId++ unless record[identifier]
- # Enrich the record with its identifier
- record[identifier] = recordId unless record[identifier]
# Enrich the record with a creation date
record[temporal.creation] = date if temporal?.creation? and not record[temporal.creation]?
# Enrich the record with a creation date
@@ -377,6 +369,34 @@ module.exports = class Records extends Schema
else
callback null, if isArray then records else records[0]
###
+ `id(records, callback)`
+ -----------------------
+ Generate new identifiers. The first arguments `records` may be the number
+ of ids to generate, a record or an array of records.
+
+ ###
+ id: (records, callback) ->
+ {redis} = @
+ {db, name, identifier, unique} = @data
+ if typeof records is 'number'
+ incr = records
+ isArray = true
+ records = for i in [0...records] then null
+ else
+ isArray = Array.isArray records
+ records = [records] unless isArray
+ incr = 0
+ for record in records then incr++ unless record[identifier]
+ redis.incrby "#{db}:#{name}_incr", incr, (err, recordId) =>
+ recordId = recordId - incr
+ return callback err if err
+ for record, i in records
+ records[i] = record = {} unless record
+ recordId++ unless record[identifier]
+ # Enrich the record with its identifier
+ record[identifier] = recordId unless record[identifier]
+ callback null, if isArray then records else records[0]
+ ###
`identify(records, [options], callback)`
----------------------------------------
View
3  lib/Schema.coffee
@@ -9,7 +9,8 @@ isEmail = (email) ->
Schema definition
=================
-A schema is defined once and must no change. We dont support schema migration at the moment. The `Records`
+Schema is a mixin from which `Records` inherits. A schema is defined once
+and must no change. We dont support schema migration at the moment. The `Records`
class inherit all the properties and method of the shema.
`ron` Reference to the Ron instance
View
37 test/id.coffee
@@ -0,0 +1,37 @@
+
+should = require 'should'
+
+try config = require '../conf/test' catch e
+ron = require '../index'
+
+describe 'id', ->
+
+ client = Users = null
+
+ before (next) ->
+ client = ron config
+ Users = client.get
+ name: 'users'
+ properties:
+ user_id: identifier: true
+ next()
+
+ beforeEach (next) ->
+ Users.clear next
+
+ after (next) ->
+ client.quit next
+
+ it "should take a number as the first argument", (next) ->
+ Users.id 2, (err, users) ->
+ users.length.should.eql 2
+ ids = for user in users then user.user_id
+ ids[0].should.eql ids[1] - 1
+ next()
+
+ it "should increment ids by default", (next) ->
+ Users.id [{},{}], (err, users) ->
+ users.length.should.eql 2
+ ids = for user in users then user.user_id
+ ids[0].should.eql ids[1] - 1
+ next()
Please sign in to comment.
Something went wrong with that request. Please try again.