Permalink
Browse files

Insert only defined properties

  • Loading branch information...
1 parent bde6319 commit 2a4febd849aa3e6a1669edd8be6037f2f9b420b4 @wdavidw wdavidw committed Mar 13, 2012
Showing with 22 additions and 9 deletions.
  1. +7 −3 lib/Records.coffee
  2. +3 −5 test/all.coffee
  3. +1 −1 test/count.coffee
  4. +11 −0 test/create.coffee
View
@@ -145,8 +145,10 @@ module.exports = class Records extends Schema
`create(records, [options], callback)`
--------------------------------------
Insert one or multiple record. The records must not already 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.
+ in the database or an error will be returned in the callback. Only
+ the defined properties are inserted.
+
+ The records passed to the function are returned in the callback enriched their new identifier property.
`records` Record object or array of record objects.
@@ -212,8 +214,10 @@ module.exports = class Records extends Schema
#multi.zadd "#{s.db}:#{s.name}_#{property}", 0, record[property]
# Store the record
r = {}
- # Filter null values
for property, value of record
+ # Insert only defined properties
+ continue unless properties[property]
+ # Filter null values
r[property] = value if value?
@serialize r
multi.hmset "#{db}:#{name}:#{recordId}", r
View
@@ -27,16 +27,14 @@ describe 'all', ->
it 'shall create 2 users and list them', (next) ->
Users.create [
username: 'my_username_1',
- email: 'my_first@email.com',
- password: 'my_password'
+ email: 'my_first@email.com'
,
username: 'my_username_2',
- email: 'my_second@email.com',
- password: 'my_password'
+ email: 'my_second@email.com'
], (err, users) ->
Users.all (err, users) ->
should.not.exist err
users.length.should.eql 2
- users[0].password.should.eql 'my_password'
+ for user in users then user.username.should.match /^my_/
next()
View
@@ -22,7 +22,7 @@ describe 'count', ->
after (next) ->
client.quit next
- it 'Test count', (next) ->
+ it 'should count the records', (next) ->
Users.create [
username: '1my_username',
email: '1my@email.com',
View
@@ -113,5 +113,16 @@ describe 'create', ->
for user in users then Object.keys(user).should.eql ['email']
Users.clear next
+ it 'should only insert defined properties', (next) ->
+ Users.create
+ username: 'my_username_1',
+ email: 'my_first@email.com',
+ password: 'my_password'
+ zombie: 'e.t. maison'
+ , (err, user) ->
+ Users.get user.user_id, (err, user) ->
+ should.not.exist user.zombie
+ Users.clear next
+

0 comments on commit 2a4febd

Please sign in to comment.