JSPM

dynamoo

0.1.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 281
  • Score
    100M100P100Q89214F
  • License MIT

Serialize and parse DynamoDB items

Package Exports

  • dynamoo

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (dynamoo) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

_______
< dynamoo >
 -------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

dynamoo

npm version npm downloads Build Status Coverage Status

Serialize and parse DynamoDB items.

API

serialize

Serializing data to store in DynamoDB is hard, but dynamoo makes it easier. Suppose you have this item you want to putItem to DynamoDB:

const item = {
  bool: true,
  func: Function.prototype,
  list: ['a', 1, false, null, { key: 'val' }],
  map: { key: 'val', nested: ['list'] },
  null: null,
  num: 42,
  set: new Set(['a', 'a', 'b']),
  string: 'strung',
  undef: undefined
}

Just serialize that data before sending, like this:

const AWS = require('aws-sdk')
const { serialize } = require('dynamoo')

const dynamo = new AWS.DynamoDB()

dynamo.putItem({ Item: serialize(item), TableName }, console.log)

The serialized Item that gets sent looks like this:

const Item = {
  bool: { BOOL: true },
  list: {
    L: [
      { S: 'a' },
      { N: '1' },
      { BOOL: false },
      { NULL: true },
      { M: { key: { S: 'val' } } }
    ]
  },
  map: {
    M: {
      key: { S: 'val' },
      nested: { L: [{ S: 'list' }] }
    }
  },
  null: { NULL: true },
  num: { N: '42' },
  set: { SS: ['a', 'b'] },
  string: { S: 'strung' }
}

Wow! It's like magic! Notice that any Function or undefined attributes are ignored, since the former doesn't serialize, and the latter technically doesn't exist.

parse

But wait... what about querying DynamoDB? The data.Items that are found need to be parsed. Got you covered on that as well:

const { parse } = require('dynamoo')

dynamo.query(params, (err, data) =>
  console.log(data.Items.map(parse))
)

Notice that parse accepts a single Item. So for query you'll need to .map(), but for getItem, you can use it like this:

dynamo.getItem({ Key, TableName }, (err, data) =>
  console.log(parse(data.Item))
)

The careful observer will notice that - with the exception of Function and undefined attributes - the serialize and parse functions are isomorphic! 😍

const { expect } = require('chai')

expect(parse(serialize(item))).to.eql(item)

Caveat emptor

By design, dynamoo only supports the following data types for attributes:

  • Array
  • Boolean
  • null
  • Number
  • Object
  • Set (of strings)
  • String

If you want to use any other fancier types, such as Map, or Set of numbers, etc., then you may need to do some additional work. Or file an issue. 😉