Permalink
Browse files

Records extends schema

  • Loading branch information...
1 parent 4a5bc34 commit 1a584ca41027ae15bea0f15baaea2f86736c047d @wdavidw committed Mar 4, 2012
View
@@ -17,13 +17,16 @@ module.exports = class Client
@redis.auth options.redis_password if options.redis_password?
@redis.select options.redis_database if options.redis_database?
- schema: (options) ->
- name = if typeof options is 'string' then options else options.name
- @schemas[name] = new Schema @, options
- @records[name] = new Records @, @schemas[name]
- @schemas[name]
+ # schema: (options) ->
+ # name = if typeof options is 'string' then options else options.name
+ # @schemas[name] = new Schema @, options
+ # @records[name] = new Records @, @schemas[name]
+ # @schemas[name]
- get: (name) ->
+ get: (options) ->
+ # @records[name]
+ name = if typeof options is 'string' then options else options.name
+ @records[name] = new Records @, options if typeof options isnt 'string' or not @records[name]?
@records[name]
quit: (callback) ->
View
@@ -1,4 +1,6 @@
+Schema = require './Schema'
+
isEmail = (email) ->
/^[a-z0-9,!#\$%&'\*\+\/\=\?\^_`\{\|}~\-]+(\.[a-z0-9,!#\$%&'\*\+\/\=\?\^_`\{\|}~\-]+)*@[a-z0-9\-]+(\.[a-z0-9\-]+)*\.([a-z]{2,})$/.test(email)
@@ -37,20 +39,20 @@ associated value is a set containing all the identifiers of the records whose pr
match the indexed value.
###
-module.exports = class Records
+module.exports = class Records extends Schema
constructor: (ron, schema) ->
@redis = ron.redis
- @schema = schema
+ super ron, schema
###
Return all records
------------------
Similar to the find method with far less options and a faster implementation.
###
all: (callback) ->
- {redis, schema} = @
- {db, name, identifier} = schema.data
+ {redis} = @
+ {db, name, identifier} = @data
redis.smembers "#{db}:#{name}_#{identifier}", (err, recordIds) ->
multi = redis.multi()
for recordId in recordIds
@@ -66,9 +68,8 @@ module.exports = class Records
success, the number of removed records is provided in the callback.
###
clear: (callback) ->
- {redis, schema} = @
- {hash} = schema
- {db, name, identifier, index, unique} = schema.data
+ {redis, hash} = @
+ {db, name, identifier, index, unique} = @data
cmds = []
count = 0
multi = redis.multi()
@@ -118,8 +119,8 @@ module.exports = class Records
Count the number of records present in the database.
###
count: (callback) ->
- {redis, schema} = @
- {db, name, identifier} = schema.data
+ {redis} = @
+ {db, name, identifier} = @data
@redis.scard "#{db}:#{name}_#{identifier}", (err, count) ->
return callback err if err
callback null, count
@@ -128,9 +129,8 @@ module.exports = class Records
Create a new record.
###
create: (records, callback) ->
- {redis, schema} = @
- {hash} = schema
- {db, name, properties, identifier, index, unique} = schema.data
+ {redis, hash} = @
+ {db, name, properties, identifier, index, unique} = @data
isArray = Array.isArray records
records = [records] if ! isArray
# Sanitize records
@@ -175,7 +175,7 @@ module.exports = class Records
return callback err if err
for result in results
return callback new Error 'Corrupted user database ' if result[0] is not "0"
- # $.type records
+ # @type records
callback null, if isArray then records else records[0]
###
@@ -186,8 +186,9 @@ module.exports = class Records
are present if the record exists or null if it doesn't.
###
exists: (records, callback) ->
- {redis, schema} = @
- {db, name, identifier, unique} = schema.data
+ {redis} = @
+ $ = @
+ {db, name, identifier, unique} = @data
isArray = Array.isArray records
records = [records] if ! isArray
multi = redis.multi()
@@ -202,9 +203,9 @@ module.exports = class Records
multi.hget "#{db}:#{name}_#{property}", record[property]
else
multi.hget "#{db}:#{name}:#{record}", identifier
- multi.exec (err, recordIds) ->
+ multi.exec (err, recordIds) =>
return callback err if err
- schema.type recordIds
+ @type recordIds
callback null, if isArray then recordIds else recordIds[0]
###
@@ -225,10 +226,10 @@ module.exports = class Records
if arguments.length is 2
callback = options
options = {}
- {redis, schema} = @
- {db, name, identifier, unique} = schema.data
+ {redis} = @
+ {db, name, identifier, unique} = @data
isArray = Array.isArray records
- records = [records] if not isArray
+ records = [records] unless isArray
cmds = []
err = null
for record, i in records
@@ -262,14 +263,14 @@ module.exports = class Records
if not options.object
records = for record in records
if record? then record[identifier] else record
- #$.type records
return callback null, if isArray then records else records[0]
+ # Run the commands
multi = redis.multi cmds
- multi.exec (err, results) ->
+ multi.exec (err, results) =>
if not options.object
records = for record in records
record[identifier]
- schema.type records
+ @type records
callback null, if isArray then records else records[0]
###
@@ -289,11 +290,11 @@ module.exports = class Records
options = {}
if Array.isArray options
options = {properties: options}
- {redis, schema} = @
- {db, name, identifier} = schema.data
+ {redis} = @
+ {db, name, identifier} = @data
isArray = Array.isArray records
records = [records] if ! isArray
- @id records, {object: true}, (err, records) ->
+ @id records, {object: true}, (err, records) =>
cmds = []
records.forEach (record, i) ->
if record[identifier] is null
@@ -314,18 +315,17 @@ module.exports = class Records
if cmds.length is 0
return callback null, if isArray then records else records[0]
multi = redis.multi cmds
- multi.exec (err, values) ->
+ multi.exec (err, values) =>
return callback err if err
- schema.type records
+ @type records
callback null, if isArray then records else records[0]
list: (options, callback) ->
if typeof options is 'function'
callback = options
options = {}
- {redis, schema} = @
- {hash} = schema
- {db, name, properties, identifier, index} = schema.data
+ {redis, hash} = @
+ {db, name, properties, identifier, index} = @data
args = []
multi = @redis.multi()
# Index
@@ -392,9 +392,8 @@ module.exports = class Records
###
remove: (records, callback) ->
- {redis, schema} = @
- {hash} = schema
- {db, name, identifier, index, unique} = schema.data
+ {redis, hash} = @
+ {db, name, identifier, index, unique} = @data
isArray = Array.isArray records
records = [records] unless isArray
@get records, [].concat(Object.keys(unique), Object.keys(index)), (err, records) ->
@@ -421,9 +420,8 @@ module.exports = class Records
Update one or several records
###
update: (records, callback) ->
- {redis, schema} = @
- {hash} = schema
- {db, name, properties, identifier, unique, index} = schema.data
+ {redis, hash} = @
+ {db, name, properties, identifier, unique, index} = @data
isArray = Array.isArray records
records = [records] unless isArray
# 1. Get values of indexed properties
View
@@ -38,59 +38,40 @@ module.exports = class Schema
if options.properties
for name, value of options.properties
@property name, value
-
- ###
- Retrieve/define a new property
- ------------------------------
- Define a new property or overwrite the definition of an
- existing property. If no schema is provide, return the
- property information.
- Calling this function with only the property argument will return the schema
- information associated with the property.
+ ###
+ Define property as en email
+ ---------------------------
+ Check that a property validate as an email
- It is possible to define a new property without any schema information by
- providing an empty object.
+ Calling this function without any argument will return all the email
+ properties.
Example:
- User.property 'id', identifier: true
- User.property 'username', unique: true
- User.property 'email', { index: true, email: true }
- User.property 'name', {}
-
+ User.unique 'username'
+ User.get { username: 'me' }, (err, user) ->
+ console.log "This is #{user.username}"
###
- property: (property, schema) ->
- if schema?
- schema ?= {}
- schema.name = property
- @data.properties[property] = schema
- @identifier property if schema.identifier
- @index property if schema.index
- @unique property if schema.unique
- @email property if schema.email
+ email: (property) ->
+ # Set the property
+ if property?
+ @data.properties[property] = {} unless @data.properties[property]?
+ @data.properties[property].email = true
+ @data.email[property] = true
@
+ # Get the property
else
- @data.properties[property]
-
+ @data.email
+
###
- Cast record values to their correct type
- ----------------------------------------
- Traverse all the record properties and update
- values with correct types.
+ Hash a key
+ ----------
+ This is a utility function used when redis key are created out of
+ uncontrolled values.
###
- type: (records) ->
- s = @schema
- {properties} = @data
- isArray = Array.isArray records
- records = [records] if ! isArray
- for record, i in records
- continue unless record?
- if typeof record is 'object'
- for property, value of record
- if properties[property]?.type is 'int' and value?
- record[property] = parseInt value, 10
- else if typeof record is 'number' or typeof record is 'string'
- records[i] = parseInt record
+ hash: (key) ->
+ key = "#{key}" if typeof key is 'number'
+ return if key? then crypto.createHash('sha1').update(key).digest('hex') else 'null'
###
Define a property as an identifier
@@ -139,6 +120,58 @@ module.exports = class Schema
# Get the property
else
Object.keys(@data.index)
+
+ ###
+ Retrieve/define a new property
+ ------------------------------
+ Define a new property or overwrite the definition of an
+ existing property. If no schema is provide, return the
+ property information.
+
+ Calling this function with only the property argument will return the schema
+ information associated with the property.
+
+ It is possible to define a new property without any schema information by
+ providing an empty object.
+
+ Example:
+ User.property 'id', identifier: true
+ User.property 'username', unique: true
+ User.property 'email', { index: true, email: true }
+ User.property 'name', {}
+
+ ###
+ property: (property, schema) ->
+ if schema?
+ schema ?= {}
+ schema.name = property
+ @data.properties[property] = schema
+ @identifier property if schema.identifier
+ @index property if schema.index
+ @unique property if schema.unique
+ @email property if schema.email
+ @
+ else
+ @data.properties[property]
+
+ ###
+ Cast record values to their correct type
+ ----------------------------------------
+ Traverse all the record properties and update
+ values with correct types.
+ ###
+ type: (records) ->
+ {properties} = @data
+ isArray = Array.isArray records
+ records = [records] if ! isArray
+ for record, i in records
+ continue unless record?
+ if typeof record is 'object'
+ for property, value of record
+ if properties[property]?.type is 'int' and value?
+ record[property] = parseInt value, 10
+ else if typeof record is 'number' or typeof record is 'string'
+ records[i] = parseInt record
###
Define a property as unique
@@ -164,37 +197,3 @@ module.exports = class Schema
# Get the property
else
Object.keys(@data.unique)
-
- ###
- Define property as en email
- ---------------------------
- Check that a property validate as an email
-
- Calling this function without any argument will return all the email
- properties.
-
- Example:
- User.unique 'username'
- User.get { username: 'me' }, (err, user) ->
- console.log "This is #{user.username}"
- ###
- email: (property) ->
- # Set the property
- if property?
- @data.properties[property] = {} unless @data.properties[property]?
- @data.properties[property].email = true
- @data.email[property] = true
- @
- # Get the property
- else
- @data.email
-
- ###
- Hash a key
- ----------
- This is a utility function used when redis key are created out of
- uncontrolled values.
- ###
- hash: (key) ->
- key = "#{key}" if typeof key is 'number'
- return if key? then crypto.createHash('sha1').update(key).digest('hex') else 'null'
Oops, something went wrong.

0 comments on commit 1a584ca

Please sign in to comment.