Skip to content

Commit

Permalink
Docs: Add parse docs page (#199)
Browse files Browse the repository at this point in the history
* 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
NoahDavey and tywalch committed Dec 22, 2022
1 parent 5d6feb5 commit 5404b3d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions www/src/config.ts
Expand Up @@ -90,6 +90,7 @@ export const SIDEBAR: Sidebar = {
{ text: 'TypeScript', link: 'en/reference/typscript' },
{ text: 'DynamoDB Client', link: 'en/reference/dynamodb-client' },
{ text: 'Events & Logging', link: 'en/reference/events-logging' },
{ text: 'Parse', link: 'en/reference/parse' },
],
Examples: [
{ text: 'Human resources database', link: 'en/examples/human-resources' },
Expand Down
51 changes: 51 additions & 0 deletions www/src/pages/en/reference/parse.mdx
@@ -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

0 comments on commit 5404b3d

Please sign in to comment.