Stephen Last
Stephen Last

Reputation: 5781

AWS DynamoDB data with and/or without types?

I'm using the aws-sdk for NodeJS to interact with a DynamoDB table. This is my first look at DynamoDB.

When using a call like getItem() or updateItem(), the data structure includes types, like this:

{
  'a': { S: 'My string' }
}

Is there any way to pass and receive these objects without the types..? So...

{
  'a': 'My string'
}

Or, any helper functions already written that will convert objects to and from this format..?

const data = dbToObj({ 'a': { S: 'My string' } })
// { 'a': 'My string' }

So I could convert to it when populating call params, and convert from it when receiving data.

Trying to avoid accessing my data like:

const myData = data.Item.something.M.somethinElse.M.qwerty.S

I know I could write something myself, just wondering if anyone knows of functions/options already available that would do this. I couldn't find any.

Upvotes: 17

Views: 9920

Answers (5)

Guilherme
Guilherme

Reputation: 1021

Consider using libraries that simplify working with interfaces and types in Typescript, among other things. I created the npm package dynamo-repo and IMO it's the simplest and easiest one to work with.

This other answer has some explanation and examples: use-type-at-dynamodb-output

Upvotes: 0

wittwermic
wittwermic

Reputation: 198

We use dynamo-easy with typescript for our production applications. (directly from browser or inside Lambda functions)

It provides the mapping from JS to DynamoDB types but also some nice abstraction for the request API.

import { Model, PartitionKey, DynamoStore } from '@shiftcoders/dynamo-easy'

@Model()
export class Person {
  @PartitionKey()
  id: string
  name: string
  yearOfBirth: number
}

const personStore = new DynamoStore(Person)

personStore
  .scan()
  .whereAttribute('yearOfBirth').equals(1958)
  .exec()
  .then(res => console.log('ALL items with yearOfBirth == 1958', res))

full disclosure: I am one of the authors of the library

Upvotes: 1

F_SO_K
F_SO_K

Reputation: 14889

Dynamoose is another modelling tool that can abstract even more extraneous code away. The github is here. I think its built on top of the AWS documentclient, although I haven't done much digging.

Upvotes: 0

Stephen Last
Stephen Last

Reputation: 5781

Found this:

Exactly what I was looking for.

Install: npm i dynamodb-data-types

Provides wrap({ 'a': 'My string' }) and unwrap({ 'a': { S: 'My string' } }) methods for doing the conversion to and from plain objects.

UPDATE

I've now also found this: AWS.DynamoDB.DocumentClient, which is part of the aws-sdk.

The document client simplifies working with items in Amazon DynamoDB by abstracting away the notion of attribute values. This abstraction annotates native JavaScript types supplied as input parameters, as well as converts annotated response data to native JavaScript types.

UPDATE 2

This is being worked on by Amazon under their awslabs github page:

Amazon DynamoDB Automarshaller

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.

It's part of their DynamoDB DataMapper For JavaScript package.

Upvotes: 12

Kannaiyan
Kannaiyan

Reputation: 13055

Dynogels offer a cleaner way to handle without worrying about types. We use it in production and works without any issues.

https://github.com/clarkie/dynogels

Account.create({email: '[email protected]', name: 'Foo Bar', age: 21}, function (err, acc) {
  console.log('created account in DynamoDB', acc.get('email'));
});

It is a ODM (Object Data Mapper) for dynamodb.

Hope it helps.

Upvotes: 0

Related Questions