Skip to content

Commit d8ec1a7

Browse files
committed
Add document for Atomic Counter
1 parent 58888db commit d8ec1a7

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ A DynamoDB based Eloquent model and Query builder for Laravel.
1818
+ [save()](#save)
1919
+ [update()](#update)
2020
+ [delete()](#delete)
21+
+ [increment() / decrement()](#increment-/-decrement)
2122
* [Advanced Queries](#advanced-queries)
2223
- [Authentication with model](#authentication-with-model)
2324
* [Register custom user provider](#register-custom-user-provider)
@@ -47,6 +48,7 @@ A DynamoDB based Eloquent model and Query builder for Laravel.
4748
+ [exclusiveStartKey()](#exclusivestartkey)
4849
* [Using Global Secondary Indexes](#using-global-secondary-indexes)
4950
+ [index()](#index)
51+
* [Atomic Counter](#atomic-counter)
5052
* [DynamoDB-specific operators for condition() and filter()](#dynamodb-specific-operators-for-condition-and-filter)
5153
+ [Comparators](#comparators)
5254
+ [functions](#functions)
@@ -252,6 +254,23 @@ $user->update([
252254
$user->delete();
253255
```
254256

257+
#### increment() / decrement()
258+
259+
When we call `increment()` and `decrement()`, the [Atomic Counter](#atomic-counter) will be used under the hood.
260+
261+
```php
262+
$user->increment('views', 1);
263+
$user->decrement('views', 1);
264+
```
265+
266+
We can also pass additional attributes to update.
267+
268+
```php
269+
$user->increment('views', 1, [
270+
'last_viewed_at' => '...',
271+
]);
272+
```
273+
255274
### Advanced Queries
256275
We can use Query Builder functions through model such as `query` `scan` `filter` `condition` `keyCondition` etc.
257276

@@ -638,6 +657,28 @@ $response = DB::table('Reply')
638657
->query();
639658
```
640659

660+
### Atomic Counter
661+
662+
DynamoDB [supports Atomic Counter](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/WorkingWithItems.html#WorkingWithItems.AtomicCounters). When we call `increment()` and `decrement()` through Model or Query Builder, Atomic Counter will be used under the hood.
663+
664+
```php
665+
DB::('Thread')->key([
666+
'ForumName' => 'Laravel',
667+
'Subject' => 'Laravel Thread 1'
668+
])->increment('Replies', 2);
669+
```
670+
671+
We can also pass additional attributes to update.
672+
673+
```php
674+
DB::('Thread')->key([
675+
'ForumName' => 'Laravel',
676+
'Subject' => 'Laravel Thread 1'
677+
])->increment('Replies', 2, [
678+
'LastPostedBy' => 'User A',
679+
]);
680+
```
681+
641682
### DynamoDB-specific operators for condition() and filter()
642683

643684
For `condition` and `filter` clauses, we can use DynamoDB's comparators and functions.

0 commit comments

Comments
 (0)