dynamo-easy
  • Introduction
  • Browser vs. Node usage
  • Get Started
    • Installation
    • Jump into code
  • API Doc
    • Config
      • Dynamo-Easy Config
      • AWS SDK Config
    • Model
      • Decorators
      • Mapping Concept
      • CustomMapper
    • Dynamo Store
      • Model Requests
    • Multi-Model Requests
    • Expressions (conditions, update)
Powered by GitBook
On this page

Was this helpful?

  1. API Doc
  2. Model

CustomMapper

With custom mappers you're able to define how the JS values are mapped (toDb) and how the DynamoDB attributes are parsed (fromDb).

PreviousMapping ConceptNextDynamo Store

Last updated 6 years ago

Was this helpful?

Scenarios when you need a custom mapper:

  • Using objects as partition key or sort key (since DynamoDB only supports [N]umber | [S]tring | [B]inary for such)

  • Working with class instances (non-primitives) instead of plain javascript objects (e.g. Dates)

  • Storing objects in a DynamoDB set (only N|S|B sets are possible)

A mapper for date objects which would be stored as [N]umbers could look like this:

date-to-number.mapper.ts
import { MapperForType, NumberAttribute } from '@shiftcoders/dynamo-easy'

export const dateToNumberMapper: MapperForType<Date, NumberAttribute> = {
  fromDb: attributeValue => new Date(parseInt(attributeValue.N, 10)),
  toDb: propertyValue => ({ N: `${propertyValue.getTime()}` }),
}
MapperForType | @shiftcoders/dynamo-easy
technical api doc