@aws-sdk/util-dynamodb provides essential utilities for marshalling and unmarshalling data between JavaScript objects and DynamoDB AttributeValue format. It offers both high-level convenience functions for complete records and low-level utilities for individual values, with comprehensive support for JavaScript data types including numbers, booleans, lists, maps, sets, and binary data.
npm install @aws-sdk/util-dynamodbimport { marshall, unmarshall, convertToAttr, convertToNative, NumberValueImpl as NumberValue } from "@aws-sdk/util-dynamodb";For CommonJS:
const { marshall, unmarshall, convertToAttr, convertToNative, NumberValueImpl: NumberValue } = require("@aws-sdk/util-dynamodb");import { DynamoDB } from "@aws-sdk/client-dynamodb";
import { marshall, unmarshall } from "@aws-sdk/util-dynamodb";
const client = new DynamoDB(clientParams);
// Marshall JavaScript object to DynamoDB format
const params = {
TableName: "Users",
Item: marshall({
id: "user-123",
name: "Alice Johnson",
age: 25,
active: true,
scores: [95, 87, 92],
metadata: { level: "premium", joinDate: "2024-01-15" },
tags: new Set(["vip", "active"]),
avatar: null,
}),
};
await client.putItem(params);
// Unmarshall DynamoDB record back to JavaScript
const getParams = {
TableName: "Users",
Key: marshall({ id: "user-123" }),
};
const { Item } = await client.getItem(getParams);
const user = unmarshall(Item); // Returns plain JavaScript objectThe package is organized around two main patterns:
marshall() and unmarshall() for complete record conversion between JavaScript objects and DynamoDB formatconvertToAttr() and convertToNative() for individual value conversion with fine-grained controlNumberValue class for numbers that exceed JavaScript's safe integer limitsHigh-level functions for converting complete JavaScript objects to and from DynamoDB record format. Handles nested objects, arrays, sets, and all DynamoDB-supported data types.
function marshall(data: unknown, options?: marshallOptions): AttributeValue | Record<string, AttributeValue> | AttributeValue[];
function unmarshall(
data: Record<string, AttributeValue> | AttributeValue,
options?: unmarshallOptions
): Record<string, NativeAttributeValue>;Low-level utilities for converting individual JavaScript values to DynamoDB AttributeValue format and vice versa. Provides fine-grained control over the conversion process.
function convertToAttr(data: NativeAttributeValue, options?: marshallOptions): AttributeValue;
function convertToNative(data: AttributeValue, options?: unmarshallOptions): NativeAttributeValue;Specialized handling for numbers that exceed JavaScript's Number.MAX_SAFE_INTEGER and MIN_SAFE_INTEGER limits, maintaining full precision as strings.
class NumberValue {
constructor(value: number | Number | BigInt | string | { N: string });
static from(value: number | Number | BigInt | string | { N: string }): NumberValue;
toAttributeValue(): { N: string };
toBigInt(): bigint;
toString(): string;
valueOf(): string;
readonly value: string;
}interface marshallOptions {
convertEmptyValues?: boolean;
removeUndefinedValues?: boolean;
convertClassInstanceToMap?: boolean;
convertTopLevelContainer?: boolean;
allowImpreciseNumbers?: boolean;
}
interface unmarshallOptions {
wrapNumbers?: boolean | ((value: string) => number | bigint | NumberValue | any);
convertWithoutMapWrapper?: boolean;
}
type NativeAttributeValue =
| NativeScalarAttributeValue
| { [key: string]: NativeAttributeValue }
| NativeAttributeValue[]
| Set<number | bigint | NumberValue | string | NativeAttributeBinary | undefined>
| InstanceType<{ new (...args: any[]): any }>;
type NativeScalarAttributeValue =
| null
| undefined
| boolean
| number
| NumberValue
| bigint
| NativeAttributeBinary
| string;
type NativeAttributeBinary =
| ArrayBuffer
| Blob
| Buffer
| DataView
| File
| Int8Array
| Uint8Array
| Uint8ClampedArray
| Int16Array
| Uint16Array
| Int32Array
| Uint32Array
| Float32Array
| Float64Array
| BigInt64Array
| BigUint64Array;