Search code examples
typescriptamazon-dynamodb

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?


Solution

  • 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.