evinrowsOP•

Just wanted to share this library I wrote. It's my first NPM package. It enables users to provide dynamo expressions with inline attribute names/values, so instead of writing:

{ // dynamodb document api
  UpdateExpression: 'set path.to.#identifier = :new_count',
  ExpressionAttributeNames: {
      '#identifier': 'count'
  },
  ExpressionAttributeValues: {
      ':new_count': 1
  }
}

You can instead write:

{ // slick api
  UpdateExpression: ["set path.to.", n('count'), " = ", v(1)],
}

I really love using TypeScript. Some of this code would've been really hard to get right without types. Since my library handles generating ExpressionAttributeNames and ExpressionAttributeValues for you, I was able to just create new types where I omit those properties and then I replaced the {Condition, Update, etc.}Expression types with my own type (the new input).

I welcome feedback!

2
NewLlama•

We have problems with DynamoDB's obtuse API as well. Our solution uses tagged template literals, I recommend taking a look for your project!

The code we write looks like this: { count: expr`if_not_exists(count, ${0}) + ${1}` },

I recommend giving them a shot! https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates

The library is too wrapped up in internal logic to opensource at this time but the implementation is simple. Here's the types for expr to get you started: export function expr(strings: TemplateStringsArray, ...placeholders: (number | string)[]): ... {

We use a regexp to generate ExpressionAttributeNames, it just turns any instances of #identifier into an expression name of the same content without the first character.

2
RandomGeordie•

You should check out something like dynamodb-onetable or dynamodb-toolbox

1