Permalink
Browse files

put api almost done

  • Loading branch information...
1 parent 75ac1d1 commit 6cf0d23c22738ef478b7991cb54aa0d9cc5cf9fb Karl Seguin committed Jan 24, 2012
Showing with 57 additions and 5 deletions.
  1. +29 −2 spec/putItemSpec.coffee
  2. +16 −3 src/alternator.coffee
  3. +5 −0 src/messages.coffee
  4. +7 −0 src/validator.coffee
View
@@ -40,6 +40,10 @@ describe 'putItem', ->
delete @data.Item
helper.assertRequestError 'putItem', @data, messages.cannotBeNull('item'), true, done
+ it "returns an error on invalid return value", (done) ->
+ @data.ReturnValue = 'INVALID'
+ helper.assertRequestError 'putItem', @data, messages.invalidReturnValue(), false, done
+
it "returns an error on missing key", (done) ->
delete @data.Item.id
helper.assertRequestError 'putItem', @data, messages.missingKey(), false, done
@@ -71,7 +75,30 @@ describe 'putItem', ->
alternator.putItem @rangeData, (err, response) ->
expect(err).toBeNull()
expect(response).toBeUndefined(response)
- db.collection('votes').count {_id: {id: 'leto', count: 4}}, (err, count) ->
- expect(count).toEqual(1)
+ done()
+
+ it "returns the undefined old value if it doesn't exist", (done) ->
+ @data.ReturnValue = 'ALL_OLD'
+ helper.async (db) ->
+ alternator.putItem @data, (err, response) ->
+ expect(err).toBeNull()
+ expect(response).toBeUndefined(response)
+ done()
+
+ it "returns the previous old value", (done) ->
+ @data.ReturnValue = 'ALL_OLD'
+ helper.async (db) ->
+ db.collection('users').insert {_id: 1, name: 'test'}, ->
+ alternator.putItem @data, (err, response) ->
+ expect(err).toBeNull()
+ expect(response).toEqual({id: 1, name: 'test'})
done()
+ it "returns the previous old value for range key", (done) ->
+ @rangeData.ReturnValue = 'ALL_OLD'
+ helper.async (db) ->
+ db.collection('votes').insert {_id: {id: 'leto', count: 4}, dynanodb_api_is_consistent: 'false', funLevel: 1}, ->
+ alternator.putItem @rangeData, (err, response) ->
+ expect(err).toBeNull()
+ expect(response).toEqual({id: 'leto', count:4, dynanodb_api_is_consistent: 'false', funLevel: 1})
+ done()
View
@@ -101,13 +101,26 @@ class Alternator
document._id = document[hashKey]
delete document[hashKey]
- this.db.collection(data.TableName).insert document, {safe: true}, (err, document) ->
- return callback(err, null) if err?
- callback(null)
+ collection = this.db.collection(data.TableName)
+ collection.findOne {_id: document._id}, (err, value) =>
+ collection.update {_id: document._id}, document, {safe: true, upsert: true}, (err, document) =>
+ return callback(err, null) if err?
+ value = if data.ReturnValue == 'ALL_OLD' then this.fixId(value, hashKey, rangeKey) else undefined
+ callback(null, value)
@tableDetails: (name, callback) =>
this.systemCollection().findOne {_id: name}, (err, value) ->
return callback(err, null) if err?
callback(null, if value? then value.details else null)
+ @fixId: (document, hashKey, rangeKey) =>
+ return undefined unless document?
+ if rangeKey?
+ document[key] = value for key, value of document._id
+ else
+ document[hashKey] = document._id
+ delete document._id
+ document
+
+
module.exports = Alternator
View
@@ -50,6 +50,11 @@ class Messages
__type: 'com.amazon.coral.validate#ValidationException'
message: util.format('One or more parameter values were invalid: Type mismatch for key id expected: %s actual: %s', expected, actual)
}
+ @invalidReturnValue: ->
+ return {
+ __type: 'com.amazon.coral.validate#ValidationException'
+ message: 'ReturnValues can only be ALL_OLD or NONE'
+ }
module.exports = Messages
View
@@ -37,6 +37,7 @@ class Validator
@putItem: (data, details, callback) ->
return unless Validator.crudDetails(data, 'Item', details, callback)
+ return unless Validator.validReturnValue(data.ReturnValue, callback)
errors = []
Validator.validPattern(data.TableName, 'tableName', Validator.tablePattern, errors)
@@ -117,6 +118,12 @@ class Validator
return false
return true
+
+ @validReturnValue: (value, callback) ->
+ return true unless value?
+ return true if value == 'ALL_OLD' || value == 'NONE'
+ callback(messages.invalidReturnValue(), null)
+ return false
@handleErrors: (errors, callback) ->
return true if errors.length == 0

0 comments on commit 6cf0d23

Please sign in to comment.