Skip to content

Commit

Permalink
refactor: updated event callbacks api
Browse files Browse the repository at this point in the history
  • Loading branch information
ndaba1 committed Dec 8, 2023
1 parent 0f2678f commit 7da89d5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/twelve-toes-wonder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@jumba/lambda-utils": patch
---

updated callback events api
44 changes: 24 additions & 20 deletions packages/lambda-utils/src/events.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { unmarshall } from "@aws-sdk/util-dynamodb";
import { DynamoDBStreamEvent } from "aws-lambda";

type OnChangeCallback<T extends Record<string, unknown>> = (
oldImage: T,
newImage: T
) => void | Promise<void>;
type OnCreateCallback<T extends Record<string, unknown>> = (
newImage: T
) => void | Promise<void>;
type OnDeleteCallback<T extends Record<string, unknown>> = (
oldImage: T
) => void | Promise<void>;
type OnChangeCallback<T extends Record<string, unknown>> = (args: {
oldImage: T;
newImage: T;
}) => void | Promise<void>;
type OnCreateCallback<T extends Record<string, unknown>> = (args: {
oldImage: T;
newImage: T;
}) => void | Promise<void>;
type OnDeleteCallback<T extends Record<string, unknown>> = (args: {
oldImage: T;
newImage: T;
}) => void | Promise<void>;

type ModelName<T> = T extends { __typename: string }
? T["__typename"]
Expand Down Expand Up @@ -111,9 +113,9 @@ class Emitter {
let modelName = eventSourceARN?.split("/")?.[1]?.split("-").shift();

// @ts-expect-error - type mismatch
let newData = unmarshall(dynamodbData?.NewImage || {});
let newImage = unmarshall(dynamodbData?.NewImage || {});
// @ts-expect-error - type mismatch
let previousData = unmarshall(dynamodbData?.OldImage || {});
let oldImage = unmarshall(dynamodbData?.OldImage || {});

console.log({ eventName, modelName, eventSourceARN });
switch (eventName) {
Expand All @@ -122,21 +124,22 @@ class Emitter {
const fns = this.onCreateCallbacks[modelName || ""] || [];
const globals = this.onCreateCallbacks["ALL_MODELS"] || [];
await Promise.all(
// @ts-expect-error - global listeners expect old and new data
globals?.map((callback) => callback(previousData, newData))
globals?.map((callback) => callback({ oldImage, newImage }))
);
await Promise.all(
fns?.map((callback) => callback({ oldImage, newImage }))
);
await Promise.all(fns?.map((callback) => callback(newData)));
}
break;
case "MODIFY":
{
const fns = this.onChangeCallbacks[modelName || ""] || [];
const globals = this.onChangeCallbacks["ALL_MODELS"] || [];
await Promise.all(
globals?.map((callback) => callback(previousData, newData))
globals?.map((callback) => callback({ oldImage, newImage }))
);
await Promise.all(
fns?.map((callback) => callback(previousData, newData))
fns?.map((callback) => callback({ oldImage, newImage }))
);
}
break;
Expand All @@ -145,10 +148,11 @@ class Emitter {
const fns = this.onDeleteCallbacks[modelName || ""] || [];
const globals = this.onDeleteCallbacks["ALL_MODELS"] || [];
await Promise.all(
// @ts-expect-error - global listeners expect old and new data
globals?.map((callback) => callback(previousData, newData))
globals?.map((callback) => callback({ oldImage, newImage }))
);
await Promise.all(
fns?.map((callback) => callback({ oldImage, newImage }))
);
await Promise.all(fns?.map((callback) => callback(previousData)));
}
break;
default:
Expand Down

0 comments on commit 7da89d5

Please sign in to comment.