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

@awslabs-community-fork/dynamodb-data-mapper-annotations

v0.7.15

Published

Annotations providing easy integration between TypeScript domain objects and the @awslabs-community-fork/dynamodb-data-mapper library

Downloads

245

Readme

Amazon DynamoDB DataMapper Annotations

Apache 2 License

This library provides annotations to allow easy integration of domain classes with the DataMapper defined in @awslabs-community-fork/dynamodb-data-mapper. These annotations are provided in a separate NPM package because they rely on two experimental features of TypeScript (decorators and metadata decoration) and depend on the reflect-metadata package.

Getting started

To use the annotations, you will first need to enable two TypeScript compiler settings: experimentalDecorators and emitDecoratorMetadata. The former enables the use of annotations in your project, and the latter emits type information about declared property classes.

Next, start adding table annotations to the domain models that represent records in your DynamoDB tables and attribute annotations to the declared properties that map to attributes of those records:

import {
    attribute,
    autoGeneratedHashKey,
    rangeKey,
    table,
    versionAttribute,
} from '@awslabs-community-fork/dynamodb-data-mapper-annotations';
import uuidV4 = require('uuid/v4');

@table('my_table')
class MyDomainClass {
    @autoGeneratedHashKey()
    id: string;

    @rangeKey({defaultProvider: () => new Date()})
    createdAt: Date;

    @versionAttribute()
    version: number;

    @attribute()
    toggle?: boolean;

    @attribute({memberType: 'String'})
    tags?: Set<string>;
    
    // This property will not be saved to DynamoDB.
    notPersistedToDynamoDb: string;
}

Using these annotations will automatically define properties at the DynamoDbSchema and DynamoDbTable symbols on the class prototype, meaning that you can pass instances of the class directly to a DataMapper instance. No need to define a schema separately; your class's property type declarations are the schema.

To declare a class corresponding to a map attribute value in a DynamoDB record, simply omit the table annotation:

import {
    attribute, 
    hashKey,
    table,
} from '@awslabs-community-fork/dynamodb-data-mapper-annotations';
import {embed} from '@awslabs-community-fork/dynamodb-data-mapper';

class Comment {
    @attribute()
    author?: string;

    @attribute()
    postedAt?: Date;

    @attribute()
    text?: string;
}

@table('posts')
class BlogPost {
    @hashKey()
    id: string;

    @attribute()
    author?: string;

    @attribute()
    postedAt?: Date;

    @attribute()
    text?: string;
    
    @attribute({memberType: embed(Comment)})
    replies?: Array<Comment>
}

Supported Annotations

attribute

Marks a property as an attribute in the mapped DynamoDB record. The annotation will attempt to infer the correct DynamoDB type from the TypeScript type metadata, though no metadata will be available for any generic type parameters used.

attribute must be called as a function and accepts an optional argument containing a partial or complete field schema definition.

autoGeneratedHashKey

Designates the property as representing the record's hash key. If no key is defined, a V4 UUID will be used.

autoGeneratedHashKey must be called as a function and accepts an optional argument containing a partial or complete field schema definition.

hashKey

Designates the property as representing the record's hash (partition) key.

hashKey must be called as a function and accepts an optional argument containing a partial or complete field schema definition.

rangeKey

Designates the property as representing the record's range (sort) key.

rangeKey must be called as a function and accepts an optional argument containing a partial or complete field schema definition.

table

Designates a class as representing a table in DynamoDB.

table must be called as a function with that table name as the only argument.

versionAttribute

Designates the property as representing the record's version attribute. The data mapper will use optimistic locking by incrementing the version attribute during put and update operations and sending a condition expression with all put, update, and delete operations that will cause the operation to fail if the record in the database has a version number that does not match the one held by the client.

versionAttribute must be called as a function and accepts an optional argument containing a partial or complete field schema definition.

Caveats

Reliance on experimental TypeScript features

Because this package relies on experimental TypeScript features, it is subject to change and should be considered beta software. Decorators are currently on the ECMAScript standards track, but the accepted form may differ from that implemented by TypeScript, which could cause the public interface presented by this project to change.

Lack of type information in generics

Please note that TypeScript does not emit any metadata about the type parameters supplied to generic types, so Array<string>, [number, string], and MyClass[] are all exposed as Array via the emitted metadata. Without additional metadata, this annotation will treat all encountered arrays as collections of untyped data. You may supply either a members declaration or a memberType declaration to direct this annotation to treat a property as a tuple or typed list, respectively.

Member type declarations are required for maps and sets, though the member type will be automatically inferred if a property is declared as being of type BinarySet or NumberValueSet (from the @awslabs-community-fork/dynamodb-auto-marshaller package).