Skip to content

Commit 24bc787

Browse files
authored
[DynamoDb] Batch write item (#679)
* [DynamoDb] BashWriteItem * minor * Dont update baseline
1 parent af3f273 commit 24bc787

File tree

10 files changed

+794
-0
lines changed

10 files changed

+794
-0
lines changed

src/DynamoDbClient.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use AsyncAws\Core\Exception\UnsupportedRegion;
88
use AsyncAws\Core\RequestContext;
99
use AsyncAws\DynamoDb\Input\BatchGetItemInput;
10+
use AsyncAws\DynamoDb\Input\BatchWriteItemInput;
1011
use AsyncAws\DynamoDb\Input\CreateTableInput;
1112
use AsyncAws\DynamoDb\Input\DeleteItemInput;
1213
use AsyncAws\DynamoDb\Input\DeleteTableInput;
@@ -20,6 +21,7 @@
2021
use AsyncAws\DynamoDb\Input\UpdateTableInput;
2122
use AsyncAws\DynamoDb\Input\UpdateTimeToLiveInput;
2223
use AsyncAws\DynamoDb\Result\BatchGetItemOutput;
24+
use AsyncAws\DynamoDb\Result\BatchWriteItemOutput;
2325
use AsyncAws\DynamoDb\Result\CreateTableOutput;
2426
use AsyncAws\DynamoDb\Result\DeleteItemOutput;
2527
use AsyncAws\DynamoDb\Result\DeleteTableOutput;
@@ -57,6 +59,28 @@ public function batchGetItem($input): BatchGetItemOutput
5759
return new BatchGetItemOutput($response, $this, $input);
5860
}
5961

