Skip to content

Commit

Permalink
chore: update docs for Single Table Design (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
sam authored Feb 8, 2022
1 parent 196375c commit b52aefc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,45 @@ Same for the `Item` in the response:

![typesafe GetItemOutput Item](img/get-item-response.gif)

### Single Table Design

Below are two `interface` declarations, representing two types of data stored in a single DynamoDB table - `User` and `Order`. Single table design in DynamoDB is achieved by creating "composite keys", e.g. `USER#${UserID}`. In TypeScript, we use template literal types to encode this in the Type System.

```ts
interface User<UserID extends string = string> {
PK: `USER#${UserID}`;
SK: `#PROFILE#${UserID}`;
Username: string;
FullName: string;
Email: string;
CreatedAt: Date;
Address: string;
}

interface Order<
UserID extends string = string,
OrderID extends string = string
> {
PK: `USER#${UserID}`;
SK: `ORDER#${OrderID}`;
Username: string;
OrderID: string;
Status: "PLACED" | "SHIPPED";
CreatedAt: Date;
Address: string;
}
```

With these two types defined, you can now use a union type to declare a `TypeSafeDynamoDB` instance aware of the two types of data in your tables:

```ts
const client: TypeSafeDynamoDB<User | Order, "PK", "SK">;
```

When making calls such as `getItem`, TypeScript will narrow the returned data type to the corresponding type based on the structure of the `Key` in your request:

![narrowed getItem](img/get-order.png)

### Filter result with ProjectionExpression

The `ProjectionExpression` field is parsed and applied to filter the returned type of `getItem` and `query`.
Expand Down
Binary file added img/get-order.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b52aefc

Please sign in to comment.