Skip to content

Commit d922796

Browse files
Merge pull request #94 from shiftcode/#78-transact-write-items
#78 transact write items
2 parents 9c2c62d + ef7fed0 commit d922796

File tree

73 files changed

+2042
-1369
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+2042
-1369
lines changed

README.md

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ When one of the following decorators is present, the value is always mapped to a
187187
We only support the native Date type and you need to explicitly mark a property to be a Date by using the @Date() decorator\
188188
(which is basically just syntactic sugar for @CustomMapper(TheDateMapper)).\
189189
If you want to use a different type for the @Date decorator (eg. Moment) you need to define a custom mapper and provide it to the dynamo easy config like this:\
190-
`updateDynamoEasyConfig({ dateMapper: MomentMapper })`\
190+
`updateDynamoEasyConfig({ dateMapper: MomentMapper })`
191191

192192

193193
A mapper for moment dates could look like this:
@@ -219,7 +219,7 @@ export const MomentMapper: MapperForType<moment.Moment, StringAttribute> = {
219219

220220

221221
## Enum
222-
Enum values are persisted as Numbers (index of enum) or string if string value was given.
222+
Enum values are persisted as Numbers (index of enum or assigned value) or string if string value was assigned.
223223

224224
# Request API
225225
To start making requests create an instance of [DynamoStore](https://shiftcode.github.io/dynamo-easy/classes/_dynamo_dynamo_store_.dynamostore.html) and execute the desired operation using the provided api.
@@ -237,6 +237,54 @@ We support the following dynamodb operations with a fluent api:
237237

238238
There is always the possibility to access the Params object directly to add values which are not covered with our api.
239239

240+
## non table tied requests
241+
Currently two type of requests exists which are not tied to one table/model and therefore are not created from a DynamoStore instance.
242+
243+
### BatchGet
244+
245+
There are two scenarios for a batch get item request. One is requesting multiple items from one table by id and the other is requesting multiple items by id from multiple
246+
tables. The first scenario is support using DynamoStore.batchGet() the second one can be achieved by using the [BatchGetRequest](https://shiftcode.github.io/dynamo-easy/classes/_dynamo_batchget_batch_get_request_.batchgetrequest.html) class.
247+
248+
```typescript
249+
import { BatchGetRequest } from '@shiftcoders/dynamo-easy'
250+
new BatchGetRequest()
251+
// table with simple primary key
252+
.forModel(YourModelWithPartitionKey, [{ id: 'myId' }], /* consistentRead */ true)
253+
// table with composite primary key (sortkey is optional)
254+
.forModel(YourModelWithCompositeKey, [{ id: 'myId', creationDate: new Date('2018-01-01') }])
255+
.exec().subscribe(response => {
256+
// an object where the items are mapped to the table name
257+
})
258+
```
259+
### TransactWriteRequest
260+
Create transactions for all-or-nothing operations with [TransactWriteRequest](https://shiftcode.github.io/dynamo-easy/classes/_dynamo_transactwrite_transact_write_request_.transactwriterequest.html) across one or more tables.
261+
The different operations are:
262+
* [TransactConditionCheck](https://shiftcode.github.io/dynamo-easy/classes/_dynamo_transactwrite_transact_condition_check_.transactconditioncheck.html)
263+
* [TransactPut](https://shiftcode.github.io/dynamo-easy/classes/_dynamo_transactwrite_transact_put_.transactput.html)
264+
* [TransactDelete](https://shiftcode.github.io/dynamo-easy/classes/_dynamo_transactwrite_transact_delete_.transactdelete.html)
265+
* [TransactUpdate](https://shiftcode.github.io/dynamo-easy/classes/_dynamo_transactwrite_transact_update_.transactupdate.html)
266+
267+
The transaction operations can optionally check for prerequisite conditions that must be satisfied before making updates.
268+
For conditions not involving a an item to write on, use the TransactConditionCheck.
269+
270+
```typescript
271+
import { TransactWriteRequest, TransactConditionCheck, TransactDelete, TransactPut, attribute } from '@shiftcoders/dynamo-easy'
272+
273+
new TransactWriteRequest()
274+
.transact(
275+
new TransactConditionCheck(YourModelWithPartitionKey, 'check-ID').onlyIf(attribute('age').gt(18)),
276+
new TransactDelete(YourModelWithCompositeKey, 'the-partition-key', 'the-sort-key'),
277+
new TransactPut(YourCustomModel, { id: 'put-ID-1', age: 21 }).ifNotExists(),
278+
)
279+
.returnItemCollectionMetrics('SIZE')
280+
.returnConsumedCapacity('TOTAL')
281+
.execFullResponse()
282+
.subscribe(resp => {
283+
console.log(resp.ItemCollectionMetrics)
284+
console.log(resp.ConsumedCapacity)
285+
})
286+
```
287+
240288
# Authentication
241289
In a real world scenario you'll have some kind of authentication to protect your dynamodb ressources. You can customize on how to authenticate when providing a custom
242290
SessionValidityEnsurer function to the DynamoStore when creating a new instance.
@@ -245,7 +293,7 @@ The default implementation is a no-op function.
245293
## Session Validity Ensurer
246294
Here is an example of an implementation using amazon cognito
247295

248-
```javascript
296+
```typescript
249297
function sessionValidityEnsurer(): Observable<boolean> {
250298
return Observable.of(this.isLoggedIn())
251299
.switchMap(isLoggedIn => {
@@ -295,30 +343,6 @@ these are the accessor rules for nested attribute types
295343
- [n] — for list elements
296344
- . (dot) — for map elements
297345

298-
## Pagination
299-
TODO
300-
301-
## BatchGet
302-
303-
There are two scenarios for a batch get item request. One is requesting multiple items from one table by id and the other is requesting multiple items by id from multiple
304-
tables.
305-
The first scenario is support using DynamoStore.batchGet() the second one must be implemented using the BatchGetItem class.
306-
307-
```typescript
308-
const request = new BatchRequest()
309-
310-
// table with simple primary key
311-
request.forModel(MyModelClass, ['idValue', 'idValue2'])
312-
313-
// table with composite primary key (sortkey is optional)
314-
request.forModel(MyOtherModelClass, [{partitionKey: 'id', sortKey: 'sortKeyValue'}])
315-
316-
request.exec().subscribe(response => {
317-
// an object where the items are mapped to the table name
318-
})
319-
320-
```
321-
322346
# Development
323347

324348
## NPM scripts
@@ -331,7 +355,7 @@ request.exec().subscribe(response => {
331355
- `npm run commit`: Commit using conventional commit style ([husky](https://github.com/typicode/husky) will tell you to use it if you haven't :wink:)
332356

333357
## Automatic releases
334-
Use the npm comand `npm run commit`, which is a convenient way to create conventional commits. Those messages are used to run [semantic releases](https://github.com/semantic-release/semantic-release),
358+
Use the npm comand `npm run commity`, which is a convenient way to create conventional commits. Those messages are used to run [semantic releases](https://github.com/semantic-release/semantic-release),
335359
which publishes our code automatically on github and npm, plus generates automatically a changelog. This setup is highly influenced by [Kent C. Dodds course on egghead.io](https://egghead.io/courses/how-to-write-an-open-source-javascript-library)
336360

337361
## Git Hooks
@@ -354,7 +378,7 @@ We use 2 git hooks:
354378
Made with :heart: by [@michaelwittwer](https://github.com/michaelwittwer) and all these wonderful contributors ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)):
355379

356380
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
357-
| [<img src="https://avatars1.githubusercontent.com/u/8394182?v=4" width="100px;"/><br /><sub>Michael Wittwer</sub>](https://www.shiftcode.ch)<br />[💻](https://github.com/shiftcode/dynamo-easy/commits?author=michaelwittwer "Code") [📖](https://github.com/shiftcode/dynamo-easy/commits?author=michaelwittwer "Documentation") [⚠️](https://github.com/shiftcode/dynamo-easy/commits?author=michaelwittwer "Tests") | [<img src="https://avatars2.githubusercontent.com/u/8321523?s=460&v=4" width="100px;"/><br /><sub>Michael Lieberherr</sub>](https://www.shiftcode.ch)<br />[💻](https://github.com/shiftcode/dynamo-easy/commits?author=michaellieberherrr "Code") [📖](https://github.com/shiftcode/dynamo-easy/commits?author=michaellieberherrr "Documentation") [⚠️](https://github.com/shiftcode/dynamo-easy/commits?author=michaellieberherrr "Tests") | [<img src="https://avatars3.githubusercontent.com/u/37636934?s=460&v=4" width="100px;"/><br /><sub>Simon Mumenthaler</sub>](https://www.shiftcode.ch)<br />[💻](https://github.com/shiftcode/dynamo-easy/commits?author=simonmumenthaler "Code") [⚠️](https://github.com/shiftcode/dynamo-easy/commits?author=simonmumenthaler "Tests") |
381+
| [<img src="https://avatars1.githubusercontent.com/u/8394182?v=4" width="100px;"/><br /><sub>Michael Wittwer</sub>](https://www.shiftcode.ch)<br />[💻](https://github.com/shiftcode/dynamo-easy/commits?author=michaelwittwer "Code") [📖](https://github.com/shiftcode/dynamo-easy/commits?author=michaelwittwer "Documentation") [⚠️](https://github.com/shiftcode/dynamo-easy/commits?author=michaelwittwer "Tests") | [<img src="https://avatars2.githubusercontent.com/u/8321523?s=460&v=4" width="100px;"/><br /><sub>Michael Lieberherr</sub>](https://www.shiftcode.ch)<br />[💻](https://github.com/shiftcode/dynamo-easy/commits?author=michaellieberherrr "Code") [📖](https://github.com/shiftcode/dynamo-easy/commits?author=michaellieberherrr "Documentation") [⚠️](https://github.com/shiftcode/dynamo-easy/commits?author=michaellieberherrr "Tests") | [<img src="https://avatars3.githubusercontent.com/u/37636934?s=460&v=4" width="100px;"/><br /><sub>Simon Mumenthaler</sub>](https://www.shiftcode.ch)<br />[💻](https://github.com/shiftcode/dynamo-easy/commits?author=simonmumenthaler "Code") [📖](https://github.com/shiftcode/dynamo-easy/commits?author=simonmumenthaler "Documentation") [⚠️](https://github.com/shiftcode/dynamo-easy/commits?author=simonmumenthaler "Tests") |
358382
| :---: | :---:| :---: |
359383
<!-- ALL-CONTRIBUTORS-LIST:END -->
360384

0 commit comments

Comments
 (0)