-
Notifications
You must be signed in to change notification settings - Fork 146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(parser): DynamoDBMarshalled
helper to parse DynamoDB data structure
#3442
Open
arnabrahman
wants to merge
10
commits into
aws-powertools:main
Choose a base branch
from
arnabrahman:3194-dynamodb-parser-helper
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
66ca32b
feat: add peer dependency for `@aws-sdk/util-dynamodb`
arnabrahman 0294014
feat: `DynamoDBMarshalled` function
arnabrahman 4f12aef
doc: `getTestEvent` function description for better understanding
arnabrahman bbf43e6
test: `DynamoDBMarshalled` function for dynamodb stream
arnabrahman 66085b8
refactor: reuse `extendedSchema` for `DynamoDBMarshalled` tests
arnabrahman 07d06f6
feat: dynamodb helper subpath export
arnabrahman 4b5f054
refactor: error message check
arnabrahman a1f9b8c
doc: `DynamoDBMarshalled` function doc description
arnabrahman 8b3721a
doc: `DynamoDBMarshalled` function description in user doc
arnabrahman 0f21776
Merge branch 'main' into 3194-dynamodb-parser-helper
arnabrahman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { DynamoDBMarshalled } from '@aws-lambda-powertools/parser/helpers/dynamodb'; | ||
import { | ||
DynamoDBStreamRecord, | ||
DynamoDBStreamSchema, | ||
} from '@aws-lambda-powertools/parser/schemas/dynamodb'; | ||
import { z } from 'zod'; | ||
|
||
const customSchema = z.object({ | ||
id: z.string(), | ||
message: z.string(), | ||
}); | ||
|
||
const extendedSchema = DynamoDBStreamSchema.extend({ | ||
Records: z.array( | ||
DynamoDBStreamRecord.extend({ | ||
dynamodb: z.object({ | ||
NewImage: DynamoDBMarshalled(customSchema).optional(), | ||
}), | ||
}) | ||
), | ||
}); | ||
|
||
type ExtendedDynamoDBStreamEvent = z.infer<typeof extendedSchema>; |
66 changes: 66 additions & 0 deletions
66
examples/snippets/parser/samples/exampleDynamoDBStreamPayload.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
{ | ||
"Records": [ | ||
{ | ||
"eventID": "1", | ||
"eventVersion": "1.0", | ||
"dynamodb": { | ||
"ApproximateCreationDateTime": 1693997155.0, | ||
"Keys": { | ||
"Id": { | ||
"N": "101" | ||
} | ||
}, | ||
"NewImage": { | ||
"Message": { | ||
"S": "New item!" | ||
}, | ||
"Id": { | ||
"N": "101" | ||
} | ||
}, | ||
"StreamViewType": "NEW_AND_OLD_IMAGES", | ||
"SequenceNumber": "111", | ||
"SizeBytes": 26 | ||
}, | ||
"awsRegion": "us-west-2", | ||
"eventName": "INSERT", | ||
"eventSourceARN": "eventsource_arn", | ||
"eventSource": "aws:dynamodb" | ||
}, | ||
{ | ||
"eventID": "2", | ||
"eventVersion": "1.0", | ||
"dynamodb": { | ||
"OldImage": { | ||
"Message": { | ||
"S": "New item!" | ||
}, | ||
"Id": { | ||
"N": "101" | ||
} | ||
}, | ||
"SequenceNumber": "222", | ||
"Keys": { | ||
"Id": { | ||
"N": "101" | ||
} | ||
}, | ||
"SizeBytes": 59, | ||
"NewImage": { | ||
"Message": { | ||
"S": "This item has changed" | ||
}, | ||
"Id": { | ||
"N": "101" | ||
} | ||
}, | ||
"StreamViewType": "NEW_AND_OLD_IMAGES" | ||
}, | ||
"awsRegion": "us-west-2", | ||
"eventName": "MODIFY", | ||
"eventSourceARN": "source_arn", | ||
"eventSource": "aws:dynamodb" | ||
} | ||
] | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import type { AttributeValue } from '@aws-sdk/client-dynamodb'; | ||
import { unmarshall } from '@aws-sdk/util-dynamodb'; | ||
import { type ZodTypeAny, z } from 'zod'; | ||
|
||
/** | ||
* A helper function to unmarshall DynamoDB stream events and validate them against a schema. | ||
* | ||
* @example | ||
* ```typescript | ||
* const mySchema = z.object({ | ||
* id: z.string(), | ||
* name: z.string(), | ||
* }); | ||
* const eventSchema = DynamoDBStreamSchema.extend({ | ||
* Records: z.array( | ||
* DynamoDBStreamRecord.extend({ | ||
* dynamodb: z.object({ | ||
* NewImage: DynamoDBMarshalled(mySchema).optional(), | ||
* }), | ||
* }) | ||
* ), | ||
* }); | ||
* type eventSchema = z.infer<typeof extendedSchema>; | ||
* ``` | ||
* For example, if you have a DynamoDB stream event like the following: | ||
* | ||
* ```json | ||
* { | ||
* "Records": [ | ||
* { | ||
* "dynamodb": { | ||
* "NewImage": { | ||
* "id": { | ||
* "S": "12345" | ||
* }, | ||
* "name": { | ||
* "S": "John Doe" | ||
* } | ||
* } | ||
* } | ||
* } | ||
* ] | ||
* } | ||
* ``` | ||
* Resulting in: | ||
* | ||
* ```json | ||
* { | ||
* "Records": [ | ||
* { | ||
* "dynamodb": { | ||
* "NewImage": { | ||
* "id": "12345", | ||
* "name": "John Doe" | ||
* } | ||
* } | ||
* } | ||
* ] | ||
* } | ||
* ``` | ||
* | ||
* @param schema - The schema to validate the JSON string against | ||
*/ | ||
const DynamoDBMarshalled = <T extends ZodTypeAny>(schema: T) => | ||
z | ||
.record(z.string(), z.custom<AttributeValue>()) | ||
.transform((str, ctx) => { | ||
try { | ||
return unmarshall(str); | ||
} catch (err) { | ||
ctx.addIssue({ | ||
code: 'custom', | ||
message: 'Could not unmarshall DynamoDB stream record', | ||
fatal: true, | ||
}); | ||
|
||
return z.NEVER; | ||
} | ||
}) | ||
.pipe(schema); | ||
|
||
export { DynamoDBMarshalled }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would appreciate suggestions on using this type.
Setting the type to
unknown
causes a TypeScript error. The@aws-sdk/util-dynamodb
package has a peer dependency on@aws-sdk/client-dynamodb
and we are using theAttributeValue
type from@aws-sdk/client-dynamodb
. Should we also declare@aws-sdk/client-dynamodb
as a peer dependency?Alternatively, we could set the type to
unknown
and suppress the TypeScript error usingbiome-ignore
, though I would prefer to avoid this approach.