DynamoDB ORM/lib

What is your experience/recommendation to handle DynamoDB queries? I started with the usual

params = {
      TableName: tableName,
      Item: {
        feedbackId,
        createdAt: moment().toISOString(),
        ...data,
        ...getHeadersUsefulData(event),
      }
    };

But there are several bad things about that. First of I haven’t typed anything, so now when I store null data it stores stuff like {NULL: true} in the db (wth?)

It displays true but that’s not the real stored value… Anyway.

I quickly looked around and found http://awspilot.github.io/dynamodb-oop/ which seems decent and much simpler to reason with. But any experienced feedback is welcome.


Edit: The way it displays in the AWS Console isn’t related to the ORM in any way, it’s just how AWS displays its data. And they display it rather bad, when {NULL: true} just shows true on the console, it’s misleading.

For JavaScript I’ve been using the AWS.DynamoDB.DocumentClient library. DynamoDB has a number of interesting quirks (like not being able to store an empty string). For null it stores {NULL: true} so it sounds like it’s working the way I would expect.

I started by using DocumentClient too, but then I had to do a PATCH endpoint with dynamic attributes, and the fancy way with the UpdateExpression was too restrictive to my taste. Not only I don’t like the format, but it didn’t look friendly with dynamic attributes:

params = {
      TableName: tableName,
      Key: {
        feedbackId,
      },
      // 'UpdateExpression' defines the attributes to be updated
      // 'ExpressionAttributeValues' defines the value in the update expression
      UpdateExpression: "SET rating = :rating, description = :description, updatedAt = :updatedAt",
      ExpressionAttributeValues: {
        ":rating": data.rating,
        ":description": data.description,
        ":updatedAt": moment().toISOString(),
      },
      ReturnValues: "ALL_NEW"
    };

This PATCH endpoint would fail when either rating or description wasn’t provided.

I started using http://awspilot.github.io/dynamodb-oop/ and I got this code now, which not only looks cleaner, but allows dynamic attributes too:

    const params = {
      updatedAt: moment().toISOString(),
      ...data,
    };

    const result = await FeedbackDBTable
      .where('feedbackId').eq(feedbackId)
      .update(params);
    callback(null, success(result));
1 Like

I feel your pain. My team just decided to write one since we couldn’t really fine one that works smoothly, and using this on production everyday. and I proudly recommend you to give it a try :slight_smile:

“interesting quirks” is a bit of an understatement… my work with it lately has been in Python, but a colleague here is using https://github.com/ryanfitz/vogels in her JS code… Looked pretty good.

1 Like

There’s also https://github.com/automategreen/dynamoose which we’re currently planning to refactor the documentation and make it a bit easier to read. =)

It’s pretty easy to use and very similar to Mongoose! ^^

1 Like