@aws/dynamodb-auto-marshaller

中文文档
0.7.1发布于 2018/07/21Apache-2.0TypeScriptCommonJS近 30 天下载 31.1万

A data marshaller that converts JavaScript types into Amazon DynamoDB AttributeValues

原始使用文档
文档目录5 节
  1. Getting started
  2. Caveats
  3. Big number support
  4. Empty value support
  5. Symbols and functions

Amazon DynamoDB Automarshaller

Apache 2 License

This library provides a Marshaller class that converts native JavaScript values to DynamoDB AttributeValues and back again. It's designed to work with ES6 features like sets, maps, and iterables, and can be configured to support data types only supported by JavaScript (such as empty binary buffers) or by Amazon DynamoDB (such as numbers of arbitrary size) with minimal tradeoffs.

Getting started

To use the Marshaller to convert a JavaScript object to the data type expected by Amazon DynamoDB, simply create an instance of the marshaller and call marshallItem:

import {BinarySet, Marshaller} from '@aws/dynamodb-auto-marshaller';

const marshaller = new Marshaller();
const original = {
    string: 'a string',
    number: 1234,
    list: [
        'a',
        'list',
        'of',
        'values',
    ],
    buffer: Buffer.from([0xde, 0xad, 0xbe, 0xef]),
    setOfBuffers: new BinarySet([
        Uint8Array.from([0xde, 0xad]),
        Uint8Array.from([0xbe, 0xef]),
        Uint8Array.from([0xfa, 0xce]),
    ] as Iterable<ArrayBufferView>),
    stringSet: new Set<string>([
        'foo',
        'bar',
        'baz',
    ]),
    any: {
        level: {
            of: {
                nesting: {
                    is: {
                        supported: true
                    }
                }
            }
        }
    },
}

// create a variable ready to be used with DynamoDB's low-level API 
const marshalled = marshaller.marshallItem(original);

// the output of `.marshallItem` can be converted back to a JavaScript type with
// `.unmarshallItem`
const unmarshalled = marshaller.unmarshallItem(original);

// With a few caveats (listed below), the unmarshalled value should have the
// same structure and data as the original value.
deepEqual(original, unmarshalled); // true

Values may be converted to and from AttributeValue objects with .marshallValue and .unmarshallValue directly:

import {Marshaller} from '@aws/dynamodb-auto-marshaller';

const marshaller = new Marshaller();
const marshalled = marshaller.marshallValue('string'); // returns {S: 'string'}
const unmarshalled = marshaller.unmarshallValue(marshalled); // returns 'string'

Caveats

There are a few categories of values that cannot be seamlessly converted between JavaScript and DynamoDB, such as big numbers, empty values, and mixed-type sets.

Big number support

By default, the marshaller will unmarshall numeric values returned by DynamoDB into instances of NumberValue rather than into native JavaScript numbers. Numbers in DynamoDB may have up to 38 digits of precision, which exceeds the precision available in a JavaScript number. NumberValue instances therefore store the value returned by DynamoDB as a string and will only coerce the value into a JavaScript number when the instance appears in an arithmetic expression, when it is passed to JSON.stringify, or when its valueOf method is called.

Similarly, numeric sets are returned as NumberValueSet instances that are compatible with both numbers and NumberValues.

To disable this behavior, pass a configuration options argument to the Marshaller constructor with unwrapNumbers set to true:

import {Marshaller} from '@aws/dynamodb-auto-marshaller';

const marshaller = new Marshaller({unwrapNumbers: true});

Empty value support

DynamoDB's data model requires that strings, sets, and binary attributes have lengths greater than zero, whereas JavaScript has no such requirement. By default, the marshaller will not attempt to alter empty values, as the marshaller would not be able to disambiguate any sigil value used from a field that was meant to legitimately contain that value.

The marshaller offers two opt-in options for handling empty values, both of which are controlled using the onEmpty configuration option.

Settting onEmpty to 'nullify' will direct the marshaller to convert empty values to null attribute values ({NULL: true}) and persist them to DynamoDB. This allows consumers of the item to know that an empty value was saved, though it will be slightly altered. When fetched from DynamoDB, the value will be unmarshalled as null:

import {Marshaller} from '@aws/dynamodb-auto-marshaller';

const marshaller = new Marshaller({onEmpty: 'nullify'});
const marshalled = marshaller.marshallValue(''); // returns {NULL: true}
const unmarshalled = marshaller.unmarshallValue(marshalled); // returns null

Setting onEmpty to 'omit' will direct the marshaller to remove empty values from the serialized item:

import {Marshaller} from '@aws/dynamodb-auto-marshaller';

const marshaller = new Marshaller({onEmpty: 'omit'});
const marshalled = marshaller.marshallValue(''); // returns undefined
const unmarshalled = marshaller.unmarshallValue(marshalled); // returns undefined

Symbols and functions

By default, the marshaller will throw an error when it encounters a symbol or function. You can direct the marshaller to instead omit such values from its output by setting the onInvalid configuration option to 'omit':

import {Marshaller} from '@aws/dynamodb-auto-marshaller';

const marshaller = new Marshaller({onInvalid: 'omit'});
const marshalled = marshaller.marshallValue(Symbol.iterator); // returns undefined
const unmarshalled = marshaller.unmarshallValue(marshalled); // returns undefined

版本与发布记录

@aws/dynamodb-auto-marshaller 共发布 11 个版本,首次发布于 2017/11/21;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.7.1latest2018/07/218 年前
0.7.02018/07/118 年前
0.6.12018/06/298 年前
0.6.02018/06/178 年前
0.5.02018/05/178 年前
0.4.12018/04/258 年前

关于 @aws/dynamodb-auto-marshaller 的常见问题

@aws/dynamodb-auto-marshaller 是什么?

A data marshaller that converts JavaScript types into Amazon DynamoDB AttributeValues

如何安装 @aws/dynamodb-auto-marshaller?

在项目目录运行 npm install @aws/dynamodb-auto-marshaller,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:aws-sdk(^2.7.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@aws/dynamodb-auto-marshaller 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@aws/dynamodb-auto-marshaller 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@aws/dynamodb-auto-marshaller 可以商用吗?

@aws/dynamodb-auto-marshaller 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@aws/dynamodb-auto-marshaller 的下载量有多少?

根据 npm 官方下载统计,@aws/dynamodb-auto-marshaller 在 2026/10/08 – 2026/11/06 这 30 天内被下载 311,455 次,日均约 10,382 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@aws/dynamodb-auto-marshaller 还在维护吗?

最新版本 0.7.1 发布于 2018/07/21(8 年前);至今共发布 11 个版本,其中过去 12 个月发布了 0 个;首次发布于 2017/11/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。