awspilot / dynamodb-oop

Speak fluent DynamoDB, write code with fashion, I Promise() 😃
https://awspilot.dev
MIT License
119 stars 28 forks source link

Allow the object passage containing actions for each attribute. #7

Closed luizstacio closed 9 years ago

luizstacio commented 9 years ago

In my case I have a list and i needed append this list in "insert_or_update" and if changing for the other method the method not attended my necessity, because I had atributes null.

/* First insert_or_update */
DynamoDB
    .table(TABLE)
    .insert_or_update({
      hash: 'USER:123',
      phones: ['9988999'],
      status: null 
    }, function (err, data) {
      if (err) throw (err);
      console.log(data);
    });
/* Second insert_or_update */
DynamoDB
    .table(TABLE)
    .insert_or_update({
      hash: 'USER:123',
      phones: ['99114999'],
      status: null 
    }, function (err, data) {
      if (err) throw (err);
      console.log(data);
    }, {
      phones: 'ADD'
    });
adrianpraja commented 9 years ago

Hi

I know about this limitation, I'll get back with a solution, however may not be a third parameter.

Thanks

luizstacio commented 9 years ago

Ok no problem, I make this for resolve my problem but I understand, not be better solution for this project.

Maybe something:

DynamoDB
    .table(TABLE)
    .actions({
       phones: 'ADD'
     })
    .insert_or_update({
      hash: 'USER:123',
      phones: ['99114999'],
      status: null 
    }, function (err, data) {
      if (err) throw (err);
      console.log(data);
    })
adrianpraja commented 9 years ago

I'm doing some tests now, will be publishing new npm version soon

what I'm testing now and hopefully makes sense, looks like this

DynamoDB
     .table(TABLE)
     .insert_or_update({
          hash: 'USER:123',
          unneded_attribute: DynamoDB.delete(),
          phones: DynamoDB.push('99114999'),
          profile_views: DynamoDB.increment(1),
          status: null
     }, function( err,data ) {
     })
luizstacio commented 9 years ago

This solution is pretty good, only one topic, I think "DynamoDB.add" make more sense. because the action in "DynamoDB.push or DynamoDB. increment" is the same.

adrianpraja commented 9 years ago

It's a bit tricky as having only add() will need to know if it operates on an array or number because they translate into different code

Also an array should support adding multiple values like .add( 5, 'aaa', {}, [] )

Same with delete() where we need to know if we need to delete the entire arrtibute or just some values from the array

luizstacio commented 9 years ago

Ok, I understand your suggestion now. =D Thanks for your attention in my necessity and congrats for this project.

adrianpraja commented 9 years ago

Updated, you can now use DynamoDB.add(number) to increment an attribute, DynamoDB.add([array]) to add elements to an attribute of list type DynamoDB.del() to remove attribute from an item

documentation updated aswell