Skip to content

Commit

Permalink
feat(dynamodb): add batchGet and batchWrite methods (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
co-go authored Sep 23, 2024
1 parent 28d3755 commit 9affc1a
Show file tree
Hide file tree
Showing 10 changed files with 5,610 additions and 809 deletions.
138 changes: 138 additions & 0 deletions dynamodb/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,35 @@
- <a href="#@winglibs/dynamodb.Client">Client</a>
- **Interfaces**
- <a href="#@winglibs/dynamodb.IClient">IClient</a>
- <a href="#@winglibs/dynamodb.IDynamoResource">IDynamoResource</a>
- <a href="#@winglibs/dynamodb.ITable">ITable</a>
- **Structs**
- <a href="#@winglibs/dynamodb.AttributeDefinition">AttributeDefinition</a>
- <a href="#@winglibs/dynamodb.BatchGetOptions">BatchGetOptions</a>
- <a href="#@winglibs/dynamodb.BatchGetOutput">BatchGetOutput</a>
- <a href="#@winglibs/dynamodb.BatchWriteOptions">BatchWriteOptions</a>
- <a href="#@winglibs/dynamodb.BatchWriteOutput">BatchWriteOutput</a>
- <a href="#@winglibs/dynamodb.ClientConfig">ClientConfig</a>
- <a href="#@winglibs/dynamodb.Connection">Connection</a>
- <a href="#@winglibs/dynamodb.Credentials">Credentials</a>
- <a href="#@winglibs/dynamodb.DeleteOptions">DeleteOptions</a>
- <a href="#@winglibs/dynamodb.DeleteOutput">DeleteOutput</a>
- <a href="#@winglibs/dynamodb.DeleteRequest">DeleteRequest</a>
- <a href="#@winglibs/dynamodb.GetOptions">GetOptions</a>
- <a href="#@winglibs/dynamodb.GetOutput">GetOutput</a>
- <a href="#@winglibs/dynamodb.GlobalSecondaryIndex">GlobalSecondaryIndex</a>
- <a href="#@winglibs/dynamodb.PutOptions">PutOptions</a>
- <a href="#@winglibs/dynamodb.PutOutput">PutOutput</a>
- <a href="#@winglibs/dynamodb.PutRequest">PutRequest</a>
- <a href="#@winglibs/dynamodb.QueryOptions">QueryOptions</a>
- <a href="#@winglibs/dynamodb.QueryOutput">QueryOutput</a>
- <a href="#@winglibs/dynamodb.ScanOptions">ScanOptions</a>
- <a href="#@winglibs/dynamodb.ScanOutput">ScanOutput</a>
- <a href="#@winglibs/dynamodb.StreamConsumerOptions">StreamConsumerOptions</a>
- <a href="#@winglibs/dynamodb.StreamRecord">StreamRecord</a>
- <a href="#@winglibs/dynamodb.StreamRecordDynamodb">StreamRecordDynamodb</a>
- <a href="#@winglibs/dynamodb.TableBatchGetOptions">TableBatchGetOptions</a>
- <a href="#@winglibs/dynamodb.TableBatchWriteOptions">TableBatchWriteOptions</a>
- <a href="#@winglibs/dynamodb.TableProps">TableProps</a>
- <a href="#@winglibs/dynamodb.TransactWriteItem">TransactWriteItem</a>
- <a href="#@winglibs/dynamodb.TransactWriteItemConditionCheck">TransactWriteItemConditionCheck</a>
Expand Down Expand Up @@ -64,6 +73,8 @@ new(props: TableProps): Table

| **Signature** | **Description** |
| --- | --- |
| <code>inflight batchGet(options: TableBatchGetOptions): BatchGetOutput</code> | *No description* |
| <code>inflight batchWrite(options: TableBatchWriteOptions): BatchWriteOutput</code> | *No description* |
| <code>inflight delete(options: DeleteOptions): DeleteOutput</code> | *No description* |
| <code>inflight get(options: GetOptions): GetOutput</code> | *No description* |
| <code>inflight put(options: PutOptions): PutOutput</code> | *No description* |
Expand Down Expand Up @@ -95,6 +106,8 @@ new(props: TableProps): Table_tfaws

| **Signature** | **Description** |
| --- | --- |
| <code>inflight batchGet(options: TableBatchGetOptions): BatchGetOutput</code> | *No description* |
| <code>inflight batchWrite(options: TableBatchWriteOptions): BatchWriteOutput</code> | *No description* |
| <code>inflight delete(options: DeleteOptions): DeleteOutput</code> | *No description* |
| <code>inflight get(options: GetOptions): GetOutput</code> | *No description* |
| <code>inflight put(options: PutOptions): PutOutput</code> | *No description* |
Expand Down Expand Up @@ -126,6 +139,8 @@ new(props: TableProps): Table_sim

| **Signature** | **Description** |
| --- | --- |
| <code>inflight batchGet(options: TableBatchGetOptions): BatchGetOutput</code> | *No description* |
| <code>inflight batchWrite(options: TableBatchWriteOptions): BatchWriteOutput</code> | *No description* |
| <code>inflight delete(options: DeleteOptions): DeleteOutput</code> | *No description* |
| <code>inflight get(options: GetOptions): GetOutput</code> | *No description* |
| <code>inflight put(options: PutOptions): PutOutput</code> | *No description* |
Expand Down Expand Up @@ -154,6 +169,8 @@ new(): Client

| **Signature** | **Description** |
| --- | --- |
| <code>inflight batchGet(options: BatchGetOptions): BatchGetOutput</code> | *No description* |
| <code>inflight batchWrite(options: BatchWriteOptions): BatchWriteOutput</code> | *No description* |
| <code>inflight delete(options: DeleteOptions): DeleteOutput</code> | *No description* |
| <code>inflight get(options: GetOptions): GetOutput</code> | *No description* |
| <code>inflight put(options: PutOptions): PutOutput</code> | *No description* |
Expand All @@ -172,6 +189,28 @@ new(): Client

#### Methods

| **Signature** | **Description** |
| --- | --- |
| <code>inflight batchGet(options: BatchGetOptions): BatchGetOutput</code> | *No description* |
| <code>inflight batchWrite(options: BatchWriteOptions): BatchWriteOutput</code> | *No description* |
| <code>inflight delete(options: DeleteOptions): DeleteOutput</code> | *No description* |
| <code>inflight get(options: GetOptions): GetOutput</code> | *No description* |
| <code>inflight put(options: PutOptions): PutOutput</code> | *No description* |
| <code>inflight query(options: QueryOptions): QueryOutput</code> | *No description* |
| <code>inflight scan(options: ScanOptions?): ScanOutput</code> | *No description* |
| <code>inflight transactWrite(options: TransactWriteOptions): TransactWriteOutput</code> | *No description* |
| <code>inflight update(options: UpdateOptions): UpdateOutput</code> | *No description* |

### IDynamoResource (interface) <a class="wing-docs-anchor" id="@winglibs/dynamodb.IDynamoResource"></a>

*No description*

#### Properties

*No properties*

#### Methods

| **Signature** | **Description** |
| --- | --- |
| <code>inflight delete(options: DeleteOptions): DeleteOutput</code> | *No description* |
Expand All @@ -194,6 +233,8 @@ new(): Client

| **Signature** | **Description** |
| --- | --- |
| <code>inflight batchGet(options: TableBatchGetOptions): BatchGetOutput</code> | *No description* |
| <code>inflight batchWrite(options: TableBatchWriteOptions): BatchWriteOutput</code> | *No description* |
| <code>inflight delete(options: DeleteOptions): DeleteOutput</code> | *No description* |
| <code>inflight get(options: GetOptions): GetOutput</code> | *No description* |
| <code>inflight put(options: PutOptions): PutOutput</code> | *No description* |
Expand All @@ -215,6 +256,53 @@ new(): Client
| <code>name</code> | <code>str</code> | *No description* |
| <code>type</code> | <code>str</code> | *No description* |

### BatchGetOptions (struct) <a class="wing-docs-anchor" id="@winglibs/dynamodb.BatchGetOptions"></a>

Input to the `batchGet` operation on a Client.

#### Properties

| **Name** | **Type** | **Description** |
| --- | --- | --- |
| <code>RequestItems</code> | <code>Map<TableBatchGetOptions></code> | *No description* |
| <code>ReturnConsumedCapacity</code> | <code>str?</code> | *No description* |

### BatchGetOutput (struct) <a class="wing-docs-anchor" id="@winglibs/dynamodb.BatchGetOutput"></a>

*No description*

#### Properties

| **Name** | **Type** | **Description** |
| --- | --- | --- |
| <code>ConsumedCapacity</code> | <code>Array<Json>?</code> | *No description* |
| <code>Responses</code> | <code>Map<Array<Json>>?</code> | *No description* |
| <code>UnprocessedKeys</code> | <code>Map<TableBatchGetOptions></code> | *No description* |

### BatchWriteOptions (struct) <a class="wing-docs-anchor" id="@winglibs/dynamodb.BatchWriteOptions"></a>

Input to the `batchWrite` operation on a Client.

#### Properties

| **Name** | **Type** | **Description** |
| --- | --- | --- |
| <code>RequestItems</code> | <code>Map<TableBatchWriteOptions></code> | *No description* |
| <code>ReturnConsumedCapacity</code> | <code>str?</code> | *No description* |
| <code>ReturnItemCollectionMetrics</code> | <code>str?</code> | *No description* |

### BatchWriteOutput (struct) <a class="wing-docs-anchor" id="@winglibs/dynamodb.BatchWriteOutput"></a>

*No description*

#### Properties

| **Name** | **Type** | **Description** |
| --- | --- | --- |
| <code>ConsumedCapacity</code> | <code>Array<Json>?</code> | *No description* |
| <code>ItemCollectionMetrics</code> | <code>Json?</code> | *No description* |
| <code>UnprocessedItems</code> | <code>Map<TableBatchWriteOptions>?</code> | *No description* |

### ClientConfig (struct) <a class="wing-docs-anchor" id="@winglibs/dynamodb.ClientConfig"></a>

*No description*
Expand Down Expand Up @@ -273,6 +361,16 @@ new(): Client
| --- | --- | --- |
| <code>Attributes</code> | <code>Json?</code> | *No description* |

### DeleteRequest (struct) <a class="wing-docs-anchor" id="@winglibs/dynamodb.DeleteRequest"></a>

Represents a request to perform a `DeleteItem` operation on an item.

#### Properties

| **Name** | **Type** | **Description** |
| --- | --- | --- |
| <code>Key</code> | <code>Json</code> | `Key` is a map of attribute name to attribute values, representing the primary key of the item to delete. All of the table's primary key attributes must be specified |

### GetOptions (struct) <a class="wing-docs-anchor" id="@winglibs/dynamodb.GetOptions"></a>

*No description*
Expand Down Expand Up @@ -337,6 +435,16 @@ new(): Client
| --- | --- | --- |
| <code>Attributes</code> | <code>Json?</code> | *No description* |

### PutRequest (struct) <a class="wing-docs-anchor" id="@winglibs/dynamodb.PutRequest"></a>

Represents a request to perform a `PutItem` operation on an item.

#### Properties

| **Name** | **Type** | **Description** |
| --- | --- | --- |
| <code>Item</code> | <code>Json</code> | A map of attribute name to attribute values, representing the primary key of an item to be processed by PutItem. All of the table's primary key attributes must be specified, and their data types must match those of the table's key schema. If any attributes are present in the item that are part of an index key schema for the table, their types must match the index key schema. |

### QueryOptions (struct) <a class="wing-docs-anchor" id="@winglibs/dynamodb.QueryOptions"></a>

*No description*
Expand Down Expand Up @@ -446,6 +554,36 @@ new(): Client
| <code>SizeBytes</code> | <code>num</code> | *No description* |
| <code>StreamViewType</code> | <code>str</code> | *No description* |

### TableBatchGetOptions (struct) <a class="wing-docs-anchor" id="@winglibs/dynamodb.TableBatchGetOptions"></a>

`TableBatchGetOptions` is used as an input to the `batchGet` operation on a Table or Client
(through [`BatchGetOptions`](#@winglibs/dynamodb.BatchGetOptions)).

#### Properties

| **Name** | **Type** | **Description** |
| --- | --- | --- |
| <code>AttributesToGet</code> | <code>Array<str>?</code> | *No description* |
| <code>ConsistentRead</code> | <code>bool?</code> | *No description* |
| <code>ExpressionAttributeNames</code> | <code>Map<str>?</code> | *No description* |
| <code>Keys</code> | <code>Array<Json></code> | *No description* |
| <code>ProjectionExpression</code> | <code>str?</code> | *No description* |
| <code>ReturnConsumedCapacity</code> | <code>str?</code> | When passed in on a **Table** resource, `ReturnConsumedCapacity` will be hoisted to the top level inside of the request. When passed in on a **Client** resource (via [`BatchGetOptions`](#@winglibs/dynamodb.BatchGetOptions)), setting `ReturnConsumedCapacity` here has no effect, set it inside of the top-level instead. |

### TableBatchWriteOptions (struct) <a class="wing-docs-anchor" id="@winglibs/dynamodb.TableBatchWriteOptions"></a>

`TableBatchWriteOptions` is used as an input to the `batchWrite` operation on a Table or Client
(through [`BatchWriteOptions`](#@winglibs/dynamodb.BatchWriteOptions)).

#### Properties

| **Name** | **Type** | **Description** |
| --- | --- | --- |
| <code>DeleteRequests</code> | <code>Array<DeleteRequest>?</code> | `DeleteRequests` contains a list of `DeleteItem` operations to perform on this table. |
| <code>PutRequests</code> | <code>Array<PutRequest>?</code> | `PutRequests` contains a list of `PutItem` operations to perform on this table. |
| <code>ReturnConsumedCapacity</code> | <code>str?</code> | When passed in on a **Table** resource, `ReturnConsumedCapacity` will be hoisted to the top level inside of the request. When passed in on a **Client** resource (via [`BatchWriteOptions`](#@winglibs/dynamodb.BatchWriteOptions)), setting `ReturnConsumedCapacity` here has no effect, set it inside of the top-level instead. |
| <code>ReturnItemCollectionMetrics</code> | <code>str?</code> | When passed in on a **Table** resource, `ReturnItemCollectionMetrics` will be hoisted to the top level inside of the request. When passed in on a **Client** resource (via [`BatchWriteOptions`](#@winglibs/dynamodb.BatchWriteOptions)), setting `ReturnConsumedCapacity` here has no effect, set it inside of the top-level instead. |

### TableProps (struct) <a class="wing-docs-anchor" id="@winglibs/dynamodb.TableProps"></a>

*No description*
Expand Down
30 changes: 29 additions & 1 deletion dynamodb/dynamodb-client.w
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub inflight class Client impl dynamodb_types.IClient {
pub inflight update(options: dynamodb_types.UpdateOptions): dynamodb_types.UpdateOutput {
let input: MutJson = options;
input.set("TableName", this.tableName);
return unsafeCast(this.client.update(input));
return unsafeCast(this.client.update(input));
}

pub inflight transactWrite(options: dynamodb_types.TransactWriteOptions): dynamodb_types.TransactWriteOutput {
Expand Down Expand Up @@ -114,4 +114,32 @@ pub inflight class Client impl dynamodb_types.IClient {
input.set("TableName", this.tableName);
return unsafeCast(this.client.query(input));
}

pub inflight batchGet(options: dynamodb_types.BatchGetOptions): dynamodb_types.BatchGetOutput {
return unsafeCast(this.client.batchGet(options));
}

pub inflight batchWrite(options: dynamodb_types.BatchWriteOptions): dynamodb_types.BatchWriteOutput {
let reqItems = MutMap<MutArray<dynamodb_types.WriteRequest>> {};

for item in options.RequestItems.entries() {
let tableReqs = MutArray<dynamodb_types.WriteRequest> [];

for DeleteRequest in item.value.DeleteRequests ?? [] {
tableReqs.push({ DeleteRequest });
}

for PutRequest in item.value.PutRequests ?? [] {
tableReqs.push({ PutRequest });
}

reqItems.set(item.key, tableReqs);
}

return unsafeCast(this.client.batchWrite({
RequestItems: Json.parse(Json.stringify(reqItems)),
ReturnConsumedCapacity: options.ReturnConsumedCapacity,
ReturnItemCollectionMetrics: options.ReturnItemCollectionMetrics,
}));
}
}
99 changes: 97 additions & 2 deletions dynamodb/dynamodb-types.w
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,94 @@ pub struct TransactWriteOptions {

pub struct TransactWriteOutput {}

/// `TableBatchGetOptions` is used as an input to the `batchGet` operation on a Table or Client
/// (through [`BatchGetOptions`](#@winglibs/dynamodb.BatchGetOptions)).
pub struct TableBatchGetOptions {
Keys: Array<Json>;
AttributesToGet: Array<str>?;
ConsistentRead: bool?;
ExpressionAttributeNames: Map<str>?;
ProjectionExpression: str?;

/// When passed in on a **Table** resource, `ReturnConsumedCapacity` will be hoisted to the top
/// level inside of the request. When passed in on a **Client** resource (via
/// [`BatchGetOptions`](#@winglibs/dynamodb.BatchGetOptions)), setting `ReturnConsumedCapacity`
/// here has no effect, set it inside of the top-level instead.
ReturnConsumedCapacity: str?;
}

/// Input to the `batchGet` operation on a Client.
pub struct BatchGetOptions {
RequestItems: Map<TableBatchGetOptions>;
ReturnConsumedCapacity: str?;
}

pub struct BatchGetOutput {
UnprocessedKeys: Map<TableBatchGetOptions>;
Responses: Map<Array<Json>>?;
ConsumedCapacity: Array<Json>?;
}

/// Represents a request to perform a `DeleteItem` operation on an item.
pub struct DeleteRequest {
/// `Key` is a map of attribute name to attribute values, representing the primary key of the item
/// to delete. All of the table's primary key attributes must be specified
Key: Json;
}

/// Represents a request to perform a `PutItem` operation on an item.
pub struct PutRequest {
/// A map of attribute name to attribute values, representing the primary key of an item to be
/// processed by PutItem. All of the table's primary key attributes must be specified, and their
/// data types must match those of the table's key schema. If any attributes are present in the item
/// that are part of an index key schema for the table, their types must match the index key
/// schema.
Item: Json;
}

// Represents an operation to perform - either `DeleteItem` or `PutItem`. Used internally to map
// from the `DeleteRequests` or `PutRequests` on `TableBatchWriteOptions` to the contract required
// from DynamoDB.
internal struct WriteRequest {
DeleteRequest: DeleteRequest?;
PutRequest: PutRequest?;
}

/// `TableBatchWriteOptions` is used as an input to the `batchWrite` operation on a Table or Client
/// (through [`BatchWriteOptions`](#@winglibs/dynamodb.BatchWriteOptions)).
pub struct TableBatchWriteOptions {
/// `DeleteRequests` contains a list of `DeleteItem` operations to perform on this table.
DeleteRequests: Array<DeleteRequest>?;

/// `PutRequests` contains a list of `PutItem` operations to perform on this table.
PutRequests: Array<PutRequest>?;

/// When passed in on a **Table** resource, `ReturnConsumedCapacity` will be hoisted to the top
/// level inside of the request. When passed in on a **Client** resource (via
/// [`BatchWriteOptions`](#@winglibs/dynamodb.BatchWriteOptions)), setting
/// `ReturnConsumedCapacity` here has no effect, set it inside of the top-level instead.
ReturnConsumedCapacity: str?;

/// When passed in on a **Table** resource, `ReturnItemCollectionMetrics` will be hoisted to the
/// top level inside of the request. When passed in on a **Client** resource (via
/// [`BatchWriteOptions`](#@winglibs/dynamodb.BatchWriteOptions)), setting
/// `ReturnConsumedCapacity` here has no effect, set it inside of the top-level instead.
ReturnItemCollectionMetrics: str?;
}

/// Input to the `batchWrite` operation on a Client.
pub struct BatchWriteOptions {
RequestItems: Map<TableBatchWriteOptions>;
ReturnConsumedCapacity: str?;
ReturnItemCollectionMetrics: str?;
}

pub struct BatchWriteOutput {
UnprocessedItems: Map<TableBatchWriteOptions>?;
ItemCollectionMetrics: Json?;
ConsumedCapacity: Array<Json>?;
}

pub struct AttributeDefinition {
name: str;
type: str;
Expand Down Expand Up @@ -221,7 +309,7 @@ pub struct Connection {
clientConfig: ClientConfig?;
}

pub inflight interface IClient {
pub inflight interface IDynamoResource {
inflight delete(options: DeleteOptions): DeleteOutput;
inflight get(options: GetOptions): GetOutput;
inflight put(options: PutOptions): PutOutput;
Expand All @@ -231,7 +319,14 @@ pub inflight interface IClient {
inflight transactWrite(options: TransactWriteOptions): TransactWriteOutput;
}

pub interface ITable extends IClient, std.IResource {
pub inflight interface IClient extends IDynamoResource {
inflight batchGet(options: BatchGetOptions): BatchGetOutput;
inflight batchWrite(options: BatchWriteOptions): BatchWriteOutput;
}

pub interface ITable extends IDynamoResource, std.IResource {
setStreamConsumer(handler: inflight (StreamRecord): void, options: StreamConsumerOptions?): void;
inflight readWriteConnection(): Connection;
inflight batchGet(options: TableBatchGetOptions): BatchGetOutput;
inflight batchWrite(options: TableBatchWriteOptions): BatchWriteOutput;
}
Loading

0 comments on commit 9affc1a

Please sign in to comment.