npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@aws/dynamodb-auto-marshaller

v0.7.1

Published

A data marshaller that converts JavaScript types into Amazon DynamoDB AttributeValues

Downloads

338,923

Readme

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