Skip to content

Commit

Permalink
Insert only defined properties
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Mar 13, 2012
1 parent bde6319 commit 2a4febd
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
10 changes: 7 additions & 3 deletions lib/Records.coffee
Expand Up @@ -145,8 +145,10 @@ module.exports = class Records extends Schema
`create(records, [options], callback)` `create(records, [options], callback)`
-------------------------------------- --------------------------------------
Insert one or multiple record. The records must not already exists 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 in the database or an error will be returned in the callback. Only
passed in the function are returned in the callback with their new identifier property. 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. `records` Record object or array of record objects.
Expand Down Expand Up @@ -212,8 +214,10 @@ module.exports = class Records extends Schema
#multi.zadd "#{s.db}:#{s.name}_#{property}", 0, record[property] #multi.zadd "#{s.db}:#{s.name}_#{property}", 0, record[property]
# Store the record # Store the record
r = {} r = {}
# Filter null values
for property, value of record for property, value of record
# Insert only defined properties
continue unless properties[property]
# Filter null values
r[property] = value if value? r[property] = value if value?
@serialize r @serialize r
multi.hmset "#{db}:#{name}:#{recordId}", r multi.hmset "#{db}:#{name}:#{recordId}", r
Expand Down
8 changes: 3 additions & 5 deletions test/all.coffee
Expand Up @@ -27,16 +27,14 @@ describe 'all', ->
it 'shall create 2 users and list them', (next) -> it 'shall create 2 users and list them', (next) ->
Users.create [ Users.create [
username: 'my_username_1', username: 'my_username_1',
email: 'my_first@email.com', email: 'my_first@email.com'
password: 'my_password'
, ,
username: 'my_username_2', username: 'my_username_2',
email: 'my_second@email.com', email: 'my_second@email.com'
password: 'my_password'
], (err, users) -> ], (err, users) ->
Users.all (err, users) -> Users.all (err, users) ->
should.not.exist err should.not.exist err
users.length.should.eql 2 users.length.should.eql 2
users[0].password.should.eql 'my_password' for user in users then user.username.should.match /^my_/
next() next()


2 changes: 1 addition & 1 deletion test/count.coffee
Expand Up @@ -22,7 +22,7 @@ describe 'count', ->
after (next) -> after (next) ->
client.quit next client.quit next


it 'Test count', (next) -> it 'should count the records', (next) ->
Users.create [ Users.create [
username: '1my_username', username: '1my_username',
email: '1my@email.com', email: '1my@email.com',
Expand Down
11 changes: 11 additions & 0 deletions test/create.coffee
Expand Up @@ -113,5 +113,16 @@ describe 'create', ->
for user in users then Object.keys(user).should.eql ['email'] for user in users then Object.keys(user).should.eql ['email']
Users.clear next 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.