Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 687
Joey Yi Zhao Avatar asked Jan 24 '26 20:01

Joey Yi Zhao


1 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[];
}
like image 80
Alex Graham Avatar answered Jan 26 '26 14:01

Alex Graham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!