Skip to content

Commit

Permalink
Merge pull request #94 from shiftcode/#78-transact-write-items
Browse files Browse the repository at this point in the history
#78 transact write items
  • Loading branch information
michaelwittwer authored Dec 6, 2018
2 parents 9c2c62d + ef7fed0 commit d922796
Show file tree
Hide file tree
Showing 73 changed files with 2,042 additions and 1,369 deletions.
82 changes: 53 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ When one of the following decorators is present, the value is always mapped to a
We only support the native Date type and you need to explicitly mark a property to be a Date by using the @Date() decorator\
(which is basically just syntactic sugar for @CustomMapper(TheDateMapper)).\
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:\
`updateDynamoEasyConfig({ dateMapper: MomentMapper })`\
`updateDynamoEasyConfig({ dateMapper: MomentMapper })`


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


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

# Request API
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.
Expand All @@ -237,6 +237,54 @@ We support the following dynamodb operations with a fluent api:

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

## non table tied requests
Currently two type of requests exists which are not tied to one table/model and therefore are not created from a DynamoStore instance.

### BatchGet

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
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.

```typescript
import { BatchGetRequest } from '@shiftcoders/dynamo-easy'
new BatchGetRequest()
// table with simple primary key
.forModel(YourModelWithPartitionKey, [{ id: 'myId' }], /* consistentRead */ true)
// table with composite primary key (sortkey is optional)
.forModel(YourModelWithCompositeKey, [{ id: 'myId', creationDate: new Date('2018-01-01') }])
.exec().subscribe(response => {
// an object where the items are mapped to the table name
})
```
### TransactWriteRequest
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.
The different operations are:
* [TransactConditionCheck](https://shiftcode.github.io/dynamo-easy/classes/_dynamo_transactwrite_transact_condition_check_.transactconditioncheck.html)
* [TransactPut](https://shiftcode.github.io/dynamo-easy/classes/_dynamo_transactwrite_transact_put_.transactput.html)
* [TransactDelete](https://shiftcode.github.io/dynamo-easy/classes/_dynamo_transactwrite_transact_delete_.transactdelete.html)
* [TransactUpdate](https://shiftcode.github.io/dynamo-easy/classes/_dynamo_transactwrite_transact_update_.transactupdate.html)

The transaction operations can optionally check for prerequisite conditions that must be satisfied before making updates.
For conditions not involving a an item to write on, use the TransactConditionCheck.

```typescript
import { TransactWriteRequest, TransactConditionCheck, TransactDelete, TransactPut, attribute } from '@shiftcoders/dynamo-easy'

new TransactWriteRequest()
.transact(
new TransactConditionCheck(YourModelWithPartitionKey, 'check-ID').onlyIf(attribute('age').gt(18)),
new TransactDelete(YourModelWithCompositeKey, 'the-partition-key', 'the-sort-key'),
new TransactPut(YourCustomModel, { id: 'put-ID-1', age: 21 }).ifNotExists(),
)
.returnItemCollectionMetrics('SIZE')
.returnConsumedCapacity('TOTAL')
.execFullResponse()
.subscribe(resp => {
console.log(resp.ItemCollectionMetrics)
console.log(resp.ConsumedCapacity)
})
```

# Authentication
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
SessionValidityEnsurer function to the DynamoStore when creating a new instance.
Expand All @@ -245,7 +293,7 @@ The default implementation is a no-op function.
## Session Validity Ensurer
Here is an example of an implementation using amazon cognito

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

## Pagination
TODO

## BatchGet

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
tables.
The first scenario is support using DynamoStore.batchGet() the second one must be implemented using the BatchGetItem class.

```typescript
const request = new BatchRequest()

// table with simple primary key
request.forModel(MyModelClass, ['idValue', 'idValue2'])

// table with composite primary key (sortkey is optional)
request.forModel(MyOtherModelClass, [{partitionKey: 'id', sortKey: 'sortKeyValue'}])

request.exec().subscribe(response => {
// an object where the items are mapped to the table name
})

```

# Development

## NPM scripts
Expand All @@ -331,7 +355,7 @@ request.exec().subscribe(response => {
- `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:)

## Automatic releases
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),
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),
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)

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

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
| [<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") |
| [<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") |
| :---: | :---:| :---: |
<!-- ALL-CONTRIBUTORS-LIST:END -->

Expand Down
Loading

0 comments on commit d922796

Please sign in to comment.