Skip to content

AWS Dynamo data serialization and deserialization for TypeScript

License

Notifications You must be signed in to change notification settings

rickosborne/dynamo-ts-adapter

Repository files navigation

DynamoDB TypeScript Adapter

This package provides very basic serialization and deserialization between DynamoDB types and TS/JS classes. It works for me, but no attempt has been made to productionize, optimize, verify, validate, or in any other way vet the viability of this code.

Author: Rick Osborne

Home: npm:dynamo-ts-adapter

Usage

Presuming some interface:

interface Widget {
  name: string;
  price: number;
  sale: boolean;
  description?: string;
  notBefore?: Date;
}

Create a parser:

const parser = new DynamoParser<Widget>('Widget')
    .requiredString('name')
    .requiredFloat('price')
    .requiredBool('sale')
    .optionalString('description')
    .optionalDate('notBefore');

From that parser you can get a deserializer that can read DynamoDB AttributeMaps:

const reader = parser.deserializer();
const widget = reader.deserialize(attributeMap);  // Widget

Similarly, you can create AttributeMaps from your value objects:

const writer = parser.serializer();
const map = writer.serialize(widget);  // DynamoDB.AttributeMap

That's pretty much it.

Caution

There's some type chicanery to do compile-time checking of parser coverage of an interface. If you get an inferred parser type of DynamoParser<void> you've either got some mismatch between type and/or required/optional for a field, or you haven't covered all fields in your interface. If you have an interface that can't strictly be matched, you'll need to as typecast your parser to tell the compiler to ignore it.

About

AWS Dynamo data serialization and deserialization for TypeScript

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published