8000 GitHub - tgandrews/omanyd at v0.1.4
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

tgandrews/omanyd

Repository files navigation

Omanyd

A simple but production ready dynamodb mapper.

Coverage Status

Features

  • Simplified data modeling and mapping to DynamoDB types
  • Data validation using Joi
  • Autogenerating IDs
  • Complete typescript typings

Missing features

  • Parallel scans
  • Paging
  • Complex querying
  • Number and binary sets
  • Boolean types
  • Date types
  • Lists

Installation

npm: npm install omanyd yarn: yarn add omanyd

Getting Started

Set the AWS environment variables before running the program

AWS_REGION="REGION" \
AWS_ACCESS_KEY_ID="ACCESS KEY ID" \
AWS_SECRET_ACCESS_KEY="SECRET ACCESS KEY" \
node app.js

These will already by defined for you if you are running in ec2 or lambda.

For running locally we recommend using the official dynamodb docker container and then providing an additional environment variable to override the dynamodb url. DYNAMODB_URL=http://localhost:8000

Define a Model

Models are defined through define. You provide the table name, schema and hashKey definition.

import omanyd from "omanyd";
import Joi from "joi";

interface Tweet {
  id: string;
  content: string;
}
const TweetStore = omanyd.define<Tweet>("Tweet", {
  hashKey: "TweetID",
  schema: {
    id: omanyd.types.id(),
    content: Joi.string(),
  },
});

Create tables (for testing)

You can create tables for use locally during tests but should be managing this with a proper tool IaC for production.

This expects all stores defined using the define method above before use. It will skip creating a table if it already exists so this cannot be used for modifying a table definition.

import { createTables } from "omanyd";

await createTables();

Delete Tables (for testing)

You can delete tables for use locally during tests but should be managing this with a proper tool IaC for production.

This expects all stores defined using the define method above before use. It then clears all saved definitions so they can be redefined.

import { deleteTables } from "omanyd";

await deleteTables();

Creating

Once you have defined your store you can create models from it and unless you provide an id then one will be created for you automatically as the omanyd.types.id() was used in the definition above

const tweet = TweetStore.create({ content: "My first tweet" });
console.log(tweet);
/*
 * { id: "958f2b51-774a-436a-951e-9834de3fe559", content: "My first tweet"  }
 */

Reading one - getting by hash key

Now that we have some data in the store we can now read it. The quickest way is reading directly by the hash key.

const readTweet = TweetStore.getByHashKey(
  "958f2b51-774a-436a-951e-9834de3fe559"
);
console.log(readTweet);
/*
 * { id: "958f2b51-774a-436a-951e-9834de3fe559", content: "My first tweet"  }
 */

Reading many - scanning

If we want all of the items in the store we can use a scan. DynamoDB scans come with some interesting caveats.

await Promise.all([
  TweetStore.create({ content: "My second tweet" }),
  TweetStore.create({ content: "My third tweet" }),
  TweetStore.create({ content: "My fourth tweet" }),
]);

const tweets = await TweetStore.scan();

console.log(tweets);
/* [
 *   { id: "958f2b51-774a-436a-951e-9834de3fe559", content: "My first tweet"  },
 *   { id: "aa6ea347-e3d3-4c73-8960-709fa47e3a4c", content: "My second tweet"  },
 *   { id: "9cd6b18a-eafd-49c2-8f0f-d3bf8e75c26e", content: "My third tweet"  },
 *   { id: "fc446fcd-d65a-4ae2-ba9f-6bd94aae8705", content: "My fourth tweet"  }
 * ]
 */

History

Omanyd was originally inspired by dynamodb and dynamoose

Support

Omanyd is provided as-is, free of charge. For support, you have a few choices:

0