在TypeScript中,我应该使用哪种类型来处理DynamoDB流事件?

11

当我在 TypeScript 中接收 DynamoDB 流事件时,我看到以下模式:

Records: [
    {
      eventID: '78dfd1ba7a17adde3cbc987e5af92f91',
      eventName: 'INSERT',
      eventVersion: '1.1',
      eventSource: 'aws:dynamodb',
      awsRegion: 'ap-southeast-2',
      dynamodb: [
        {
                    "id": {
                        "S": "xxx"
                    },
                    "type": {
                        "S": "xxx"
                    }
                },
                "NewImage": {
                  ...
                },
                "OldImage": { ... }
      ],
      eventSourceARN: 'arn:aws:dynamodb:ap-southeast-2:115136697128:table/joeyDevices/stream/2020-07-10T04:42:54.695'
    }
]

有没有我可以在 TypeScript 中使用的类型定义来表示这个事件?
3个回答

20

有没有在TypeScript中可以用来处理这个事件的类型定义?

如果您正在使用AWS Lambda来处理Dynamo流事件,则可以在@types/aws-lambda包中找到类型定义。

import { DynamoDBStreamEvent } from "aws-lambda";

您可以在 @types / aws-lambda 的github代码库中查看完整的类型定义。

// http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_streams_StreamRecord.html
export interface StreamRecord {
    ApproximateCreationDateTime?: number;
    Keys?: { [key: string]: AttributeValue };
    NewImage?: { [key: string]: AttributeValue };
    OldImage?: { [key: string]: AttributeValue };
    SequenceNumber?: string;
    SizeBytes?: number;
    StreamViewType?: 'KEYS_ONLY' | 'NEW_IMAGE' | 'OLD_IMAGE' | 'NEW_AND_OLD_IMAGES';
}

// http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_streams_Record.html
export interface DynamoDBRecord {
    awsRegion?: string;
    dynamodb?: StreamRecord;
    eventID?: string;
    eventName?: 'INSERT' | 'MODIFY' | 'REMOVE';
    eventSource?: string;
    eventSourceARN?: string;
    eventVersion?: string;
    userIdentity?: any;
}

// http://docs.aws.amazon.com/lambda/latest/dg/eventsources.html#eventsources-ddb-update
export interface DynamoDBStreamEvent {
    Records: DynamoDBRecord[];
}

4

仅作为已接受答案的补充说明

// http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_streams_StreamRecord.html
export interface StreamRecord<T> {
    ApproximateCreationDateTime?: number;
    Keys?: { [key: string]: AttributeValue };
    NewImage?: T;
    OldImage?: T;
    SequenceNumber?: string;
    SizeBytes?: number;
    StreamViewType?: 'KEYS_ONLY' | 'NEW_IMAGE' | 'OLD_IMAGE' | 'NEW_AND_OLD_IMAGES';
}

// http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_streams_Record.html
export interface DynamoDBRecord<T> {
    awsRegion?: string;
    dynamodb?: StreamRecord<T>;
    eventID?: string;
    eventName?: 'INSERT' | 'MODIFY' | 'REMOVE';
    eventSource?: string;
    eventSourceARN?: string;
    eventVersion?: string;
    userIdentity?: any;
}

// http://docs.aws.amazon.com/lambda/latest/dg/eventsources.html#eventsources-ddb-update
export interface DynamoDBStreamEvent<T> {
    Records: DynamoDBRecord<T>[];
}

如果您正在自己的项目中进行实现,那么应该使用自己的类型来扩展它。

看起来不错,为什么不在aws-lambda项目上提交PR呢? - ThomasP1988
2
不错的观点 @ThomasP1988 :D - 可能会在周末做那个 - X97

1
在JavaScript中,你可以使用AWS.DynamoDB.Converter.unmarshall将DynamoDB的持久化形式解组成一个对象。 Amazon DynamoDB DataMapper利用@aws/dynamodb-data-marshaller@aws/dynamodb-expressions包,实现了应用程序域类与其在Amazon DynamoDB中持久化形式之间的易于互操作。你的工作是创建相关的应用程序类,并向类的原型添加属性。

谢谢您的回复。我找到了另一个库https://awslabs.github.io/dynamodb-data-mapper-js/packages/dynamodb-data-marshaller/,它看起来与您发布的那个类似。这个库也是来自AWS的github。我不确定为什么他们发布了两个库来做同一件事情。 - Joey Yi Zhao

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接