'Is there a less verbose way to write DynamoDB update params?

I am new to Dynamo and I created a simple todo API with Serverless Framework and TypeScript

To update an Item I have to do this huge params const

const params = {
  TableName: process.env.DYNAMO_TABLE_TODO,
  Key: {
    id: event.pathParameters.id,
  },
  ExpressionAttributeNames: {
    "#todo_text": "text",
  },
  ExpressionAttributeValues: {
    ":text": data.text,
    ":checked": data.checked,
    ":updatedAt": timestamp,
  },
  UpdateExpression: "SET #todo_text = :text, checked = :checked, updatedAt = :updatedAt",
  ReturnValues: "ALL_NEW",
}

// update the todo in the database
dynamoDb.update(params, (error, result) => {
  if (error) throw error

  callback(null, {
    statusCode: 200,
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(result.Attributes),
  })
})

Is there a more cleaner way to do this? With Mongoose I will simply do

Model.findOneAndUpdate({{ _id: id }, {
  text: data.text,
  checked: data.checked,
  updatedAt: timestamp
})

It's not only about code being ugly or not, if I want a property A2 be written to the database only if A1 is present in Mongoose I can just remove that property from the second object, while in Dynamo I would have to write a different ExpressionAttributeValues and UpdateExpression adding more probabilities of me or other dev making an error



Solution 1:[1]

Since you are referring to Mongoose which is an abstraction, it's only fair to answer the question by providing a similar abstraction.

There's a modelling tool inspired by mongoose which should provide what you're asking for. https://github.com/dynamoose/dynamoose

This will allow you to do stuff like this:

await User.update({"id": 1, "name": "Bob"}); // This code will set `name` to Bob for the user where `id` = 1

from https://dynamoosejs.com/guide/Model#modelupdatekey-updateobj-settings-callback

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 McKean