62+
/**
63+
* The `BatchWriteItem` operation puts or deletes multiple items in one or more tables. A single call to
64+
* `BatchWriteItem` can write up to 16 MB of data, which can comprise as many as 25 put or delete requests. Individual
65+
* items to be written can be as large as 400 KB.
66+
*
67+
* @see https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-dynamodb-2012-08-10.html#batchwriteitem
68+
*
69+
* @param array{
70+
* RequestItems: array<string, array>,
71+
* ReturnConsumedCapacity?: \AsyncAws\DynamoDb\Enum\ReturnConsumedCapacity::*,
72+
* ReturnItemCollectionMetrics?: \AsyncAws\DynamoDb\Enum\ReturnItemCollectionMetrics::*,
73+
* @region?: string,
74+
* }|BatchWriteItemInput $input
75+
*/
76+
public function batchWriteItem($input): BatchWriteItemOutput
77+
{
78+
$input = BatchWriteItemInput::create($input);
79+
$response = $this->getResponse($input->request(), new RequestContext(['operation' => 'BatchWriteItem', 'region' => $input->getRegion()]));
80+
81+
return new BatchWriteItemOutput($response);
82+
}
83+
6084
/**
6185
* The `CreateTable` operation adds a new table to your account. In an AWS account, table names must be unique within
6286
* each Region. That is, you can have two tables with same name if you create the tables in different Regions.

src/Input/BatchWriteItemInput.php

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
<?php
2+
3+
namespace AsyncAws\DynamoDb\Input;
4+
5+
use AsyncAws\Core\Exception\InvalidArgument;
6+
use AsyncAws\Core\Input;
7+
use AsyncAws\Core\Request;
8+
use AsyncAws\Core\Stream\StreamFactory;
9+
use AsyncAws\DynamoDb\Enum\ReturnConsumedCapacity;
10+
use AsyncAws\DynamoDb\Enum\ReturnItemCollectionMetrics;
11+
use AsyncAws\DynamoDb\ValueObject\WriteRequest;
12+
13+
final class BatchWriteItemInput extends Input
14+
{
15+
/**
16+
* A map of one or more table names and, for each table, a list of operations to be performed (`DeleteRequest` or
17+
* `PutRequest`). Each element in the map consists of the following:.
18+
*
19+
* @required
20+
*
21+
* @var array<string, array>
22+
*/
23+
private $RequestItems;
24+
25+
/**
26+
* @var null|ReturnConsumedCapacity::*
27+
*/
28+
private $ReturnConsumedCapacity;
29+
30+
/**
31+
* Determines whether item collection metrics are returned. If set to `SIZE`, the response includes statistics about
32+
* item collections, if any, that were modified during the operation are returned in the response. If set to `NONE` (the
33+
* default), no statistics are returned.
34+
*
35+
* @var null|ReturnItemCollectionMetrics::*
36+
*/
37+
private $ReturnItemCollectionMetrics;
38+
39+
/**
40+
* @param array{
41+
* RequestItems?: array<string, array>,
42+
* ReturnConsumedCapacity?: \AsyncAws\DynamoDb\Enum\ReturnConsumedCapacity::*,
43+
* ReturnItemCollectionMetrics?: \AsyncAws\DynamoDb\Enum\ReturnItemCollectionMetrics::*,
44+
* @region?: string,
45+
* } $input
46+
*/
47+
public function __construct(array $input = [])
48+
{
49+
$this->RequestItems = [];
50+
foreach ($input['RequestItems'] ?? [] as $key => $item) {
51+
$this->RequestItems[$key] = array_map(function ($v) {return WriteRequest::create($v); }, $item);
52+
}
53+
$this->ReturnConsumedCapacity = $input['ReturnConsumedCapacity'] ?? null;
54+
$this->ReturnItemCollectionMetrics = $input['ReturnItemCollectionMetrics'] ?? null;
55+
parent::__construct($input);
56+
}
57+
58+
public static function create($input): self
59+
{
60+
return $input instanceof self ? $input : new self($input);
61+
}
62+
63+
/**
64+
* @return array<string, array>
65+
*/
66+
public function getRequestItems(): array
67+
{
68+
return $this->RequestItems;
69+
}
70+
71+
/**
72+
* @return ReturnConsumedCapacity::*|null
73+
*/
74+
public function getReturnConsumedCapacity(): ?string
75+
{
76+
return $this->ReturnConsumedCapacity;
77+
}
78+
79+
/**
80+
* @return ReturnItemCollectionMetrics::*|null
81+
*/
82+
public function getReturnItemCollectionMetrics(): ?string
83+
{
84+
return $this->ReturnItemCollectionMetrics;
85+
}
86+
87+
/**
88+
* @internal
89+
*/
90+
public function request(): Request
91+
{
92+
// Prepare headers
93+
$headers = [
94+
'Content-Type' => 'application/x-amz-json-1.0',
95+
'X-Amz-Target' => 'DynamoDB_20120810.BatchWriteItem',
96+
];
97+
98+
// Prepare query
99+
$query = [];
100+
101+
// Prepare URI
102+
$uriString = '/';
103+
104+
// Prepare Body
105+
$bodyPayload = $this->requestBody();
106+
$body = empty($bodyPayload) ? '{}' : json_encode($bodyPayload);
107+
108+
// Return the Request
109+
return new Request('POST', $uriString, $query, $headers, StreamFactory::create($body));
110+
}
111+
112+
/**
113+
* @param array<string, array> $value
114+
*/
115+
public function setRequestItems(array $value): self
116+
{
117+
$this->RequestItems = $value;
118+
119+
return $this;
120+
}
121+
122+
/**
123+
* @param ReturnConsumedCapacity::*|null $value
124+
*/
125+
public function setReturnConsumedCapacity(?string $value): self
126+
{
127+
$this->ReturnConsumedCapacity = $value;
128+
129+
return $this;
130+
}
131+
132+
/**
133+
* @param ReturnItemCollectionMetrics::*|null $value
134+
*/
135+
public function setReturnItemCollectionMetrics(?string $value): self
136+
{
137+
$this->ReturnItemCollectionMetrics = $value;
138+
139+
return $this;
140+
}
141+
142+
private function requestBody(): array
143+
{
144+
$payload = [];
145+
146+
foreach ($this->RequestItems as $name => $v) {
147+
$index = -1;
148+
foreach ($v as $listValue) {
149+
++$index;
150+
$payload['RequestItems'][$name][$index] = $listValue->requestBody();
151+
}
152+
}
153+
if (null !== $v = $this->ReturnConsumedCapacity) {
154+
if (!ReturnConsumedCapacity::exists($v)) {
155+
throw new InvalidArgument(sprintf('Invalid parameter "ReturnConsumedCapacity" for "%s". The value "%s" is not a valid "ReturnConsumedCapacity".', __CLASS__, $v));
156+
}
157+
$payload['ReturnConsumedCapacity'] = $v;
158+
}
159+
if (null !== $v = $this->ReturnItemCollectionMetrics) {
160+
if (!ReturnItemCollectionMetrics::exists($v)) {
161+
throw new InvalidArgument(sprintf('Invalid parameter "ReturnItemCollectionMetrics" for "%s". The value "%s" is not a valid "ReturnItemCollectionMetrics".', __CLASS__, $v));
162+
}
163+
$payload['ReturnItemCollectionMetrics'] = $v;
164+
}
165+
166+
return $payload;
167+
}
168+
}

0 commit comments

Comments
 (0)