dynamodb-niceupdate

Creates DynamoDB param object for an update operation, using UpdateExpression

Usage no npm install needed!

<script type="module">
  import dynamodbNiceupdate from 'https://cdn.skypack.dev/dynamodb-niceupdate';
</script>

README

dynamodb-niceupdate

Creates DynamoDB's DocumentClient.update() param object that performs an update operation, using UpdateExpression. Any empty value will be recursively removed, to make DynamoDB happy.


npm Build Status Coverage Status

Usage

import { createFieldsUpdateParams } from 'dynamodb-niceupdate'

const params = createFieldsUpdateParams({
  tableName: 'DynamoDB_Table_name',
  keySchema: { Id: '123' },
  item: {
    a: 'b',
    b: { foo: ['bar'] },
    c: 3,
    d: [], // this empty list is removed
    e: {}, // this empty set is removed
    f: [{ x: '' }], // this empty object is removed
  }
})

assert.deepEqual(params, {
  TableName: 'DynamoDB_Table_name',
  ReturnValues: 'ALL_NEW',
  Key: { Id: '123' },
  ExpressionAttributeNames: {
    '#updatedOn': 'UpdatedOn',
    '#field0': `a`,
    '#field1': `b`,
    '#field2': `c`,
    '#field3': `d`,
    '#field4': `e`,
    '#field5': `f`
  },
  UpdateExpression: 'REMOVE #field3, #field4, #field5 SET #updatedOn=:now, #field0=:value0, #field1=:value1, #field2=:value2',
  ExpressionAttributeValues: {
    ':now': `${timestamp}`,
    ':value0': 'b',
    ':value1': { foo: ['bar'] },
    ':value2': 3
  }
})

const doc = new DynamoDB.DocumentClient({})
doc.update(params, callback)

Checkout the tests in src/index.spec.js.