npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tuplo/dynoexpr

v3.5.1

Published

Expression builder for AWS.DynamoDB.DocumentClient

Downloads

13,866

Readme

Introduction

Converts a plain object to a DynamoDB expression with all variables and names replaced with safe placeholders. It supports Condition, KeyCondition, Filter, Projection and Update expressions. The resulting expressions can then be used with AWS.DynamoDB.DocumentClient requests.

import dynoexpr from '@tuplo/dynoexpr';

const params = dynoexpr({
  KeyCondition: { id: '567' },
  Condition: { rating: '> 4.5' },
  Filter: { color: 'blue' },
  Projection: ['weight', 'size'],
});

/*
{
  KeyConditionExpression: '(#nca40fdf5 = :v8dcca6b2)',
  ExpressionAttributeValues: {
    ':v8dcca6b2': '567',
    ':vc95fafc8': 4.5,
    ':v792aabee': 'blue'
  },
  ConditionExpression: '(#n0f1c2905 > :vc95fafc8)',
  FilterExpression: '(#n2d334799 = :v792aabee)',
  ProjectionExpression: '#neb86488e,#n0367c420',
  ExpressionAttributeNames: {
    '#nca40fdf5': 'id',
    '#n0f1c2905': 'rating',
    '#n2d334799': 'color',
    '#neb86488e': 'weight',
    '#n0367c420': 'size'
  }
}
*/

Install

$ npm install @tuplo/dynoexpr

# or with yarn
$ yarn add @tuplo/dynoexpr

Usage

Passing parameters to DocumentClient

const docClient = new AWS.DynamoDB.DocumentClient();

const params = dynoexpr({
  KeyCondition: {
    HashKey: 'key',
    RangeKey: 'between 2015 and 2019',
  },
});

const results = await docClient
  .query({ TableName: 'table', ...params })
  .promise();

Using multiple expressions on the same field

You can use multiple expressions on the same field, by packing them into an array and assigning it to the key with the field's name.

const params = dynoexpr({
  Condition: {
    color: ['attribute_not_exists', 'yellow', 'blue'],
  },
  ConditionLogicalOperator: 'OR',
});

/*
{
  ConditionExpression: '(attribute_not_exists(#n2d334799)) OR (#n2d334799 = :v0d81c8cd) OR (#n2d334799 = :v792aabee)',
  ExpressionAttributeNames: {
    '#n2d334799': 'color'
  },
  ExpressionAttributeValues: {
    ':v0d81c8cd': 'yellow',
    ':v792aabee': 'blue'
  }
}
*/

Using functions

DynamoDB supports a number of functions to be evaluated when parsing expressions. You don't need to reference the path argument because that's identified by the object's key.

const params = dynoexpr({
  Condition: {
    docs: 'attribute_exists',
    brand: 'attribute_not_exists',
    extra: 'attribute_type(NULL)',
    color: 'begins_with dark',
    address: 'contains(Seattle)',
    description: 'size < 20',
  },
});

/*
{
  ConditionExpression: '(attribute_exists(#nd0a55266)) AND (attribute_not_exists(#n4e5f8507)) AND (attribute_type(#n4a177797,:v64b0a475)) AND (begins_with(#n2d334799,:v1fdc3f67)) AND (contains(#n3af77f77,:v26425a2a)) AND (size(#nb6c8f268) < :vde9019e3)',
  ExpressionAttributeNames: {
    '#nd0a55266': 'docs',
    '#n4e5f8507': 'brand',
    '#n4a177797': 'extra',
    '#n2d334799': 'color',
    '#n3af77f77': 'address',
    '#nb6c8f268': 'description'
  },
  ExpressionAttributeValues: {
    ':v64b0a475': 'NULL',
    ':v1fdc3f67': 'dark',
    ':v26425a2a': 'Seattle',
    ':vde9019e3': 20
  }
}
*/

Using multiple expressions on the same request

const params = dynoexpr({
  Update: { Sum: 'Sum + 20' },
  Condition: { Sum: `< 100` },
});

/*
{
  ConditionExpression: '(#n5af617ef < :va88c83b0)',
  ExpressionAttributeNames: {
    '#n5af617ef': 'Sum'
  },
  ExpressionAttributeValues: {
    ':va88c83b0': 100,
    ':vde9019e3': 20
  },
  UpdateExpression: 'SET #n5af617ef = #n5af617ef + :vde9019e3'
}
*/

Working with Sets

If a value is provided as a Set, it will be converted to DocumentClient.DynamoDbSet. But dynoexpr doesn't include DocumentClient so you need to provide it.

import { DocumentClient } from "aws-sdk/clients/dynamodb";

const params = dynoexpr({
  DocumentClient,
  Update: {
    Color: new Set(['Orange', 'Purple'])
  },
})

/*
{
  UpdateExpression: 'SET #n8979552b = :v3add0a80',
  ExpressionAttributeNames: {
    '#n8979552b': 'Color'
  },
  ExpressionAttributeValues: {
    ':v3add0a80': Set { wrapperName: 'Set', values: [Array], type: 'String' }
  }
}
*/

When using UpdateAdd or UpdateDelete, arrays are converted to DynamoDbSet

import { DocumentClient } from "aws-sdk/clients/dynamodb";

const params = dynoexpr({
  DocumentClient,
  UpdateAdd: {
    Color: ['Orange', 'Purple']
  }
})

/*
{
  UpdateExpression: 'ADD #ndc9f7295 :v3add0a80',
  ExpressionAttributeNames: { 
    '#ndc9f7295': 'Color'
  },
  ExpressionAttributeValues: {
    ':v3add0a80': Set { wrapperName: 'Set', values: [Array], type: 'String' }
  }
}
*/

