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

tynamodb

v0.3.0

Published

An abstracted DynamoDB document client and expression dsl.

Downloads

10

Readme

TynamoDB

An abstracted DynamoDB document client and expression dsl.

Getting Started

This package is based on the AWS SDK for JavaScript v3. For more information about AWS SDK, please check the official documentation.

Prerequisites

The following versions of Node.js and TypeScript are required:

  • Node.js 20 or higher
  • TypeScript 4.7 or higher

This package is pure ESM, and you must configure your project to use the ESM package.

Installation

npm install tynamodb @aws-sdk/client-dynamodb @aws-sdk/lib-dynamodb

Usage

First you need to create a client.

import { TynamoDB } from 'tynamodb';

const tynamodb: TynamoDB = new TynamoDB();

Instead of using the default configuration, you can configure the client yourself.

import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { DynamoDBDocument } from '@aws-sdk/lib-dynamodb';
import { TynamoDB } from 'tynamodb';

const client: DynamoDBClient = new DynamoDBClient({ ... });
const documentClient: DynamoDBDocument = DynamoDBDocument.from(client, { ... });
const tynamodb: TynamoDB = new TynamoDB(documentClient);

You can call operations with expression dsl.

import { attribute, value } from 'tynamodb';

await tynamodb.put({
    tableName: 'example',
    item: {
        key: 'value',
        foo: 'bar',
    },
    conditionExpression: attribute('key').notExists(),
});

Expression DSL

Expression dsl makes it easy to write expressions and handle placeholders easily.

import { and, attribute, update, value } from 'tynamodb';

await tynamodb.update({
    tableName: 'example',
    key: {
        key: 'value',
    },
    conditionExpression: and(
        attribute('foo').equal(value('bar')),
        attribute('foo').notEqual(value('baz')),
    ),
    updateExpression: update(
        attribute('foo').set(value('baz')),
    ),
});

Duplicate expression attribute names and values ​​are automatically optimized as follows:

{
    ConditionExpression: '#0 = :0 AND #0 <> :1',
    UpdateExpression: 'SET #0 = :1',
    ExpressionAttributeNames: {
        '#0': 'foo',
    },
    ExpressionAttributeValues: {
        ':0': 'bar',
        ':1': 'baz',
    },
}

If you are not using the TynamoDB client, you must evaluate the expression yourself, as follows:

import { ExpressionAttributeNames, ExpressionAttributeValues, and, attribute, update, value } from 'tynamodb';

const conditionExpression: Expression = and(
    attribute('foo').equal(value('bar')),
    attribute('foo').notEqual(value('baz')),
);
const updateExpression: Expression = update(
    attribute('foo').set(value('baz')),
);

const names: ExpressionAttributeNames = new ExpressionAttributeNames();
const values: ExpressionAttributeValues = new ExpressionAttributeValues();

expect(conditionExpression.evaluate(names, values)).toBe('#0 = :0 AND #0 <> :1');
expect(updateExpression.evaluate(names, values)).toBe('SET #0 = :1');

expect(names.serialize()).toStrictEqual({
    '#0': 'foo',
});
expect(values.serialize()).toStrictEqual({
    ':0': 'bar',
    ':1': 'baz',
});

Expression dsl prevents invalid expressions from being written. For example, the following code fails to compile:

import { attribute, update, value } from 'tynamodb';

update(
    attribute('foo').equal(value('bar')),
);

Nested attribute expressions and attribute expressions containing special characters can be created as follows:

attribute('a', 'b', 0, 'c', 1, 2, 'd.e', '34');

Created attribute expression are evaluated as follows:

{
    Expression: '#0.#1[0].#2[1][2].#3.#4',
    ExpressionAttributeNames: {
        '#0': 'a',
        '#1': 'b',
        '#2': 'c',
        '#3': 'd.e',
        '#4': '34',
    },
}

License

Distributed under the MIT License. See the LICENSE file for more details.