sam-goodwin / typesafe-dynamodb

TypeSafe type definitions for the AWS DynamoDB API
Apache License 2.0
207 stars 11 forks source link

feat: add batchGetItem types #17

Closed brendo-m closed 2 years ago

brendo-m commented 2 years ago

Basic implementation, seems to work. It doesn't quite fit with the spirit of the library though because you can call batchGetItem for multiple tables. Should we impose that TypesafeDynamoDBv{2,3} only operates on a single table? if so, parameterizing the table name would improve the typing of the response here.

import { TypeSafeDynamoDBv2 } from "typesafe-dynamodb/lib/client-v2";

const typesafeClient: TypeSafeDynamoDBv2<"MyTableName", Record, "key", "sort"> = client;
const response = await table.batchGetItem({...}).promise();
const responseTable = response.Responses!.MyTableName; // type checks
sam-goodwin commented 2 years ago

The problem with TableName is that developers don't usually hard code literal values. Can we still narrow in that case without the literal string value? They would have to cast:

type MyTableName<Prefix extends string = string> = `MyTable-${Prefix}`

const myTableName = process.env.MyTableName as MyTableName
sam-goodwin commented 2 years ago

Perhaps we should provide a type safe BatchGetItemEntry (whatever its name is) for the specific table. Then use that function like how we overrode commands.

const GetMyTableItem = TypeSafeGetItem<..>;)

client.batchGet({
  Items: [GetMyTableItem(..)]
})
brendo-m commented 2 years ago

The problem with TableName is that developers don't usually hard code literal values. Can we still narrow in that case without the literal string value? They would have to cast:

Good point

brendo-m commented 2 years ago

Pushed a fix for the misplaced ProjectionExpression, and the response needed to have ToAttributeMap<...>

github-actions[bot] commented 2 years ago

This pull request is now marked as stale because it hasn't seen activity for a while. Add a comment or it will be closed soon. If you wish to exclude this issue from being marked as stale, add the "backlog" label.

github-actions[bot] commented 2 years ago

Closing this pull request as it hasn't seen activity for a while. Please add a comment @mentioning a maintainer to reopen. If you wish to exclude this issue from being marked as stale, add the "backlog" label.