DynamoDB Data Type Marshaller and Update Expression Builder

4.0.1 · active · verified Sun Apr 19

dynamodb-data-types is a JavaScript utility library designed to simplify the interaction with Amazon DynamoDB's native data type representations. It provides functions to convert standard JavaScript objects into DynamoDB's attribute value format (marshalling) and vice-versa (unmarshalling), handling complex nested structures and various primitive types. The library's current stable version is 4.0.1. Its release cadence is moderate, with major versions introducing significant features like the `UpdateExpression` builder in v4.0.0. A key differentiator is its focus on accurately handling DynamoDB's specific type mappings (e.g., numbers as strings `{N: '1'}`, sets, lists, maps, booleans, and nulls), alongside its ability to construct complex `UpdateExpression` payloads, including automatic handling of DynamoDB reserved keywords and proper `ExpressionAttributeValues` and `ExpressionAttributeNames` generation. This functionality reduces boilerplate code and common errors when performing update operations.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates marshalling/unmarshalling JavaScript objects to DynamoDB format and building a complex `UpdateExpression` with `SET`, `ADD`, `REMOVE`, and `DELETE` clauses, including handling nested attributes and reserved keywords.

import { AttributeValue, updateExpr } from 'dynamodb-data-types';

async function demonstrateDynamoDBUtils() {
  const originalData = {
    id: 'user#123', 
    username: 'jsmith',
    email: 'jsmith@example.com',
    age: 30,
    preferences: { 
      theme: 'dark', 
      notifications: true 
    },
    tags: ['active', 'premium'],
    lastLogin: null
  };

  // Marshalling JavaScript object to DynamoDB format
  const marshalledData = AttributeValue.wrap(originalData);
  console.log('Marshalled Data:', JSON.stringify(marshalledData, null, 2));
  // Expected: { id: {S: 'user#123'}, username: {S: 'jsmith'}, ... }

  // Unmarshalling DynamoDB format back to JavaScript object
  const unmarshalledData = AttributeValue.unwrap(marshalledData);
  console.log('Unmarshalled Data:', JSON.stringify(unmarshalledData, null, 2));
  // Expected: { id: 'user#123', username: 'jsmith', ... }

  // Building an UpdateExpression for a DynamoDB UpdateItem API call
  const updatePayload = updateExpr()
    .set({ 
      email: 'john.smith@example.com', 
      'preferences.theme': 'light' // Nested attributes supported
    })
    .add({ age: 1 }) // Increment age by 1
    .remove('lastLogin')
    .delete({ tags: ['active'] }) // Remove 'active' from the tags set/list
    .expr(); // Generate the final expression object

  console.log('Update Expression Payload:', JSON.stringify(updatePayload, null, 2));
  /* Expected: 
    {
      UpdateExpression: "SET email = :a, #A.#B = :b ADD age :c REMOVE lastLogin DELETE tags :d",
      ExpressionAttributeValues: { ":a": {"S":"john.smith@example.com"}, ":b": {"S":"light"}, ":c": {"N":"1"}, ":d": {"SS":["active"]} },
      ExpressionAttributeNames: { "#A":"preferences", "#B":"theme" }
    } 
  */
}

demonstrateDynamoDBUtils();

view raw JSON →