当我在打字稿中收到 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'
}
]
我可以在打字稿中使用此事件的类型定义吗?
我可以在打字稿中使用此事件的类型定义吗?
如果您使用 AWS Lambda 处理发电机流事件,您可以在
@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[];
}
只是已接受答案的附录
// 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>[];
}
如果您在自己的项目中实现,则应该使用自己的类型扩展它。
在 JavaScript 中,您可以使用 AWS.DynamoDB.Converter.unmarshall 将 DynamoDB 的持久形式解组为对象。
@aws/dynamodb-data-marshaller
和 @aws/dynamodb-expressions
包,并允许应用程序的域类与其在 Amazon DynamoDB 中的持久形式之间轻松实现互操作。您的工作是创建相关的应用程序类并向该类的原型添加属性。
如果您使用的是 v3 sdk 那么您可以这样做:
import type { _Record } from '@aws-sdk/client-dynamodb-streams'
Records: _Rocord[]