Skip to content

Commit 4d9497c

Browse files
committed
support BatchGetItem and BatchWriteItem
1 parent edb21cb commit 4d9497c

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed

src/Kitar/Dynamodb/Model/Model.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,10 @@ public function __call($method, $parameters)
354354
"putItem",
355355
"deleteItem",
356356
"updateItem",
357+
"batchGetItem",
358+
"batchPutItem",
359+
"batchDeleteItem",
360+
"batchWriteItem",
357361
"scan",
358362
"filter",
359363
"filterIn",

src/Kitar/Dynamodb/Query/Builder.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ class Builder extends BaseBuilder
4141
'remove' => []
4242
];
4343

44+
/**
45+
* Keys array for BatchGetItem
46+
* https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchGetItem.html
47+
* @var array
48+
*/
49+
public $batch_get_keys = [];
50+
51+
/**
52+
* RequestItems array for BatchWriteItem
53+
* https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchWriteItem.html
54+
* @var array
55+
*/
56+
public $batch_write_request_items = [];
57+
4458
/**
4559
* ScanIndexForward option.
4660
*/
@@ -312,6 +326,48 @@ public function updateItem($item)
312326
return $this->process('updateItem', 'processSingleItem');
313327
}
314328

329+
public function batchGetItem($keys)
330+
{
331+
$this->batch_get_keys = $keys;
332+
333+
return $this->process('batchGetItem', 'processBatchGetItems');
334+
}
335+
336+
public function batchPutItem($items)
337+
{
338+
$this->batch_write_request_items = array_map(function ($item) {
339+
return [
340+
'PutRequest' => [
341+
'Item' => $item,
342+
],
343+
];
344+
}, $items);
345+
346+
return $this->batchWriteItem();
347+
}
348+
349+
public function batchDeleteItem($keys)
350+
{
351+
$this->batch_write_request_items = array_map(function ($key) {
352+
return [
353+
'DeleteRequest' => [
354+
'Key' => $key,
355+
],
356+
];
357+
}, $keys);
358+
359+
return $this->batchWriteItem();
360+
}
361+
362+
public function batchWriteItem($request_items = [])
363+
{
364+
if (! empty($request_items)) {
365+
$this->batch_write_request_items = $request_items;
366+
}
367+
368+
return $this->process('batchWriteItem', null);
369+
}
370+
315371
/**
316372
* @inheritdoc
317373
*/
@@ -539,6 +595,8 @@ protected function process($query_method, $processor_method)
539595
$this->grammar->compileKey($this->key),
540596
$this->grammar->compileItem($this->item),
541597
$this->grammar->compileUpdates($this->updates),
598+
$this->grammar->compileBatchGetRequestItems($this->from, $this->batch_get_keys),
599+
$this->grammar->compileBatchWriteRequestItems($this->from, $this->batch_write_request_items),
542600
$this->grammar->compileDynamodbLimit($this->limit),
543601
$this->grammar->compileScanIndexForward($this->scan_index_forward),
544602
$this->grammar->compileExclusiveStartKey($this->exclusive_start_key),

src/Kitar/Dynamodb/Query/Grammar.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,54 @@ public function compileUpdates($updates)
135135
];
136136
}
137137

138+
public function compileBatchGetRequestItems($table_name, $keys)
139+
{
140+
if (empty($keys)) {
141+
return [];
142+
}
143+
144+
$marshaler = $this->marshaler;
145+
$marshaled_items = array_map(function ($key) use ($marshaler) {
146+
return $marshaler->marshalItem($key);
147+
}, $keys);
148+
149+
$table_name = $this->tablePrefix . $table_name;
150+
151+
return [
152+
'RequestItems' => [
153+
$table_name => [
154+
'Keys' => $marshaled_items,
155+
],
156+
]
157+
];
158+
}
159+
160+
public function compileBatchWriteRequestItems($table_name, $request_items)
161+
{
162+
if (empty($request_items)) {
163+
return [];
164+
}
165+
166+
$marshaler = $this->marshaler;
167+
$marshaled_items = array_map(function ($request_item) use ($marshaler) {
168+
return array_map(function ($request_body) use ($marshaler) {
169+
$marshaled = [];
170+
foreach ($request_body as $key => $body) {
171+
$marshaled[$key] = $marshaler->marshalItem($body);
172+
}
173+
return $marshaled;
174+
}, $request_item);
175+
}, $request_items);
176+
177+
$table_name = $this->tablePrefix . $table_name;
178+
179+
return [
180+
'RequestItems' => [
181+
$table_name => $marshaled_items,
182+
],
183+
];
184+
}
185+
138186
/**
139187
* Compile the Limit attribute.
140188
*

src/Kitar/Dynamodb/Query/Processor.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,29 @@ public function processMultipleItems(Result $awsResponse, $modelClass = null)
7878
return $item;
7979
});
8080
}
81+
82+
public function processBatchGetItems(Result $awsResponse, $modelClass = null)
83+
{
84+
$response = $this->unmarshal($awsResponse);
85+
86+
if (empty($modelClass)) {
87+
return $response;
88+
}
89+
90+
$items = collect();
91+
92+
foreach ($response['Responses'] as $_ => $table_items) {
93+
foreach ($table_items as $item) {
94+
$item = (new $modelClass)->newFromBuilder($item);
95+
$items->push($item);
96+
}
97+
}
98+
99+
unset($response['Responses']);
100+
101+
return $items->map(function ($item) use ($response) {
102+
$item->setMeta($response);
103+
return $item;
104+
});
105+
}
81106
}

0 commit comments

Comments
 (0)