DynamoDB Stream Framework
npm install @serverless-seoul/dynamorm-stream

Framework for DynamoDB Stream Event processing on Lambda. Based on (https://github.com/serverless-seoul/dynamorm)
``typescript${record.dynamodb.newImage.S}
export function handler(event, content) {
event.records.forEach((record) => {
if (record.eventName === "INSERT") {
SlackNotifier.notify();`
}
})
}
But clearly, there are some missing things here,
typescript
export function handler(event, content) {
await notifySlack(event.records.filter(record => record.eventName === "INSERT"));
await backuptoS3(event.records.filter(record => record.eventName === "REMOVE"));
}
`
And this is really dangerous if backuptoS3 throws Error.
In that case, whole Lambda invocation go to error, thus this batch of DynamoDB Stream events marked as failed.
And when DynamoDB Stream failed to process some events, it retries with same events until either events expires (24 hours after created) or process successed
So if there is bug on backuptoS3, you'll get slack notification infinitely
To avoid this, you should do something like
`typescript
export function handler(event, content) {
try {
await notifySlack(event.records.filter(record => record.eventName === "INSERT"));
} catch (e) {
console.error(e);
} try {
await backuptoS3(event.records.filter(record => record.eventName === "REMOVE"));
} catch (e) {
console.error(e);
}
}
`
Not that cool right?
Usage
`typescript
import { Decorator, Query, Table, Config } from "@serverless-seoul/dynamorm";// First Define your DynamoDB Table
@Decorator.Table({ name: "prod-Card" })
class Card extends Table {
@Decorator.Attribute()
public id: number;
@Decorator.Attribute()
public title: string;
@Decorator.Attribute({ timeToLive: true })
public expiresAt: number;
@Decorator.FullPrimaryKey('id', 'title')
static readonly primaryKey: Query.FullPrimaryKey;
@Decorator.Writer()
static readonly writer: Query.Writer;
}
import * as DynamoTypesStream from "@serverless-seoul/dynamorm-stream";
// This is lambda event handler. "exports.handler"
export const handler = DynamoTypesStream.createLambdaHandler(
createHandler(
Card,
"Series",
[
{
eventType: "INSERT",
name: "New Card Informer",
async handler(events) {
// Events automatically typed as Array>
events.forEach(event => {
console.log("New Card! :", event.newRecord.id);
})
},
}, {
eventType: "INSERT",
name: "New Card Error",
async handler(events) {
throw new Error("XXXX");
},
}, {
eventType: "REMOVE",
name: "Deleted Card Informer",
async handler(events) {
// Events automatically typed as Array>
events.forEach(event => {
console.log("Deleted Card! :", event.newRecord.id);
})
}
}
],
async catchError(handlerDefinition, events, error) {
// This is global Error handler
console.log(handlerDefinition.name, events, error)
// --> "New Card Error", [{ event: "deleted", oldRecord: new Card(), new Error("XXX")];
}
)
)
`And
createLambdaHandler is optional as you can imagine. if you already have your own wrapper for lambda function
you might only use createHandler, which is (event: LambdaEvent) => Promise`typescript
/**
*
* @param tableClass DynamoTypes Class
* @param strategy handler Execution strategy. Map execute all handlers once, Series excute handlers one by one
* @param handlers
*/
export function createHandler(
tableClass: ITable,
strategy: "Map" | "Series" = "Series",
handlers: Array>
)
``