What is the type should I use for dynamodb stream event in typescript?

Viewed 4175

When I receive an dynamodb stream event in typescript, I see below schema:

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'
    }
]

Is there a type definition I can use for this event in typescript?

3 Answers

Is there a type definition I can use for this event in typescript?

If you are using an AWS Lambda to process dynamo stream events, you can find type definitions in the @types/aws-lambda package.

import { DynamoDBStreamEvent } from "aws-lambda";

You can see the full type definition here in the github repo for @types/aws-lambda.

// 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[];
}

Just an addendum to the already accepted answer

// 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>[];
}

You should extend it with your own type if you are implementing in your own project.

In JavaScript, you can use AWS.DynamoDB.Converter.unmarshall to unmarshall DynamoDB's persisted form into an object.

Amazon DynamoDB DataMapper takes advantage of @aws/dynamodb-data-marshaller and @aws/dynamodb-expressions packages and allows easy interoperability between your application's domain classes and their persisted form in Amazon DynamoDB. Your job is to create the relevant application class and to add properties to the prototype of the class.

Related