Search code examples
amazon-web-serviceslistaws-lambdaamazon-dynamodb

DynamoDB how to limit UpdateExpression list_append to a maximum array size


Is there a neat trick to update a list field of a DynamoDB document by appending and also culling the first entry if the array will become over a certain size?

This is the update parameters I am using currently:

            let params = {
                TableName: process.env.TABLE_NAME,
                Key: {
                    "MACAddress": String(mac)
                },
                UpdateExpression: 'set #evnts = list_append(#evnts, :es)',
                ExpressionAttributeNames: {
                    "#evnts": 'Events'
                },
                ExpressionAttributeValues: {
                    ":es": [eventData]
                },
                ReturnValues: "UPDATED_NEW"
            };

I have gone through the UpdateExpression documentation from AWS and there does not appear to be a helper function or sample relevant to this use case


Solution

  • You can pull the item to the client, modify it there, then put it back. You can use OCC (optimistic concurrency control) to ensure no other modifications happened between your get and put. With OCC you keep a version or other identifier that changes on each write and condition the write that the value hasn't changed since it was read.

    That's the only neat trick. :)