Git Product home page Git Product logo

dynamo's Introduction

best-dynamo-js

Why

DynamoDB is a great scalable DB, but it still has lots of frictions with regards to developer experience. This repo was created to be the best toolkit for addressing these frictions in js. It leverages the v3 @aws-sdk/client-dynamodb, as well as the @aws-sdk/lib-dynamodb enhancement library.

Goal

To create the best, most useful set of middlewares and utility commands to make your experience with dynamo in js better.

Usage

npm install best-dynamo-js

Middlewares

In order to use the middlewares, they simply need to be imported and added to the middlewareStack that is part of the dynamo client. For example:

import { DynamoDB } from '@aws-sdk/client-dynamodb'
import { DynamoDBDocument } from '@aws-sdk/lib-dynamodb'
import { commands, middlewares } from 'best-dynamo-js'

const client = DynamoDBDocument.from(new DynamoDB({ ...config }))

// add a middleware to the stack (without options the default is for the initialisation step)
client.middlewareStack.add(middlewares.attributeEnhancer)

Below is the current list of available middlewares:

  • attributeEnhancer

The most annoying thing about using dynamo is having to create aliases for attributes and values throughout your requests. This middleware automatically generates these aliases for you behind the scenes, allowing you to make far simpler, and readable requests. For example:

const response = await client.query({
  TableName: 'my-table',
  IndexName: 'my-index',
  KeyCondition: 'some_variable > 5 and some_text = "hello",
  ProjectionExpression: 'keyA, keyB, keyC'
})

This will automatically parse the values, and generate appropriate aliases behind the scenes, transforming the input into valid dynamo input.

Commands

A lot of the time when using dynamo you find yourself rewriting common utility functions for doing things that should probably be built in. While built in commands within dynamo can be called via client.command, these utility functions require client to usually be passed in as the first param. This it to maintain extensibility with this repo, and not step on dynamos toes. Below are the available extra commands:

  • queryAll(client, input)

This automatically manages ExclusiveStartKey pagination, and returns the full item set for a given query:

const items = await queryAll(client, {
  TableName: 'my-table',
  KeyCondition: 'test = "something"
})
  • scanAll(client, input)

This is similar to the queryAll function, but for scanning. It handles pagination behind the scenes and returns the full scanned set:

const items = await scanAll(client, {
  TableName: 'my-table'
})

Credits

This library was built @Wethrift

dynamo's People

Contributors

matt-way avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.