@aws/dynamodb-data-mapper-annotations

中文文档
0.7.3发布于 2018/08/16Apache-2.0TypeScriptCommonJS近 30 天下载 7.7万

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

原始使用文档
文档目录11 节
  1. Getting started
  2. Supported Annotations
  3. attribute
  4. autoGeneratedHashKey
  5. hashKey
  6. rangeKey
  7. table
  8. versionAttribute
  9. Caveats
  10. Reliance on experimental TypeScript features
  11. Lack of type information in generics

Amazon DynamoDB DataMapper Annotations

Apache 2 License

This library provides annotations to allow easy integration of domain classes with the DataMapper defined in @aws/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 '@aws/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 '@aws/dynamodb-data-mapper-annotations';
import {embed} from '@aws/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 @aws/dynamodb-auto-marshaller package).

版本与发布记录

@aws/dynamodb-data-mapper-annotations 共发布 18 个版本,首次发布于 2017/11/21;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.7.3latest2018/08/168 年前
0.7.22018/07/268 年前
0.7.12018/07/218 年前
0.7.02018/07/118 年前
0.6.12018/06/298 年前
0.6.02018/06/178 年前

关于 @aws/dynamodb-data-mapper-annotations 的常见问题

@aws/dynamodb-data-mapper-annotations 是什么?

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

如何安装 @aws/dynamodb-data-mapper-annotations?

在项目目录运行 npm install @aws/dynamodb-data-mapper-annotations,或使用 pnpm add / yarn add / bun add。

@aws/dynamodb-data-mapper-annotations 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@aws/dynamodb-data-mapper-annotations 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@aws/dynamodb-data-mapper-annotations 可以商用吗?

@aws/dynamodb-data-mapper-annotations 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@aws/dynamodb-data-mapper-annotations 的下载量有多少?

根据 npm 官方下载统计,@aws/dynamodb-data-mapper-annotations 在 2026/10/08 – 2026/11/06 这 30 天内被下载 76,764 次,日均约 2,559 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@aws/dynamodb-data-mapper-annotations 还在维护吗?

最新版本 0.7.3 发布于 2018/08/16(8 年前);至今共发布 18 个版本,其中过去 12 个月发布了 0 个;首次发布于 2017/11/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。