Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add parse docs page * Add section for parsing dynamo db stream events * Update www/src/pages/en/reference/parse.mdx Co-authored-by: Tyler W. Walch <tywalch@gmail.com> * Update www/src/pages/en/reference/parse.mdx Co-authored-by: Tyler W. Walch <tywalch@gmail.com> * Update www/src/pages/en/reference/parse.mdx Co-authored-by: Tyler W. Walch <tywalch@gmail.com> * Update config to show parse page Co-authored-by: Tyler W. Walch <tywalch@gmail.com>
- Loading branch information
Showing
2 changed files
with
52 additions
and
0 deletions.
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,51 @@ | ||
--- | ||
title: Parse | ||
description: Parse DocumentClient objects into business entities | ||
keywords: | ||
- electrodb | ||
- docs | ||
- concepts | ||
- dynamodb | ||
- parse | ||
layout: ../../../layouts/MainLayout.astro | ||
--- | ||
|
||
## Parse | ||
The parse method can be given a DocClient response and return a typed and formatted ElectroDB item. | ||
|
||
ElectroDB's `parse()` method accepts results from `get`, `delete`, `put`, `update`, `query`, and `scan` operations, applies all the same operations as though the item was retrieved by ElectroDB itself, and will return `null` (or empty array for `query` results) if the item could not be parsed. | ||
|
||
```typescript | ||
const myEntity = new Entity({...}); | ||
const getResults = await docClient.get({...}).promise(); | ||
const queryResults = await docClient.query({...}).promise(); | ||
const updateResults = await docClient.update({...}).promise(); | ||
const formattedGetResults = myEntity.parse(getResults); | ||
const formattedQueryResults = myEntity.parse(formattedQueryResults); | ||
``` | ||
|
||
### Dynamo Streams | ||
The parse method can also help handle events that come through dynamo events (`DynamoDBStreamEvent`). In order to do this, you must first unmarshal the response from dynamo using the DynamoDB `Converter` and then call `parse()` | ||
|
||
```typescript | ||
import DynamoDB from "aws-sdk/clients/dynamodb"; | ||
import { DynamoDBStreamEvent } from 'aws-lambda'; | ||
import { MyEntity } from "./my-entity"; | ||
|
||
function main(event: DynamoDBStreamEvent) { | ||
const parsed = event.Records | ||
.map(record => DynamoDB.Converter.unmarshall(record.dynamodb.NewImage)) | ||
.map(item => MyEntity.parse({ Item: item })) | ||
|
||
console.log(parsed) | ||
} | ||
``` | ||
|
||
### Options | ||
|
||
Parse also accepts an optional `options` object as a second argument (see the section [Query Options](#query-options) to learn more). Currently, the following query options are relevant to the `parse()` method: | ||
|
||
Option | Type | Default | Notes | ||
----------------- | -------- | ------------------ | ----- | ||
ignoreOwnership | boolean | `true` | This property defaults to `true` here, unlike elsewhere in the application when it defaults to `false`. You can overwrite the default here with your own preference. | ||
attributes | string[] | _(all attributes)_ | The `attributes` option allows you to specify a subset of attributes to return |