Keep existing Expressions, AttributeNames and AttributeValues

const params = dynoexpr({
  Filter: { color: 'blue' },
  ProjectionExpression: '#year',
  ExpressionAttributeNames: {
    '#year': 'year',
  },
});

/*
{
  ProjectionExpression: '#year',
  ExpressionAttributeNames: {
    '#year': 'year',
    '#n2d334799': 'color'
  },
  FilterExpression: '(#n2d334799 = :v792aabee)',
  ExpressionAttributeValues: {
    ':v792aabee': 'blue'
  }
}
*/

Using object paths on expressions

You can provide a path to an attribute on a deep object, each node will be escaped.

const params = dynoexpr({
  Update: {
    'foo.bar.baz': 'foo.bar.baz + 1'
  }
});

/*
{
  ExpressionAttributeNames: {
    "#n22f4f0ae": "bar",
    "#n5f0025bb": "foo",
    "#n82504b33": "baz",
  },
  ExpressionAttributeValues: {
    ":vc823bd86": 1,
  },
  UpdateExpression:
    "SET #n5f0025bb.#n22f4f0ae.#n82504b33 = #n5f0025bb.#n22f4f0ae.#n82504b33 + :vc823bd86",
}
*/

If one of the nodes needs to escape some of its characters, use double quotes around it, like this:

const params = dynoexpr({
  Update: {
    'foo."bar-cuz".baz': 'foo."bar-cuz".baz + 1'
  }
});

Parsing atomic requests, only expressions will be replaced

You can pass the whole request parameters to dynoexpr - only the expression builders will be replaced.

const params = dynoexpr({
  TableName: 'Table',
  Key: { HashKey: 'key' },
  ReturnConsumedCapacity: 'TOTAL',
  KeyCondition: {
    color: 'begins_with dark',
  },
});

/*
{
  TableName: 'Table',
  Key: { HashKey: 'key' },
  ReturnConsumedCapacity: 'TOTAL',
  KeyConditionExpression: '(begins_with(#n2d334799,:v1fdc3f67))',
  ExpressionAttributeNames: {
    '#n2d334799': 'color'
  },
  ExpressionAttributeValues: {
    ':v1fdc3f67': 'dark'
  }
}
*/

Parsing Batch requests

const params = dynoexpr({
  RequestItems: {
    'Table-1': {
      Keys: [{ foo: 'bar' }],
      Projection: ['a', 'b'],
    },
  },
  ReturnConsumedCapacity: 'TOTAL',
});

/*
{
  "RequestItems":{
    "Table-1":{
      "Keys": [{"foo":"bar"}],
      "ProjectionExpression": "#na0f0d7ff,#ne4645342",
      "ExpressionAttributeNames":{
        "#na0f0d7ff": "a",
        "#ne4645342": "b"
      }
    }
  },
  "ReturnConsumedCapacity": "TOTAL"
}
*/

Parsing Transact requests

const params = dynoexpr({
  TransactItems: [{
    Get: {
      TableName: 'Table-1',
      Key: { id: 'foo' },
      Projection: ['a', 'b'],
    },
  }],
  ReturnConsumedCapacity: 'INDEXES',
});

/*
{
  "TransactItems": [
    { 
      "Get": {
        "TableName": "Table-1",
        "Key": { "id": "foo" },
        "ProjectionExpression": "#na0f0d7ff,#ne4645342",
        "ExpressionAttributeNames": {
          "#na0f0d7ff":"a",
          "#ne4645342":"b"
        }
      }
    }
  ],
  "ReturnConsumedCapacity": "INDEXES"
}
*/

Type the resulting parameters

The resulting object is compatible with all DocumentClient requests, but if you want to be type-safe, dynoexpr accepts a generic type to be applied to the return value.

const params = dynoexpr<AWS.DocumentClient.UpdateItemInput>({
  TableName: 'Table',
  Key: 1,
  UpdateSet: { color: 'pink' },
});

API

dynoexpr<T>(params)

params

Expression builder parameters

type DynamoDbPrimitive = string | number | boolean | object;
type DynamoDbValue =
  | DynamoDbPrimitive
  | DynamoDbPrimitive[]
  | Set<DynamoDbPrimitive>;

// all attributes are optional, depending on what expression(s) are to be built
{
  Condition: { [key: string]: DynamoDbValue },
  ConditionLogicalOperator: 'AND' | 'OR',

  KeyCondition: { [key: string]: DynamoDbValue },
  KeyConditionLogicalOperator: 'AND' | 'OR',

  FilterCondition: { [key: string]: DynamoDbValue },
  FilterLogicalOperator: 'AND' | 'OR',

  Projection: string[],

  Update: { [key: string]: DynamoDbValue },
  UpdateAction: 'SET' | 'ADD' | 'DELETE' | 'REMOVE',

  UpdateSet: { [key: string]: DynamoDbValue },
  UpdateAdd: { [key: string]: DynamoDbValue },
  UpdateDelete: { [key: string]: DynamoDbValue },
  UpdateRemove: { [key: string]: DynamoDbValue },
  
  DocumentClient: AWS.DynamoDB.DocumentClient
}

Return value

Parameters accepted by AWS.DynamoDB.DocumentClient

// all attributes are optional depending on the expression(s) being built
{
  ConditionExpression: string,

  KeyConditionExpression: string,

  FilterConditionExpression: string,

  ProjectionExpression: string,

  UpdateExpression: string,

  ExpressionAttributeNames: { [key: string]: string },
  ExpressionAttributeValues: { [key: string]: string },
}

License

MIT