|
1 | | -# DynamoDB Toolbox - v0.2 |
| 1 | +# DynamoDB Toolbox |
2 | 2 |
|
3 | | -[](https://travis-ci.org/jeremydaly/dynamodb-toolbox) |
| 3 | +[](https://travis-ci.org/jeremydaly/dynamodb-toolbox) |
4 | 4 | [](https://www.npmjs.com/package/dynamodb-toolbox) |
5 | 5 | [](https://www.npmjs.com/package/dynamodb-toolbox) |
6 | 6 | [](https://coveralls.io/github/jeremydaly/dynamodb-toolbox?branch=master) |
7 | 7 |
|
8 | 8 |  |
9 | 9 |
|
10 | | - |
11 | | -### **NOTE:** This project is in BETA. Please submit [issues/feedback](https://github.com/jeremydaly/dynamodb-toolbox/issues) or feel free to contact me on Twitter [@jeremy_daly](https://twitter.com/jeremy_daly). |
12 | | - |
13 | 10 | ## Single Table Designs have never been this easy! |
14 | 11 |
|
15 | 12 | The **DynamoDB Toolbox** is a set of tools that makes it easy to work with [Amazon DynamoDB](https://aws.amazon.com/dynamodb/) and the [DocumentClient](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/dynamodb-example-document-client.html). It's designed with **Single Tables** in mind, but works just as well with multiple tables. It lets you define your Entities (with typings and aliases) and map them to your DynamoDB tables. You can then **generate the API parameters** to `put`, `get`, `delete`, `update`, `query`, `scan`, `batchGet`, and `batchWrite` data by passing in JavaScript objects. The DynamoDB Toolbox will map aliases, validate and coerce types, and even write complex `UpdateExpression`s for you. 😉 |
@@ -146,12 +143,13 @@ If you like working with ORMs, that's great, and you should definitely give thes |
146 | 143 | - **Projection Builder:** Specify which attributes and paths should be returned for each entity type, and automatically filter the results. |
147 | 144 | - **Secondary Index Support:** Map your secondary indexes (GSIs and LSIs) to your table, and dynamically link your entity attributes. |
148 | 145 | - **Batch Operations:** Full support for batch operations with a simpler interface to work with multiple entities and tables. |
| 146 | +- **Transactions:** Full support for transaction with a simpler interface to work with multiple entities and tables. |
149 | 147 | - **Default Value Dependency Graphs:** Create dynamic attribute defaults by chaining other dynamic attribute defaults together. |
| 148 | +- **TypeScript Support:** v0.3 of this library was rewritten in TypeScript to provide strong typing support. Additional work is still required to support schema typing. |
150 | 149 |
|
151 | 150 | ## Table of Contents |
152 | 151 |
|
153 | | -- [DynamoDB Toolbox - v0.2](#dynamodb-toolbox---v02) |
154 | | - - [**NOTE:** This project is in BETA. Please submit issues/feedback or feel free to contact me on Twitter [@jeremy_daly](https://twitter.com/jeremy_daly).](#note-this-project-is-in-beta-please-submit-issuesfeedback-or-feel-free-to-contact-me-on-twitter-jeremy_daly) |
| 152 | +- [DynamoDB Toolbox](#dynamodb-toolbox) |
155 | 153 | - [Single Table Designs have never been this easy!](#single-table-designs-have-never-been-this-easy) |
156 | 154 | - [Installation and Basic Usage](#installation-and-basic-usage) |
157 | 155 | - [This is *NOT* an ORM (at least it's not trying to be)](#this-is-not-an-orm-at-least-its-not-trying-to-be) |
@@ -184,6 +182,11 @@ If you like working with ORMs, that's great, and you should definitely give thes |
184 | 182 | - [Return Data](#return-data-2) |
185 | 183 | - [batchWrite(items [,options] [,parameters])](#batchwriteitems-options-parameters) |
186 | 184 | - [Return Data](#return-data-3) |
| 185 | + - [transactGet(items [,options] [,parameters])](#transactgetitems-options-parameters) |
| 186 | + - [Accessing items from multiple tables](#accessing-items-from-multiple-tables) |
| 187 | + - [Return Data](#return-data-4) |
| 188 | + - [transactWrite(items [,options] [,parameters])](#transactwriteitems-options-parameters) |
| 189 | + - [Return Data](#return-data-5) |
187 | 190 | - [parse(entity, input [,include])](#parseentity-input-include) |
188 | 191 | - [get(entity, key [,options] [,parameters])](#getentity-key-options-parameters) |
189 | 192 | - [delete(entity, key [,options] [,parameters])](#deleteentity-key-options-parameters) |
@@ -572,7 +575,7 @@ The `scan()` method accepts two arguments. The first argument is an `options` ob |
572 | 575 | | limit | `number` | The maximum number of items to retrieve per scan. (Limit) | |
573 | 576 | | consistent | `boolean` | Enable a consistent read of the items (ConsistentRead) | |
574 | 577 | | capacity | `string` | Return the amount of consumed capacity. One of either `none`, `total`, or `indexes` (ReturnConsumedCapacity) | |
575 | | -| select | `string` | The attributes to be returned in the result. One of either `string` | `all_attributes`, `all_projected_attributes`, `specific_attributes`, or `count` (Select) | |
| 578 | +| select | `string` | The attributes to be returned in the result. One of either `all_attributes`, `all_projected_attributes`, `specific_attributes`, or `count` (Select) | |
576 | 579 | | filters | `array` or `object` | A complex `object` or `array` of objects that specifies the scan's filter condition. See [Filters and Conditions](#filters-and-conditions). (FilterExpression) | |
577 | 580 | | attributes | `array` or `object` | An `array` or array of complex `objects` that specify which attributes should be returned. See [Projection Expression](#projection-expression) below (ProjectionExpression) | |
578 | 581 | | startKey | `object` | An object that contains the `partitionKey` and `sortKey` of the first item that this operation will evaluate. (ExclusiveStartKey) | |
@@ -689,6 +692,82 @@ If you prefer to specify your own parameters, the optional third argument allows |
689 | 692 | #### Return Data |
690 | 693 | The data is returned with the same response syntax as the [DynamoDB BatchWriteItem API](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchWriteItem.html). If `autoExecute` and `autoParse` are enabled, a `.next()` method will be available on the returned object. Calling this function will call the `batchWrite` method again using the same options and passing any `UnprocessedItems` in as the `RequestItems`. This is a convenience method for retrying unprocessed keys. |
691 | 694 |
|
| 695 | + |
| 696 | + |
| 697 | + |
| 698 | + |
| 699 | +### transactGet(items [,options] [,parameters]) |
| 700 | + |
| 701 | +> TransactGetItems is a synchronous operation that atomically retrieves multiple items from one or more tables (but not from indexes) in a single account and Region. |
| 702 | +
|
| 703 | +The `transactGet` method is a wrapper for the [DynamoDB TransactGetItems API](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_TransactGetItems.html). The DynamoDB Toolbox `transactGet` method supports all **TransactGetItem** API operations. The `transactGet` method returns a `Promise` and you must use `await` or `.then()` to retrieve the results. An alternative, synchronous method named `transactGetParams` can be used, but will only retrieve the generated parameters. |
| 704 | + |
| 705 | +The `transacthGet` method accepts three arguments. The first is an `array` of item keys to get. The DynamoDB Toolbox provides the `getTransaction` method on your entities to help you generate the proper key configuration. You can specify different entity types as well as entities from different tables, and this library will handle the proper payload construction. |
| 706 | + |
| 707 | +The optional second argument accepts an `options` object. The following options are all optional (corresponding TransactGetItems API references in parentheses): |
| 708 | + |
| 709 | +| Option | Type | Description | |
| 710 | +| -------- | :--: | ----------- | |
| 711 | +| capacity | `string` | Return the amount of consumed capacity. One of either `none`, `total`, or `indexes` (ReturnConsumedCapacity) | |
| 712 | +| execute | `boolean` | Enables/disables automatic execution of the DocumentClient method (default: *inherited from Table*) | |
| 713 | +| parse | `boolean` | Enables/disables automatic parsing of returned data when `autoExecute` evaluates to `true` (default: *inherited from Table*) | |
| 714 | + |
| 715 | +#### Accessing items from multiple tables |
| 716 | +Transaction items are atomic, so each `Get` contains the table name and key necessary to retrieve the item. The library will automatically handle adding the necessary information and will parse each entity automatically for you. |
| 717 | + |
| 718 | +```javascript |
| 719 | +const results = await MyTable.transactGet( |
| 720 | + [ |
| 721 | + User.getTransaction({ family: 'Brady', name: 'Mike' }), |
| 722 | + User.getTransaction({ family: 'Brady', name: 'Carol' }), |
| 723 | + Pet.getTransaction({ family: 'Brady', name: 'Tiger' }) |
| 724 | + ], |
| 725 | + { capacity: 'total' } |
| 726 | +) |
| 727 | +``` |
| 728 | + |
| 729 | +If you prefer to specify your own parameters, the optional third argument allows you to add custom parameters. [See Adding custom parameters and clauses](#adding-custom-parameters-and-clauses) for more information. |
| 730 | + |
| 731 | +#### Return Data |
| 732 | +The data is returned with the same response syntax as the [DynamoDB TransactGetItems API](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_TransactGetItems.html). If `autoExecute` and `autoParse` are enabled, any `Responses` data returned will be parsed into its corresponding Entity's aliases. Otherwise, the DocumentClient will return the unmarshalled data. |
| 733 | + |
| 734 | +### transactWrite(items [,options] [,parameters]) |
| 735 | + |
| 736 | +> TransactWriteItems is a synchronous write operation that groups up to 25 action requests. The actions are completed atomically so that either all of them succeed, or all of them fail. |
| 737 | +
|
| 738 | +The `transactWrite` method is a wrapper for the [DynamoDB TransactWriteItems API](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_TransactWriteItems.html). The DynamoDB Toolbox `transactWrite` method supports all **TransactWriteItems** API operations. The `transactWrite` method returns a `Promise` and you must use `await` or `.then()` to retrieve the results. An alternative, synchronous method named `transactWriteParams` can be used, but will only retrieve the generated parameters. |
| 739 | + |
| 740 | +The `transactWrite` method accepts three arguments. The first is an `array` of item keys to either `put`, `delete`, `update` or `conditionCheck`. The DynamoDB Toolbox provides `putTransaction`,`deleteTransaction`, `updateTransaction`, and `conditionCheck` methods on your entities to help you generate the proper configuration for each item. You can specify different entity types as well as entities from different tables, and this library will handle the proper payload construction. |
| 741 | + |
| 742 | +The optional second argument accepts an `options` object. The following options are all optional (corresponding TransactWriteItems API references in parentheses): |
| 743 | + |
| 744 | +| Option | Type | Description | |
| 745 | +| -------- | :--: | ----------- | |
| 746 | +| capacity | `string` | Return the amount of consumed capacity. One of either `none`, `total`, or `indexes` (ReturnConsumedCapacity) | |
| 747 | +| metrics | `string` | Return item collection metrics. If set to `size`, the response includes statistics about item collections, if any, that were modified during the operation are returned in the response. One of either `none` or `size` (ReturnItemCollectionMetrics) | |
| 748 | +| token | `string` | Optional token to make the call idempotent, meaning that multiple identical calls have the same effect as one single call. (ClientRequestToken) | |
| 749 | +| execute | `boolean` | Enables/disables automatic execution of the DocumentClient method (default: *inherited from Entity*) | |
| 750 | +| parse | `boolean` | Enables/disables automatic parsing of returned data when `autoExecute` evaluates to `true` (default: *inherited from Entity*) | |
| 751 | + |
| 752 | +```javascript |
| 753 | +const result = await Default.transactWrite( |
| 754 | + [ |
| 755 | + Pet.conditionCheck({ family: 'Brady', name: 'Tiger' }, { conditions: { attr: 'alive', eq: false } }, |
| 756 | + Pet.deleteTransaction({ family: 'Brady', name: 'Tiger' }), |
| 757 | + User.putTransaction({ family: 'Brady', name: 'Carol', age: 40, roles: ['mother','wife'] }), |
| 758 | + User.putTransaction({ family: 'Brady', name: 'Mike', age: 42, roles: ['father','husband'] }) |
| 759 | + ],{ |
| 760 | + capacity: 'total', |
| 761 | + metrics: 'size', |
| 762 | + } |
| 763 | +) |
| 764 | +``` |
| 765 | +
|
| 766 | +If you prefer to specify your own parameters, the optional third argument allows you to add custom parameters. [See Adding custom parameters and clauses](#adding-custom-parameters-and-clauses) for more information. |
| 767 | +
|
| 768 | +#### Return Data |
| 769 | +The data is returned with the same response syntax as the [DynamoDB TransactWriteItems API](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_TransactWriteItems.html). |
| 770 | +
|
692 | 771 | ### parse(entity, input [,include]) |
693 | 772 |
|
694 | 773 | Executes the `parse` method of the supplied `entity`. The `entity` must be a `string` that references the name of an Entity associated with the table. See the [Entity `parse` method](#parseinput-include) for additional parameters and behavior. |
@@ -1182,4 +1261,4 @@ let results = await MyEntity.update(item, {}, { |
1182 | 1261 | <IMG SRC="https://ad.doubleclick.net/ddm/trackimp/N1116303.3950900PODSEARCH.COM/B24770737.285235234;dc_trk_aid=479074825;dc_trk_cid=139488579;ord=[timestamp];dc_lat=;dc_rdid=;tag_for_child_directed_treatment=;tfua=;gdpr=${GDPR};gdpr_consent=${GDPR_CONSENT_755}?" BORDER="0" HEIGHT="1" WIDTH="1" ALT="Advertisement"> |
1183 | 1262 |
|
1184 | 1263 | ## Contributions and Feedback |
1185 | | -Contributions, ideas and bug reports are welcome and greatly appreciated. Please add [issues](https://github.com/jeremydaly/dynamodb-toolbox/issues) for suggestions and bug reports or create a pull request. You can also contact me on Twitter: [@jeremy_daly](https://twitter.com/jeremy_daly). |
| 1264 | +Contributions, ideas and bug reports are welcome and greatly appreciated. Please add [issues](https://github.com/jeremydaly/dynamodb-toolbox/issues) for suggestions and bug reports or create a pull request. You can also contact me on Twitter: [@jeremy_daly](https://twitter.com/jeremy_daly). |
0 commit comments