0

I have a node application saving data to Amazon SimpleDb to store name/value pairs data using the rjrodger/simpledb node driver (https://github.com/rjrodger/simpledb) . I call putItem as below:

sdb.putItem('mySimpleDbTable','Record_0001', 
{
   statusUpdate:'1'
},
function(err,res,meta){
  if (err) {
     console.log('Status Update error: ' + err.Message);
   }
  console.log("Memories, you're talking about memories: "+JSON.stringify(res)) 
})

Inserts a record like below:

{ $ItemName: 'Record_0001' { statusUpdate: '1' }}

Then after a few operations, I would like to update statusUpdateto '2' like this:

{ $ItemName: 'Record_0001' { statusUpdate: '2' }}

No matter how I try, the Node simpleDb driver always inserts a new attribute like below:

{ $ItemName: 'Record_0001' { statusUpdate: '1',  statusUpdate: '2' }}

If I try

sdb.putItem('mySimpleDbTable','Record_0001', 
{
statusUpdate:['2', true]
}

Then I get -

{ $ItemName: 'Record_0001' { statusUpdate: '1',  statusUpdate: '2', statusUpdate: 'true' }}

How can I use the "replace:true" parameter of the putItem to just update the value instead of inserting a new value.

Please help.

Ram Iyer
  • 1,404
  • 2
  • 20
  • 27

2 Answers2

2

Your code is always setting Replace parameter to false so these values are getting added to existing values. If you set it to true then existing values of attribute always gets replaced by new value. i.e.

Optionally, you can supply the Replace parameter for each individual attribute. Setting this value to true causes the new attribute value to replace the existing attribute value(s) if any exist. Otherwise, Amazon SimpleDB simply inserts the attribute values. For example, if an item has the attributes { 'a', '1' }, { 'b', '2'}, and { 'b', '3' } and the requester calls BatchPutAttributes using the attributes { 'b', '4' } with the Replace parameter set to true, the final attributes of the item are changed to { 'a', '1' } and { 'b', '4' }. This occurs because the new 'b' attribute replaces the old value.

-- Source

statusUpdate:['2', true]

Here true is not that replace parameter. See the results. Code is taking true as a value to that attribute.

Ashish Pancholi
  • 4,569
  • 13
  • 50
  • 88
1

There is something else going on here - that behaviour is not expected.

I have added a test case for this - checkout out this commit: https://github.com/rjrodger/simpledb/commit/236aad0feefad9844548c4fde76e996ee3c4d347

and run in folder test: expresso simpledb.test.js -o putItemHappy (you may need to npm install expresso)

Also, trying running with the debuglogger enabled: sdb = new simpledb.SimpleDB({...}),simpledb.debuglogger)

This will print logs to the console.

The putattrs function in lib/simpledb.js:203 does the put attributes setup - the replace mode is set if you don't provide an array - your Array.isArray hasn't been overwritten, right?

... ah

I think I see what is going on.

Have you tried:

sdb.putItem('mySimpleDbTable','Record_0001', 
{
  statusUpdate:'2'
}

This should replace.

If you use an array (['2',true]), it does not replace - it assumes you want to set multiple values.