dynamodb-toolbox/dynamodb-toolbox

Attributes with defaults that have depend on unresolvable attribute should be ignored

Closed this issue · 0 comments

We noticed that entities that have defaults that depend on other attributes are incorrectly handled. Looking at the unit tests in src/__tests__/type-infering.unit.test.ts this was noticed before but never addressed.

The following entity:

    const ent = new Entity({
      name: 'Test',
      attributes: {
        pk: { type: 'string', partitionKey: true, default: 'pk' },
        sk: { type: 'string', sortKey: true, dependsOn: ['parent'],
          default: (data: { parent: string }) => `parent#${data.parent}`,
        },
        parent: { type: 'string'},
      },
      table: DefaultTable
    } as const)

When calling the updateParams method ent.updateParams({}) the result will look like below. Notice how the sortkey sk that depends on the attribute parent end up as 'parent#undefined':

   {
      TableName: 'test-table',
      Key: { pk: 'pk', sk: 'parent#undefined' },
      UpdateExpression: 'SET #_ct = if_not_exists(#_ct,:_ct), #_md = :_md, #_et = if_not_exists(#_et,:_et)',
      ExpressionAttributeNames: { '#_ct': '_ct', '#_md': '_md', '#_et': '_et' },
      ExpressionAttributeValues: {
        ':_ct': '2024-02-12T01:27:06.090Z',
        ':_md': '2024-02-12T01:27:06.090Z',
        ':_et': 'Test'
      }
    }