├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── docs ├── assets │ ├── css │ │ ├── main.css │ │ └── main.css.map │ ├── images │ │ ├── icons.png │ │ ├── icons@2x.png │ │ ├── widgets.png │ │ └── widgets@2x.png │ └── js │ │ ├── main.js │ │ └── search.js ├── globals.html ├── index.html └── packages │ ├── dynamodb-auto-marshaller │ ├── assets │ │ ├── css │ │ │ ├── main.css │ │ │ └── main.css.map │ │ ├── images │ │ │ ├── icons.png │ │ │ ├── icons@2x.png │ │ │ ├── widgets.png │ │ │ └── widgets@2x.png │ │ └── js │ │ │ ├── main.js │ │ │ └── search.js │ ├── classes │ │ ├── binaryset.html │ │ ├── marshaller.html │ │ ├── numbervalue.html │ │ ├── numbervalueset.html │ │ └── objectset.html │ ├── globals.html │ ├── index.html │ └── interfaces │ │ ├── marshallingoptions.html │ │ ├── unmarshalledlistattributevalue.html │ │ └── unmarshalledmapattributevalue.html │ ├── dynamodb-batch-iterator │ ├── assets │ │ ├── css │ │ │ ├── main.css │ │ │ └── main.css.map │ │ ├── images │ │ │ ├── icons.png │ │ │ ├── icons@2x.png │ │ │ ├── widgets.png │ │ │ └── widgets@2x.png │ │ └── js │ │ │ ├── main.js │ │ │ └── search.js │ ├── classes │ │ ├── batchget.html │ │ ├── batchoperation.html │ │ └── batchwrite.html │ ├── globals.html │ ├── index.html │ └── interfaces │ │ ├── batchgetoptions.html │ │ ├── batchstate.html │ │ ├── pertableoptions.html │ │ ├── tableoptions.html │ │ ├── tablestate.html │ │ ├── tablethrottlingtracker.html │ │ └── throttledtableconfiguration.html │ ├── dynamodb-data-mapper-annotations │ ├── assets │ │ ├── css │ │ │ ├── main.css │ │ │ └── main.css.map │ │ ├── images │ │ │ ├── icons.png │ │ │ ├── icons@2x.png │ │ │ ├── widgets.png │ │ │ └── widgets@2x.png │ │ └── js │ │ │ ├── main.js │ │ │ └── search.js │ ├── classes │ │ ├── author.html │ │ ├── comment.html │ │ └── post.html │ ├── globals.html │ ├── index.html │ └── interfaces │ │ ├── classannotation.html │ │ └── propertyannotation.html │ ├── dynamodb-data-mapper │ ├── assets │ │ ├── css │ │ │ ├── main.css │ │ │ └── main.css.map │ │ ├── images │ │ │ ├── icons.png │ │ │ ├── icons@2x.png │ │ │ ├── widgets.png │ │ │ └── widgets@2x.png │ │ └── js │ │ │ ├── main.js │ │ │ └── search.js │ ├── classes │ │ ├── datamapper.html │ │ ├── itemnotfoundexception.html │ │ ├── iterator.html │ │ ├── paginator.html │ │ ├── parallelscaniterator.html │ │ ├── parallelscanpaginator.html │ │ ├── queryiterator.html │ │ ├── querypaginator.html │ │ ├── scaniterator.html │ │ └── scanpaginator.html │ ├── globals.html │ ├── index.html │ └── interfaces │ │ ├── basescanoptions.html │ │ ├── basesequentialscanoptions.html │ │ ├── batchgetoptions.html │ │ ├── batchgettableoptions.html │ │ ├── batchstate.html │ │ ├── createtableoptions.html │ │ ├── ctorbearer.html │ │ ├── datamapperconfiguration.html │ │ ├── deleteoptions.html │ │ ├── deleteparameters.html │ │ ├── documenttypeoptions.html │ │ ├── executeupdateexpressionoptions.html │ │ ├── getoptions.html │ │ ├── getparameters.html │ │ ├── globalsecondaryindexoptions.html │ │ ├── initializedscanstate.html │ │ ├── localsecondaryindexoptions.html │ │ ├── parallelscanoptions.html │ │ ├── parallelscanworkeroptions.html │ │ ├── perindexoptions.html │ │ ├── provisionedthroughput.html │ │ ├── putoptions.html │ │ ├── putparameters.html │ │ ├── queryoptions.html │ │ ├── queryparameters.html │ │ ├── readconsistencyconfiguration.html │ │ ├── scanoptions.html │ │ ├── sharedsecondaryindexoptions.html │ │ ├── stringtoanyobjectmap.html │ │ ├── uninitializedscanstate.html │ │ ├── updateoptions.html │ │ └── updateparameters.html │ ├── dynamodb-data-marshaller │ ├── assets │ │ ├── css │ │ │ ├── main.css │ │ │ └── main.css.map │ │ ├── images │ │ │ ├── icons.png │ │ │ ├── icons@2x.png │ │ │ ├── widgets.png │ │ │ └── widgets@2x.png │ │ └── js │ │ │ ├── main.js │ │ │ └── search.js │ ├── classes │ │ ├── invalidschemaerror.html │ │ └── invalidvalueerror.html │ ├── globals.html │ ├── index.html │ └── interfaces │ │ ├── anytype.html │ │ ├── attributetypemap.html │ │ ├── basetype.html │ │ ├── binarytype.html │ │ ├── booleantype.html │ │ ├── collectiontype.html │ │ ├── customtype.html │ │ ├── datetype.html │ │ ├── documenttype.html │ │ ├── hashtype.html │ │ ├── keyabletype.html │ │ ├── keyschema.html │ │ ├── keytypemap.html │ │ ├── listtype.html │ │ ├── maptype.html │ │ ├── marshalledexpression.html │ │ ├── nulltype.html │ │ ├── numbertype.html │ │ ├── perindexkeys.html │ │ ├── schema.html │ │ ├── settype.html │ │ ├── stringtype.html │ │ ├── tupletype.html │ │ └── zeroargumentsconstructor.html │ ├── dynamodb-expressions │ ├── assets │ │ ├── css │ │ │ ├── main.css │ │ │ └── main.css.map │ │ ├── images │ │ │ ├── icons.png │ │ │ ├── icons@2x.png │ │ │ ├── widgets.png │ │ │ └── widgets@2x.png │ │ └── js │ │ │ ├── main.js │ │ │ └── search.js │ ├── classes │ │ ├── attributepath.html │ │ ├── attributevalue.html │ │ ├── expressionattributes.html │ │ ├── functionexpression.html │ │ ├── mathematicalexpression.html │ │ └── updateexpression.html │ ├── globals.html │ ├── index.html │ └── interfaces │ │ ├── andexpression.html │ │ ├── attributebearingexpression.html │ │ ├── attributeexistspredicate.html │ │ ├── attributename.html │ │ ├── attributenotexistspredicate.html │ │ ├── attributetypepredicate.html │ │ ├── basefunctionexpressionpredicate.html │ │ ├── beginswithpredicate.html │ │ ├── betweenexpressionpredicate.html │ │ ├── binarycomparisonpredicate.html │ │ ├── conditionexpressionsubject.html │ │ ├── containspredicate.html │ │ ├── equalityexpressionpredicate.html │ │ ├── greaterthanexpressionpredicate.html │ │ ├── greaterthanorequaltoexpressionpredicate.html │ │ ├── inequalityexpressionpredicate.html │ │ ├── lessthanexpressionpredicate.html │ │ ├── lessthanorequaltoexpressionpredicate.html │ │ ├── listindex.html │ │ ├── membershipexpressionpredicate.html │ │ ├── notexpression.html │ │ └── orexpression.html │ └── dynamodb-query-iterator │ ├── assets │ ├── css │ │ ├── main.css │ │ └── main.css.map │ ├── images │ │ ├── icons.png │ │ ├── icons@2x.png │ │ ├── widgets.png │ │ └── widgets@2x.png │ └── js │ │ ├── main.js │ │ └── search.js │ ├── classes │ ├── dynamodbpaginator.html │ ├── itemiterator.html │ ├── parallelscaniterator.html │ ├── parallelscanpaginator.html │ ├── queryiterator.html │ ├── querypaginator.html │ ├── scaniterator.html │ └── scanpaginator.html │ ├── globals.html │ ├── index.html │ └── interfaces │ ├── dynamodbpaginatorinterface.html │ ├── dynamodbresultspage.html │ ├── initializedscanstate.html │ ├── parallelscaninput.html │ └── uninitializedscanstate.html ├── lerna.json ├── package.json ├── packages ├── dynamodb-auto-marshaller │ ├── .npmignore │ ├── LICENSE │ ├── README.md │ ├── package.json │ ├── src │ │ ├── BinarySet.spec.ts │ │ ├── BinarySet.ts │ │ ├── Marshaller.spec.ts │ │ ├── Marshaller.ts │ │ ├── NumberValue.spec.ts │ │ ├── NumberValue.ts │ │ ├── NumberValueSet.spec.ts │ │ ├── NumberValueSet.ts │ │ ├── ObjectSet.ts │ │ ├── index.ts │ │ ├── isArrayBuffer.spec.ts │ │ └── isArrayBuffer.ts │ ├── tsconfig.json │ └── tsconfig.test.json ├── dynamodb-batch-iterator │ ├── .npmignore │ ├── CHANGELOG.md │ ├── LICENSE │ ├── README.md │ ├── package.json │ ├── src │ │ ├── BatchGet.spec.ts │ │ ├── BatchGet.ts │ │ ├── BatchGetOptions.ts │ │ ├── BatchOperation.ts │ │ ├── BatchWrite.spec.ts │ │ ├── BatchWrite.ts │ │ ├── index.ts │ │ ├── itemIdentifier.spec.ts │ │ ├── itemIdentifier.ts │ │ └── types.ts │ ├── tsconfig.json │ └── tsconfig.test.json ├── dynamodb-data-mapper-annotations │ ├── .npmignore │ ├── LICENSE │ ├── README.md │ ├── package.json │ ├── src │ │ ├── annotationShapes.ts │ │ ├── attribute.spec.ts │ │ ├── attribute.ts │ │ ├── autoGeneratedHashKey.spec.ts │ │ ├── autoGeneratedHashKey.ts │ │ ├── constants.ts │ │ ├── exampleSchema.fixture.ts │ │ ├── functional.spec.ts │ │ ├── hashKey.spec.ts │ │ ├── hashKey.ts │ │ ├── index.ts │ │ ├── rangeKey.spec.ts │ │ ├── rangeKey.ts │ │ ├── table.spec.ts │ │ ├── table.ts │ │ ├── versionAttribute.spec.ts │ │ └── versionAttribute.ts │ ├── tsconfig.json │ └── tsconfig.test.json ├── dynamodb-data-mapper │ ├── .gitignore │ ├── .npmignore │ ├── CHANGELOG.md │ ├── LICENSE │ ├── README.md │ ├── jest.integration.js │ ├── package.json │ ├── src │ │ ├── BatchState.ts │ │ ├── DataMapper.integ.ts │ │ ├── DataMapper.spec.ts │ │ ├── DataMapper.ts │ │ ├── ItemNotFoundException.spec.ts │ │ ├── ItemNotFoundException.ts │ │ ├── Iterator.ts │ │ ├── Paginator.ts │ │ ├── ParallelScanIterator.ts │ │ ├── ParallelScanPaginator.ts │ │ ├── QueryIterator.ts │ │ ├── QueryPaginator.ts │ │ ├── ScanIterator.ts │ │ ├── ScanPaginator.ts │ │ ├── asyncIteratorSymbolPolyfill.ts │ │ ├── buildScanInput.ts │ │ ├── constants.ts │ │ ├── embed.spec.ts │ │ ├── embed.ts │ │ ├── index.ts │ │ ├── marshallStartKey.ts │ │ ├── namedParameters │ │ │ ├── BatchGetOptions.ts │ │ │ ├── CreateTableOptions.ts │ │ │ ├── DataMapperConfiguration.ts │ │ │ ├── DeleteOptions.ts │ │ │ ├── ExecuteUpdateExpressionOptions.ts │ │ │ ├── GetOptions.ts │ │ │ ├── ProvisionedThroughput.ts │ │ │ ├── PutOptions.ts │ │ │ ├── QueryOptions.ts │ │ │ ├── ReadConsistencyConfiguration.ts │ │ │ ├── ScanOptions.ts │ │ │ ├── SecondaryIndexOptions.ts │ │ │ ├── UpdateOptions.ts │ │ │ └── index.ts │ │ ├── protocols.spec.ts │ │ └── protocols.ts │ ├── tsconfig.json │ └── tsconfig.test.json ├── dynamodb-data-marshaller │ ├── .npmignore │ ├── LICENSE │ ├── README.md │ ├── package.json │ ├── src │ │ ├── InvalidSchemaError.ts │ │ ├── InvalidValueError.ts │ │ ├── KeySchema.ts │ │ ├── Schema.spec.ts │ │ ├── Schema.ts │ │ ├── SchemaType.spec.ts │ │ ├── SchemaType.ts │ │ ├── index.ts │ │ ├── isKey.spec.ts │ │ ├── isKey.ts │ │ ├── keysFromSchema.spec.ts │ │ ├── keysFromSchema.ts │ │ ├── marshallExpression.spec.ts │ │ ├── marshallExpression.ts │ │ ├── marshallItem.spec.ts │ │ ├── marshallItem.ts │ │ ├── marshallKey.spec.ts │ │ ├── marshallKey.ts │ │ ├── toSchemaName.spec.ts │ │ ├── toSchemaName.ts │ │ ├── unmarshallItem.spec.ts │ │ └── unmarshallItem.ts │ ├── tsconfig.json │ └── tsconfig.test.json ├── dynamodb-expressions │ ├── .npmignore │ ├── LICENSE │ ├── README.md │ ├── package.json │ ├── src │ │ ├── AttributeBearingExpression.ts │ │ ├── AttributePath.spec.ts │ │ ├── AttributePath.ts │ │ ├── AttributeValue.spec.ts │ │ ├── AttributeValue.ts │ │ ├── ConditionExpression.spec.ts │ │ ├── ConditionExpression.ts │ │ ├── ExpressionAttributes.spec.ts │ │ ├── ExpressionAttributes.ts │ │ ├── FunctionExpression.spec.ts │ │ ├── FunctionExpression.ts │ │ ├── MathematicalExpression.spec.ts │ │ ├── MathematicalExpression.ts │ │ ├── ProjectionExpression.spec.ts │ │ ├── ProjectionExpression.ts │ │ ├── UpdateExpression.spec.ts │ │ ├── UpdateExpression.ts │ │ └── index.ts │ ├── tsconfig.json │ └── tsconfig.test.json └── dynamodb-query-iterator │ ├── .npmignore │ ├── CHANGELOG.md │ ├── LICENSE │ ├── README.md │ ├── package.json │ ├── src │ ├── DynamoDbPaginator.ts │ ├── DynamoDbPaginatorInterface.ts │ ├── DynamoDbResultsPage.ts │ ├── ItemIterator.ts │ ├── ParallelScanInput.ts │ ├── ParallelScanIterator.spec.ts │ ├── ParallelScanIterator.ts │ ├── ParallelScanPaginator.spec.ts │ ├── ParallelScanPaginator.ts │ ├── QueryIterator.spec.ts │ ├── QueryIterator.ts │ ├── QueryPaginator.spec.ts │ ├── QueryPaginator.ts │ ├── ScanIterator.spec.ts │ ├── ScanIterator.ts │ ├── ScanPaginator.spec.ts │ ├── ScanPaginator.ts │ ├── index.ts │ ├── mergeConsumedCapacities.spec.ts │ └── mergeConsumedCapacities.ts │ ├── tsconfig.json │ └── tsconfig.test.json ├── src └── index.ts └── tsconfig.json /.gitignore: -------------------------------------------------------------------------------- 1 | yarn.lock 2 | package-lock.json 3 | .idea/ 4 | 5 | # Created by .ignore support plugin (hsz.mobi) 6 | logs 7 | *.log 8 | npm-debug.log* 9 | yarn-debug.log* 10 | yarn-error.log* 11 | pids 12 | *.pid 13 | *.seed 14 | *.pid.lock 15 | .vscode 16 | lib-cov 17 | coverage 18 | build 19 | .nyc_output 20 | .grunt 21 | bower_components 22 | .lock-wscript 23 | build/Release 24 | node_modules/ 25 | jspm_packages/ 26 | typings/ 27 | .npm 28 | .eslintcache 29 | .node_repl_history 30 | *.tgz 31 | .yarn-integrity 32 | .env 33 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "node" 4 | - "lts/*" 5 | - "11" 6 | - "10" 7 | - "9" 8 | - "8" 9 | - "6" 10 | 11 | sudo: false 12 | 13 | before_script: 14 | - npm run bootstrap 15 | 16 | script: 17 | - npm test -- --runInBand 18 | -------------------------------------------------------------------------------- /docs/assets/images/icons.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/dynamodb-data-mapper-js/3ffe4bfb9187cf21bf87f72212526fa72172e555/docs/assets/images/icons.png -------------------------------------------------------------------------------- /docs/assets/images/icons@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/dynamodb-data-mapper-js/3ffe4bfb9187cf21bf87f72212526fa72172e555/docs/assets/images/icons@2x.png -------------------------------------------------------------------------------- /docs/assets/images/widgets.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/dynamodb-data-mapper-js/3ffe4bfb9187cf21bf87f72212526fa72172e555/docs/assets/images/widgets.png -------------------------------------------------------------------------------- /docs/assets/images/widgets@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/dynamodb-data-mapper-js/3ffe4bfb9187cf21bf87f72212526fa72172e555/docs/assets/images/widgets@2x.png -------------------------------------------------------------------------------- /docs/assets/js/search.js: -------------------------------------------------------------------------------- 1 | var typedoc = typedoc || {}; 2 | typedoc.search = typedoc.search || {}; 3 | typedoc.search.data = {"kinds":{},"rows":[]}; -------------------------------------------------------------------------------- /docs/packages/dynamodb-auto-marshaller/assets/images/icons.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/dynamodb-data-mapper-js/3ffe4bfb9187cf21bf87f72212526fa72172e555/docs/packages/dynamodb-auto-marshaller/assets/images/icons.png -------------------------------------------------------------------------------- /docs/packages/dynamodb-auto-marshaller/assets/images/icons@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/dynamodb-data-mapper-js/3ffe4bfb9187cf21bf87f72212526fa72172e555/docs/packages/dynamodb-auto-marshaller/assets/images/icons@2x.png -------------------------------------------------------------------------------- /docs/packages/dynamodb-auto-marshaller/assets/images/widgets.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/dynamodb-data-mapper-js/3ffe4bfb9187cf21bf87f72212526fa72172e555/docs/packages/dynamodb-auto-marshaller/assets/images/widgets.png -------------------------------------------------------------------------------- /docs/packages/dynamodb-auto-marshaller/assets/images/widgets@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/dynamodb-data-mapper-js/3ffe4bfb9187cf21bf87f72212526fa72172e555/docs/packages/dynamodb-auto-marshaller/assets/images/widgets@2x.png -------------------------------------------------------------------------------- /docs/packages/dynamodb-batch-iterator/assets/images/icons.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/dynamodb-data-mapper-js/3ffe4bfb9187cf21bf87f72212526fa72172e555/docs/packages/dynamodb-batch-iterator/assets/images/icons.png -------------------------------------------------------------------------------- /docs/packages/dynamodb-batch-iterator/assets/images/icons@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/dynamodb-data-mapper-js/3ffe4bfb9187cf21bf87f72212526fa72172e555/docs/packages/dynamodb-batch-iterator/assets/images/icons@2x.png -------------------------------------------------------------------------------- /docs/packages/dynamodb-batch-iterator/assets/images/widgets.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/dynamodb-data-mapper-js/3ffe4bfb9187cf21bf87f72212526fa72172e555/docs/packages/dynamodb-batch-iterator/assets/images/widgets.png -------------------------------------------------------------------------------- /docs/packages/dynamodb-batch-iterator/assets/images/widgets@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/dynamodb-data-mapper-js/3ffe4bfb9187cf21bf87f72212526fa72172e555/docs/packages/dynamodb-batch-iterator/assets/images/widgets@2x.png -------------------------------------------------------------------------------- /docs/packages/dynamodb-data-mapper-annotations/assets/images/icons.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/dynamodb-data-mapper-js/3ffe4bfb9187cf21bf87f72212526fa72172e555/docs/packages/dynamodb-data-mapper-annotations/assets/images/icons.png -------------------------------------------------------------------------------- /docs/packages/dynamodb-data-mapper-annotations/assets/images/icons@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/dynamodb-data-mapper-js/3ffe4bfb9187cf21bf87f72212526fa72172e555/docs/packages/dynamodb-data-mapper-annotations/assets/images/icons@2x.png -------------------------------------------------------------------------------- /docs/packages/dynamodb-data-mapper-annotations/assets/images/widgets.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/dynamodb-data-mapper-js/3ffe4bfb9187cf21bf87f72212526fa72172e555/docs/packages/dynamodb-data-mapper-annotations/assets/images/widgets.png -------------------------------------------------------------------------------- /docs/packages/dynamodb-data-mapper-annotations/assets/images/widgets@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/dynamodb-data-mapper-js/3ffe4bfb9187cf21bf87f72212526fa72172e555/docs/packages/dynamodb-data-mapper-annotations/assets/images/widgets@2x.png -------------------------------------------------------------------------------- /docs/packages/dynamodb-data-mapper-annotations/assets/js/search.js: -------------------------------------------------------------------------------- 1 | var typedoc = typedoc || {}; 2 | typedoc.search = typedoc.search || {}; 3 | typedoc.search.data = {"kinds":{"32":"Variable","64":"Function","128":"Class","256":"Interface","1024":"Property"},"rows":[{"id":0,"kind":256,"name":"ClassAnnotation","url":"interfaces/classannotation.html","classes":"tsd-kind-interface"},{"id":1,"kind":256,"name":"PropertyAnnotation","url":"interfaces/propertyannotation.html","classes":"tsd-kind-interface"},{"id":2,"kind":32,"name":"METADATA_TYPE_KEY","url":"globals.html#metadata_type_key","classes":"tsd-kind-variable"},{"id":3,"kind":64,"name":"attribute","url":"globals.html#attribute","classes":"tsd-kind-function"},{"id":4,"kind":64,"name":"hashKey","url":"globals.html#hashkey","classes":"tsd-kind-function"},{"id":5,"kind":64,"name":"autoGeneratedHashKey","url":"globals.html#autogeneratedhashkey","classes":"tsd-kind-function"},{"id":6,"kind":64,"name":"rangeKey","url":"globals.html#rangekey","classes":"tsd-kind-function"},{"id":7,"kind":64,"name":"table","url":"globals.html#table","classes":"tsd-kind-function"},{"id":8,"kind":64,"name":"versionAttribute","url":"globals.html#versionattribute","classes":"tsd-kind-function"},{"id":9,"kind":128,"name":"Author","url":"classes/author.html","classes":"tsd-kind-class"},{"id":10,"kind":1024,"name":"name","url":"classes/author.html#name","classes":"tsd-kind-property tsd-parent-kind-class","parent":"Author"},{"id":11,"kind":1024,"name":"socialMediaHandles","url":"classes/author.html#socialmediahandles","classes":"tsd-kind-property tsd-parent-kind-class","parent":"Author"},{"id":12,"kind":1024,"name":"photo","url":"classes/author.html#photo","classes":"tsd-kind-property tsd-parent-kind-class","parent":"Author"},{"id":13,"kind":128,"name":"Comment","url":"classes/comment.html","classes":"tsd-kind-class"},{"id":14,"kind":1024,"name":"timestamp","url":"classes/comment.html#timestamp","classes":"tsd-kind-property tsd-parent-kind-class","parent":"Comment"},{"id":15,"kind":1024,"name":"approved","url":"classes/comment.html#approved","classes":"tsd-kind-property tsd-parent-kind-class","parent":"Comment"},{"id":16,"kind":1024,"name":"subject","url":"classes/comment.html#subject","classes":"tsd-kind-property tsd-parent-kind-class","parent":"Comment"},{"id":17,"kind":1024,"name":"text","url":"classes/comment.html#text","classes":"tsd-kind-property tsd-parent-kind-class","parent":"Comment"},{"id":18,"kind":1024,"name":"author","url":"classes/comment.html#author","classes":"tsd-kind-property tsd-parent-kind-class","parent":"Comment"},{"id":19,"kind":1024,"name":"upvotes","url":"classes/comment.html#upvotes","classes":"tsd-kind-property tsd-parent-kind-class","parent":"Comment"},{"id":20,"kind":1024,"name":"downvotes","url":"classes/comment.html#downvotes","classes":"tsd-kind-property tsd-parent-kind-class","parent":"Comment"},{"id":21,"kind":1024,"name":"replies","url":"classes/comment.html#replies","classes":"tsd-kind-property tsd-parent-kind-class","parent":"Comment"},{"id":22,"kind":128,"name":"Post","url":"classes/post.html","classes":"tsd-kind-class"},{"id":23,"kind":1024,"name":"id","url":"classes/post.html#id","classes":"tsd-kind-property tsd-parent-kind-class","parent":"Post"},{"id":24,"kind":1024,"name":"createdAt","url":"classes/post.html#createdat","classes":"tsd-kind-property tsd-parent-kind-class","parent":"Post"},{"id":25,"kind":1024,"name":"version","url":"classes/post.html#version","classes":"tsd-kind-property tsd-parent-kind-class","parent":"Post"},{"id":26,"kind":1024,"name":"author","url":"classes/post.html#author","classes":"tsd-kind-property tsd-parent-kind-class","parent":"Post"},{"id":27,"kind":1024,"name":"content","url":"classes/post.html#content","classes":"tsd-kind-property tsd-parent-kind-class","parent":"Post"},{"id":28,"kind":1024,"name":"title","url":"classes/post.html#title","classes":"tsd-kind-property tsd-parent-kind-class","parent":"Post"},{"id":29,"kind":1024,"name":"subtitle","url":"classes/post.html#subtitle","classes":"tsd-kind-property tsd-parent-kind-class","parent":"Post"},{"id":30,"kind":1024,"name":"imageLink","url":"classes/post.html#imagelink","classes":"tsd-kind-property tsd-parent-kind-class","parent":"Post"},{"id":31,"kind":1024,"name":"corrections","url":"classes/post.html#corrections","classes":"tsd-kind-property tsd-parent-kind-class","parent":"Post"},{"id":32,"kind":1024,"name":"replies","url":"classes/post.html#replies","classes":"tsd-kind-property tsd-parent-kind-class","parent":"Post"},{"id":33,"kind":1024,"name":"tags","url":"classes/post.html#tags","classes":"tsd-kind-property tsd-parent-kind-class","parent":"Post"}]}; -------------------------------------------------------------------------------- /docs/packages/dynamodb-data-mapper/assets/images/icons.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/dynamodb-data-mapper-js/3ffe4bfb9187cf21bf87f72212526fa72172e555/docs/packages/dynamodb-data-mapper/assets/images/icons.png -------------------------------------------------------------------------------- /docs/packages/dynamodb-data-mapper/assets/images/icons@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/dynamodb-data-mapper-js/3ffe4bfb9187cf21bf87f72212526fa72172e555/docs/packages/dynamodb-data-mapper/assets/images/icons@2x.png -------------------------------------------------------------------------------- /docs/packages/dynamodb-data-mapper/assets/images/widgets.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/dynamodb-data-mapper-js/3ffe4bfb9187cf21bf87f72212526fa72172e555/docs/packages/dynamodb-data-mapper/assets/images/widgets.png -------------------------------------------------------------------------------- /docs/packages/dynamodb-data-mapper/assets/images/widgets@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/dynamodb-data-mapper-js/3ffe4bfb9187cf21bf87f72212526fa72172e555/docs/packages/dynamodb-data-mapper/assets/images/widgets@2x.png -------------------------------------------------------------------------------- /docs/packages/dynamodb-data-marshaller/assets/images/icons.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/dynamodb-data-mapper-js/3ffe4bfb9187cf21bf87f72212526fa72172e555/docs/packages/dynamodb-data-marshaller/assets/images/icons.png -------------------------------------------------------------------------------- /docs/packages/dynamodb-data-marshaller/assets/images/icons@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/dynamodb-data-mapper-js/3ffe4bfb9187cf21bf87f72212526fa72172e555/docs/packages/dynamodb-data-marshaller/assets/images/icons@2x.png -------------------------------------------------------------------------------- /docs/packages/dynamodb-data-marshaller/assets/images/widgets.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/dynamodb-data-mapper-js/3ffe4bfb9187cf21bf87f72212526fa72172e555/docs/packages/dynamodb-data-marshaller/assets/images/widgets.png -------------------------------------------------------------------------------- /docs/packages/dynamodb-data-marshaller/assets/images/widgets@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/dynamodb-data-mapper-js/3ffe4bfb9187cf21bf87f72212526fa72172e555/docs/packages/dynamodb-data-marshaller/assets/images/widgets@2x.png -------------------------------------------------------------------------------- /docs/packages/dynamodb-expressions/assets/images/icons.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/dynamodb-data-mapper-js/3ffe4bfb9187cf21bf87f72212526fa72172e555/docs/packages/dynamodb-expressions/assets/images/icons.png -------------------------------------------------------------------------------- /docs/packages/dynamodb-expressions/assets/images/icons@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/dynamodb-data-mapper-js/3ffe4bfb9187cf21bf87f72212526fa72172e555/docs/packages/dynamodb-expressions/assets/images/icons@2x.png -------------------------------------------------------------------------------- /docs/packages/dynamodb-expressions/assets/images/widgets.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/dynamodb-data-mapper-js/3ffe4bfb9187cf21bf87f72212526fa72172e555/docs/packages/dynamodb-expressions/assets/images/widgets.png -------------------------------------------------------------------------------- /docs/packages/dynamodb-expressions/assets/images/widgets@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/dynamodb-data-mapper-js/3ffe4bfb9187cf21bf87f72212526fa72172e555/docs/packages/dynamodb-expressions/assets/images/widgets@2x.png -------------------------------------------------------------------------------- /docs/packages/dynamodb-query-iterator/assets/images/icons.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/dynamodb-data-mapper-js/3ffe4bfb9187cf21bf87f72212526fa72172e555/docs/packages/dynamodb-query-iterator/assets/images/icons.png -------------------------------------------------------------------------------- /docs/packages/dynamodb-query-iterator/assets/images/icons@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/dynamodb-data-mapper-js/3ffe4bfb9187cf21bf87f72212526fa72172e555/docs/packages/dynamodb-query-iterator/assets/images/icons@2x.png -------------------------------------------------------------------------------- /docs/packages/dynamodb-query-iterator/assets/images/widgets.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/dynamodb-data-mapper-js/3ffe4bfb9187cf21bf87f72212526fa72172e555/docs/packages/dynamodb-query-iterator/assets/images/widgets.png -------------------------------------------------------------------------------- /docs/packages/dynamodb-query-iterator/assets/images/widgets@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/dynamodb-data-mapper-js/3ffe4bfb9187cf21bf87f72212526fa72172e555/docs/packages/dynamodb-query-iterator/assets/images/widgets@2x.png -------------------------------------------------------------------------------- /lerna.json: -------------------------------------------------------------------------------- 1 | { 2 | "lerna": "2.11.0", 3 | "version": "0.7.3", 4 | "hoist": true 5 | } 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dynamodb-data-mapper-js", 3 | "private": true, 4 | "scripts": { 5 | "bootstrap": "lerna bootstrap", 6 | "docs": "typedoc src && lerna run docs", 7 | "pretest": "lerna run pretest", 8 | "test": "jest" 9 | }, 10 | "author": { 11 | "name": "AWS SDK for JavaScript Team", 12 | "email": "aws-sdk-js@amazon.com" 13 | }, 14 | "license": "Apache-2.0", 15 | "devDependencies": { 16 | "@types/jest": "^24", 17 | "@types/node": "^8.0.4", 18 | "jest": "^24", 19 | "lerna": "^3.13", 20 | "typedoc": "^0.14.0", 21 | "typescript": "^3.4" 22 | }, 23 | "dependencies": { 24 | "aws-sdk": "^2.7.0" 25 | }, 26 | "workspaces": [ 27 | "packages/*" 28 | ], 29 | "jest": { 30 | "testEnvironment": "node", 31 | "testPathIgnorePatterns": ["/node_modules/", ".ts"] 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /packages/dynamodb-auto-marshaller/.npmignore: -------------------------------------------------------------------------------- 1 | /src 2 | /node_modules 3 | /coverage 4 | 5 | *.spec.d.ts 6 | *.spec.js 7 | *.spec.js.map 8 | 9 | tsconfig.json 10 | tsconfig.test.json 11 | -------------------------------------------------------------------------------- /packages/dynamodb-auto-marshaller/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@aws/dynamodb-auto-marshaller", 3 | "version": "0.7.1", 4 | "description": "A data marshaller that converts JavaScript types into Amazon DynamoDB AttributeValues", 5 | "keywords": [ 6 | "aws", 7 | "dynamodb" 8 | ], 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/awslabs/dynamodb-data-mapper-js.git" 12 | }, 13 | "bugs": { 14 | "url": "https://github.com/awslabs/dynamodb-data-mapper-js/issues" 15 | }, 16 | "homepage": "https://awslabs.github.io/dynamodb-data-mapper-js/packages/dynamodb-auto-marshaller/", 17 | "main": "./build/index.js", 18 | "types": "./build/index.d.ts", 19 | "scripts": { 20 | "docs": "typedoc src", 21 | "prepublishOnly": "tsc", 22 | "pretest": "tsc -p tsconfig.test.json", 23 | "test": "jest \"build/(.+).spec.js\"" 24 | }, 25 | "author": { 26 | "name": "AWS SDK for JavaScript Team", 27 | "email": "aws-sdk-js@amazon.com" 28 | }, 29 | "license": "Apache-2.0", 30 | "devDependencies": { 31 | "@types/jest": "^24", 32 | "@types/node": "^8.0.4", 33 | "aws-sdk": "^2.7.0", 34 | "jest": "^24", 35 | "typedoc": "^0.14.0", 36 | "typescript": "^3.4" 37 | }, 38 | "peerDependencies": { 39 | "aws-sdk": "^2.7.0" 40 | }, 41 | "dependencies": { 42 | "tslib": "^1.9" 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /packages/dynamodb-auto-marshaller/src/BinarySet.ts: -------------------------------------------------------------------------------- 1 | import {ObjectSet} from "./ObjectSet"; 2 | 3 | export type BinaryValue = ArrayBuffer|ArrayBufferView; 4 | 5 | /** 6 | * A set of binary values represented as either ArrayBuffer objects or 7 | * ArrayBufferView objects. Equality is determined by the underlying byte 8 | * sequence and not by the identity or view window type of the provided value. 9 | */ 10 | export class BinarySet extends ObjectSet { 11 | delete(value: BinaryValue): boolean { 12 | const valueView = getBinaryView(value); 13 | const scrubbedValues = this._values.filter(item => { 14 | return !binaryEquals(getBinaryView(item), valueView); 15 | }); 16 | 17 | const numRemoved = this._values.length - scrubbedValues.length; 18 | this._values = scrubbedValues; 19 | 20 | return numRemoved > 0; 21 | } 22 | 23 | /** 24 | * @inheritDoc 25 | * 26 | * Equality is determined by inspecting the bytes of the ArrayBuffer or 27 | * ArrayBufferView. 28 | * 29 | * @example On a little-endian system, the following values would be 30 | * considered equal: 31 | * 32 | * new Uint32Array([0xdeadbeef]); 33 | * (new Uint32Array([0xdeadbeef])).buffer; 34 | * new Uint16Array([0xbeef, 0xdead]); 35 | * new Uint8Array([0xef, 0xbe, 0xad, 0xde]); 36 | */ 37 | has(value: BinaryValue): boolean { 38 | const valueView = getBinaryView(value); 39 | 40 | for (let item of this) { 41 | if (binaryEquals(getBinaryView(item), valueView)) { 42 | return true; 43 | } 44 | } 45 | 46 | return false; 47 | } 48 | } 49 | 50 | function binaryEquals(a: DataView, b: DataView): boolean { 51 | if (a.byteLength !== b.byteLength) { 52 | return false; 53 | } 54 | 55 | for (let i = 0; i < a.byteLength; i++) { 56 | if (a.getUint8(i) !== b.getUint8(i)) { 57 | return false; 58 | } 59 | } 60 | 61 | return true; 62 | } 63 | 64 | function getBinaryView(value: BinaryValue): DataView { 65 | return ArrayBuffer.isView(value) 66 | ? new DataView(value.buffer, value.byteOffset, value.byteLength) 67 | : new DataView(value); 68 | } 69 | -------------------------------------------------------------------------------- /packages/dynamodb-auto-marshaller/src/NumberValue.spec.ts: -------------------------------------------------------------------------------- 1 | import {NumberValue} from "./NumberValue"; 2 | 3 | describe('NumberValue', function() { 4 | it('should store numbers', function() { 5 | const number = new NumberValue(123); 6 | expect(number.toString()).toBe('123'); 7 | }); 8 | 9 | it('should store numeric strings', function() { 10 | const number = new NumberValue('123.1'); 11 | expect(number.toString()).toBe('123.1'); 12 | }); 13 | 14 | it( 15 | 'should store numeric values that would lose precision if converted to JavaScript numbers', 16 | function() { 17 | const number = new NumberValue('900719925474099100'); 18 | if (typeof (Number as any).isSafeInteger === 'function') { 19 | expect((Number as any).isSafeInteger(number.valueOf())) 20 | .toBe(false); 21 | } 22 | expect(number.toString()).toBe('900719925474099100'); 23 | } 24 | ); 25 | 26 | it('should convert numeric strings to numbers', function() { 27 | const number = new NumberValue('123.1'); 28 | expect(number.valueOf()).toBe(123.1); 29 | }); 30 | 31 | it('should allow easy conversion of the value into a number', () => { 32 | const safeNum = new NumberValue('123'); 33 | expect(+safeNum).toBe(123); 34 | expect((safeNum as any) + 1).toBe(124); 35 | }); 36 | 37 | it('should appear as a numeric value when converted to JSON', function() { 38 | expect(JSON.stringify({ 39 | number: new NumberValue('123'), 40 | nested: { 41 | number: new NumberValue('234') 42 | } 43 | })).toBe('{"number":123,"nested":{"number":234}}'); 44 | }); 45 | 46 | it( 47 | 'should reply to Object.prototype.toString with [object DynamoDbNumberValue]', 48 | () => { 49 | const number = new NumberValue('900719925474099100'); 50 | expect(Object.prototype.toString.call(number)) 51 | .toBe('[object DynamoDbNumberValue]'); 52 | } 53 | ); 54 | 55 | describe('::isNumberValue', () => { 56 | it('should return `true` for NumberValue objects', () => { 57 | expect(NumberValue.isNumberValue(new NumberValue('0'))).toBe(true); 58 | }); 59 | 60 | it('should return `false` for other values', () => { 61 | for (const invalid of [ 62 | 'string', 63 | 123, 64 | null, 65 | void 0, 66 | true, 67 | [], 68 | {}, 69 | new Uint8Array(10)] 70 | ) { 71 | expect(NumberValue.isNumberValue(invalid)).toBe(false); 72 | } 73 | }); 74 | }); 75 | }); 76 | -------------------------------------------------------------------------------- /packages/dynamodb-auto-marshaller/src/NumberValue.ts: -------------------------------------------------------------------------------- 1 | const NUMBER_VALUE_TAG = 'DynamoDbNumberValue'; 2 | const EXPECTED_TAG = `[object ${NUMBER_VALUE_TAG}]`; 3 | 4 | /** 5 | * A number that may contain greater precision than can safely be stored in 6 | * JavaScript's `number` data type. Numerical values are represented internally 7 | * as strings (the format used by DynamoDB's JSON-based data representation 8 | * schema). 9 | */ 10 | export class NumberValue { 11 | public readonly value: string; 12 | public readonly [Symbol.toStringTag] = NUMBER_VALUE_TAG; 13 | 14 | constructor(value: string|number) { 15 | this.value = value.toString().trim(); 16 | } 17 | 18 | /** 19 | * Convert the value to its desired JSON representation. Called by 20 | * `JSON.stringify`. 21 | */ 22 | toJSON(): number { 23 | return this.valueOf(); 24 | } 25 | 26 | /** 27 | * Convert the value to its desired string representation. Called 28 | * automatically when objects are coerced into strings. 29 | */ 30 | toString(): string { 31 | return this.value; 32 | } 33 | 34 | /** 35 | * Convert the value to its desired literal representation. Called 36 | * automatically when objects appear in arithmetic expressions. 37 | */ 38 | valueOf(): number { 39 | return Number(this.value); 40 | } 41 | 42 | /** 43 | * Evaluate whether the provided value is a NumberValue object. 44 | */ 45 | static isNumberValue(arg: any): arg is NumberValue { 46 | return (typeof NumberValue === 'function' && arg instanceof NumberValue) 47 | || Object.prototype.toString.call(arg) === EXPECTED_TAG; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /packages/dynamodb-auto-marshaller/src/NumberValueSet.ts: -------------------------------------------------------------------------------- 1 | import {ObjectSet} from "./ObjectSet"; 2 | import {NumberValue} from "./NumberValue"; 3 | 4 | /** 5 | * A set of numeric values represented internally as NumberValue objects. 6 | * Equality is determined by the string representation of the number and not by 7 | * the identity or data type of the provided value. 8 | */ 9 | export class NumberValueSet extends ObjectSet { 10 | /** 11 | * @inheritDoc 12 | * 13 | * If a number or string is provided, it will be converted to a NumberValue 14 | * object. 15 | */ 16 | add(value: NumberValue|number|string) { 17 | if (typeof value === 'number' || typeof value === 'string') { 18 | value = new NumberValue(value); 19 | } 20 | 21 | super.add(value); 22 | return this; 23 | } 24 | 25 | delete(value: NumberValue|number|string): boolean { 26 | const valueString = value.toString(); 27 | const scrubbedValues = this._values 28 | .filter(item => item.toString() !== valueString); 29 | 30 | const numRemoved = this._values.length - scrubbedValues.length; 31 | this._values = scrubbedValues; 32 | 33 | return numRemoved > 0; 34 | } 35 | 36 | has(value: NumberValue|number|string): boolean { 37 | const valueString = value.toString(); 38 | for (let item of this) { 39 | if (item.toString() === valueString) { 40 | return true; 41 | } 42 | } 43 | 44 | return false; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /packages/dynamodb-auto-marshaller/src/ObjectSet.ts: -------------------------------------------------------------------------------- 1 | export abstract class ObjectSet implements Set { 2 | /** 3 | * Returns the string literal 'Set' for use by Object.prototype.toString. 4 | * This allows for identifying Sets without checking constructor identity. 5 | */ 6 | public readonly [Symbol.toStringTag]: 'Set' = 'Set'; 7 | 8 | protected _values: Array = []; 9 | 10 | /** 11 | * Creates a new ObjectSet and optionally seeds it with values. 12 | * 13 | * @param iterable An optional iterable of values to add to the set. 14 | */ 15 | constructor(iterable?: Iterable) { 16 | if (iterable) { 17 | for (let item of iterable) { 18 | this.add(item); 19 | } 20 | } 21 | } 22 | 23 | /** 24 | * Add a value to the set. If the value is already contained in the set, it 25 | * will not be added a second time. 26 | * 27 | * @param value The value to add 28 | */ 29 | add(value: T): this { 30 | if (!this.has(value)) { 31 | this._values.push(value); 32 | } 33 | 34 | return this; 35 | } 36 | 37 | /** 38 | * Remove all values from the set. 39 | */ 40 | clear(): void { 41 | this._values = []; 42 | } 43 | 44 | /** 45 | * Removes a particular value from the set. If the value was contained in 46 | * the set prior to this method being called, `true` will be returned; if 47 | * the value was not in the set, `false` will be returned. In either case, 48 | * the value provided will not be in the set after this method returns. 49 | * 50 | * @param value The value to remove from the set. 51 | */ 52 | abstract delete(value: T): boolean; 53 | 54 | /** 55 | * Returns an iterable two-member tuples for each item in the set, where 56 | * the item is provided twice. 57 | * 58 | * Part of the ES2015 Set specification for compatibility with Map objects. 59 | */ 60 | entries(): IterableIterator<[T, T]> { 61 | return this._values.map<[T, T]>( 62 | value => [value, value] 63 | )[Symbol.iterator](); 64 | } 65 | 66 | /** 67 | * Invokes a callback once for each member of the set. 68 | * 69 | * @param callback The function to invoke with each set member 70 | * @param thisArg The `this` context on which to invoke the callback 71 | */ 72 | forEach( 73 | callback: ( 74 | value: T, 75 | value2: T, 76 | set: Set 77 | ) => void, 78 | thisArg?: any 79 | ): void { 80 | this._values.forEach((value, index, array) => { 81 | callback.call(thisArg, value, value, this); 82 | }, thisArg); 83 | } 84 | 85 | /** 86 | * Determines if a provided value is already a member of the set. 87 | * 88 | * @param value The value against which set members should be checked 89 | */ 90 | abstract has(value: T): boolean; 91 | 92 | /** 93 | * Returns an IterableIterator of each member of the set. 94 | */ 95 | keys(): IterableIterator { 96 | return this[Symbol.iterator](); 97 | } 98 | 99 | /** 100 | * Returns the number of members in the set. 101 | */ 102 | get size(): number { 103 | return this._values.length; 104 | } 105 | 106 | /** 107 | * Returns an IterableIterator of each member of the set. 108 | */ 109 | values(): IterableIterator { 110 | return this[Symbol.iterator](); 111 | } 112 | 113 | /** 114 | * Returns an IterableIterator of each member of the set. 115 | */ 116 | [Symbol.iterator](): IterableIterator { 117 | return this._values[Symbol.iterator](); 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /packages/dynamodb-auto-marshaller/src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './BinarySet'; 2 | export * from './Marshaller'; 3 | export * from './NumberValue'; 4 | export * from './NumberValueSet'; 5 | -------------------------------------------------------------------------------- /packages/dynamodb-auto-marshaller/src/isArrayBuffer.spec.ts: -------------------------------------------------------------------------------- 1 | import {isArrayBuffer} from "./isArrayBuffer"; 2 | 3 | describe('isArrayBuffer', () => { 4 | const arrayBufferConstructor = ArrayBuffer; 5 | 6 | afterEach(() => { 7 | (ArrayBuffer as any) = arrayBufferConstructor; 8 | }); 9 | 10 | it('should return true for ArrayBuffer objects', () => { 11 | expect(isArrayBuffer(new ArrayBuffer(0))).toBe(true); 12 | }); 13 | 14 | it('should return false for ArrayBufferView objects', () => { 15 | const view = new Uint8Array(0); 16 | 17 | expect(isArrayBuffer(view)).toBe(false); 18 | expect(isArrayBuffer(view.buffer)).toBe(true); 19 | }); 20 | 21 | it('should return false for scalar values', () => { 22 | for (let scalar of ['string', 123.234, true, null, void 0]) { 23 | expect(isArrayBuffer(scalar)).toBe(false); 24 | } 25 | }); 26 | 27 | it( 28 | 'should return true for ArrayBuffers created with a different instance of the ArrayBuffer constructor', 29 | () => { 30 | const buffer = new ArrayBuffer(0); 31 | (ArrayBuffer as any) = () => buffer; 32 | 33 | expect(buffer).not.toBeInstanceOf(ArrayBuffer); 34 | expect(isArrayBuffer(buffer)).toBe(true); 35 | } 36 | ); 37 | }); 38 | -------------------------------------------------------------------------------- /packages/dynamodb-auto-marshaller/src/isArrayBuffer.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Determines if the provided argument is an ArrayBuffer object. Compatible with 3 | * ArrayBuffers created in separate iframes and VMs. 4 | */ 5 | export function isArrayBuffer(arg: any): arg is ArrayBuffer { 6 | return (typeof ArrayBuffer === 'function' && arg instanceof ArrayBuffer) || 7 | Object.prototype.toString.call(arg) === '[object ArrayBuffer]'; 8 | } 9 | -------------------------------------------------------------------------------- /packages/dynamodb-auto-marshaller/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": [ 5 | "es5", 6 | "es2015.iterable", 7 | "es2015.promise", 8 | "es2015.collection", 9 | "es2015.symbol.wellknown" 10 | ], 11 | "downlevelIteration": true, 12 | "importHelpers": true, 13 | "module": "commonjs", 14 | "strict": true, 15 | "declaration": true, 16 | "rootDir": "./src", 17 | "outDir": "./build" 18 | }, 19 | "typedocOptions": { 20 | "mode": "file", 21 | "out": "../../docs/packages/dynamodb-auto-marshaller", 22 | "excludeNotExported": true, 23 | "excludePrivate": true, 24 | "hideGenerator": true 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /packages/dynamodb-auto-marshaller/tsconfig.test.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "inlineSourceMap": true, 5 | "inlineSources": true, 6 | "rootDir": "./src", 7 | "outDir": "./build" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /packages/dynamodb-batch-iterator/.npmignore: -------------------------------------------------------------------------------- 1 | /src 2 | /node_modules 3 | /coverage 4 | 5 | *.spec.d.ts 6 | *.spec.js 7 | *.spec.js.map 8 | 9 | tsconfig.json 10 | tsconfig.test.json 11 | -------------------------------------------------------------------------------- /packages/dynamodb-batch-iterator/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | All notable changes to this project will be documented in this file. 3 | 4 | The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) 5 | and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). 6 | 7 | ## [0.7.1] 8 | Remove package rollup at `./build/index.mjs` due to bundler incompatibilities. 9 | 10 | ## [0.7.0] 11 | Add a package rollup at `./build/index.mjs` to support tree shaking. 12 | 13 | ## [0.3.1] 14 | ### Fixed 15 | - When the source for a batch operation is a synchronous iterable, exhaust the 16 | source before interleaving throttled items. 17 | - When a write is returned as unprocessed, do not yield the marshalled form. 18 | 19 | ## [0.3.0] 20 | Initial release 21 | -------------------------------------------------------------------------------- /packages/dynamodb-batch-iterator/README.md: -------------------------------------------------------------------------------- 1 | # Amazon DynamoDB Batch Iteration 2 | 3 | [![Apache 2 License](https://img.shields.io/github/license/awslabs/dynamodb-data-mapper-js.svg?style=flat)](http://aws.amazon.com/apache-2-0/) 4 | 5 | This library provides utilities for automatically submitting arbitrarily-sized 6 | batches of reads and writes to DynamoDB using well-formed `BatchGetItem` and 7 | `BatchWriteItem` operations, respectively. Partial successes (i.e., 8 | `BatchGetItem` operations that return some responses and some unprocessed keys 9 | or `BatchWriteItem` operations that return some unprocessed items) will retry 10 | the unprocessed items automatically using exponential backoff. 11 | 12 | ## Getting started 13 | 14 | ### Reading batches of items 15 | 16 | Create a `BatchGet` object, supplying an instantiated DynamoDB client from the 17 | AWS SDK for JavaScript and an iterable of keys that you wish to retrieve. The 18 | iterable may be synchronous (such as an array) or asynchronous (such as an 19 | object stream wrapped with [async-iter-stream](https://github.com/calvinmetcalf/async-iter-stream)'s 20 | `wrap` method). 21 | 22 | ```typescript 23 | import { BatchGet } from '@aws/dynamodb-batch-iterator'; 24 | import DynamoDB = require('aws-sdk/clients/dynamodb'); 25 | 26 | const dynamoDb = new DynamoDB({region: 'us-west-2'}); 27 | const keys = [ 28 | ['tableName', {keyProperty: {N: '0'}}], 29 | ['tableName', {keyProperty: {N: '1'}}], 30 | ['tableName', {keyProperty: {N: '2'}}], 31 | // etc., continuing to count up to 32 | ['tableName', {keyProperty: {N: '1001'}}], 33 | ]; 34 | 35 | for await (const item of new BatchGet(dynamoDb, keys)) { 36 | console.log(item); 37 | } 38 | ``` 39 | 40 | The above code snippet will automatically split the provided keys into 41 | `BatchGetItem` requests of 100 or fewer keys, and any unprocessed keys will be 42 | automatically retried until they are handled. The above code will execute at 43 | least 11 `BatchGetItem` operations, dependening on how many items are returned 44 | without processing due to insufficient provisioned read capacity. 45 | 46 | Each item yielded in the `for...await...of` loop will be a single DynamoDB 47 | record. Iteration will stop once each key has been retrieved or an error has 48 | been encountered. 49 | 50 | ### Writing batches of items 51 | 52 | Create a `BatchWrite` object, supplying an instantiated DynamoDB client from the 53 | AWS SDK for JavaScript and an iterable of write requests that you wish to 54 | execute. The iterable may be synchronous (such as an array) or asynchronous 55 | (such as an object stream wrapped with [async-iter-stream](https://github.com/calvinmetcalf/async-iter-stream)'s 56 | `wrap` method). 57 | 58 | Each write request should contain either a `DeleteRequest` key or a `PutRequest` 59 | key as described [in the Amazon DynamoDB API reference](http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_WriteRequest.html#DDB-Type-WriteRequest-DeleteRequest). 60 | 61 | ```typescript 62 | import { BatchWrite } from '@aws/dynamodb-batch-iterator'; 63 | import DynamoDB = require('aws-sdk/clients/dynamodb'); 64 | 65 | const dynamoDb = new DynamoDB({region: 'us-west-2'}); 66 | const keys = [ 67 | ['tableName', {DeleteRequest: {Key: {keyProperty: {N: '0'}}}}], 68 | ['tableName', {PutRequest: {Item: {keyProperty: {N: '1'}, otherProperty: {BOOL: false}}}}], 69 | ['tableName', {DeleteRequest: {Key: {keyProperty: {N: '2'}}}}], 70 | ['tableName', {PutRequest: {Item: {keyProperty: {N: '3'}, otherProperty: {BOOL: false}}}}], 71 | ['tableName', {N: '2'}], 72 | // etc., continuing to count up to 73 | ['tableName', {DeleteRequest: {Key: {keyProperty: {N: '102'}}}}], 74 | ]; 75 | 76 | for await (const item of new BatchWrite(dynamoDb, keys)) { 77 | console.log(item); 78 | } 79 | ``` 80 | 81 | The above code snippet will automatically split the provided keys into 82 | `BatchWriteItem` requests of 25 or fewer write request objects, and any 83 | unprocessed request objects will be automatically retried until they are 84 | handled. The above code will execute at least 5 `BatchWriteItem` operations, 85 | dependening on how many items are returned without processing due to 86 | insufficient provisioned write capacity. 87 | 88 | Each item yielded in the `for...await...of` loop will be a single write request 89 | that has succeeded. Iteration will stop once each request has been handled or an 90 | error has been encountered. 91 | -------------------------------------------------------------------------------- /packages/dynamodb-batch-iterator/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@aws/dynamodb-batch-iterator", 3 | "version": "0.7.1", 4 | "description": "Abstraction for DynamoDB batch reads and writes for that handles batch splitting and partial retries with exponential backoff", 5 | "keywords": [ 6 | "aws", 7 | "dynamodb" 8 | ], 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/awslabs/dynamodb-data-mapper-js.git" 12 | }, 13 | "bugs": { 14 | "url": "https://github.com/awslabs/dynamodb-data-mapper-js/issues" 15 | }, 16 | "homepage": "https://awslabs.github.io/dynamodb-data-mapper-js/packages/dynamodb-batch-iterator/", 17 | "main": "./build/index.js", 18 | "types": "./build/index.d.ts", 19 | "scripts": { 20 | "docs": "typedoc src", 21 | "prepublishOnly": "tsc", 22 | "pretest": "tsc -p tsconfig.test.json", 23 | "test": "jest \"build/(.+).spec.js\"" 24 | }, 25 | "author": { 26 | "name": "AWS SDK for JavaScript Team", 27 | "email": "aws-sdk-js@amazon.com" 28 | }, 29 | "license": "Apache-2.0", 30 | "devDependencies": { 31 | "@types/jest": "^24", 32 | "@types/node": "^8.0.4", 33 | "aws-sdk": "^2.7.0", 34 | "jest": "^24", 35 | "typedoc": "^0.14.0", 36 | "typescript": "^3.4" 37 | }, 38 | "peerDependencies": { 39 | "aws-sdk": "^2.7.0" 40 | }, 41 | "dependencies": { 42 | "tslib": "^1.9", 43 | "utf8-bytes": "^0.0.1" 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /packages/dynamodb-batch-iterator/src/BatchGet.ts: -------------------------------------------------------------------------------- 1 | import { BatchGetOptions, PerTableOptions } from './BatchGetOptions'; 2 | import { BatchOperation } from './BatchOperation'; 3 | import { SyncOrAsyncIterable, TableState } from './types'; 4 | import { AttributeMap, BatchGetItemInput } from 'aws-sdk/clients/dynamodb'; 5 | import DynamoDB = require('aws-sdk/clients/dynamodb'); 6 | 7 | export const MAX_READ_BATCH_SIZE = 100; 8 | 9 | /** 10 | * Retrieves items from DynamoDB in batches of 100 or fewer via one or more 11 | * BatchGetItem operations. The items may be from any number of tables. 12 | * 13 | * This method will automatically retry any get requests returned by DynamoDB as 14 | * unprocessed. Exponential backoff on unprocessed items is employed on a 15 | * per-table basis. 16 | */ 17 | export class BatchGet extends BatchOperation { 18 | protected readonly batchSize = MAX_READ_BATCH_SIZE; 19 | 20 | private readonly consistentRead?: boolean; 21 | private readonly options: PerTableOptions; 22 | 23 | /** 24 | * @param client The AWS SDK client with which to communicate with 25 | * DynamoDB. 26 | * @param items A synchronous or asynchronous iterable of tuples 27 | * describing the reads to execute. The first member of the 28 | * tuple should be the name of the table from which to 29 | * read, and the second should be the marshalled key. 30 | * @param options Additional options to apply to the operations executed. 31 | */ 32 | constructor( 33 | client: DynamoDB, 34 | items: SyncOrAsyncIterable<[string, AttributeMap]>, 35 | { 36 | ConsistentRead, 37 | PerTableOptions = {}, 38 | }: BatchGetOptions = {} 39 | ) { 40 | super(client, items); 41 | this.consistentRead = ConsistentRead; 42 | this.options = PerTableOptions; 43 | } 44 | 45 | protected async doBatchRequest() { 46 | const operationInput: BatchGetItemInput = {RequestItems: {}}; 47 | let batchSize = 0; 48 | 49 | while (this.toSend.length > 0) { 50 | const [tableName, item] = this.toSend.shift() as [string, AttributeMap]; 51 | if (operationInput.RequestItems[tableName] === undefined) { 52 | const { 53 | projection, 54 | consistentRead, 55 | attributeNames, 56 | } = this.state[tableName]; 57 | 58 | operationInput.RequestItems[tableName] = { 59 | Keys: [], 60 | ConsistentRead: consistentRead, 61 | ProjectionExpression: projection, 62 | ExpressionAttributeNames: attributeNames, 63 | }; 64 | } 65 | operationInput.RequestItems[tableName].Keys.push(item); 66 | 67 | if (++batchSize === this.batchSize) { 68 | break; 69 | } 70 | } 71 | 72 | const { 73 | Responses = {}, 74 | UnprocessedKeys = {}, 75 | } = await this.client.batchGetItem(operationInput).promise(); 76 | 77 | const unprocessedTables = new Set(); 78 | for (const table of Object.keys(UnprocessedKeys)) { 79 | unprocessedTables.add(table); 80 | this.handleThrottled(table, UnprocessedKeys[table].Keys); 81 | } 82 | 83 | this.movePendingToThrottled(unprocessedTables); 84 | 85 | for (const table of Object.keys(Responses)) { 86 | const tableData = this.state[table]; 87 | tableData.backoffFactor = Math.max(0, tableData.backoffFactor - 1); 88 | for (const item of Responses[table]) { 89 | this.pending.push([table, item]); 90 | } 91 | } 92 | } 93 | 94 | protected getInitialTableState(tableName: string): TableState { 95 | const { 96 | ExpressionAttributeNames, 97 | ProjectionExpression, 98 | ConsistentRead = this.consistentRead, 99 | } = this.options[tableName] || {} as PerTableOptions; 100 | 101 | return { 102 | ...super.getInitialTableState(tableName), 103 | attributeNames: ExpressionAttributeNames, 104 | projection: ProjectionExpression, 105 | consistentRead: ConsistentRead 106 | }; 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /packages/dynamodb-batch-iterator/src/BatchGetOptions.ts: -------------------------------------------------------------------------------- 1 | import { 2 | ConsistentRead, 3 | ExpressionAttributeNameMap, 4 | ProjectionExpression, 5 | } from "aws-sdk/clients/dynamodb"; 6 | 7 | export interface BatchGetOptions { 8 | /** 9 | * The default read consistency to apply to gets. 10 | */ 11 | ConsistentRead?: ConsistentRead; 12 | 13 | /** 14 | * Options to apply for all reads directed to a specific table. 15 | */ 16 | PerTableOptions?: PerTableOptions; 17 | } 18 | 19 | export interface PerTableOptions { 20 | [tableName: string]: TableOptions; 21 | } 22 | 23 | export interface TableOptions { 24 | /** 25 | * The read consistency to apply to reads against this table. 26 | */ 27 | ConsistentRead?: ConsistentRead; 28 | 29 | /** 30 | * One or more substitution tokens for attribute names in an expression. 31 | */ 32 | ExpressionAttributeNames?: ExpressionAttributeNameMap; 33 | 34 | /** 35 | * A string that identifies one or more attributes to retrieve from the 36 | * table. 37 | */ 38 | ProjectionExpression?: ProjectionExpression; 39 | } 40 | -------------------------------------------------------------------------------- /packages/dynamodb-batch-iterator/src/BatchWrite.ts: -------------------------------------------------------------------------------- 1 | import { BatchOperation } from './BatchOperation'; 2 | import { itemIdentifier } from './itemIdentifier'; 3 | import { WriteRequest } from './types'; 4 | import { BatchWriteItemInput } from 'aws-sdk/clients/dynamodb'; 5 | 6 | export const MAX_WRITE_BATCH_SIZE = 25; 7 | 8 | /** 9 | * Puts or deletes items from DynamoDB in batches of 25 or fewer via one or more 10 | * BatchWriteItem operations. The items may belong to any number of tables. 11 | * 12 | * The iterable of writes to perform may be synchronous or asynchronous and is 13 | * expected to yield tuples describing the writes to be performed. The first 14 | * member should be the table name, and the second should be {WriteRequest} 15 | * object that defines either a put request or a delete request. 16 | * 17 | * This method will automatically retry any write requests returned by DynamoDB 18 | * as unprocessed. Exponential backoff on unprocessed items is employed on a 19 | * per-table basis. 20 | */ 21 | export class BatchWrite extends BatchOperation { 22 | protected readonly batchSize = MAX_WRITE_BATCH_SIZE; 23 | 24 | protected async doBatchRequest() { 25 | const inFlight: Array<[string, WriteRequest]> = []; 26 | const operationInput: BatchWriteItemInput = {RequestItems: {}}; 27 | 28 | let batchSize = 0; 29 | while (this.toSend.length > 0) { 30 | const [ 31 | tableName, 32 | marshalled 33 | ] = this.toSend.shift() as [string, WriteRequest]; 34 | 35 | inFlight.push([tableName, marshalled]); 36 | 37 | if (operationInput.RequestItems[tableName] === undefined) { 38 | operationInput.RequestItems[tableName] = []; 39 | } 40 | operationInput.RequestItems[tableName].push(marshalled); 41 | 42 | if (++batchSize === this.batchSize) { 43 | break; 44 | } 45 | } 46 | 47 | const { 48 | UnprocessedItems = {} 49 | } = await this.client.batchWriteItem(operationInput).promise(); 50 | const unprocessedTables = new Set(); 51 | 52 | for (const table of Object.keys(UnprocessedItems)) { 53 | unprocessedTables.add(table); 54 | const unprocessed: Array = []; 55 | for (const item of UnprocessedItems[table]) { 56 | if (item.DeleteRequest || item.PutRequest) { 57 | unprocessed.push(item as WriteRequest); 58 | 59 | const identifier = itemIdentifier(table, item as WriteRequest); 60 | for (let i = inFlight.length - 1; i >= 0; i--) { 61 | const [tableName, attributes] = inFlight[i]; 62 | if ( 63 | tableName === table && 64 | itemIdentifier(tableName, attributes) === identifier 65 | ) { 66 | inFlight.splice(i, 1); 67 | } 68 | } 69 | } 70 | } 71 | 72 | this.handleThrottled(table, unprocessed); 73 | } 74 | 75 | this.movePendingToThrottled(unprocessedTables); 76 | 77 | const processedTables = new Set(); 78 | for (const [tableName, marshalled] of inFlight) { 79 | processedTables.add(tableName); 80 | this.pending.push([tableName, marshalled]); 81 | } 82 | 83 | for (const tableName of processedTables) { 84 | this.state[tableName].backoffFactor = 85 | Math.max(0, this.state[tableName].backoffFactor - 1); 86 | } 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /packages/dynamodb-batch-iterator/src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './BatchGet'; 2 | export * from './BatchGetOptions'; 3 | export * from './BatchWrite'; 4 | export * from './types'; 5 | -------------------------------------------------------------------------------- /packages/dynamodb-batch-iterator/src/itemIdentifier.spec.ts: -------------------------------------------------------------------------------- 1 | import { itemIdentifier } from './itemIdentifier'; 2 | 3 | describe('itemIdentifier', () => { 4 | it('should serialize all top-level string attributes', () => { 5 | expect( 6 | itemIdentifier('table', {DeleteRequest: {Key: {foo: {S: 'bar'}}}}) 7 | ).toBe('table::delete::foo=bar'); 8 | 9 | expect( 10 | itemIdentifier('table', {PutRequest: {Item: {foo: {S: 'bar'}}}}) 11 | ).toBe('table::put::foo=bar'); 12 | }); 13 | 14 | it('should serialize all top-level number attributes', () => { 15 | expect( 16 | itemIdentifier('table', {DeleteRequest: {Key: {foo: {N: '1'}}}}) 17 | ).toBe('table::delete::foo=1'); 18 | 19 | expect( 20 | itemIdentifier('table', {PutRequest: {Item: {foo: {N: '1'}}}}) 21 | ).toBe('table::put::foo=1'); 22 | }); 23 | 24 | it('should serialize all top-level binary attributes', () => { 25 | expect( 26 | itemIdentifier('table', {DeleteRequest: {Key: {foo: {B: Uint8Array.from([0xde, 0xad])}}}}) 27 | ).toBe('table::delete::foo=222,173'); 28 | 29 | expect( 30 | itemIdentifier('table', {PutRequest: {Item: {foo: {B: Uint8Array.from([0xde, 0xad])}}}}) 31 | ).toBe('table::put::foo=222,173'); 32 | }); 33 | 34 | it( 35 | 'should serialize different representations of the same binary data in the same way', 36 | () => { 37 | expect( 38 | itemIdentifier( 39 | 'table', 40 | {DeleteRequest: {Key: {foo: {B: '🐎👱❤'}}}} 41 | ) 42 | ).toBe( 43 | itemIdentifier( 44 | 'table', 45 | {DeleteRequest: {Key: {foo: {B: Uint8Array.from([240, 159, 144, 142, 240, 159, 145, 177, 226, 157, 164])}}}} 46 | ) 47 | ); 48 | 49 | expect( 50 | itemIdentifier( 51 | 'table', 52 | {DeleteRequest: {Key: {foo: {B: '🐎👱❤'}}}} 53 | ) 54 | ).toBe( 55 | itemIdentifier( 56 | 'table', 57 | {DeleteRequest: {Key: {foo: {B: Uint8Array.from([240, 159, 144, 142, 240, 159, 145, 177, 226, 157, 164]).buffer}}}} 58 | ) 59 | ); 60 | } 61 | ); 62 | 63 | it('should throw when an invalid binary value is provided', () => { 64 | expect( 65 | () => itemIdentifier('table', {PutRequest: {Item: {foo: {B: []}}}}) 66 | ).toThrow(); 67 | }); 68 | 69 | it( 70 | 'should throw when neither a PutRequest nor a DeleteRequest is provided', 71 | () => { 72 | expect( 73 | () => itemIdentifier('table', {} as any) 74 | ).toThrow(); 75 | } 76 | ); 77 | }); -------------------------------------------------------------------------------- /packages/dynamodb-batch-iterator/src/itemIdentifier.ts: -------------------------------------------------------------------------------- 1 | import { WriteRequest } from './types'; 2 | import { AttributeMap, BinaryAttributeValue } from 'aws-sdk/clients/dynamodb'; 3 | const bytes = require('utf8-bytes'); 4 | 5 | /** 6 | * @internal 7 | */ 8 | export function itemIdentifier( 9 | tableName: string, 10 | {DeleteRequest, PutRequest}: WriteRequest 11 | ): string { 12 | if (DeleteRequest) { 13 | return `${tableName}::delete::${ 14 | serializeKeyTypeAttributes(DeleteRequest.Key) 15 | }`; 16 | } else if (PutRequest) { 17 | return `${tableName}::put::${ 18 | serializeKeyTypeAttributes(PutRequest.Item) 19 | }`; 20 | } 21 | 22 | throw new Error(`Invalid write request provided`); 23 | } 24 | 25 | function serializeKeyTypeAttributes(attributes: AttributeMap): string { 26 | const keyTypeProperties: Array = []; 27 | for (const property of Object.keys(attributes).sort()) { 28 | const attribute = attributes[property]; 29 | if (attribute.B) { 30 | keyTypeProperties.push(`${property}=${toByteArray(attribute.B)}`); 31 | } else if (attribute.N) { 32 | keyTypeProperties.push(`${property}=${attribute.N}`); 33 | } else if (attribute.S) { 34 | keyTypeProperties.push(`${property}=${attribute.S}`); 35 | } 36 | } 37 | 38 | return keyTypeProperties.join('&'); 39 | } 40 | 41 | function toByteArray(value: BinaryAttributeValue): Uint8Array { 42 | if (ArrayBuffer.isView(value)) { 43 | return new Uint8Array( 44 | value.buffer, 45 | value.byteOffset, 46 | value.byteLength 47 | ); 48 | } 49 | 50 | if (typeof value === 'string') { 51 | return Uint8Array.from(bytes(value)); 52 | } 53 | 54 | if (isArrayBuffer(value)) { 55 | return new Uint8Array(value); 56 | } 57 | 58 | throw new Error('Unrecognized binary type'); 59 | } 60 | 61 | function isArrayBuffer(arg: any): arg is ArrayBuffer { 62 | return (typeof ArrayBuffer === 'function' && arg instanceof ArrayBuffer) || 63 | Object.prototype.toString.call(arg) === '[object ArrayBuffer]'; 64 | } -------------------------------------------------------------------------------- /packages/dynamodb-batch-iterator/src/types.ts: -------------------------------------------------------------------------------- 1 | import { 2 | AttributeMap, 3 | ConsistentRead, 4 | DeleteRequest, 5 | ExpressionAttributeNameMap, 6 | ProjectionExpression, 7 | PutRequest, 8 | WriteRequest as DynamoDbWriteRequest 9 | } from "aws-sdk/clients/dynamodb"; 10 | 11 | /** 12 | * A synchronous or asynchronous iterable. 13 | */ 14 | export type SyncOrAsyncIterable = Iterable|AsyncIterable; 15 | 16 | /** 17 | * @internal 18 | */ 19 | export interface BatchState { 20 | [tableName: string]: TableState; 21 | } 22 | 23 | /** 24 | * @internal 25 | */ 26 | export interface TableState { 27 | attributeNames?: ExpressionAttributeNameMap; 28 | backoffFactor: number; 29 | consistentRead?: ConsistentRead; 30 | name: string; 31 | projection?: ProjectionExpression; 32 | tableThrottling?: TableThrottlingTracker; 33 | } 34 | 35 | /** 36 | * @internal 37 | */ 38 | export type TableStateElement = AttributeMap|WriteRequest; 39 | 40 | /** 41 | * @internal 42 | */ 43 | export interface TableThrottlingTracker { 44 | backoffWaiter: Promise>; 45 | unprocessed: Array; 46 | } 47 | 48 | /** 49 | * @internal 50 | */ 51 | export interface ThrottledTableConfiguration< 52 | Element extends TableStateElement 53 | > extends TableState { 54 | tableThrottling: TableThrottlingTracker; 55 | } 56 | 57 | /** 58 | * A write request for which exactly one of the `PutRequest` and `DeleteRequest` 59 | * properties has been defined. 60 | */ 61 | export type WriteRequest = 62 | DynamoDbWriteRequest & { PutRequest: PutRequest, DeleteRequest?: undefined } | 63 | DynamoDbWriteRequest & { DeleteRequest: DeleteRequest, PutRequest?: undefined }; 64 | -------------------------------------------------------------------------------- /packages/dynamodb-batch-iterator/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": [ 5 | "es5", 6 | "es2015.iterable", 7 | "es2015.promise", 8 | "es2015.collection", 9 | "es2015.symbol.wellknown", 10 | "esnext.asynciterable" 11 | ], 12 | "downlevelIteration": true, 13 | "importHelpers": true, 14 | "module": "commonjs", 15 | "noUnusedLocals": true, 16 | "strict": true, 17 | "declaration": true, 18 | "sourceMap": true, 19 | "rootDir": "./src", 20 | "outDir": "./build" 21 | }, 22 | "typedocOptions": { 23 | "mode": "file", 24 | "out": "../../docs/packages/dynamodb-batch-iterator", 25 | "excludeNotExported": true, 26 | "excludePrivate": true, 27 | "hideGenerator": true 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /packages/dynamodb-batch-iterator/tsconfig.test.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "sourceMap": false, 5 | "inlineSourceMap": true, 6 | "inlineSources": true, 7 | "rootDir": "./src", 8 | "outDir": "./build" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper-annotations/.npmignore: -------------------------------------------------------------------------------- 1 | /src 2 | /node_modules 3 | /coverage 4 | 5 | *.spec.d.ts 6 | *.spec.js 7 | *.spec.js.map 8 | 9 | tsconfig.json 10 | tsconfig.test.json 11 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper-annotations/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@aws/dynamodb-data-mapper-annotations", 3 | "version": "0.7.3", 4 | "description": "Annotations providing easy integration between TypeScript domain objects and the @aws/dynamodb-data-mapper library", 5 | "keywords": [ 6 | "aws", 7 | "dynamodb" 8 | ], 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/awslabs/dynamodb-data-mapper-js.git" 12 | }, 13 | "bugs": { 14 | "url": "https://github.com/awslabs/dynamodb-data-mapper-js/issues" 15 | }, 16 | "homepage": "https://awslabs.github.io/dynamodb-data-mapper-js/packages/dynamodb-data-mapper-annotations/", 17 | "main": "./build/index.js", 18 | "types": "./build/index.d.ts", 19 | "scripts": { 20 | "build": "tsc", 21 | "docs": "typedoc src", 22 | "integ": "npm run pretest && jest --config=jest.integration.js", 23 | "prepublishOnly": "npm run build", 24 | "pretest": "tsc -p tsconfig.test.json", 25 | "test": "jest \"build/(.+).spec.js\"" 26 | }, 27 | "author": { 28 | "name": "AWS SDK for JavaScript Team", 29 | "email": "aws-sdk-js@amazon.com" 30 | }, 31 | "license": "Apache-2.0", 32 | "devDependencies": { 33 | "@types/jest": "^24", 34 | "@types/node": "^8.0.4", 35 | "@types/uuid": "^3.0.0", 36 | "aws-sdk": "^2.7.0", 37 | "jest": "^24", 38 | "typedoc": "^0.14.0", 39 | "typescript": "^3.4" 40 | }, 41 | "dependencies": { 42 | "@aws/dynamodb-auto-marshaller": "^0.7.1", 43 | "@aws/dynamodb-data-mapper": "^0.7.3", 44 | "@aws/dynamodb-data-marshaller": "^0.7.3", 45 | "reflect-metadata": "^0.1.10", 46 | "tslib": "^1.9", 47 | "uuid": "^3.0.0" 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper-annotations/src/annotationShapes.ts: -------------------------------------------------------------------------------- 1 | import {ZeroArgumentsConstructor} from '@aws/dynamodb-data-marshaller'; 2 | 3 | export interface ClassAnnotation { 4 | (target: ZeroArgumentsConstructor): void; 5 | } 6 | 7 | export interface PropertyAnnotation { 8 | (target: Object, propertyKey: string|symbol): void; 9 | } -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper-annotations/src/autoGeneratedHashKey.spec.ts: -------------------------------------------------------------------------------- 1 | import { autoGeneratedHashKey } from './autoGeneratedHashKey'; 2 | import { v4 } from 'uuid'; 3 | 4 | jest.mock('./attribute', () => ({attribute: jest.fn()})); 5 | import { attribute } from './attribute'; 6 | 7 | describe('autoGeneratedHashKey', () => { 8 | beforeEach(() => { 9 | (attribute as any).mockClear(); 10 | }); 11 | 12 | it( 13 | 'should call attribute with a defined type, keyType, and defaultProvider', 14 | () => { 15 | autoGeneratedHashKey(); 16 | 17 | expect((attribute as any).mock.calls.length).toBe(1); 18 | expect((attribute as any).mock.calls[0]).toEqual([ 19 | { 20 | type: 'String', 21 | keyType: 'HASH', 22 | defaultProvider: v4, 23 | } 24 | ]); 25 | } 26 | ); 27 | 28 | it('should pass through any supplied parameters', () => { 29 | const attributeName = 'foo' 30 | autoGeneratedHashKey({attributeName}); 31 | 32 | expect((attribute as any).mock.calls[0][0]) 33 | .toMatchObject({attributeName}); 34 | }); 35 | }); 36 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper-annotations/src/autoGeneratedHashKey.ts: -------------------------------------------------------------------------------- 1 | import {PropertyAnnotation} from './annotationShapes'; 2 | import {hashKey} from './hashKey'; 3 | import {StringType} from '@aws/dynamodb-data-marshaller'; 4 | import {v4} from 'uuid'; 5 | 6 | export function autoGeneratedHashKey( 7 | parameters: Partial = {} 8 | ): PropertyAnnotation { 9 | return hashKey({ 10 | ...parameters, 11 | type: 'String', 12 | defaultProvider: v4, 13 | }); 14 | } -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper-annotations/src/constants.ts: -------------------------------------------------------------------------------- 1 | export const METADATA_TYPE_KEY = 'design:type'; 2 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper-annotations/src/exampleSchema.fixture.ts: -------------------------------------------------------------------------------- 1 | import { 2 | attribute, 3 | autoGeneratedHashKey, 4 | rangeKey, 5 | table, 6 | versionAttribute, 7 | } from "./"; 8 | import { embed } from '@aws/dynamodb-data-mapper'; 9 | 10 | export class Author { 11 | @attribute() 12 | name?: string; 13 | 14 | @attribute({memberType: {type: 'String'}}) 15 | socialMediaHandles?: Map; 16 | 17 | @attribute() 18 | photo?: Uint8Array; 19 | } 20 | 21 | export class Comment { 22 | /** 23 | * The time at which this comment was posted 24 | */ 25 | @attribute() 26 | timestamp?: Date; 27 | 28 | /** 29 | * Whether this comment has been approved by a moderator. 30 | */ 31 | @attribute() 32 | approved?: boolean; 33 | 34 | /** 35 | * The title of the comment 36 | */ 37 | @attribute() 38 | subject?: string; 39 | 40 | /** 41 | * The text of the comment 42 | */ 43 | @attribute() 44 | text?: string; 45 | 46 | /** 47 | * The handle of the comment author 48 | */ 49 | @attribute() 50 | author?: string; 51 | 52 | /** 53 | * The number of upvotes this comment has received. 54 | */ 55 | @attribute() 56 | upvotes?: number; 57 | 58 | /** 59 | * The number of downvotes this comment has received. 60 | */ 61 | @attribute() 62 | downvotes?: number; 63 | 64 | /** 65 | * Replies to this comment 66 | */ 67 | @attribute({ memberType: embed(Comment) }) 68 | replies?: Array; 69 | } 70 | 71 | @table('Posts') 72 | export class Post { 73 | @autoGeneratedHashKey() 74 | id?: string; 75 | 76 | @rangeKey() 77 | createdAt?: Date; 78 | 79 | @versionAttribute() 80 | version?: number; 81 | 82 | @attribute() 83 | author?: Author; 84 | 85 | @attribute() 86 | content?: string; 87 | 88 | @attribute() 89 | title?: string; 90 | 91 | @attribute() 92 | subtitle?: string; 93 | 94 | @attribute() 95 | imageLink?: string; 96 | 97 | @attribute({ memberType: { type: 'String' }}) 98 | corrections?: Array; 99 | 100 | /** 101 | * Replies to this post 102 | */ 103 | @attribute({ memberType: embed(Comment) }) 104 | replies?: Array; 105 | 106 | @attribute({ memberType: 'String' }) 107 | tags?: Set; 108 | } 109 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper-annotations/src/functional.spec.ts: -------------------------------------------------------------------------------- 1 | import { Author, Comment, Post } from './exampleSchema.fixture'; 2 | import { DataMapper, DynamoDbSchema } from '@aws/dynamodb-data-mapper'; 3 | import { isSchema } from '@aws/dynamodb-data-marshaller'; 4 | 5 | jest.mock('uuid', () => ({v4: jest.fn(() => 'uuid')})); 6 | 7 | describe('annotations', () => { 8 | it('should create a schema that includes references to property schemas', () => { 9 | const postSchema = (Post.prototype as any)[DynamoDbSchema]; 10 | expect(isSchema(postSchema)).toBe(true); 11 | expect(isSchema(postSchema.author.members)).toBe(true); 12 | expect(isSchema(postSchema.replies.memberType.members)).toBe(true); 13 | }); 14 | 15 | it('should support recursive shapes in the generated schema', () => { 16 | const commentSchema = (Comment.prototype as any)[DynamoDbSchema]; 17 | expect(isSchema(commentSchema)).toBe(true); 18 | expect(isSchema(commentSchema.replies.memberType.members)).toBe(true); 19 | expect(commentSchema.replies.memberType.members).toBe(commentSchema); 20 | }); 21 | 22 | it('should marshall a full object graph according to the schema', async () => { 23 | const promiseFunc = jest.fn(() => Promise.resolve({Item: {}})); 24 | const mockDynamoDbClient = { 25 | config: {}, 26 | putItem: jest.fn(() => ({promise: promiseFunc})), 27 | }; 28 | 29 | const mapper = new DataMapper({ 30 | client: mockDynamoDbClient as any, 31 | }); 32 | 33 | const post = new Post(); 34 | post.createdAt = new Date(0); 35 | post.author = new Author(); 36 | post.author.name = 'John Smith'; 37 | post.author.photo = Uint8Array.from([0xde, 0xad, 0xbe, 0xef]); 38 | post.author.socialMediaHandles = new Map([ 39 | ['github', 'john_smith_27834231'], 40 | ['twitter', 'theRealJohnSmith'], 41 | ]); 42 | post.title = 'Review of Rob Loblaw\'s Law Blog'; 43 | post.subtitle = 'Does it live up to the hype?'; 44 | post.content = "It's a great law blog."; 45 | post.corrections = [ 46 | 'The first edition of this post did not adequately attest to the law blog\'s greatness.' 47 | ]; 48 | post.replies = [new Comment()]; 49 | 50 | post.replies[0].author = 'Rob Loblaw'; 51 | post.replies[0].timestamp = new Date(0); 52 | post.replies[0].subject = 'Great review'; 53 | post.replies[0].text = 'Appreciate the congrats'; 54 | post.replies[0].upvotes = 35; 55 | post.replies[0].downvotes = 0; 56 | post.replies[0].approved = true; 57 | 58 | const reply = new Comment(); 59 | reply.author = 'John Smith'; 60 | reply.timestamp = new Date(60000); 61 | reply.subject = 'Great review of my review'; 62 | reply.text = 'Thanks for reading!'; 63 | reply.approved = true; 64 | 65 | post.replies[0].replies = [reply]; 66 | 67 | await mapper.put(post); 68 | 69 | expect((mockDynamoDbClient.putItem.mock.calls[0] as any)[0]) 70 | .toMatchObject({ 71 | ConditionExpression: 'attribute_not_exists(#attr0)', 72 | ExpressionAttributeNames: {'#attr0': 'version'}, 73 | TableName: 'Posts', 74 | Item: { 75 | author: {M: { 76 | name: {S: "John Smith"}, 77 | photo: {B: Uint8Array.from([0xde, 0xad, 0xbe, 0xef])}, 78 | socialMediaHandles: {M: { 79 | github: {S: "john_smith_27834231"}, 80 | twitter: {S: "theRealJohnSmith"} 81 | }} 82 | }}, 83 | content: {S: "It's a great law blog."}, 84 | corrections: {L: [ 85 | {S: "The first edition of this post did not adequately attest to the law blog's greatness."} 86 | ]}, 87 | createdAt: {N: "0"}, 88 | id: {S: "uuid"}, 89 | replies: {L: [ 90 | {M: { 91 | approved: {BOOL: true}, 92 | author: {S: "Rob Loblaw"}, 93 | downvotes: {N: "0"}, 94 | replies: {L: [ 95 | {M: { 96 | approved: {BOOL: true}, 97 | author: {S: "John Smith"}, 98 | subject: {S: "Great review of my review"}, 99 | text: {S: "Thanks for reading!"}, 100 | timestamp: {N: "60"} 101 | }} 102 | ]}, 103 | subject: {S: "Great review"}, 104 | text: {S: "Appreciate the congrats"}, 105 | timestamp: {N: "0"}, 106 | upvotes: {N: "35"} 107 | }} 108 | ]}, 109 | subtitle: {S: "Does it live up to the hype?"}, 110 | title: {S: "Review of Rob Loblaw's Law Blog"}, 111 | version: {N: "0"} 112 | }, 113 | }); 114 | }); 115 | }); 116 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper-annotations/src/hashKey.spec.ts: -------------------------------------------------------------------------------- 1 | import { hashKey } from './hashKey'; 2 | 3 | jest.mock('./attribute', () => ({attribute: jest.fn()})); 4 | import {attribute} from './attribute'; 5 | 6 | describe('hashKey', () => { 7 | beforeEach(() => { 8 | (attribute as any).mockClear(); 9 | }); 10 | 11 | it('should call attribute with a defined keyType', () => { 12 | hashKey(); 13 | 14 | expect((attribute as any).mock.calls.length).toBe(1); 15 | expect((attribute as any).mock.calls[0]).toEqual([ 16 | {keyType: 'HASH'} 17 | ]); 18 | }); 19 | 20 | it('should pass through any supplied parameters', () => { 21 | const attributeName = 'foo' 22 | hashKey({attributeName}); 23 | 24 | expect((attribute as any).mock.calls[0][0]) 25 | .toMatchObject({attributeName}); 26 | }); 27 | }); 28 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper-annotations/src/hashKey.ts: -------------------------------------------------------------------------------- 1 | import {PropertyAnnotation} from './annotationShapes'; 2 | import {attribute} from './attribute'; 3 | import { 4 | BinaryType, 5 | CustomType, 6 | DateType, 7 | NumberType, 8 | StringType, 9 | } from '@aws/dynamodb-data-marshaller'; 10 | 11 | export function hashKey( 12 | parameters: Partial|DateType|NumberType|StringType> = {} 13 | ): PropertyAnnotation { 14 | return attribute({ 15 | ...parameters, 16 | keyType: 'HASH', 17 | }); 18 | } -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper-annotations/src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './attribute'; 2 | export * from './autoGeneratedHashKey'; 3 | export * from './hashKey'; 4 | export * from './rangeKey'; 5 | export * from './table'; 6 | export * from './versionAttribute'; 7 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper-annotations/src/rangeKey.spec.ts: -------------------------------------------------------------------------------- 1 | import { rangeKey } from './rangeKey'; 2 | 3 | jest.mock('./attribute', () => ({attribute: jest.fn()})); 4 | import { attribute } from './attribute'; 5 | 6 | describe('rangeKey', () => { 7 | beforeEach(() => { 8 | (attribute as any).mockClear(); 9 | }); 10 | 11 | it('should call attribute with a defined keyType', () => { 12 | rangeKey(); 13 | 14 | expect((attribute as any).mock.calls.length).toBe(1); 15 | expect((attribute as any).mock.calls[0]).toEqual([ 16 | {keyType: 'RANGE'} 17 | ]); 18 | }); 19 | 20 | it('should pass through any supplied parameters', () => { 21 | const attributeName = 'foo' 22 | rangeKey({attributeName}); 23 | 24 | expect((attribute as any).mock.calls[0][0]) 25 | .toMatchObject({attributeName}); 26 | }); 27 | }); 28 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper-annotations/src/rangeKey.ts: -------------------------------------------------------------------------------- 1 | import {PropertyAnnotation} from './annotationShapes'; 2 | import {attribute} from './attribute'; 3 | import { 4 | BinaryType, 5 | CustomType, 6 | DateType, 7 | NumberType, 8 | StringType, 9 | } from '@aws/dynamodb-data-marshaller'; 10 | 11 | export function rangeKey( 12 | parameters: Partial|DateType|NumberType|StringType> = {} 13 | ): PropertyAnnotation { 14 | return attribute({ 15 | ...parameters, 16 | keyType: 'RANGE', 17 | }); 18 | } -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper-annotations/src/table.spec.ts: -------------------------------------------------------------------------------- 1 | import {table} from "./table"; 2 | import {DynamoDbTable} from '@aws/dynamodb-data-mapper'; 3 | 4 | describe('table', () => { 5 | it( 6 | 'should bind the provided table name to the target in a way compatible with the DynamoDbTable protocol', 7 | () => { 8 | class MyDocument {} 9 | const tableName = 'tableName'; 10 | const decorator = table(tableName); 11 | decorator(MyDocument); 12 | 13 | expect((new MyDocument() as any)[DynamoDbTable]).toBe(tableName); 14 | } 15 | ); 16 | }); 17 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper-annotations/src/table.ts: -------------------------------------------------------------------------------- 1 | import {ClassAnnotation} from './annotationShapes'; 2 | import {DynamoDbTable} from '@aws/dynamodb-data-mapper'; 3 | 4 | /** 5 | * Declare a TypeScript class to represent items in a table in a way that is 6 | * understandable by the AWS DynamoDB DataMapper for JavaScript. Meant to be 7 | * used as a TypeScript class decorator in projects compiled with the 8 | * `experimentalDecorators` option enabled. 9 | * 10 | * @see https://www.typescriptlang.org/docs/handbook/decorators.html 11 | * @see https://www.typescriptlang.org/docs/handbook/compiler-options.html 12 | */ 13 | export function table(tableName: string): ClassAnnotation { 14 | return constructor => { 15 | constructor.prototype[DynamoDbTable] = tableName; 16 | }; 17 | } 18 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper-annotations/src/versionAttribute.spec.ts: -------------------------------------------------------------------------------- 1 | import { versionAttribute } from './versionAttribute'; 2 | 3 | jest.mock('./attribute', () => ({attribute: jest.fn()})); 4 | import { attribute } from './attribute'; 5 | 6 | describe('versionAttribute', () => { 7 | beforeEach(() => { 8 | (attribute as any).mockClear(); 9 | }); 10 | 11 | it( 12 | 'should call attribute with a defined type and versionAttribute trait', 13 | () => { 14 | versionAttribute(); 15 | 16 | expect((attribute as any).mock.calls.length).toBe(1); 17 | expect((attribute as any).mock.calls[0]).toEqual([ 18 | { 19 | type: 'Number', 20 | versionAttribute: true, 21 | } 22 | ]); 23 | } 24 | ); 25 | 26 | it('should pass through any supplied parameters', () => { 27 | const attributeName = 'foo' 28 | versionAttribute({attributeName}); 29 | 30 | expect((attribute as any).mock.calls[0][0]) 31 | .toMatchObject({attributeName}); 32 | }); 33 | }); 34 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper-annotations/src/versionAttribute.ts: -------------------------------------------------------------------------------- 1 | import {PropertyAnnotation} from './annotationShapes'; 2 | import {attribute} from './attribute'; 3 | import {NumberType} from '@aws/dynamodb-data-marshaller'; 4 | 5 | export function versionAttribute( 6 | parameters: Partial = {} 7 | ): PropertyAnnotation { 8 | return attribute({ 9 | ...parameters, 10 | type: 'Number', 11 | versionAttribute: true, 12 | }); 13 | } -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper-annotations/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": [ 5 | "es5", 6 | "es2015.iterable", 7 | "es2015.promise", 8 | "es2015.collection", 9 | "es2015.symbol.wellknown", 10 | "esnext.asynciterable" 11 | ], 12 | "downlevelIteration": true, 13 | "importHelpers": true, 14 | "module": "commonjs", 15 | "strict": true, 16 | "noUnusedLocals": true, 17 | "declaration": true, 18 | "sourceMap": true, 19 | "experimentalDecorators": true, 20 | "emitDecoratorMetadata": true, 21 | "rootDir": "./src", 22 | "outDir": "./build" 23 | }, 24 | "typedocOptions": { 25 | "mode": "file", 26 | "out": "../../docs/packages/dynamodb-data-mapper-annotations", 27 | "excludeNotExported": true, 28 | "excludePrivate": true, 29 | "hideGenerator": true 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper-annotations/tsconfig.test.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "sourceMap": false, 5 | "inlineSourceMap": true, 6 | "inlineSources": true, 7 | "rootDir": "./src", 8 | "outDir": "./build" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/.gitignore: -------------------------------------------------------------------------------- 1 | !jest.integration.js 2 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/.npmignore: -------------------------------------------------------------------------------- 1 | /src 2 | /node_modules 3 | /coverage 4 | 5 | *.spec.d.ts 6 | *.spec.js 7 | *.spec.js.map 8 | 9 | tsconfig.json 10 | tsconfig.test.json 11 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | All notable changes to this project will be documented in this file. 3 | 4 | The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) 5 | and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). 6 | 7 | ## [0.7.3] 8 | ### Added 9 | - Export query/scan/parallelScan iterator and paginator classes. 10 | 11 | ## [0.7.2] 12 | ### Fixed 13 | - Sort key configurations in `CreateTableInput` so that hash keys always appear 14 | first. 15 | 16 | ## [0.7.1] 17 | ### Removed 18 | - Remove package rollup at `./build/index.mjs` due to bundler incompatibilities. 19 | 20 | ## [0.7.0] 21 | ### Added 22 | - Add a package rollup at `./build/index.mjs` to support tree shaking. 23 | 24 | ## [0.6.0] 25 | ### Fixed 26 | - Update `query` and `scan` to serialize whatever key properties are provided 27 | without injecting any defaulted values. 28 | - Update `DataMapper` for TypeScript 2.9 compatibility. 29 | 30 | ### Added 31 | - Use purpose-built async iterable objects as the return value for `query`, 32 | `scan`, and `parallelScan`. 33 | - Report the `count`, `scannedCount`, and `consumedCapacity` tallied over the 34 | lifetime of a `query`, `scan`, or `parallelScan` as properties on the 35 | returned iterable. 36 | - Provide a method to get the underlying paginator for a `query`, `scan`, or 37 | `parallelScan` iterator. The paginator may be used to suspend and resume 38 | iteration at any page boundary. 39 | - Add `limit` parameter to `scan` and `query` to automatically cease iteration 40 | once a certain number of items have been returned or the results have been 41 | exhausted, whichever comes first. 42 | 43 | ## [0.5.0] 44 | ### Fixed 45 | - Add default message to `ItemNotFoundException` 46 | - Ensure options provided are used when `query` is called with a named 47 | parameter bag. 48 | ### Added 49 | - Add support for executing custom update expressions. 50 | 51 | ## [0.4.2] 52 | ### Fixed 53 | - Ensure `query` and `scan` marshall exclusive start keys for the specified 54 | index. 55 | 56 | ## [0.4.1] 57 | ### Fixed 58 | - Ensure `query` returns instances of the provided model class. 59 | 60 | ## [0.4.0] 61 | ### Added 62 | - Add `createTable` to create tables based on table names and schemas bound to 63 | constructor prototypes 64 | - Add `ensureTableExists` to create a table only if it does not already exist 65 | - Add `deleteTable` to delete tables based on table names bound to constructor 66 | prototypes 67 | - Add `ensureTableNotExists` to delete a table only if it is not already 68 | deleted 69 | 70 | ## [0.3.2] 71 | ### Fixed 72 | - Only include expression name or value substitions when a substitution has 73 | occurred 74 | 75 | ## [0.3.1] 76 | ### Fixed 77 | - Ensure retried writes in a `batchDelete`, `batchPut`, or `batchWrite` are 78 | only yielded once 79 | 80 | ## [0.3.0] 81 | ### Added 82 | - Add `batchGet`, which allows a synchronous or asynchronous iterable of items 83 | (like those supplied to `get`) to be automatically grouped into 84 | `BatchGetItem` operations. 85 | - Add `batchDelete`, which allows a synchronous or asynchronous iterable of 86 | items (like those supplied to `delete`) to be automatically grouped into 87 | `BatchWriteItem` operations. 88 | - Add `batchPut`, which allows a synchronous or asynchronous iterable of 89 | items (like those supplied to `put`) to be automatically grouped into 90 | `BatchWriteItem` operations. 91 | - Add `batchWrite`, which allows a synchronous or asynchronous iterable of 92 | tuples of tags (`'put'` or `'delete'`) and items (like those supplied to the 93 | `put` or `delete` methods, respectively) to be automatically grouped into 94 | `BatchWriteItem` operations. 95 | 96 | ## [0.2.1] 97 | ### Added 98 | - Add the ability to call all DataMapper methods with positional rather than 99 | named parameters 100 | - Add API documentation 101 | 102 | ### Deprecated 103 | - Deprecate calling DataMapper methods with a single bag of named parameters 104 | 105 | ## [0.2.0] 106 | ### Removed 107 | - **BREAKING CHANGE**: Removed the `returnValues` parameter from `put`. `put` 108 | will now always return the value that was persisted, thereby providing 109 | access to injected defaults and accurate version numbers. 110 | 111 | ### Added 112 | - Add a `parallelScan` method to the DataMapper. 113 | - Add optional parameters to the `scan` method to allow its use as a parallel 114 | scan worker 115 | - Add a `pageSize` parameter to `query` and `scan` to limit the size of pages 116 | fetched during a read. `pageSize` was previously called `limit`. 117 | 118 | ### Changed 119 | - Use TSLib instead of having TypeScript generate helpers to reduce bundle size 120 | 121 | ### Deprecated 122 | - Deprecate `limit` parameter on `query` and `scan`. It has been renamed to 123 | `pageSize`, though a value provided for `limit` will still be used if no 124 | `pageSize` parameter is provided. 125 | 126 | ## [0.1.1] 127 | ### Fixed 128 | - Update dependency version to match released version identifier 129 | 130 | ## [0.1.0] 131 | Initial release 132 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/jest.integration.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | collectCoverage: true, 3 | testMatch: [ 4 | '**/?(*.)(integ).js' 5 | ] 6 | }; 7 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@aws/dynamodb-data-mapper", 3 | "version": "0.7.3", 4 | "description": "A schema-based data mapper for Amazon DynamoDB", 5 | "keywords": [ 6 | "aws", 7 | "dynamodb" 8 | ], 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/awslabs/dynamodb-data-mapper-js.git" 12 | }, 13 | "bugs": { 14 | "url": "https://github.com/awslabs/dynamodb-data-mapper-js/issues" 15 | }, 16 | "homepage": "https://awslabs.github.io/dynamodb-data-mapper-js/packages/dynamodb-data-mapper/", 17 | "main": "./build/index.js", 18 | "types": "./build/index.d.ts", 19 | "scripts": { 20 | "build": "tsc", 21 | "docs": "typedoc src", 22 | "integ": "npm run pretest && jest --config=jest.integration.js", 23 | "prepublishOnly": "npm run build", 24 | "pretest": "tsc -p tsconfig.test.json", 25 | "test": "jest \"build/(.+).spec.js\"" 26 | }, 27 | "author": { 28 | "name": "AWS SDK for JavaScript Team", 29 | "email": "aws-sdk-js@amazon.com" 30 | }, 31 | "license": "Apache-2.0", 32 | "devDependencies": { 33 | "@types/jest": "^24", 34 | "@types/node": "^8.0.4", 35 | "aws-sdk": "^2.7.0", 36 | "jest": "^24", 37 | "typedoc": "^0.14.0", 38 | "typescript": "^3.4" 39 | }, 40 | "dependencies": { 41 | "@aws/dynamodb-auto-marshaller": "^0.7.1", 42 | "@aws/dynamodb-batch-iterator": "^0.7.1", 43 | "@aws/dynamodb-data-marshaller": "^0.7.3", 44 | "@aws/dynamodb-expressions": "^0.7.3", 45 | "@aws/dynamodb-query-iterator": "^0.7.1", 46 | "tslib": "^1.9" 47 | }, 48 | "peerDependencies": { 49 | "aws-sdk": "^2.7.0" 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/src/BatchState.ts: -------------------------------------------------------------------------------- 1 | import {Schema, ZeroArgumentsConstructor} from '@aws/dynamodb-data-marshaller'; 2 | 3 | export interface BatchState { 4 | [tableName: string]: { 5 | keyProperties: Array; 6 | itemSchemata: { 7 | [identifier: string]: { 8 | schema: Schema; 9 | constructor: ZeroArgumentsConstructor; 10 | }; 11 | }; 12 | }; 13 | } 14 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/src/ItemNotFoundException.spec.ts: -------------------------------------------------------------------------------- 1 | import {ItemNotFoundException} from "./ItemNotFoundException"; 2 | import {GetItemInput} from "aws-sdk/clients/dynamodb"; 3 | 4 | describe('ItemNotFoundException', () => { 5 | it('should include the request sent as part of the error', () => { 6 | const getItemInput: GetItemInput = { 7 | TableName: 'foo', 8 | Key: { 9 | fizz: {S: 'buzz'}, 10 | }, 11 | }; 12 | 13 | const exception = new ItemNotFoundException(getItemInput, 'message'); 14 | expect(exception.message).toBe('message'); 15 | expect(exception.itemSought).toBe(getItemInput); 16 | }); 17 | 18 | it('should identify itself by name', () => { 19 | const exception = new ItemNotFoundException({} as any, 'message'); 20 | expect(exception.name).toBe('ItemNotFoundException'); 21 | }); 22 | 23 | it( 24 | 'should construct a default message from the item sought if no message supplied', 25 | () => { 26 | const exception = new ItemNotFoundException({Key: {foo: {S: "bar"}}, TableName: "MyTable"}); 27 | expect(exception.message).toBe( 28 | 'No item with the key {"foo":{"S":"bar"}} found in the MyTable table.' 29 | ); 30 | } 31 | ); 32 | }); 33 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/src/ItemNotFoundException.ts: -------------------------------------------------------------------------------- 1 | import {GetItemInput} from "aws-sdk/clients/dynamodb"; 2 | 3 | /** 4 | * An exception thrown when an item was sought with a DynamoDB::GetItem 5 | * request and not found. Includes the original request sent as 6 | * `itemSought`. 7 | */ 8 | export class ItemNotFoundException extends Error { 9 | readonly name = 'ItemNotFoundException'; 10 | 11 | constructor( 12 | public readonly itemSought: GetItemInput, 13 | message: string = defaultErrorMessage(itemSought) 14 | ) { 15 | super(message); 16 | } 17 | } 18 | 19 | function defaultErrorMessage(itemSought: GetItemInput): string { 20 | return `No item with the key ${ 21 | JSON.stringify(itemSought.Key) 22 | } found in the ${itemSought.TableName} table.`; 23 | } 24 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/src/Iterator.ts: -------------------------------------------------------------------------------- 1 | import { Paginator as AbstractPaginator } from './Paginator'; 2 | import { ConsumedCapacity } from 'aws-sdk/clients/dynamodb'; 3 | 4 | require('./asyncIteratorSymbolPolyfill'); 5 | 6 | export abstract class Iterator< 7 | T, 8 | Paginator extends AbstractPaginator 9 | > implements AsyncIterableIterator { 10 | private _count = 0; 11 | private lastResolved: Promise> = Promise.resolve() as any; 12 | private readonly pending: Array = []; 13 | 14 | protected lastYielded?: T; 15 | 16 | protected constructor(private readonly paginator: Paginator) {} 17 | 18 | /** 19 | * @inheritDoc 20 | */ 21 | [Symbol.asyncIterator]() { 22 | return this; 23 | } 24 | 25 | /** 26 | * @inheritDoc 27 | */ 28 | next(): Promise> { 29 | this.lastResolved = this.lastResolved.then(() => this.getNext()); 30 | return this.lastResolved; 31 | } 32 | 33 | /** 34 | * Detaches the underlying paginator from this iterator and returns it. The 35 | * paginator will yield arrays of unmarshalled items, with each yielded 36 | * array corresponding to a single call to the underlying API. As with the 37 | * underlying API, pages may contain a variable number of items or no items, 38 | * in which case an empty array will be yielded. 39 | * 40 | * Calling this method will disable further iteration. 41 | */ 42 | pages(): Paginator { 43 | // Prevent the iterator from being used further and squelch any uncaught 44 | // promise rejection warnings 45 | this.lastResolved = Promise.reject(new Error( 46 | 'The underlying paginator has been detached from this iterator.' 47 | )); 48 | this.lastResolved.catch(() => {}); 49 | 50 | return this.paginator; 51 | } 52 | 53 | /** 54 | * @inheritDoc 55 | */ 56 | return(): Promise> { 57 | // Prevent any further use of this iterator 58 | this.lastResolved = Promise.reject(new Error( 59 | 'Iteration has been manually interrupted and may not be resumed' 60 | )); 61 | this.lastResolved.catch(() => {}); 62 | 63 | // Empty the pending queue to free up memory 64 | this.pending.length = 0; 65 | return this.paginator.return() as any; 66 | } 67 | 68 | /** 69 | * Retrieve the reported capacity consumed by this iterator. Will be 70 | * undefined unless returned consumed capacity is requested. 71 | */ 72 | get consumedCapacity(): ConsumedCapacity|undefined { 73 | return this.paginator.consumedCapacity; 74 | } 75 | 76 | /** 77 | * Retrieve the number of items yielded thus far by this iterator. 78 | */ 79 | get count() { 80 | return this._count; 81 | } 82 | 83 | /** 84 | * Retrieve the number of items scanned thus far during the execution of 85 | * this iterator. This number should be the same as {@link count} unless a 86 | * filter expression was used. 87 | */ 88 | get scannedCount() { 89 | return this.paginator.scannedCount; 90 | } 91 | 92 | private async getNext(): Promise> { 93 | if (this.pending.length > 0) { 94 | this.lastYielded = this.pending.shift()!; 95 | this._count++; 96 | return { 97 | done: false, 98 | value: this.lastYielded 99 | } 100 | } 101 | 102 | return this.paginator.next().then(({value = [], done}) => { 103 | if (!done) { 104 | this.pending.push(...value); 105 | return this.getNext(); 106 | } 107 | 108 | this.lastYielded = undefined; 109 | return {done: true} as IteratorResult; 110 | }); 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/src/Paginator.ts: -------------------------------------------------------------------------------- 1 | import { getSchema } from './protocols'; 2 | import { DynamoDbPaginatorInterface } from '@aws/dynamodb-query-iterator'; 3 | import { 4 | Schema, 5 | unmarshallItem, 6 | ZeroArgumentsConstructor, 7 | } from '@aws/dynamodb-data-marshaller'; 8 | import { ConsumedCapacity } from 'aws-sdk/clients/dynamodb'; 9 | 10 | require('./asyncIteratorSymbolPolyfill'); 11 | 12 | export abstract class Paginator implements AsyncIterableIterator> { 13 | private readonly itemSchema: Schema; 14 | private lastKey?: T; 15 | private lastResolved: Promise>> = Promise.resolve() as any; 16 | 17 | protected constructor( 18 | private readonly paginator: DynamoDbPaginatorInterface, 19 | private readonly valueConstructor: ZeroArgumentsConstructor 20 | ) { 21 | this.itemSchema = getSchema(valueConstructor.prototype); 22 | } 23 | 24 | /** 25 | * @inheritDoc 26 | */ 27 | [Symbol.asyncIterator]() { 28 | return this; 29 | } 30 | 31 | /** 32 | * @inheritDoc 33 | */ 34 | next(): Promise>> { 35 | this.lastResolved = this.lastResolved.then(() => this.getNext()); 36 | return this.lastResolved; 37 | } 38 | 39 | /** 40 | * @inheritDoc 41 | */ 42 | return(): Promise>> { 43 | // Prevent any further use of this iterator 44 | this.lastResolved = Promise.reject(new Error( 45 | 'Iteration has been manually interrupted and may not be resumed' 46 | )); 47 | this.lastResolved.catch(() => {}); 48 | 49 | return this.paginator.return() as any; 50 | } 51 | 52 | /** 53 | * Retrieve the reported capacity consumed by this paginator. Will be 54 | * undefined unless returned consumed capacity is requested. 55 | */ 56 | get consumedCapacity(): ConsumedCapacity|undefined { 57 | return this.paginator.consumedCapacity; 58 | } 59 | 60 | /** 61 | * Retrieve the number of items yielded thus far by this paginator. 62 | */ 63 | get count() { 64 | return this.paginator.count; 65 | } 66 | 67 | /** 68 | * Retrieve the last reported `LastEvaluatedKey`, unmarshalled according to 69 | * the schema used by this paginator. 70 | */ 71 | get lastEvaluatedKey(): Partial|undefined { 72 | return this.lastKey; 73 | } 74 | 75 | /** 76 | * Retrieve the number of items scanned thus far during the execution of 77 | * this paginator. This number should be the same as {@link count} unless a 78 | * filter expression was used. 79 | */ 80 | get scannedCount() { 81 | return this.paginator.scannedCount; 82 | } 83 | 84 | private async getNext(): Promise>> { 85 | return this.paginator.next().then(({value = {}, done}) => { 86 | if (!done) { 87 | this.lastKey = value.LastEvaluatedKey && unmarshallItem( 88 | this.itemSchema, 89 | value.LastEvaluatedKey, 90 | this.valueConstructor 91 | ); 92 | 93 | return { 94 | value: (value.Items || []).map(item => unmarshallItem( 95 | this.itemSchema, 96 | item, 97 | this.valueConstructor 98 | )), 99 | done: false 100 | }; 101 | } 102 | 103 | return {done: true} as IteratorResult>; 104 | }); 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/src/ParallelScanIterator.ts: -------------------------------------------------------------------------------- 1 | import { Iterator } from './Iterator'; 2 | import { ParallelScanOptions } from './namedParameters'; 3 | import { ParallelScanPaginator } from './ParallelScanPaginator'; 4 | import { ZeroArgumentsConstructor } from '@aws/dynamodb-data-marshaller'; 5 | import DynamoDB = require('aws-sdk/clients/dynamodb'); 6 | 7 | /** 8 | * Iterates over each item returned by a parallel DynamoDB scan until no more 9 | * pages are available. 10 | */ 11 | export class ParallelScanIterator extends 12 | Iterator> 13 | { 14 | constructor( 15 | client: DynamoDB, 16 | itemConstructor: ZeroArgumentsConstructor, 17 | segments: number, 18 | options: ParallelScanOptions & { tableNamePrefix?: string } = {} 19 | ) { 20 | super(new ParallelScanPaginator( 21 | client, 22 | itemConstructor, 23 | segments, 24 | options 25 | )); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/src/ParallelScanPaginator.ts: -------------------------------------------------------------------------------- 1 | import { buildScanInput } from './buildScanInput'; 2 | import { 3 | ParallelScanOptions, 4 | ParallelScanState, 5 | ScanState, 6 | } from './namedParameters'; 7 | import { Paginator } from './Paginator'; 8 | import { getSchema } from './protocols'; 9 | import { 10 | ParallelScanInput, 11 | ParallelScanPaginator as BasePaginator, 12 | ParallelScanState as BaseParallelScanState, 13 | ScanState as BaseScanState, 14 | } from '@aws/dynamodb-query-iterator'; 15 | import { 16 | marshallKey, 17 | Schema, 18 | unmarshallItem, 19 | ZeroArgumentsConstructor, 20 | } from '@aws/dynamodb-data-marshaller'; 21 | import DynamoDB = require('aws-sdk/clients/dynamodb'); 22 | 23 | /** 24 | * Iterates over each page of items returned by a parallel DynamoDB scan until 25 | * no more pages are available. 26 | */ 27 | export class ParallelScanPaginator extends Paginator { 28 | private readonly _ctor: ZeroArgumentsConstructor; 29 | private readonly _paginator: BasePaginator; 30 | private readonly _schema: Schema; 31 | 32 | constructor( 33 | client: DynamoDB, 34 | itemConstructor: ZeroArgumentsConstructor, 35 | segments: number, 36 | options: ParallelScanOptions & { tableNamePrefix?: string } = {} 37 | ) { 38 | const schema = getSchema(itemConstructor.prototype); 39 | const input: ParallelScanInput = { 40 | ...buildScanInput(itemConstructor, options), 41 | TotalSegments: segments, 42 | ExclusiveStartKey: undefined, 43 | Segment: undefined 44 | }; 45 | 46 | let scanState: BaseParallelScanState|undefined; 47 | if (options.scanState) { 48 | scanState = options.scanState.map( 49 | ({initialized, lastEvaluatedKey: lastKey}) => ({ 50 | initialized, 51 | LastEvaluatedKey: lastKey 52 | ? marshallKey(schema, lastKey, options.indexName) 53 | : undefined 54 | } as BaseScanState) 55 | ); 56 | } 57 | 58 | const paginator = new BasePaginator(client, input, scanState); 59 | super(paginator, itemConstructor); 60 | 61 | this._paginator = paginator; 62 | this._ctor = itemConstructor; 63 | this._schema = schema; 64 | } 65 | 66 | /** 67 | * The `lastEvaluatedKey` attribute is not available on parallel scans. Use 68 | * {@link scanState} instead. 69 | */ 70 | get lastEvaluatedKey() { 71 | return undefined; 72 | } 73 | 74 | /** 75 | * A snapshot of the current state of a parallel scan. May be used to resume 76 | * a parallel scan with a separate paginator. 77 | */ 78 | get scanState(): ParallelScanState { 79 | return this._paginator.scanState.map( 80 | ({initialized, LastEvaluatedKey}) => ({ 81 | initialized, 82 | lastEvaluatedKey: LastEvaluatedKey 83 | ? unmarshallItem(this._schema, LastEvaluatedKey, this._ctor) 84 | : undefined 85 | } as ScanState) 86 | ); 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/src/QueryIterator.ts: -------------------------------------------------------------------------------- 1 | import { Iterator } from './Iterator'; 2 | import { QueryOptions } from './namedParameters'; 3 | import { QueryPaginator } from './QueryPaginator'; 4 | import { ZeroArgumentsConstructor } from '@aws/dynamodb-data-marshaller'; 5 | import { 6 | ConditionExpression, 7 | ConditionExpressionPredicate, 8 | } from '@aws/dynamodb-expressions'; 9 | import DynamoDB = require('aws-sdk/clients/dynamodb'); 10 | 11 | /** 12 | * Iterates over each item returned by a DynamoDB query until no more pages are 13 | * available. 14 | */ 15 | export class QueryIterator extends Iterator> { 16 | constructor( 17 | client: DynamoDB, 18 | valueConstructor: ZeroArgumentsConstructor, 19 | keyCondition: ConditionExpression | 20 | {[propertyName: string]: ConditionExpressionPredicate|any}, 21 | options?: QueryOptions & {tableNamePrefix?: string} 22 | ) { 23 | super( 24 | new QueryPaginator(client, valueConstructor, keyCondition, options) 25 | ); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/src/QueryPaginator.ts: -------------------------------------------------------------------------------- 1 | import { marshallStartKey } from './marshallStartKey'; 2 | import { QueryOptions } from './namedParameters'; 3 | import { Paginator } from './Paginator'; 4 | import { getSchema, getTableName } from './protocols'; 5 | import { QueryPaginator as BasePaginator } from '@aws/dynamodb-query-iterator'; 6 | import { 7 | marshallConditionExpression, 8 | marshallProjectionExpression, 9 | ZeroArgumentsConstructor, 10 | } from '@aws/dynamodb-data-marshaller'; 11 | import { 12 | ConditionExpression, 13 | ConditionExpressionPredicate, 14 | ExpressionAttributes, 15 | isConditionExpression, 16 | isConditionExpressionPredicate, 17 | } from '@aws/dynamodb-expressions'; 18 | import { QueryInput } from 'aws-sdk/clients/dynamodb'; 19 | import DynamoDB = require('aws-sdk/clients/dynamodb'); 20 | 21 | /** 22 | * Iterates over each page of items returned by a DynamoDB query until no more 23 | * pages are available. 24 | */ 25 | export class QueryPaginator extends Paginator { 26 | constructor( 27 | client: DynamoDB, 28 | valueConstructor: ZeroArgumentsConstructor, 29 | keyCondition: ConditionExpression | 30 | {[propertyName: string]: ConditionExpressionPredicate|any}, 31 | options: QueryOptions & {tableNamePrefix?: string} = {} 32 | ) { 33 | const itemSchema = getSchema(valueConstructor.prototype); 34 | 35 | let { 36 | filter, 37 | indexName, 38 | limit, 39 | pageSize, 40 | projection, 41 | readConsistency, 42 | scanIndexForward, 43 | startKey, 44 | tableNamePrefix: prefix, 45 | } = options; 46 | 47 | const req: QueryInput = { 48 | TableName: getTableName(valueConstructor.prototype, prefix), 49 | ScanIndexForward: scanIndexForward, 50 | Limit: pageSize, 51 | IndexName: indexName, 52 | }; 53 | 54 | if (readConsistency === 'strong') { 55 | req.ConsistentRead = true; 56 | } 57 | 58 | const attributes = new ExpressionAttributes(); 59 | req.KeyConditionExpression = marshallConditionExpression( 60 | normalizeKeyCondition(keyCondition), 61 | itemSchema, 62 | attributes 63 | ).expression; 64 | 65 | if (filter) { 66 | req.FilterExpression = marshallConditionExpression( 67 | filter, 68 | itemSchema, 69 | attributes 70 | ).expression; 71 | } 72 | 73 | if (projection) { 74 | req.ProjectionExpression = marshallProjectionExpression( 75 | projection, 76 | itemSchema, 77 | attributes 78 | ).expression; 79 | } 80 | 81 | if (Object.keys(attributes.names).length > 0) { 82 | req.ExpressionAttributeNames = attributes.names; 83 | } 84 | 85 | if (Object.keys(attributes.values).length > 0) { 86 | req.ExpressionAttributeValues = attributes.values; 87 | } 88 | 89 | if (startKey) { 90 | req.ExclusiveStartKey = marshallStartKey(itemSchema, startKey); 91 | } 92 | 93 | super( 94 | new BasePaginator(client, req, limit), 95 | valueConstructor 96 | ); 97 | } 98 | } 99 | 100 | function normalizeKeyCondition( 101 | keyCondition: ConditionExpression | 102 | {[key: string]: ConditionExpressionPredicate|any} 103 | ): ConditionExpression { 104 | if (isConditionExpression(keyCondition)) { 105 | return keyCondition; 106 | } 107 | 108 | const conditions: Array = []; 109 | for (const property of Object.keys(keyCondition)) { 110 | const predicate = keyCondition[property]; 111 | if (isConditionExpressionPredicate(predicate)) { 112 | conditions.push({ 113 | ...predicate, 114 | subject: property, 115 | }); 116 | } else { 117 | conditions.push({ 118 | type: 'Equals', 119 | subject: property, 120 | object: predicate, 121 | }); 122 | } 123 | } 124 | 125 | if (conditions.length === 1) { 126 | return conditions[0]; 127 | } 128 | 129 | return {type: 'And', conditions}; 130 | } 131 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/src/ScanIterator.ts: -------------------------------------------------------------------------------- 1 | import { Iterator } from './Iterator'; 2 | import { SequentialScanOptions } from './namedParameters'; 3 | import { ScanPaginator } from './ScanPaginator'; 4 | import { ZeroArgumentsConstructor } from '@aws/dynamodb-data-marshaller'; 5 | import DynamoDB = require('aws-sdk/clients/dynamodb'); 6 | 7 | /** 8 | * Iterates over each item returned by a DynamoDB scan until no more pages are 9 | * available. 10 | */ 11 | export class ScanIterator extends Iterator> { 12 | constructor( 13 | client: DynamoDB, 14 | valueConstructor: ZeroArgumentsConstructor, 15 | options?: SequentialScanOptions 16 | ) { 17 | super(new ScanPaginator(client, valueConstructor, options)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/src/ScanPaginator.ts: -------------------------------------------------------------------------------- 1 | import { buildScanInput } from './buildScanInput'; 2 | import { SequentialScanOptions } from './namedParameters'; 3 | import { Paginator } from './Paginator'; 4 | import { ScanPaginator as BasePaginator } from '@aws/dynamodb-query-iterator'; 5 | import { ZeroArgumentsConstructor } from '@aws/dynamodb-data-marshaller'; 6 | import DynamoDB = require('aws-sdk/clients/dynamodb'); 7 | 8 | /** 9 | * Iterates over each page of items returned by a DynamoDB scan until no more 10 | * pages are available. 11 | */ 12 | export class ScanPaginator extends Paginator { 13 | constructor( 14 | client: DynamoDB, 15 | itemConstructor: ZeroArgumentsConstructor, 16 | options: SequentialScanOptions = {} 17 | ) { 18 | super( 19 | new BasePaginator(client, buildScanInput(itemConstructor, options), options.limit), 20 | itemConstructor 21 | ); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/src/asyncIteratorSymbolPolyfill.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Provides a simple polyfill for runtime environments that provide a Symbol 3 | * implementation but do not have Symbol.asyncIterator available by default. 4 | */ 5 | 6 | if (Symbol && !Symbol.asyncIterator) { 7 | (Symbol as any).asyncIterator = Symbol.for("__@@asyncIterator__"); 8 | } 9 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/src/buildScanInput.ts: -------------------------------------------------------------------------------- 1 | import { marshallStartKey } from './marshallStartKey'; 2 | import { SequentialScanOptions } from './namedParameters'; 3 | import { getSchema, getTableName } from './protocols'; 4 | import { 5 | marshallConditionExpression, 6 | marshallProjectionExpression, 7 | ZeroArgumentsConstructor, 8 | } from '@aws/dynamodb-data-marshaller'; 9 | import { ExpressionAttributes } from '@aws/dynamodb-expressions'; 10 | import { ScanInput } from 'aws-sdk/clients/dynamodb'; 11 | 12 | /** 13 | * @internal 14 | */ 15 | export function buildScanInput( 16 | valueConstructor: ZeroArgumentsConstructor, 17 | options: SequentialScanOptions = {} 18 | ): ScanInput { 19 | const { 20 | filter, 21 | indexName, 22 | pageSize, 23 | projection, 24 | readConsistency, 25 | segment, 26 | startKey, 27 | tableNamePrefix: prefix, 28 | totalSegments, 29 | } = options; 30 | 31 | const req: ScanInput = { 32 | TableName: getTableName(valueConstructor.prototype, prefix), 33 | Limit: pageSize, 34 | IndexName: indexName, 35 | Segment: segment, 36 | TotalSegments: totalSegments, 37 | }; 38 | 39 | if (readConsistency === 'strong') { 40 | req.ConsistentRead = true; 41 | } 42 | 43 | const schema = getSchema(valueConstructor.prototype); 44 | 45 | const attributes = new ExpressionAttributes(); 46 | 47 | if (filter) { 48 | req.FilterExpression = marshallConditionExpression( 49 | filter, 50 | schema, 51 | attributes 52 | ).expression; 53 | } 54 | 55 | if (projection) { 56 | req.ProjectionExpression = marshallProjectionExpression( 57 | projection, 58 | schema, 59 | attributes 60 | ).expression; 61 | } 62 | 63 | if (Object.keys(attributes.names).length > 0) { 64 | req.ExpressionAttributeNames = attributes.names; 65 | } 66 | 67 | if (Object.keys(attributes.values).length > 0) { 68 | req.ExpressionAttributeValues = attributes.values; 69 | } 70 | 71 | if (startKey) { 72 | req.ExclusiveStartKey = marshallStartKey(schema, startKey); 73 | } 74 | 75 | return req; 76 | } 77 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/src/constants.ts: -------------------------------------------------------------------------------- 1 | export const VERSION = '0.4.0'; 2 | 3 | export const MAX_WRITE_BATCH_SIZE = 25; 4 | 5 | export const MAX_READ_BATCH_SIZE = 100; 6 | 7 | export type OnMissingStrategy = 'remove'|'skip'; 8 | 9 | export type ReadConsistency = 'eventual'|'strong'; 10 | 11 | export interface StringToAnyObjectMap {[key: string]: any;} 12 | 13 | export type SyncOrAsyncIterable = Iterable|AsyncIterable; 14 | 15 | export type WriteType = 'put'|'delete'; 16 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/src/embed.spec.ts: -------------------------------------------------------------------------------- 1 | import {embed} from "./embed"; 2 | import {DynamoDbSchema} from "./protocols"; 3 | 4 | describe('embed', () => { 5 | const schema = {foo: {type: 'String'}}; 6 | class Embeddable {} 7 | Object.defineProperty(Embeddable.prototype, DynamoDbSchema as any, { 8 | value: schema 9 | }); 10 | 11 | it( 12 | 'should return a SchemaType using the embedded schema of a document constructor', 13 | () => { 14 | const schemaType = embed(Embeddable); 15 | 16 | expect(schemaType.type).toBe('Document'); 17 | expect(schemaType.members).toEqual(schema); 18 | expect(schemaType.valueConstructor).toBe(Embeddable); 19 | } 20 | ); 21 | 22 | it('should pass through a defined attributeName', () => { 23 | const attributeName = 'attributeName'; 24 | const schemaType = embed(Embeddable, {attributeName}); 25 | 26 | expect(schemaType.attributeName).toBe(attributeName); 27 | }); 28 | 29 | it('should pass through a defined defaultProvider', () => { 30 | const defaultProvider = () => new Embeddable(); 31 | const schemaType = embed(Embeddable, {defaultProvider}); 32 | 33 | expect(schemaType.defaultProvider).toBe(defaultProvider); 34 | }); 35 | }); 36 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/src/embed.ts: -------------------------------------------------------------------------------- 1 | import {DynamoDbSchema} from "./protocols"; 2 | import { 3 | DocumentType, 4 | ZeroArgumentsConstructor, 5 | } from '@aws/dynamodb-data-marshaller'; 6 | 7 | export interface DocumentTypeOptions { 8 | defaultProvider?: () => T; 9 | attributeName?: string; 10 | } 11 | 12 | export function embed( 13 | documentConstructor: ZeroArgumentsConstructor, 14 | {attributeName, defaultProvider}: DocumentTypeOptions = {} 15 | ): DocumentType { 16 | return { 17 | type: 'Document', 18 | members: (documentConstructor.prototype as any)[DynamoDbSchema] || {}, 19 | attributeName, 20 | defaultProvider, 21 | valueConstructor: documentConstructor 22 | }; 23 | } 24 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './constants'; 2 | export * from './DataMapper'; 3 | export * from './embed'; 4 | export * from './ItemNotFoundException'; 5 | export * from './namedParameters'; 6 | export * from './ParallelScanIterator'; 7 | export * from './ParallelScanPaginator'; 8 | export * from './protocols'; 9 | export * from './QueryIterator'; 10 | export * from './QueryPaginator'; 11 | export * from './ScanIterator'; 12 | export * from './ScanPaginator'; 13 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/src/marshallStartKey.ts: -------------------------------------------------------------------------------- 1 | import { marshallValue, Schema } from '@aws/dynamodb-data-marshaller'; 2 | import { Key } from 'aws-sdk/clients/dynamodb'; 3 | 4 | /** 5 | * @internal 6 | */ 7 | export function marshallStartKey( 8 | schema: Schema, 9 | startKey: {[key: string]: any} 10 | ): Key { 11 | const key: Key = {}; 12 | for (const propertyName of Object.keys(startKey)) { 13 | const propSchema = schema[propertyName]; 14 | const { attributeName = propertyName } = propSchema; 15 | if (propSchema) { 16 | key[attributeName] = marshallValue( 17 | propSchema, 18 | startKey[propertyName] 19 | )!; 20 | } 21 | } 22 | 23 | return key; 24 | } 25 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/src/namedParameters/BatchGetOptions.ts: -------------------------------------------------------------------------------- 1 | import { GetOptions } from './GetOptions'; 2 | import { ReadConsistencyConfiguration } from './ReadConsistencyConfiguration'; 3 | import { Schema } from "@aws/dynamodb-data-marshaller"; 4 | 5 | export interface BatchGetOptions extends ReadConsistencyConfiguration { 6 | /** 7 | * Options to apply to specific tables when performing a batch get operation 8 | * that reads from multiple tables. 9 | */ 10 | perTableOptions?: { 11 | [tableName: string]: BatchGetTableOptions; 12 | }; 13 | } 14 | 15 | export interface BatchGetTableOptions extends GetOptions { 16 | /** 17 | * The schema to use when mapping the supplied `projection` option to the 18 | * attribute names used in DynamoDB. 19 | * 20 | * This parameter is only necessary if a batch contains items from multiple 21 | * classes that map to the *same* table using *different* property names to 22 | * represent the same DynamoDB attributes. 23 | * 24 | * If not supplied, the schema associated with the first item associated 25 | * with a given table will be used in its place. 26 | */ 27 | projectionSchema?: Schema; 28 | } 29 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/src/namedParameters/CreateTableOptions.ts: -------------------------------------------------------------------------------- 1 | import { ProvisionedThroughput } from './ProvisionedThroughput'; 2 | import { PerIndexOptions } from './SecondaryIndexOptions'; 3 | 4 | interface BaseCreateTableOptions { 5 | streamViewType?: StreamViewType; 6 | indexOptions?: PerIndexOptions; 7 | billingMode?: BillingMode; 8 | sseSpecification?: SseSpecification; 9 | } 10 | 11 | export interface SseSpecification { 12 | sseType: SseType; 13 | kmsMasterKeyId?: string; 14 | } 15 | 16 | export interface ProvisionedCreateTableOptions extends ProvisionedThroughput, BaseCreateTableOptions { 17 | billingMode?: 'PROVISIONED'; 18 | } 19 | 20 | export interface OnDemandCreateTableOptions extends BaseCreateTableOptions { 21 | billingMode: 'PAY_PER_REQUEST'; 22 | } 23 | 24 | export type CreateTableOptions = ProvisionedCreateTableOptions | OnDemandCreateTableOptions; 25 | 26 | export type BillingMode = 'PROVISIONED' | 'PAY_PER_REQUEST'; 27 | 28 | /** 29 | * Server-side encryption type: 30 | * AES256 - Server-side encryption which uses the AES256 algorithm (not applicable). 31 | * KMS - Server-side encryption which uses AWS Key Management Service. 32 | */ 33 | export type SseType = 'AES256' | 'KMS'; 34 | 35 | export type StreamViewType = 36 | 'NEW_IMAGE' | 37 | 'OLD_IMAGE' | 38 | 'NEW_AND_OLD_IMAGES' | 39 | 'KEYS_ONLY' | 40 | 'NONE'; 41 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/src/namedParameters/DataMapperConfiguration.ts: -------------------------------------------------------------------------------- 1 | import DynamoDB = require("aws-sdk/clients/dynamodb"); 2 | import { ReadConsistency } from '../constants'; 3 | 4 | export interface DataMapperConfiguration { 5 | /** 6 | * The low-level DynamoDB client to use to execute API operations. 7 | */ 8 | client: DynamoDB; 9 | 10 | /** 11 | * The default read consistency to use when loading items. If not specified, 12 | * 'eventual' will be used. 13 | */ 14 | readConsistency?: ReadConsistency; 15 | 16 | /** 17 | * Whether operations should NOT by default honor the version attribute 18 | * specified in the schema by incrementing the attribute and preventing the 19 | * operation from taking effect if the local version is out of date. 20 | */ 21 | skipVersionCheck?: boolean; 22 | 23 | /** 24 | * A prefix to apply to all table names. 25 | */ 26 | tableNamePrefix?: string; 27 | } 28 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/src/namedParameters/DeleteOptions.ts: -------------------------------------------------------------------------------- 1 | import { StringToAnyObjectMap } from '../constants'; 2 | import { ConditionExpression } from '@aws/dynamodb-expressions'; 3 | 4 | export interface DeleteOptions { 5 | /** 6 | * A condition on which this delete operation's completion will be 7 | * predicated. 8 | */ 9 | condition?: ConditionExpression; 10 | 11 | /** 12 | * The values to return from this operation. 13 | */ 14 | returnValues?: 'ALL_OLD'|'NONE'; 15 | 16 | /** 17 | * Whether this operation should NOT honor the version attribute specified 18 | * in the schema by incrementing the attribute and preventing the operation 19 | * from taking effect if the local version is out of date. 20 | */ 21 | skipVersionCheck?: boolean; 22 | } 23 | 24 | /** 25 | * @deprecated 26 | */ 27 | export interface DeleteParameters< 28 | T extends StringToAnyObjectMap = StringToAnyObjectMap 29 | > extends DeleteOptions { 30 | /** 31 | * The item being deleted. 32 | */ 33 | item: T; 34 | } 35 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/src/namedParameters/ExecuteUpdateExpressionOptions.ts: -------------------------------------------------------------------------------- 1 | import { ConditionExpression } from '@aws/dynamodb-expressions'; 2 | 3 | export interface ExecuteUpdateExpressionOptions { 4 | /** 5 | * A condition on which this update operation's completion will be 6 | * predicated. 7 | */ 8 | condition?: ConditionExpression; 9 | } 10 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/src/namedParameters/GetOptions.ts: -------------------------------------------------------------------------------- 1 | import { ReadConsistencyConfiguration } from './ReadConsistencyConfiguration'; 2 | import { StringToAnyObjectMap } from '../constants'; 3 | import { ProjectionExpression } from "@aws/dynamodb-expressions"; 4 | 5 | export interface GetOptions extends ReadConsistencyConfiguration { 6 | /** 7 | * The item attributes to get. 8 | */ 9 | projection?: ProjectionExpression; 10 | } 11 | 12 | /** 13 | * @deprecated 14 | */ 15 | export interface GetParameters< 16 | T extends StringToAnyObjectMap = StringToAnyObjectMap 17 | > extends GetOptions { 18 | /** 19 | * The item being loaded. 20 | */ 21 | item: T; 22 | } 23 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/src/namedParameters/ProvisionedThroughput.ts: -------------------------------------------------------------------------------- 1 | export interface ProvisionedThroughput { 2 | readCapacityUnits?: number; 3 | writeCapacityUnits?: number; 4 | } 5 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/src/namedParameters/PutOptions.ts: -------------------------------------------------------------------------------- 1 | import { StringToAnyObjectMap } from '../constants'; 2 | import { ConditionExpression } from '@aws/dynamodb-expressions'; 3 | 4 | export interface PutOptions { 5 | /** 6 | * A condition on whose evaluation this put operation's completion will be 7 | * predicated. 8 | */ 9 | condition?: ConditionExpression; 10 | 11 | /** 12 | * Whether this operation should NOT honor the version attribute specified 13 | * in the schema by incrementing the attribute and preventing the operation 14 | * from taking effect if the local version is out of date. 15 | */ 16 | skipVersionCheck?: boolean; 17 | } 18 | 19 | /** 20 | * @deprecated 21 | */ 22 | export interface PutParameters< 23 | T extends StringToAnyObjectMap = StringToAnyObjectMap 24 | > extends PutOptions { 25 | /** 26 | * The object to be saved. 27 | */ 28 | item: T; 29 | } 30 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/src/namedParameters/QueryOptions.ts: -------------------------------------------------------------------------------- 1 | import { ReadConsistencyConfiguration } from './ReadConsistencyConfiguration'; 2 | import { StringToAnyObjectMap } from '../constants'; 3 | import { ZeroArgumentsConstructor } from '@aws/dynamodb-data-marshaller'; 4 | import { 5 | ConditionExpression, 6 | ConditionExpressionPredicate, 7 | ProjectionExpression, 8 | } from '@aws/dynamodb-expressions'; 9 | 10 | export interface QueryOptions extends ReadConsistencyConfiguration { 11 | /** 12 | * A condition expression that DynamoDB applies after the Query operation, 13 | * but before the data is returned to you. Items that do not satisfy the 14 | * FilterExpression criteria are not returned. 15 | * 16 | * A FilterExpression does not allow key attributes. You cannot define a 17 | * filter expression based on a partition key or a sort key. 18 | */ 19 | filter?: ConditionExpression; 20 | 21 | /** 22 | * The name of an index to query. This index can be any local secondary 23 | * index or global secondary index on the table. 24 | */ 25 | indexName?: string; 26 | 27 | /** 28 | * The maximum number of items to fetch over all pages of the query. 29 | */ 30 | limit?: number; 31 | 32 | /** 33 | * The maximum number of items to fetch per page of results. 34 | */ 35 | pageSize?: number; 36 | 37 | /** 38 | * The item attributes to get. 39 | */ 40 | projection?: ProjectionExpression; 41 | 42 | /** 43 | * Specifies the order for index traversal: If true, the traversal is 44 | * performed in ascending order; if false, the traversal is performed in 45 | * descending order. 46 | * 47 | * Items with the same partition key value are stored in sorted order by 48 | * sort key. If the sort key data type is Number, the results are stored in 49 | * numeric order. For type String, the results are stored in order of ASCII 50 | * character code values. For type Binary, DynamoDB treats each byte of the 51 | * binary data as unsigned. 52 | */ 53 | scanIndexForward?: boolean; 54 | 55 | /** 56 | * The primary key of the first item that this operation will evaluate. When 57 | * querying an index, only the `lastEvaluatedKey` derived from a previous 58 | * query operation on the same index should be supplied for this parameter. 59 | */ 60 | startKey?: {[key: string]: any}; 61 | } 62 | 63 | /** 64 | * @deprecated 65 | */ 66 | export interface QueryParameters< 67 | T extends StringToAnyObjectMap = StringToAnyObjectMap 68 | > extends QueryOptions { 69 | /** 70 | * The condition that specifies the key value(s) for items to be retrieved 71 | * by the Query action. 72 | */ 73 | keyCondition: ConditionExpression | 74 | {[propertyName: string]: ConditionExpressionPredicate|any}; 75 | 76 | /** 77 | * A constructor that creates objects representing one record returned by 78 | * the query operation. 79 | */ 80 | valueConstructor: ZeroArgumentsConstructor; 81 | } 82 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/src/namedParameters/ReadConsistencyConfiguration.ts: -------------------------------------------------------------------------------- 1 | import { ReadConsistency } from '../constants'; 2 | 3 | export interface ReadConsistencyConfiguration { 4 | /** 5 | * The read consistency to require when reading from DynamoDB. 6 | */ 7 | readConsistency?: ReadConsistency; 8 | } 9 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/src/namedParameters/SecondaryIndexOptions.ts: -------------------------------------------------------------------------------- 1 | import { ProvisionedThroughput } from './ProvisionedThroughput'; 2 | 3 | export type SecondaryIndexProjection = 'all'|'keys'|Array; 4 | 5 | export interface SharedSecondaryIndexOptions { 6 | projection: SecondaryIndexProjection; 7 | } 8 | 9 | export interface GlobalSecondaryIndexOptions extends 10 | SharedSecondaryIndexOptions, 11 | ProvisionedThroughput 12 | { 13 | type: 'global'; 14 | } 15 | 16 | export interface LocalSecondaryIndexOptions extends 17 | SharedSecondaryIndexOptions 18 | { 19 | type: 'local'; 20 | } 21 | 22 | export type SecondaryIndexOptions 23 | = GlobalSecondaryIndexOptions | LocalSecondaryIndexOptions; 24 | 25 | export interface PerIndexOptions { 26 | [indexName: string]: SecondaryIndexOptions; 27 | } 28 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/src/namedParameters/UpdateOptions.ts: -------------------------------------------------------------------------------- 1 | import { OnMissingStrategy, StringToAnyObjectMap } from '../constants'; 2 | import { ConditionExpression } from '@aws/dynamodb-expressions'; 3 | 4 | export interface UpdateOptions { 5 | /** 6 | * A condition on whose evaluation this update operation's completion will 7 | * be predicated. 8 | */ 9 | condition?: ConditionExpression; 10 | 11 | /** 12 | * Whether the absence of a value defined in the schema should be treated as 13 | * a directive to remove the property from the item. 14 | */ 15 | onMissing?: OnMissingStrategy; 16 | 17 | /** 18 | * Whether this operation should NOT honor the version attribute specified 19 | * in the schema by incrementing the attribute and preventing the operation 20 | * from taking effect if the local version is out of date. 21 | */ 22 | skipVersionCheck?: boolean; 23 | } 24 | 25 | export interface UpdateParameters< 26 | T extends StringToAnyObjectMap = StringToAnyObjectMap 27 | > extends UpdateOptions { 28 | /** 29 | * The object to be saved. 30 | */ 31 | item: T; 32 | } 33 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/src/namedParameters/index.ts: -------------------------------------------------------------------------------- 1 | export * from './BatchGetOptions'; 2 | export * from './CreateTableOptions'; 3 | export * from './DataMapperConfiguration'; 4 | export * from './DeleteOptions'; 5 | export * from './ExecuteUpdateExpressionOptions'; 6 | export * from './GetOptions'; 7 | export * from './ProvisionedThroughput'; 8 | export * from './PutOptions'; 9 | export * from './QueryOptions'; 10 | export * from './ScanOptions'; 11 | export * from './SecondaryIndexOptions'; 12 | export * from './UpdateOptions'; 13 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/src/protocols.spec.ts: -------------------------------------------------------------------------------- 1 | import { 2 | DynamoDbSchema, 3 | DynamoDbTable, 4 | getSchema, 5 | getTableName, 6 | } from './protocols'; 7 | 8 | describe('getSchema', () => { 9 | it('should return the schema bound at the DynamoDbSchema symbol', () => { 10 | const schema = {}; 11 | expect(getSchema({[DynamoDbSchema]: schema})).toBe(schema); 12 | }); 13 | 14 | it('should throw if the provided object does not have a schema', () => { 15 | expect(() => getSchema({})).toThrow(); 16 | }); 17 | }); 18 | 19 | describe('getTableName', () => { 20 | it('should return the name bound at the DynamoDbTable symbol', () => { 21 | expect(getTableName({[DynamoDbTable]: 'foo'})).toBe('foo'); 22 | }); 23 | 24 | it('should throw if the provided object does not have a table name', () => { 25 | expect(() => getTableName({})).toThrow(); 26 | }); 27 | }); 28 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/src/protocols.ts: -------------------------------------------------------------------------------- 1 | import {Schema} from '@aws/dynamodb-data-marshaller'; 2 | 3 | /** 4 | * Table metadata is reported by items submitted to the data mapper via methods 5 | * identified by symbols. This is done both to disambiguate data (which should 6 | * always be identified by string keys) from metadata and also to allow an 7 | * eventually integration with the First-Class Protocols proposal as described 8 | * at {@link https://github.com/michaelficarra/proposal-first-class-protocols} 9 | * (currently at stage 1 in the ECMAScript change acceptance process). 10 | * 11 | * Because the protocol proposal allows implementation to be declared 12 | * dynamically at runtime (and also because TypeScript does not allow 13 | * user-defined symbols to appear in type declarations), protocol adherence 14 | * should be detected on objects at runtime rather than on types via static 15 | * analysis. 16 | */ 17 | 18 | /** 19 | * Used to designate the mapping of an object from its JavaScript form to its 20 | * representation in a DynamoDB Table or nested map. 21 | * 22 | * @example 23 | * 24 | * class FooDocument { 25 | * [DynamoDbSchema]() { 26 | * return { 27 | * bar: {type: 'String'}, 28 | * baz: {type: 'Number'}, 29 | * }; 30 | * } 31 | * } 32 | */ 33 | export const DynamoDbSchema = Symbol('DynamoDbSchema'); 34 | 35 | export function getSchema(item: any): Schema { 36 | if (item) { 37 | const schema = item[DynamoDbSchema]; 38 | if (schema && typeof schema === 'object') { 39 | return schema; 40 | } 41 | } 42 | 43 | throw new Error( 44 | 'The provided item did not adhere to the DynamoDbDocument protocol.' + 45 | ' No object property was found at the `DynamoDbSchema` symbol' 46 | ); 47 | } 48 | 49 | /** 50 | * Used to designate that an object represents a row of the named DynamoDB 51 | * table. Meant to be used in conjunction with {DynamoDbSchema}. 52 | * 53 | * @example 54 | * 55 | * class FooDocument { 56 | * [DynamoDbTable]() { 57 | * return 'FooTable'; 58 | * } 59 | * 60 | * [DynamoDbSchema]() { 61 | * return { 62 | * bar: {type: 'String'}, 63 | * baz: {type: 'Number'}, 64 | * }; 65 | * } 66 | * } 67 | */ 68 | export const DynamoDbTable = Symbol('DynamoDbTableName'); 69 | 70 | export function getTableName(item: any, tableNamePrefix: string = ''): string { 71 | if (item) { 72 | const tableName = item[DynamoDbTable]; 73 | if (typeof tableName === 'string') { 74 | return tableNamePrefix + tableName; 75 | } 76 | } 77 | 78 | throw new Error( 79 | 'The provided item did not adhere to the DynamoDbTable protocol. No' + 80 | ' string property was found at the `DynamoDbTable` symbol' 81 | ); 82 | } 83 | 84 | /** 85 | * Used to designate which fields on an object have been changed. The method 86 | * identified by this symbol should return a iterable that enumerates the fields 87 | * that have been altered. 88 | * 89 | * @example 90 | * 91 | * class FooDocument { 92 | * constructor() { 93 | * this._dirtyFields = new Set(); 94 | * this._foo = ''; 95 | * } 96 | * 97 | * get foo() { 98 | * return this._foo; 99 | * } 100 | * 101 | * set foo(value) { 102 | * this._foo = value; 103 | * this._dirtyFields.add('foo'); 104 | * } 105 | * 106 | * [DynamoDbDirtyFields]() { 107 | * return this._dirtyFields.values(); 108 | * } 109 | * } 110 | */ 111 | export const DynamoDbDirtyFields = Symbol('DynamoDbDirtyFields'); 112 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": [ 5 | "es5", 6 | "es2015.iterable", 7 | "es2015.promise", 8 | "es2015.collection", 9 | "es2015.symbol.wellknown", 10 | "esnext.asynciterable" 11 | ], 12 | "downlevelIteration": true, 13 | "importHelpers": true, 14 | "module": "commonjs", 15 | "noUnusedLocals": true, 16 | "strict": true, 17 | "declaration": true, 18 | "sourceMap": true, 19 | "rootDir": "./src", 20 | "outDir": "./build" 21 | }, 22 | "typedocOptions": { 23 | "mode": "file", 24 | "out": "../../docs/packages/dynamodb-data-mapper", 25 | "excludeNotExported": true, 26 | "excludePrivate": true, 27 | "hideGenerator": true 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /packages/dynamodb-data-mapper/tsconfig.test.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "sourceMap": false, 5 | "inlineSourceMap": true, 6 | "inlineSources": true, 7 | "rootDir": "./src", 8 | "outDir": "./build" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /packages/dynamodb-data-marshaller/.npmignore: -------------------------------------------------------------------------------- 1 | /src 2 | /node_modules 3 | /coverage 4 | 5 | *.spec.d.ts 6 | *.spec.js 7 | *.spec.js.map 8 | 9 | tsconfig.json 10 | tsconfig.test.json 11 | -------------------------------------------------------------------------------- /packages/dynamodb-data-marshaller/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@aws/dynamodb-data-marshaller", 3 | "version": "0.7.3", 4 | "description": "A schema-based data marshaller for Amazon DynamoDB", 5 | "keywords": [ 6 | "aws", 7 | "dynamodb" 8 | ], 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/awslabs/dynamodb-data-mapper-js.git" 12 | }, 13 | "bugs": { 14 | "url": "https://github.com/awslabs/dynamodb-data-mapper-js/issues" 15 | }, 16 | "homepage": "https://awslabs.github.io/dynamodb-data-mapper-js/packages/dynamodb-data-marshaller/", 17 | "main": "./build/index.js", 18 | "types": "./build/index.d.ts", 19 | "scripts": { 20 | "docs": "typedoc src", 21 | "prepublishOnly": "tsc", 22 | "pretest": "tsc -p tsconfig.test.json", 23 | "test": "jest \"build/(.+).spec.js\"" 24 | }, 25 | "author": { 26 | "name": "AWS SDK for JavaScript Team", 27 | "email": "aws-sdk-js@amazon.com" 28 | }, 29 | "license": "Apache-2.0", 30 | "devDependencies": { 31 | "@types/jest": "^24", 32 | "@types/node": "^8.0.4", 33 | "aws-sdk": "^2.7.0", 34 | "jest": "^24", 35 | "typedoc": "^0.14.0", 36 | "typescript": "^3.4" 37 | }, 38 | "dependencies": { 39 | "@aws/dynamodb-auto-marshaller": "^0.7.1", 40 | "@aws/dynamodb-expressions": "^0.7.3", 41 | "tslib": "^1.9", 42 | "utf8-bytes": "^0.0.1" 43 | }, 44 | "peerDependencies": { 45 | "aws-sdk": "^2.7.0" 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /packages/dynamodb-data-marshaller/src/InvalidSchemaError.ts: -------------------------------------------------------------------------------- 1 | import {SchemaType} from "./SchemaType"; 2 | 3 | /** 4 | * An error thrown when a marshaller or unmarshaller cannot understand a node of 5 | * the provided schema. 6 | */ 7 | export class InvalidSchemaError extends Error { 8 | constructor(public readonly node: SchemaType, message?: string) { 9 | super(message); 10 | } 11 | } -------------------------------------------------------------------------------- /packages/dynamodb-data-marshaller/src/InvalidValueError.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * An error thrown by the marshaller when a node of the provided input cannot be 3 | * marshalled into the type specified in the schema. 4 | */ 5 | export class InvalidValueError extends Error { 6 | constructor( 7 | public readonly invalidValue: any, 8 | message?: string 9 | ) { 10 | super(message); 11 | } 12 | } -------------------------------------------------------------------------------- /packages/dynamodb-data-marshaller/src/KeySchema.ts: -------------------------------------------------------------------------------- 1 | import { KeyType } from './SchemaType'; 2 | 3 | export interface AttributeTypeMap { 4 | [attributeName: string]: ScalarAttributeType; 5 | } 6 | 7 | export interface KeySchema { 8 | attributes: AttributeTypeMap; 9 | 10 | tableKeys: KeyTypeMap; 11 | 12 | indexKeys: PerIndexKeys; 13 | } 14 | 15 | export interface KeyTypeMap { 16 | [attributeName: string]: KeyType; 17 | } 18 | 19 | export interface PerIndexKeys { 20 | [indexName: string]: KeyTypeMap; 21 | } 22 | 23 | export type ScalarAttributeType = 'S'|'N'|'B'; 24 | -------------------------------------------------------------------------------- /packages/dynamodb-data-marshaller/src/Schema.spec.ts: -------------------------------------------------------------------------------- 1 | import {isSchema} from "./Schema"; 2 | 3 | describe('isSchema', () => { 4 | it('should reject scalar values', () => { 5 | for (let scalar of ['string', 123, true, null, void 0]) { 6 | expect(isSchema(scalar)).toBe(false); 7 | } 8 | }); 9 | 10 | it('should accept empty objects', () => { 11 | expect(isSchema({})).toBe(true); 12 | }); 13 | 14 | it('should accept objects whose members are all schema types', () => { 15 | expect(isSchema({ 16 | foo: {type: 'Binary'}, 17 | bar: {type: 'Boolean'}, 18 | baz: {type: 'String'}, 19 | quux: { 20 | type: 'Document', 21 | members: { 22 | fizz: {type: 'Set', memberType: 'String'}, 23 | buzz: { 24 | type: 'Tuple', 25 | members: [ 26 | { 27 | type: 'List', 28 | memberType: {type: 'Set', memberType: 'Number'}, 29 | }, 30 | { 31 | type: 'Map', 32 | memberType: {type: 'Date'}, 33 | } 34 | ] 35 | }, 36 | }, 37 | }, 38 | })).toBe(true); 39 | }); 40 | 41 | it('should reject objects whose members are not all schema types', () => { 42 | expect(isSchema({ 43 | foo: {type: 'Binary'}, 44 | bar: {type: 'Boolean'}, 45 | baz: {type: 'String'}, 46 | quux: 'string', 47 | })).toBe(false); 48 | }); 49 | }); 50 | -------------------------------------------------------------------------------- /packages/dynamodb-data-marshaller/src/Schema.ts: -------------------------------------------------------------------------------- 1 | import {isSchemaType, SchemaType} from "./SchemaType"; 2 | 3 | /** 4 | * A key => value mapping outlining how to convert an arbitrary JavaScript 5 | * object into a strongly typed DynamoDB AttributeMap and back again. 6 | */ 7 | export interface Schema { 8 | [key: string]: SchemaType; 9 | } 10 | 11 | /** 12 | * Evaluates whether the provided argument is a Schema object 13 | */ 14 | export function isSchema(arg: any): arg is Schema { 15 | if (!Boolean(arg) || typeof arg !== 'object') { 16 | return false; 17 | } 18 | 19 | for (let key of Object.keys(arg)) { 20 | if (!isSchemaType(arg[key])) { 21 | return false; 22 | } 23 | } 24 | 25 | return true; 26 | } -------------------------------------------------------------------------------- /packages/dynamodb-data-marshaller/src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './InvalidSchemaError'; 2 | export * from './InvalidValueError'; 3 | export * from './isKey'; 4 | export * from './KeySchema'; 5 | export * from './keysFromSchema'; 6 | export * from './marshallExpression'; 7 | export * from './marshallItem'; 8 | export * from './marshallKey'; 9 | export * from './Schema'; 10 | export * from './SchemaType'; 11 | export * from './toSchemaName'; 12 | export * from './unmarshallItem'; 13 | -------------------------------------------------------------------------------- /packages/dynamodb-data-marshaller/src/isKey.spec.ts: -------------------------------------------------------------------------------- 1 | import { isKey } from './isKey'; 2 | import { SchemaType, TypeTag } from './SchemaType'; 3 | 4 | const keyableTypes: Array = [ 5 | 'Binary', 6 | 'Custom', 7 | 'Date', 8 | 'Number', 9 | 'String', 10 | ]; 11 | 12 | const unkeyableTypes: Array = [ 13 | 'Any', 14 | 'Boolean', 15 | 'Collection', 16 | 'Document', 17 | 'Hash', 18 | 'List', 19 | 'Map', 20 | 'Null', 21 | 'Set', 22 | 'Tuple', 23 | ]; 24 | 25 | describe('isKey', () => { 26 | for (const notKeyType of unkeyableTypes) { 27 | it(`should return false if the field is of type ${notKeyType}`, () => { 28 | expect( 29 | isKey({type: notKeyType, keyType: 'HASH'} as SchemaType) 30 | ).toBe(false); 31 | }); 32 | } 33 | 34 | for (const keyType of keyableTypes) { 35 | it(`should return false if the field is of type ${keyType}`, () => { 36 | expect( 37 | isKey({type: keyType, keyType: 'HASH'} as SchemaType) 38 | ).toBe(true); 39 | }); 40 | } 41 | 42 | it( 43 | 'should return true if the field is an index key and the index name was supplied', 44 | () => { 45 | expect(isKey( 46 | { type: 'String', indexKeyConfigurations: {foo: 'HASH'}}, 47 | 'foo' 48 | )).toBe(true); 49 | } 50 | ); 51 | 52 | it( 53 | 'should return false if the field is an index key and no index name was supplied', 54 | () => { 55 | expect( 56 | isKey({ type: 'String', indexKeyConfigurations: {foo: 'HASH'}}) 57 | ).toBe(false); 58 | } 59 | ); 60 | 61 | it( 62 | 'should return false if the field is an index key and a different index name was supplied', 63 | () => { 64 | expect(isKey( 65 | { type: 'String', indexKeyConfigurations: {foo: 'HASH'}}, 66 | 'bar' 67 | )).toBe(false); 68 | } 69 | ); 70 | 71 | it( 72 | 'should return false if the field is a table key and an index name was supplied', 73 | () => { 74 | expect(isKey( 75 | { type: 'String', keyType: 'HASH'}, 76 | 'foo' 77 | )).toBe(false); 78 | } 79 | ); 80 | 81 | it( 82 | 'should return true if the field is both a table and an index key', 83 | () => { 84 | expect(isKey( 85 | { 86 | type: 'String', 87 | keyType: 'HASH', 88 | indexKeyConfigurations: {foo: 'HASH'} 89 | }, 90 | 'foo' 91 | )).toBe(true); 92 | } 93 | ); 94 | }); 95 | -------------------------------------------------------------------------------- /packages/dynamodb-data-marshaller/src/isKey.ts: -------------------------------------------------------------------------------- 1 | import {SchemaType} from './SchemaType'; 2 | 3 | export function isKey(fieldSchema: SchemaType, indexName?: string): boolean { 4 | if ( 5 | fieldSchema.type === 'Binary' || 6 | fieldSchema.type === 'Custom' || 7 | fieldSchema.type === 'Date' || 8 | fieldSchema.type === 'Number' || 9 | fieldSchema.type === 'String' 10 | ) { 11 | return indexName !== undefined 12 | ? Boolean( 13 | fieldSchema.indexKeyConfigurations && 14 | fieldSchema.indexKeyConfigurations[indexName] 15 | ) : Boolean(fieldSchema.keyType); 16 | } 17 | 18 | return false; 19 | } 20 | -------------------------------------------------------------------------------- /packages/dynamodb-data-marshaller/src/keysFromSchema.spec.ts: -------------------------------------------------------------------------------- 1 | import { keysFromSchema } from './keysFromSchema'; 2 | import { Schema } from './Schema'; 3 | 4 | describe('keysFromSchema', () => { 5 | const schema: Schema = { 6 | partitionKey: { 7 | type: 'Number', 8 | keyType: 'HASH', 9 | }, 10 | createdAt: { 11 | type: 'Date', 12 | keyType: 'RANGE', 13 | indexKeyConfigurations: { 14 | chronological: 'HASH', 15 | globalIndex: 'RANGE' 16 | }, 17 | attributeName: 'timestamp' 18 | }, 19 | createdBy: { 20 | type: 'String', 21 | indexKeyConfigurations: { 22 | globalIndex: 'HASH', 23 | localIndex: 'RANGE' 24 | }, 25 | attributeName: 'creator', 26 | }, 27 | binaryKey: { 28 | type: 'Binary', 29 | indexKeyConfigurations: { 30 | binaryIndex: 'HASH' 31 | } 32 | }, 33 | customKey: { 34 | type: 'Custom', 35 | attributeType: 'S', 36 | marshall: str => str, 37 | unmarshall: av => av.S, 38 | indexKeyConfigurations: { 39 | binaryIndex: 'RANGE', 40 | }, 41 | }, 42 | listProp: { type: 'Collection' }, 43 | }; 44 | 45 | it('should identify the table keys', () => { 46 | expect(keysFromSchema(schema).tableKeys).toEqual({ 47 | partitionKey: 'HASH', 48 | timestamp: 'RANGE', 49 | }); 50 | }); 51 | 52 | it('should identify any index keys', () => { 53 | expect(keysFromSchema(schema).indexKeys).toEqual({ 54 | binaryIndex: { 55 | binaryKey: 'HASH', 56 | customKey: 'RANGE' 57 | }, 58 | chronological: { 59 | timestamp: 'HASH', 60 | }, 61 | globalIndex: { 62 | creator: 'HASH', 63 | timestamp: 'RANGE', 64 | }, 65 | localIndex: { 66 | creator: 'RANGE', 67 | }, 68 | }); 69 | }); 70 | 71 | it('should record the attribute type of any value used as a key', () => { 72 | expect(keysFromSchema(schema).attributes).toEqual({ 73 | partitionKey: 'N', 74 | timestamp: 'N', 75 | creator: 'S', 76 | binaryKey: 'B', 77 | customKey: 'S', 78 | }); 79 | }); 80 | 81 | it( 82 | 'should throw if a custom property does not define an attribute type', 83 | () => { 84 | const schema: Schema = { 85 | customKey: { 86 | type: 'Custom', 87 | keyType: 'HASH', 88 | marshall: str => str, 89 | unmarshall: av => av.S, 90 | }, 91 | }; 92 | expect(() => keysFromSchema(schema)).toThrow(); 93 | } 94 | ); 95 | }); 96 | -------------------------------------------------------------------------------- /packages/dynamodb-data-marshaller/src/keysFromSchema.ts: -------------------------------------------------------------------------------- 1 | import { 2 | AttributeTypeMap, 3 | KeySchema, 4 | KeyTypeMap, 5 | ScalarAttributeType, 6 | } from './KeySchema'; 7 | import {Schema} from './Schema'; 8 | import { 9 | BinaryType, 10 | CustomType, 11 | DateType, 12 | NumberType, 13 | StringType, 14 | } from './SchemaType'; 15 | 16 | export function keysFromSchema(schema: Schema): KeySchema { 17 | const attributes: AttributeTypeMap = {}; 18 | const tableKeys: KeyTypeMap = {}; 19 | const indexKeys: {[key: string]: KeyTypeMap} = {}; 20 | 21 | for (const propertyName of Object.keys(schema)) { 22 | const fieldSchema = schema[propertyName]; 23 | if ( 24 | fieldSchema.type === 'Binary' || 25 | fieldSchema.type === 'Custom' || 26 | fieldSchema.type === 'Date' || 27 | fieldSchema.type === 'Number' || 28 | fieldSchema.type === 'String' 29 | ) { 30 | const { 31 | attributeName = propertyName 32 | } = fieldSchema; 33 | 34 | if (fieldSchema.keyType) { 35 | attributes[attributeName] = attributeType(fieldSchema); 36 | tableKeys[attributeName] = fieldSchema.keyType; 37 | } 38 | 39 | if ( 40 | fieldSchema.indexKeyConfigurations && 41 | Object.keys(fieldSchema.indexKeyConfigurations).length > 0 42 | ) { 43 | attributes[attributeName] = attributeType(fieldSchema); 44 | 45 | for (const indexName of Object.keys( 46 | fieldSchema.indexKeyConfigurations 47 | )) { 48 | if (!(indexName in indexKeys)) { 49 | indexKeys[indexName] = {}; 50 | } 51 | indexKeys[indexName][attributeName] 52 | = fieldSchema.indexKeyConfigurations[indexName]; 53 | } 54 | } 55 | } 56 | } 57 | 58 | return {attributes, tableKeys, indexKeys}; 59 | } 60 | 61 | function attributeType( 62 | fieldSchema: BinaryType|CustomType|DateType|NumberType|StringType 63 | ): ScalarAttributeType { 64 | switch (fieldSchema.type) { 65 | case 'Binary': 66 | return 'B'; 67 | case 'Custom': 68 | if (!fieldSchema.attributeType) { 69 | throw new Error( 70 | 'Invalid schema: no attribute type defined for custom field' 71 | ); 72 | } 73 | return fieldSchema.attributeType; 74 | case 'Date': 75 | case 'Number': 76 | return 'N'; 77 | case 'String': 78 | return 'S'; 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /packages/dynamodb-data-marshaller/src/marshallKey.spec.ts: -------------------------------------------------------------------------------- 1 | import {marshallKey} from './marshallKey'; 2 | import {Schema} from './Schema'; 3 | 4 | describe('marshallKey', () => { 5 | const schema: Schema = { 6 | fizz: { 7 | type: 'String', 8 | keyType: 'HASH', 9 | attributeName: 'foo', 10 | }, 11 | buzz: { 12 | type: 'Date', 13 | keyType: 'RANGE', 14 | indexKeyConfigurations: {bar: 'HASH'}, 15 | attributeName: 'bar', 16 | }, 17 | pop: { 18 | type: 'Number', 19 | indexKeyConfigurations: {foo: 'HASH'} 20 | }, 21 | notAKey: { 22 | type: 'Tuple', 23 | members: [ 24 | {type: 'Boolean'}, 25 | {type: 'String'}, 26 | ] 27 | }, 28 | }; 29 | 30 | const input = { 31 | fizz: 'baz', 32 | buzz: new Date(1000), 33 | pop: 10, 34 | notAKey: [true, 'quux'] 35 | }; 36 | 37 | it('should only marshall key fields', () => { 38 | expect(marshallKey(schema, input)) 39 | .toEqual({ 40 | foo: {S: 'baz'}, 41 | bar: {N: '1'} 42 | }); 43 | }); 44 | 45 | it( 46 | 'should marshall key fields for the correct index if an index name is supplied', 47 | () => { 48 | expect(marshallKey(schema, input, 'foo')).toEqual({pop: {N: '10'}}); 49 | expect(marshallKey(schema, input, 'bar')).toEqual({bar: {N: '1'}}); 50 | } 51 | ); 52 | }); 53 | -------------------------------------------------------------------------------- /packages/dynamodb-data-marshaller/src/marshallKey.ts: -------------------------------------------------------------------------------- 1 | import {isKey} from './isKey'; 2 | import {marshallValue} from './marshallItem'; 3 | import {Schema} from './Schema'; 4 | import {AttributeMap} from 'aws-sdk/clients/dynamodb'; 5 | 6 | export function marshallKey( 7 | schema: Schema, 8 | input: {[key: string]: any}, 9 | indexName?: string 10 | ): AttributeMap { 11 | const marshalled: AttributeMap = {}; 12 | 13 | for (const propertyKey of Object.keys(schema)) { 14 | const fieldSchema = schema[propertyKey]; 15 | if (isKey(fieldSchema, indexName)) { 16 | const {attributeName = propertyKey} = fieldSchema; 17 | const value = marshallValue(fieldSchema, input[propertyKey]); 18 | if (value) { 19 | marshalled[attributeName] = value; 20 | } 21 | } 22 | } 23 | 24 | return marshalled; 25 | } 26 | -------------------------------------------------------------------------------- /packages/dynamodb-data-marshaller/src/toSchemaName.spec.ts: -------------------------------------------------------------------------------- 1 | import {Schema} from './Schema'; 2 | import {toSchemaName} from './toSchemaName'; 3 | import {AttributePath} from '@aws/dynamodb-expressions'; 4 | 5 | const testCases = new Map(); 6 | const schema: Schema = { 7 | unchanged: {type: 'String'}, 8 | property: { 9 | type: 'String', 10 | attributeName: 'attributeName', 11 | }, 12 | foo: { 13 | type: 'Document', 14 | attributeName: 'topLevelFoo', 15 | members: { 16 | foo: { 17 | type: 'String', 18 | attributeName: 'fizz', 19 | }, 20 | bar: { 21 | type: 'String', 22 | attributeName: 'buzz', 23 | }, 24 | baz: { 25 | type: 'String', 26 | attributeName: 'pop', 27 | } 28 | } 29 | }, 30 | bar: { 31 | type: 'List', 32 | attributeName: 'topLevelBar', 33 | memberType: { 34 | type: 'Document', 35 | members: { 36 | tom: { 37 | type: 'String', 38 | attributeName: 'jerry' 39 | }, 40 | bugs: { 41 | type: 'String', 42 | attributeName: 'daffy' 43 | }, 44 | itchy: { 45 | type: 'List', 46 | attributeName: 'scratchy', 47 | memberType: { 48 | type: 'List', 49 | memberType: { 50 | type: 'Document', 51 | members: { 52 | nameToReplace: { 53 | type: 'String', 54 | attributeName: 'replacementName' 55 | } 56 | } 57 | } 58 | } 59 | } 60 | } 61 | } 62 | }, 63 | untypedHash: { 64 | type: 'Hash', 65 | attributeName: 'terminalType' 66 | } 67 | }; 68 | 69 | testCases.set('unchanged', 'unchanged'); 70 | testCases.set('property', 'attributeName'); 71 | testCases.set('foo.foo', 'topLevelFoo.fizz'); 72 | testCases.set('foo.bar', 'topLevelFoo.buzz'); 73 | testCases.set('foo.baz', 'topLevelFoo.pop'); 74 | testCases.set('bar[11].tom', 'topLevelBar[11].jerry'); 75 | testCases.set('bar[21].bugs', 'topLevelBar[21].daffy'); 76 | testCases.set( 77 | 'bar[1].itchy[23][2].nameToReplace', 78 | 'topLevelBar[1].scratchy[23][2].replacementName' 79 | ); 80 | testCases.set('untypedHash.foo.bar.baz', 'terminalType.foo.bar.baz'); 81 | testCases.set( 82 | 'unknownProperty.access[1][2].baz', 83 | 'unknownProperty.access[1][2].baz' 84 | ); 85 | 86 | describe('toSchemaName', () => { 87 | for (const [input, output] of testCases) { 88 | it(`should convert a path of ${input} to ${output}`, () => { 89 | expect(toSchemaName(input, schema).elements) 90 | .toEqual(new AttributePath(output).elements); 91 | }); 92 | } 93 | }); 94 | -------------------------------------------------------------------------------- /packages/dynamodb-data-marshaller/src/toSchemaName.ts: -------------------------------------------------------------------------------- 1 | import { AttributePath } from '@aws/dynamodb-expressions'; 2 | import { Schema } from './Schema'; 3 | import { SchemaType } from './SchemaType'; 4 | 5 | export function toSchemaName( 6 | path: AttributePath|string, 7 | schema: Schema 8 | ): AttributePath { 9 | if (typeof path === 'string') { 10 | path = new AttributePath(path); 11 | } 12 | const elements = path.elements.map(el => ({...el})); 13 | 14 | let cursor: SchemaType = { 15 | type: 'Document', 16 | members: schema 17 | }; 18 | for (const element of elements) { 19 | if ( 20 | element.type === 'AttributeName' && 21 | cursor && 22 | cursor.type === 'Document' 23 | ) { 24 | const {name} = element; 25 | element.name = getSchemaName(name, cursor.members); 26 | cursor = cursor.members[name]; 27 | } else if ( 28 | element.type === 'ListIndex' && 29 | cursor && 30 | cursor.type === 'List' 31 | ) { 32 | cursor = (cursor as any).memberType; 33 | } else { 34 | break; 35 | } 36 | } 37 | 38 | return new AttributePath(elements); 39 | } 40 | 41 | export function getSchemaName(propertyName: string, schema: Schema): string { 42 | const fieldSchema = schema[propertyName]; 43 | if (fieldSchema) { 44 | const {attributeName = propertyName} = fieldSchema; 45 | return attributeName; 46 | } 47 | 48 | return propertyName; 49 | } 50 | -------------------------------------------------------------------------------- /packages/dynamodb-data-marshaller/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": [ 5 | "es5", 6 | "es2015.iterable", 7 | "es2015.promise", 8 | "es2015.collection", 9 | "es2015.symbol.wellknown" 10 | ], 11 | "downlevelIteration": true, 12 | "importHelpers": true, 13 | "module": "commonjs", 14 | "strict": true, 15 | "noUnusedLocals": true, 16 | "declaration": true, 17 | "sourceMap": true, 18 | "rootDir": "./src", 19 | "outDir": "./build" 20 | }, 21 | "typedocOptions": { 22 | "mode": "file", 23 | "out": "../../docs/packages/dynamodb-data-marshaller", 24 | "excludeNotExported": true, 25 | "excludePrivate": true, 26 | "hideGenerator": true 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /packages/dynamodb-data-marshaller/tsconfig.test.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "sourceMap": false, 5 | "inlineSourceMap": true, 6 | "inlineSources": true, 7 | "sourceRoot": "./src", 8 | "outDir": "./build" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /packages/dynamodb-expressions/.npmignore: -------------------------------------------------------------------------------- 1 | /src 2 | /node_modules 3 | /coverage 4 | 5 | *.spec.d.ts 6 | *.spec.js 7 | *.spec.js.map 8 | 9 | tsconfig.json 10 | tsconfig.test.json 11 | -------------------------------------------------------------------------------- /packages/dynamodb-expressions/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@aws/dynamodb-expressions", 3 | "version": "0.7.3", 4 | "description": "Composable expression objects for Amazon DynamoDB", 5 | "keywords": [ 6 | "aws", 7 | "dynamodb" 8 | ], 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/awslabs/dynamodb-data-mapper-js.git" 12 | }, 13 | "bugs": { 14 | "url": "https://github.com/awslabs/dynamodb-data-mapper-js/issues" 15 | }, 16 | "homepage": "https://awslabs.github.io/dynamodb-data-mapper-js/packages/dynamodb-expressions/", 17 | "main": "./build/index.js", 18 | "types": "./build/index.d.ts", 19 | "scripts": { 20 | "docs": "typedoc src", 21 | "prepublishOnly": "tsc", 22 | "pretest": "tsc -p tsconfig.test.json", 23 | "test": "jest \"build/(.+).spec.js\"" 24 | }, 25 | "author": { 26 | "name": "AWS SDK for JavaScript Team", 27 | "email": "aws-sdk-js@amazon.com" 28 | }, 29 | "license": "Apache-2.0", 30 | "devDependencies": { 31 | "@types/jest": "^24", 32 | "@types/node": "^8.0.4", 33 | "aws-sdk": "^2.7.0", 34 | "jest": "^24", 35 | "typedoc": "^0.14.0", 36 | "typescript": "^3.4" 37 | }, 38 | "peerDependencies": { 39 | "aws-sdk": "^2.7.0" 40 | }, 41 | "dependencies": { 42 | "@aws/dynamodb-auto-marshaller": "^0.7.1", 43 | "tslib": "^1.9" 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /packages/dynamodb-expressions/src/AttributeBearingExpression.ts: -------------------------------------------------------------------------------- 1 | import {ExpressionAttributes} from "./ExpressionAttributes"; 2 | 3 | export interface AttributeBearingExpression { 4 | /** 5 | * Convert the function expression represented by this object into the 6 | * string format expected by DynamoDB. Any attribute names and values 7 | * will be replaced with substitutions supplied by the provided 8 | * ExpressionAttributes object. 9 | */ 10 | serialize(attributes: ExpressionAttributes): string; 11 | } 12 | -------------------------------------------------------------------------------- /packages/dynamodb-expressions/src/AttributePath.spec.ts: -------------------------------------------------------------------------------- 1 | import {AttributePath, PathElement} from "./AttributePath"; 2 | 3 | describe('AttributePath', () => { 4 | it('should convert a string path to a list of elements', () => { 5 | expect( 6 | new AttributePath('foo.bar.baz[3][4][2].fizz[0].buzz[1]').elements 7 | ).toEqual([ 8 | {type: 'AttributeName', name: 'foo'}, 9 | {type: 'AttributeName', name: 'bar'}, 10 | {type: 'AttributeName', name: 'baz'}, 11 | {type: 'ListIndex', index: 3}, 12 | {type: 'ListIndex', index: 4}, 13 | {type: 'ListIndex', index: 2}, 14 | {type: 'AttributeName', name: 'fizz'}, 15 | {type: 'ListIndex', index: 0}, 16 | {type: 'AttributeName', name: 'buzz'}, 17 | {type: 'ListIndex', index: 1}, 18 | ]); 19 | }); 20 | 21 | it('should clone an iterable of elements passed to the constructor', () => { 22 | const elements: Array = [ 23 | {type: 'AttributeName', name: 'foo'}, 24 | {type: 'AttributeName', name: 'bar'}, 25 | {type: 'AttributeName', name: 'baz'}, 26 | {type: 'ListIndex', index: 3}, 27 | {type: 'ListIndex', index: 4}, 28 | {type: 'ListIndex', index: 2}, 29 | {type: 'AttributeName', name: 'fizz'}, 30 | {type: 'ListIndex', index: 0}, 31 | {type: 'AttributeName', name: 'buzz'}, 32 | {type: 'ListIndex', index: 1}, 33 | ]; 34 | const path = new AttributePath(elements); 35 | 36 | expect(path.elements).toEqual(elements); 37 | expect(path.elements).not.toBe(elements); 38 | 39 | elements.shift(); 40 | expect(path.elements).not.toEqual(elements); 41 | expect(path.elements.slice(1)).toEqual(elements); 42 | }); 43 | 44 | it('should allow attribute names with embedded control characters', () => { 45 | expect(new AttributePath('_bracket_\\[_period_\\._backslash_\\\\_unescaped_backslash_\\_.foo').elements).toEqual([ 46 | {type: 'AttributeName', name: '_bracket_[_period_._backslash_\\_unescaped_backslash_\\_'}, 47 | {type: 'AttributeName', name: 'foo'}, 48 | ]); 49 | }); 50 | 51 | describe('path correctness checking', () => { 52 | it( 53 | 'should throw an error when a path begins with a control character', 54 | () => { 55 | expect(() => new AttributePath('[1]')) 56 | .toThrowError(/Invalid control character/); 57 | } 58 | ); 59 | 60 | it( 61 | 'should throw an error when a list index access contains invalid characters', 62 | () => { 63 | expect(() => new AttributePath('foo[a]')) 64 | .toThrowError(/Invalid array index character/); 65 | } 66 | ); 67 | 68 | it( 69 | 'should throw an error when a list index access contains no characters', 70 | () => { 71 | expect(() => new AttributePath('foo[]')) 72 | .toThrowError(/Invalid array index/); 73 | } 74 | ); 75 | 76 | it( 77 | 'should throw an error when an identifier immediately follows a list index access', 78 | () => { 79 | expect(() => new AttributePath('foo[1]a')) 80 | .toThrowError(/Bare identifier encountered/); 81 | } 82 | ); 83 | }); 84 | 85 | describe('::isAttributePath', () => { 86 | const ctor = AttributePath; 87 | 88 | afterEach(() => { 89 | (AttributePath as any) = ctor; 90 | }); 91 | 92 | it('should return true for AttributePath objects', () => { 93 | expect( 94 | AttributePath.isAttributePath(new AttributePath('foo')) 95 | ).toBe(true); 96 | }); 97 | 98 | it('should return false for scalar values', () => { 99 | for (let scalar of ['string', 123.234, true, null, void 0]) { 100 | expect(AttributePath.isAttributePath(scalar)).toBe(false); 101 | } 102 | }); 103 | 104 | it( 105 | 'should return true for AttributePaths created with a different instance of the AttributePath constructor', 106 | () => { 107 | const {isAttributePath} = AttributePath; 108 | const path = new AttributePath('foo.bar'); 109 | (AttributePath as any) = () => path; 110 | 111 | expect(path).not.toBeInstanceOf(AttributePath); 112 | expect(isAttributePath(path)).toBe(true); 113 | } 114 | ); 115 | }); 116 | }); 117 | -------------------------------------------------------------------------------- /packages/dynamodb-expressions/src/AttributeValue.spec.ts: -------------------------------------------------------------------------------- 1 | import {AttributeValue} from "./AttributeValue"; 2 | 3 | describe('AttributeValue', () => { 4 | describe('::isAttributeValue', () => { 5 | it('should accept valid attribute values', () => { 6 | const value = new AttributeValue({ 7 | S: 'string', 8 | }); 9 | 10 | expect( 11 | AttributeValue.isAttributeValue(value) 12 | ).toBe(true); 13 | }); 14 | 15 | it('should reject non-matching values', () => { 16 | for (const notAttributeValue of [ 17 | false, 18 | true, 19 | null, 20 | void 0, 21 | 'string', 22 | 123, 23 | [], 24 | {}, 25 | new Uint8Array(12), 26 | {foo: 'bar'}, 27 | {name: 'foo', arguments: 'bar'}, 28 | {S: 'string'} 29 | ]) { 30 | expect( 31 | AttributeValue.isAttributeValue(notAttributeValue) 32 | ).toBe(false); 33 | } 34 | }); 35 | }); 36 | }); 37 | -------------------------------------------------------------------------------- /packages/dynamodb-expressions/src/AttributeValue.ts: -------------------------------------------------------------------------------- 1 | import {AttributeValue as BaseAttributeValue} from 'aws-sdk/clients/dynamodb'; 2 | 3 | const MARSHALLED_ATTRIBUTE_VALUE_TAG = 'AmazonDynamoDbAttributeValue'; 4 | const EXPECTED_TOSTRING = `[object ${MARSHALLED_ATTRIBUTE_VALUE_TAG}]`; 5 | 6 | /** 7 | * An object containing an already-marshalled DynamoDB AttributeValue. 8 | * Intended to allow marshalled AttributeValues to be identified as such 9 | * and distinguished from maps that resemble marshalled values. 10 | */ 11 | export class AttributeValue { 12 | readonly [Symbol.toStringTag] = MARSHALLED_ATTRIBUTE_VALUE_TAG; 13 | 14 | constructor( 15 | public readonly marshalled: BaseAttributeValue 16 | ) {} 17 | 18 | static isAttributeValue(arg: any): arg is AttributeValue { 19 | return arg instanceof AttributeValue 20 | || Object.prototype.toString.call(arg) === EXPECTED_TOSTRING; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /packages/dynamodb-expressions/src/ExpressionAttributes.ts: -------------------------------------------------------------------------------- 1 | import {AttributePath} from "./AttributePath"; 2 | import {AttributeValue} from './AttributeValue'; 3 | import {Marshaller} from "@aws/dynamodb-auto-marshaller"; 4 | import { 5 | AttributeValue as AttributeValueModel, 6 | ExpressionAttributeNameMap, 7 | ExpressionAttributeValueMap, 8 | } from 'aws-sdk/clients/dynamodb'; 9 | 10 | /** 11 | * An object that manages expression attribute name and value substitution. 12 | */ 13 | export class ExpressionAttributes { 14 | readonly names: ExpressionAttributeNameMap = {}; 15 | readonly values: ExpressionAttributeValueMap = {}; 16 | readonly marshaller = new Marshaller(); 17 | 18 | private readonly nameMap: {[attributeName: string]: string} = {}; 19 | private _ctr = 0; 20 | 21 | /** 22 | * Add an attribute path to this substitution context. 23 | * 24 | * @returns The substitution value to use in the expression. The same 25 | * attribute name will always be converted to the same substitution value 26 | * when supplied to the same ExpressionAttributes object multiple times. 27 | */ 28 | addName(path: AttributePath|string): string { 29 | if (AttributePath.isAttributePath(path)) { 30 | let escapedPath = ''; 31 | for (const element of path.elements) { 32 | if (element.type === 'AttributeName') { 33 | escapedPath += `.${this.addAttributeName(element.name)}`; 34 | } else { 35 | escapedPath += `[${element.index}]`; 36 | } 37 | } 38 | 39 | return escapedPath.substring(1); 40 | } 41 | 42 | return this.addName(new AttributePath(path)); 43 | } 44 | 45 | /** 46 | * Add an attribute value to this substitution context. 47 | * 48 | * @returns The substitution value to use in the expression. 49 | */ 50 | addValue(value: any): string { 51 | const modeledAttrValue = AttributeValue.isAttributeValue(value) 52 | ? value.marshalled as AttributeValueModel 53 | : this.marshaller.marshallValue(value) as AttributeValueModel; 54 | 55 | const substitution = `:val${this._ctr++}`; 56 | this.values[substitution] = modeledAttrValue; 57 | 58 | return substitution; 59 | } 60 | 61 | private addAttributeName(attributeName: string): string { 62 | if (!(attributeName in this.nameMap)) { 63 | this.nameMap[attributeName] = `#attr${this._ctr++}`; 64 | this.names[this.nameMap[attributeName]] = attributeName; 65 | } 66 | 67 | return this.nameMap[attributeName]; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /packages/dynamodb-expressions/src/FunctionExpression.spec.ts: -------------------------------------------------------------------------------- 1 | import {FunctionExpression} from "./FunctionExpression"; 2 | import {ExpressionAttributes} from "./ExpressionAttributes"; 3 | import {AttributePath} from "./AttributePath"; 4 | 5 | describe('FunctionExpression', () => { 6 | const basicFunctionExpression = new FunctionExpression( 7 | 'foo', 8 | new AttributePath('bar'), 9 | 'baz' 10 | ); 11 | 12 | describe('::isFunctionExpression', () => { 13 | it('should accept valid function expressions', () => { 14 | expect( 15 | FunctionExpression.isFunctionExpression(basicFunctionExpression) 16 | ).toBe(true); 17 | }); 18 | 19 | it('should reject non-matching values', () => { 20 | for (const notFunctionExpression of [ 21 | false, 22 | true, 23 | null, 24 | void 0, 25 | 'string', 26 | 123, 27 | [], 28 | {}, 29 | new Uint8Array(12), 30 | {foo: 'bar'}, 31 | {name: 'foo', arguments: 'bar'}, 32 | ]) { 33 | expect( 34 | FunctionExpression.isFunctionExpression(notFunctionExpression) 35 | ).toBe(false); 36 | } 37 | }); 38 | }); 39 | 40 | describe('#serialize', () => { 41 | it('should serialize basic function expressions', () => { 42 | const attributes = new ExpressionAttributes(); 43 | expect( 44 | basicFunctionExpression.serialize(attributes) 45 | ).toBe('foo(#attr0, :val1)'); 46 | 47 | expect(attributes.names).toEqual({ 48 | '#attr0': 'bar', 49 | }); 50 | 51 | expect(attributes.values).toEqual({ 52 | ':val1': {S: 'baz'}, 53 | }); 54 | }); 55 | 56 | it('should support nested function expressions', () => { 57 | const nestedFunction = new FunctionExpression( 58 | 'foo', 59 | new AttributePath('bar'), 60 | 'baz', 61 | new FunctionExpression('fizz', new FunctionExpression('buzz', new AttributePath('bar'))) 62 | ) 63 | const attributes = new ExpressionAttributes(); 64 | 65 | expect( 66 | nestedFunction.serialize(attributes) 67 | ).toBe('foo(#attr0, :val1, fizz(buzz(#attr0)))'); 68 | 69 | expect(attributes.names).toEqual({ 70 | '#attr0': 'bar', 71 | }); 72 | 73 | expect(attributes.values).toEqual({ 74 | ':val1': {S: 'baz'}, 75 | }); 76 | }); 77 | }); 78 | }); 79 | -------------------------------------------------------------------------------- /packages/dynamodb-expressions/src/FunctionExpression.ts: -------------------------------------------------------------------------------- 1 | import {AttributeBearingExpression} from "./AttributeBearingExpression"; 2 | import {AttributePath} from "./AttributePath"; 3 | import {ExpressionAttributes} from "./ExpressionAttributes"; 4 | 5 | const FUNCTION_EXPRESSION_TAG = 'AmazonDynamoDbFunctionExpression'; 6 | const EXPECTED_TOSTRING = `[object ${FUNCTION_EXPRESSION_TAG}]`; 7 | 8 | /** 9 | * An object representing a DynamoDB function expression. 10 | */ 11 | export class FunctionExpression implements AttributeBearingExpression { 12 | readonly [Symbol.toStringTag] = FUNCTION_EXPRESSION_TAG; 13 | readonly args: Array; 14 | 15 | constructor( 16 | readonly name: string, 17 | ...args: Array 18 | ) { 19 | this.args = args; 20 | } 21 | 22 | serialize(attributes: ExpressionAttributes) { 23 | const expressionSafeArgs: Array = this.args.map(arg => { 24 | if (AttributePath.isAttributePath(arg)) { 25 | return attributes.addName(arg); 26 | } else if (FunctionExpression.isFunctionExpression(arg)) { 27 | return arg.serialize(attributes); 28 | } 29 | 30 | return attributes.addValue(arg); 31 | }); 32 | 33 | return `${this.name}(${expressionSafeArgs.join(', ')})`; 34 | } 35 | 36 | /** 37 | * Evaluate whether the provided value is a FunctionExpression object. 38 | */ 39 | static isFunctionExpression(arg: any): arg is FunctionExpression { 40 | return arg instanceof FunctionExpression 41 | || Object.prototype.toString.call(arg) === EXPECTED_TOSTRING; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /packages/dynamodb-expressions/src/MathematicalExpression.spec.ts: -------------------------------------------------------------------------------- 1 | import { AttributePath } from './AttributePath'; 2 | import { ExpressionAttributes } from './ExpressionAttributes'; 3 | import { FunctionExpression } from './FunctionExpression'; 4 | import { MathematicalExpression } from './MathematicalExpression'; 5 | import { 6 | ExpressionAttributeNameMap, 7 | ExpressionAttributeValueMap 8 | } from 'aws-sdk/clients/dynamodb'; 9 | 10 | describe('MathematicalExpression', () => { 11 | const validExpressions: Array<[ 12 | MathematicalExpression, 13 | string, 14 | ExpressionAttributeNameMap, 15 | ExpressionAttributeValueMap 16 | ]> = [ 17 | [ 18 | new MathematicalExpression(new AttributePath('foo'), '+', 1), 19 | '#attr0 + :val1', 20 | { '#attr0': 'foo' }, 21 | { ':val1': {N: '1'} }, 22 | ], 23 | [ 24 | new MathematicalExpression( 25 | new FunctionExpression( 26 | 'if_not_exists', 27 | new AttributePath('current_id'), 28 | 0 29 | ), 30 | '+', 31 | 1 32 | ), 33 | 'if_not_exists(#attr0, :val1) + :val2', 34 | { '#attr0': 'current_id' }, 35 | { 36 | ':val1': {N: '0'}, 37 | ':val2': {N: '1'}, 38 | }, 39 | ] 40 | ]; 41 | 42 | describe('::isMathematicalExpression', () => { 43 | it('should accept valid mathematical expressions', () => { 44 | for (const [expr, _1, _2, _3] of validExpressions) { 45 | expect(MathematicalExpression.isMathematicalExpression(expr)) 46 | .toBe(true); 47 | } 48 | }); 49 | 50 | it('should reject non-matching values', () => { 51 | for (const notMathematicalExpression of [ 52 | false, 53 | true, 54 | null, 55 | void 0, 56 | 'string', 57 | 123, 58 | [], 59 | {}, 60 | new Uint8Array(12), 61 | {foo: 'bar'}, 62 | ]) { 63 | expect( 64 | MathematicalExpression 65 | .isMathematicalExpression(notMathematicalExpression) 66 | ).toBe(false); 67 | } 68 | }); 69 | }); 70 | 71 | describe('#serialize', () => { 72 | it('should serialize basic mathematical expressions', () => { 73 | for (const [ 74 | expression, 75 | serialized, 76 | expectedNames, 77 | expectedValues, 78 | ] of validExpressions) { 79 | const attributes = new ExpressionAttributes(); 80 | expect(expression.serialize(attributes)).toBe(serialized); 81 | expect(attributes.names).toEqual(expectedNames); 82 | expect(attributes.values).toEqual(expectedValues); 83 | } 84 | }); 85 | }); 86 | }); 87 | -------------------------------------------------------------------------------- /packages/dynamodb-expressions/src/MathematicalExpression.ts: -------------------------------------------------------------------------------- 1 | import {AttributeBearingExpression} from "./AttributeBearingExpression"; 2 | import {AttributePath} from "./AttributePath"; 3 | import {ExpressionAttributes} from "./ExpressionAttributes"; 4 | import {FunctionExpression} from "./FunctionExpression"; 5 | 6 | export type MathematicalExpressionOperand = AttributePath|FunctionExpression|string|number; 7 | 8 | const MATHEMATICAL_EXPRESSION_TAG = 'AmazonDynamoDbMathematicalExpression'; 9 | const EXPECTED_TOSTRING = `[object ${MATHEMATICAL_EXPRESSION_TAG}]`; 10 | 11 | /** 12 | * An object representing a DynamoDB function expression. 13 | */ 14 | export class MathematicalExpression implements AttributeBearingExpression { 15 | readonly [Symbol.toStringTag] = MATHEMATICAL_EXPRESSION_TAG; 16 | 17 | constructor( 18 | readonly lhs: MathematicalExpressionOperand, 19 | readonly operator: '+'|'-', 20 | readonly rhs: MathematicalExpressionOperand 21 | ) {} 22 | 23 | serialize(attributes: ExpressionAttributes) { 24 | const safeArgs = [this.lhs, this.rhs].map( 25 | arg => { 26 | if (FunctionExpression.isFunctionExpression(arg)) { 27 | return arg.serialize(attributes); 28 | } 29 | return AttributePath.isAttributePath(arg) || typeof arg === 'string' 30 | ? attributes.addName(arg) 31 | : attributes.addValue(arg); 32 | } 33 | ); 34 | return `${safeArgs[0]} ${this.operator} ${safeArgs[1]}`; 35 | } 36 | 37 | /** 38 | * Evaluate whether the provided value is a MathematicalExpression object. 39 | */ 40 | static isMathematicalExpression(arg: any): arg is MathematicalExpression { 41 | return arg instanceof MathematicalExpression 42 | || Object.prototype.toString.call(arg) === EXPECTED_TOSTRING; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /packages/dynamodb-expressions/src/ProjectionExpression.spec.ts: -------------------------------------------------------------------------------- 1 | import {AttributePath} from "./AttributePath"; 2 | import {ExpressionAttributes} from "./ExpressionAttributes"; 3 | import { 4 | ProjectionExpression, 5 | serializeProjectionExpression 6 | } from './ProjectionExpression'; 7 | 8 | describe('ProjectionExpression', () => { 9 | it('should allow the addition of scalar values', () => { 10 | const attributes = new ExpressionAttributes(); 11 | 12 | expect(serializeProjectionExpression( 13 | ['foo', 'bar', 'baz', 'quux'], 14 | attributes 15 | )).toBe('#attr0, #attr1, #attr2, #attr3'); 16 | expect(attributes.names).toEqual({ 17 | '#attr0': 'foo', 18 | '#attr1': 'bar', 19 | '#attr2': 'baz', 20 | '#attr3': 'quux', 21 | }); 22 | }); 23 | 24 | it('should allow the addition of list index dereferences', () => { 25 | const attributes = new ExpressionAttributes(); 26 | 27 | expect(serializeProjectionExpression( 28 | [new AttributePath('foo[2]')], 29 | attributes 30 | )).toBe('#attr0[2]'); 31 | expect(attributes.names).toEqual({ 32 | '#attr0': 'foo', 33 | }); 34 | }); 35 | 36 | it('should allow the addition of nested attributes', () => { 37 | const attributes = new ExpressionAttributes(); 38 | 39 | expect(serializeProjectionExpression( 40 | [new AttributePath('foo.bar')], 41 | attributes 42 | )).toBe('#attr0.#attr1'); 43 | expect(attributes.names).toEqual({ 44 | '#attr0': 'foo', 45 | '#attr1': 'bar', 46 | }); 47 | }); 48 | 49 | it( 50 | 'should allow the nesting of complex attributes to an arbitrary depth', 51 | () => { 52 | const attributes = new ExpressionAttributes(); 53 | expect(serializeProjectionExpression( 54 | [new AttributePath('snap.foo[2].bar[3].baz[4].quux')], 55 | attributes 56 | )).toBe('#attr0.#attr1[2].#attr2[3].#attr3[4].#attr4'); 57 | expect(attributes.names).toEqual({ 58 | '#attr0': 'snap', 59 | '#attr1': 'foo', 60 | '#attr2': 'bar', 61 | '#attr3': 'baz', 62 | '#attr4': 'quux', 63 | }); 64 | } 65 | ); 66 | }); 67 | -------------------------------------------------------------------------------- /packages/dynamodb-expressions/src/ProjectionExpression.ts: -------------------------------------------------------------------------------- 1 | import {AttributePath} from "./AttributePath"; 2 | import {ExpressionAttributes} from './ExpressionAttributes'; 3 | 4 | /** 5 | * An array of attributes to project. Each may be represented as 6 | * either an AttributePath object or as a string. 7 | */ 8 | export type ProjectionExpression = Array; 9 | 10 | /** 11 | * Convert the supplied projection expression to the string format 12 | * expected by DynamoDB, substituting all attribute paths using the 13 | * provided ExpressionAttributes object. 14 | */ 15 | export function serializeProjectionExpression( 16 | projection: ProjectionExpression, 17 | attributes: ExpressionAttributes 18 | ): string { 19 | const serialized: Array = []; 20 | for (const projected of projection) { 21 | serialized.push(attributes.addName(projected)); 22 | } 23 | 24 | return serialized.join(', '); 25 | } 26 | -------------------------------------------------------------------------------- /packages/dynamodb-expressions/src/UpdateExpression.ts: -------------------------------------------------------------------------------- 1 | import {AttributeBearingExpression} from "./AttributeBearingExpression"; 2 | import {AttributePath} from "./AttributePath"; 3 | import {AttributeValue} from './AttributeValue'; 4 | import {ExpressionAttributes} from "./ExpressionAttributes"; 5 | import {FunctionExpression} from "./FunctionExpression"; 6 | import {MathematicalExpression} from "./MathematicalExpression"; 7 | 8 | /** 9 | * An object representing a DynamoDB update expression. 10 | */ 11 | export class UpdateExpression implements AttributeBearingExpression { 12 | readonly toAdd = new Map(); 13 | readonly toDelete = new Map(); 14 | readonly toRemove = new Set(); 15 | readonly toSet = new Map(); 16 | 17 | /** 18 | * Add a directive to the expression's `add` clause. 19 | */ 20 | add(path: AttributePath|string, value: any): void { 21 | this.toAdd.set( 22 | AttributePath.isAttributePath(path) ? path : new AttributePath(path), 23 | value 24 | ); 25 | } 26 | 27 | /** 28 | * Add a directive to the expression's `delete` clause. 29 | */ 30 | delete(path: AttributePath|string, value: any): void { 31 | this.toDelete.set( 32 | AttributePath.isAttributePath(path) ? path : new AttributePath(path), 33 | value 34 | ); 35 | } 36 | 37 | /** 38 | * Add a directive to the expression's `remove` clause. 39 | */ 40 | remove(path: AttributePath|string): void { 41 | this.toRemove.add( 42 | AttributePath.isAttributePath(path) ? path : new AttributePath(path) 43 | ); 44 | } 45 | 46 | /** 47 | * Add a directive to the expression's `set` clause. 48 | */ 49 | set( 50 | path: AttributePath|string, 51 | value: AttributeValue|FunctionExpression|MathematicalExpression|any 52 | ): void { 53 | this.toSet.set( 54 | AttributePath.isAttributePath(path) ? path : new AttributePath(path), 55 | value 56 | ); 57 | } 58 | 59 | serialize(attributes: ExpressionAttributes): string { 60 | const clauses: Array = []; 61 | const phrases: Array = []; 62 | for (const [mapping, verb] of [ 63 | [this.toAdd, 'ADD'], 64 | [this.toDelete, 'DELETE'], 65 | ] as Array<[Map, string]>) { 66 | for (const [key, value] of mapping.entries()) { 67 | phrases.push( 68 | `${attributes.addName(key)} ${attributes.addValue(value)}` 69 | ); 70 | } 71 | 72 | if (phrases.length > 0) { 73 | clauses.push(`${verb} ${phrases.join(', ')}`); 74 | phrases.length = 0; 75 | } 76 | } 77 | 78 | for (const [key, value] of this.toSet.entries()) { 79 | phrases.push(`${attributes.addName(key)} = ${ 80 | FunctionExpression.isFunctionExpression(value) || MathematicalExpression.isMathematicalExpression(value) 81 | ? value.serialize(attributes) : attributes.addValue(value) 82 | }`); 83 | } 84 | if (phrases.length > 0) { 85 | clauses.push(`SET ${phrases.join(', ')}`); 86 | phrases.length = 0; 87 | } 88 | 89 | for (const keyToRemove of this.toRemove) { 90 | phrases.push(attributes.addName(keyToRemove)); 91 | } 92 | if (phrases.length > 0) { 93 | clauses.push(`REMOVE ${phrases.join(', ')}`); 94 | phrases.length = 0; 95 | } 96 | 97 | return clauses.join(' '); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /packages/dynamodb-expressions/src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './AttributePath'; 2 | export * from './AttributeValue'; 3 | export * from './ConditionExpression'; 4 | export * from './ExpressionAttributes'; 5 | export * from './FunctionExpression'; 6 | export * from './MathematicalExpression'; 7 | export * from './ProjectionExpression'; 8 | export * from './UpdateExpression'; 9 | -------------------------------------------------------------------------------- /packages/dynamodb-expressions/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": [ 5 | "es5", 6 | "es2015.iterable", 7 | "es2015.promise", 8 | "es2015.collection", 9 | "es2015.symbol.wellknown" 10 | ], 11 | "downlevelIteration": true, 12 | "importHelpers": true, 13 | "module": "commonjs", 14 | "strict": true, 15 | "declaration": true, 16 | "sourceMap": true, 17 | "rootDir": "./src", 18 | "outDir": "./build" 19 | }, 20 | "typedocOptions": { 21 | "mode": "file", 22 | "out": "../../docs/packages/dynamodb-expressions", 23 | "excludeNotExported": true, 24 | "excludePrivate": true, 25 | "hideGenerator": true 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /packages/dynamodb-expressions/tsconfig.test.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "sourceMap": false, 5 | "inlineSourceMap": true, 6 | "inlineSources": true, 7 | "rootDir": "./src", 8 | "outDir": "./build" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /packages/dynamodb-query-iterator/.npmignore: -------------------------------------------------------------------------------- 1 | /src 2 | /node_modules 3 | /coverage 4 | 5 | *.spec.d.ts 6 | *.spec.js 7 | *.spec.js.map 8 | 9 | tsconfig.json 10 | tsconfig.test.json 11 | -------------------------------------------------------------------------------- /packages/dynamodb-query-iterator/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | All notable changes to this project will be documented in this file. 3 | 4 | The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) 5 | and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). 6 | 7 | ## [0.7.1] 8 | Remove package rollup at `./build/index.mjs` due to bundler incompatibilities. 9 | 10 | ## [0.7.0] 11 | Add a package rollup at `./build/index.mjs` to support tree shaking. 12 | 13 | ## [0.6.0] 14 | Initial release 15 | -------------------------------------------------------------------------------- /packages/dynamodb-query-iterator/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@aws/dynamodb-query-iterator", 3 | "version": "0.7.1", 4 | "description": "Abstraction for DynamoDB queries and scans that handles pagination and parallel worker coordination", 5 | "keywords": [ 6 | "aws", 7 | "dynamodb" 8 | ], 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/awslabs/dynamodb-data-mapper-js.git" 12 | }, 13 | "bugs": { 14 | "url": "https://github.com/awslabs/dynamodb-data-mapper-js/issues" 15 | }, 16 | "homepage": "https://awslabs.github.io/dynamodb-data-mapper-js/packages/dynamodb-scan-iterator/", 17 | "main": "./build/index.js", 18 | "types": "./build/index.d.ts", 19 | "scripts": { 20 | "docs": "typedoc src", 21 | "prepublishOnly": "tsc", 22 | "pretest": "tsc -p tsconfig.test.json", 23 | "test": "jest \"build/(.+).spec.js\"" 24 | }, 25 | "author": { 26 | "name": "AWS SDK for JavaScript Team", 27 | "email": "aws-sdk-js@amazon.com" 28 | }, 29 | "license": "Apache-2.0", 30 | "devDependencies": { 31 | "@types/jest": "^24", 32 | "@types/node": "^8.0.4", 33 | "aws-sdk": "^2.7.0", 34 | "jest": "^24", 35 | "typedoc": "^0.14.0", 36 | "typescript": "^3.4" 37 | }, 38 | "peerDependencies": { 39 | "aws-sdk": "^2.7.0" 40 | }, 41 | "dependencies": { 42 | "tslib": "^1.9" 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /packages/dynamodb-query-iterator/src/DynamoDbPaginator.ts: -------------------------------------------------------------------------------- 1 | import { DynamoDbPaginatorInterface } from './DynamoDbPaginatorInterface'; 2 | import { DynamoDbResultsPage } from './DynamoDbResultsPage'; 3 | import { mergeConsumedCapacities } from './mergeConsumedCapacities'; 4 | import { ConsumedCapacity, Key } from 'aws-sdk/clients/dynamodb'; 5 | 6 | if (Symbol && !Symbol.asyncIterator) { 7 | (Symbol as any).asyncIterator = Symbol.for("__@@asyncIterator__"); 8 | } 9 | 10 | export abstract class DynamoDbPaginator implements DynamoDbPaginatorInterface { 11 | private _consumedCapacity?: ConsumedCapacity; 12 | private _count = 0; 13 | private _lastKey?: Key; 14 | private _scannedCount = 0; 15 | private lastResolved: Promise> 16 | = Promise.resolve(); 17 | 18 | protected constructor(private readonly limit?: number) {} 19 | 20 | /** 21 | * @inheritDoc 22 | */ 23 | [Symbol.asyncIterator](): AsyncIterableIterator { 24 | return this; 25 | } 26 | 27 | /** 28 | * @inheritDoc 29 | */ 30 | get consumedCapacity(): ConsumedCapacity|undefined { 31 | return this._consumedCapacity; 32 | } 33 | 34 | /** 35 | * @inheritDoc 36 | */ 37 | get count(): number { 38 | return this._count; 39 | } 40 | 41 | /** 42 | * Get the LastEvaluatedKey of the last result page yielded by this 43 | * paginator or undefined if the scan has already been exhausted. 44 | */ 45 | get lastEvaluatedKey(): Key|undefined { 46 | return this._lastKey; 47 | } 48 | 49 | /** 50 | * @inheritDoc 51 | */ 52 | next(): Promise> { 53 | this.lastResolved = this.lastResolved.then(() => { 54 | if (this.count >= (this.limit === undefined ? Infinity : this.limit)) { 55 | return {done: true} as IteratorResult; 56 | } 57 | 58 | return this.getNext().then(({done, value}) => { 59 | if (value && !done) { 60 | this._lastKey = value.LastEvaluatedKey; 61 | this._count += (value.Items || []).length; 62 | this._scannedCount += (value.ScannedCount || 0); 63 | this._consumedCapacity = mergeConsumedCapacities( 64 | this._consumedCapacity, 65 | value.ConsumedCapacity 66 | ); 67 | } 68 | 69 | return { value, done }; 70 | }) 71 | } 72 | ); 73 | 74 | return this.lastResolved; 75 | } 76 | 77 | /** 78 | * @inheritDoc 79 | */ 80 | return(): Promise> { 81 | // Prevent any further use of this iterator 82 | this.lastResolved = Promise.reject(new Error( 83 | 'Iteration has been manually interrupted and may not be resumed' 84 | )); 85 | this.lastResolved.catch(() => {}); 86 | 87 | return Promise.resolve( 88 | {done: true} as IteratorResult 89 | ); 90 | } 91 | 92 | /** 93 | * @inheritDoc 94 | */ 95 | get scannedCount(): number { 96 | return this._scannedCount; 97 | } 98 | 99 | /** 100 | * Perform the next iteration 101 | */ 102 | protected abstract getNext(): Promise>; 103 | 104 | protected getNextPageSize(requestedPageSize?: number): number|undefined { 105 | if (this.limit === undefined) { 106 | return requestedPageSize; 107 | } 108 | 109 | return Math.min( 110 | requestedPageSize === undefined ? Infinity : requestedPageSize, 111 | this.limit - this.count 112 | ); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /packages/dynamodb-query-iterator/src/DynamoDbPaginatorInterface.ts: -------------------------------------------------------------------------------- 1 | import { DynamoDbResultsPage } from './DynamoDbResultsPage'; 2 | import { ConsumedCapacity } from 'aws-sdk/clients/dynamodb'; 3 | 4 | export interface DynamoDbPaginatorInterface extends 5 | AsyncIterableIterator 6 | { 7 | /** 8 | * The capacity units consumed by the Scan operation. The data returned 9 | * includes the total provisioned throughput consumed, along with statistics 10 | * for the table and any indexes involved in the operation. ConsumedCapacity 11 | * is only returned if the ReturnConsumedCapacity parameter was specified. 12 | */ 13 | readonly consumedCapacity: ConsumedCapacity|undefined; 14 | 15 | /** 16 | * The number of items in the results yielded. 17 | */ 18 | readonly count: number; 19 | 20 | /** 21 | * The number of items evaluated, before any ScanFilter is applied. A high 22 | * scannedCount value with few, or no, Count results indicates an 23 | * inefficient Scan operation. For more information, see Count and 24 | * ScannedCount in the Amazon DynamoDB Developer Guide. 25 | */ 26 | readonly scannedCount: number; 27 | 28 | /** 29 | * @inheritDoc 30 | */ 31 | return(): Promise>; 32 | } 33 | -------------------------------------------------------------------------------- /packages/dynamodb-query-iterator/src/DynamoDbResultsPage.ts: -------------------------------------------------------------------------------- 1 | import { 2 | AttributeMap, 3 | ConsumedCapacity, 4 | Key, 5 | } from "aws-sdk/clients/dynamodb"; 6 | 7 | export interface DynamoDbResultsPage { 8 | /** 9 | * An array of retrieved items. 10 | */ 11 | Items?: Array; 12 | 13 | /** 14 | * The number of items in the response. If you used a filter in the request, 15 | * then Count is the number of items returned after the filter was applied, 16 | * and ScannedCount is the number of matching items before the filter was 17 | * applied. If you did not use a filter in the request, then Count and 18 | * ScannedCount are the same. 19 | */ 20 | Count?: number; 21 | 22 | /** 23 | * The number of items evaluated, before any filter is applied. A high 24 | * ScannedCount value with few, or no, Count results indicates an 25 | * inefficient operation. For more information, see Count and ScannedCount 26 | * in the Amazon DynamoDB Developer Guide. If you did not use a filter in 27 | * the request, then ScannedCount is the same as Count. 28 | */ 29 | ScannedCount?: number; 30 | 31 | /** 32 | * The primary key of the item where the operation stopped, inclusive of the 33 | * previous result set. Use this value to start a new operation, excluding 34 | * this value in the new request. If LastEvaluatedKey is empty, then the 35 | * "last page" of results has been processed and there is no more data to be 36 | * retrieved. If LastEvaluatedKey is not empty, it does not necessarily mean 37 | * that there is more data in the result set. The only way to know when you 38 | * have reached the end of the result set is when LastEvaluatedKey is empty. 39 | */ 40 | LastEvaluatedKey?: Key; 41 | 42 | /** 43 | * The capacity units consumed by the operation. The data returned includes 44 | * the total provisioned throughput consumed, along with statistics for the 45 | * table and any indexes involved in the operation. ConsumedCapacity is only 46 | * returned if the ReturnConsumedCapacity parameter was specified For more 47 | * information, see Provisioned Throughput in the Amazon DynamoDB Developer 48 | * Guide. 49 | */ 50 | ConsumedCapacity?: ConsumedCapacity; 51 | } 52 | -------------------------------------------------------------------------------- /packages/dynamodb-query-iterator/src/ItemIterator.ts: -------------------------------------------------------------------------------- 1 | import { DynamoDbPaginatorInterface } from './DynamoDbPaginatorInterface'; 2 | import { AttributeMap, ConsumedCapacity } from 'aws-sdk/clients/dynamodb'; 3 | 4 | if (Symbol && !Symbol.asyncIterator) { 5 | (Symbol as any).asyncIterator = Symbol.for("__@@asyncIterator__"); 6 | } 7 | 8 | export abstract class ItemIterator< 9 | Paginator extends DynamoDbPaginatorInterface 10 | > implements AsyncIterableIterator { 11 | 12 | private _iteratedCount = 0; 13 | private lastResolved: Promise> = Promise.resolve(); 14 | private readonly pending: Array = []; 15 | 16 | protected constructor(private readonly paginator: Paginator) {} 17 | 18 | /** 19 | * @inheritDoc 20 | */ 21 | [Symbol.asyncIterator](): AsyncIterableIterator { 22 | return this; 23 | } 24 | 25 | /** 26 | * The capacity units consumed by the Scan operation. The data returned 27 | * includes the total provisioned throughput consumed, along with statistics 28 | * for the table and any indexes involved in the operation. ConsumedCapacity 29 | * is only returned if the ReturnConsumedCapacity parameter was specified. 30 | */ 31 | get consumedCapacity(): ConsumedCapacity|undefined { 32 | return this.paginator.consumedCapacity; 33 | } 34 | 35 | /** 36 | * The number of items that have been iterated over. 37 | */ 38 | get count(): number { 39 | return this._iteratedCount; 40 | } 41 | 42 | /** 43 | * @inheritDoc 44 | */ 45 | next(): Promise> { 46 | this.lastResolved = this.lastResolved.then(() => this.getNext()); 47 | return this.lastResolved; 48 | } 49 | 50 | /** 51 | * Detaches the underlying paginator from this iterator and returns it. The 52 | * paginator will yield arrays of unmarshalled items, with each yielded 53 | * array corresponding to a single call to the underlying API. As with the 54 | * underlying API, pages may contain a variable number of items or no items, 55 | * in which case an empty array will be yielded. 56 | * 57 | * Calling this method will disable further iteration. 58 | */ 59 | pages(): Paginator { 60 | // Prevent the iterator from being used further and squelch any uncaught 61 | // promise rejection warnings 62 | this.lastResolved = Promise.reject(new Error( 63 | 'The underlying paginator has been detached from this iterator.' 64 | )); 65 | this.lastResolved.catch(() => {}); 66 | 67 | return this.paginator; 68 | } 69 | 70 | /** 71 | * @inheritDoc 72 | */ 73 | return(): Promise> { 74 | // Prevent any further use of this iterator 75 | this.lastResolved = Promise.reject(new Error( 76 | 'Iteration has been manually interrupted and may not be resumed' 77 | )); 78 | this.lastResolved.catch(() => {}); 79 | 80 | // Clear the pending queue to free up memory 81 | this.pending.length = 0; 82 | return this.paginator.return().then(doneSigil); 83 | } 84 | 85 | /** 86 | * The number of items evaluated, before any ScanFilter is applied. A high 87 | * scannedCount value with few, or no, Count results indicates an 88 | * inefficient Scan operation. For more information, see Count and 89 | * ScannedCount in the Amazon DynamoDB Developer Guide. 90 | */ 91 | get scannedCount(): number { 92 | return this.paginator.scannedCount; 93 | } 94 | 95 | private getNext(): Promise> { 96 | if (this.pending.length > 0) { 97 | this._iteratedCount++; 98 | return Promise.resolve({ 99 | value: this.pending.shift()!, 100 | done: false 101 | }); 102 | } 103 | 104 | return this.paginator.next().then(({done, value}) => { 105 | if (done) { 106 | return {done} as IteratorResult; 107 | } 108 | 109 | this.pending.push(...value.Items || []); 110 | return this.getNext(); 111 | }); 112 | } 113 | } 114 | 115 | function doneSigil() { 116 | return {done: true} as IteratorResult; 117 | } 118 | -------------------------------------------------------------------------------- /packages/dynamodb-query-iterator/src/ParallelScanInput.ts: -------------------------------------------------------------------------------- 1 | import { ScanInput } from 'aws-sdk/clients/dynamodb'; 2 | 3 | export interface ParallelScanInput extends ScanInput { 4 | /** 5 | * The exclusive start key for a particular scan segment must be coordinated 6 | * across all active segments. To resume a previously suspending parallel 7 | * scan, provide a `scanState` initializer when creating a 8 | * ParallelScanPaginator. 9 | */ 10 | ExclusiveStartKey?: undefined; 11 | 12 | /** 13 | * The segment identifier for each request will be assigned by the parallel 14 | * scan orchestrator. 15 | */ 16 | Segment?: undefined; 17 | 18 | /** 19 | * @inheritDoc 20 | * 21 | * `TotalSegments` **MUST** be specified when initializing or resuming a 22 | * parallel scan. 23 | */ 24 | TotalSegments: number; 25 | } 26 | -------------------------------------------------------------------------------- /packages/dynamodb-query-iterator/src/ParallelScanIterator.ts: -------------------------------------------------------------------------------- 1 | import { ItemIterator } from './ItemIterator'; 2 | import { ParallelScanInput } from './ParallelScanInput'; 3 | import { 4 | ParallelScanPaginator, 5 | ParallelScanState, 6 | } from './ParallelScanPaginator'; 7 | import DynamoDB = require('aws-sdk/clients/dynamodb'); 8 | 9 | export class ParallelScanIterator extends ItemIterator { 10 | constructor( 11 | client: DynamoDB, 12 | input: ParallelScanInput, 13 | scanState?: ParallelScanState 14 | ) { 15 | super(new ParallelScanPaginator(client, input, scanState)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /packages/dynamodb-query-iterator/src/QueryIterator.ts: -------------------------------------------------------------------------------- 1 | import { ItemIterator } from './ItemIterator'; 2 | import { QueryPaginator } from './QueryPaginator'; 3 | import { QueryInput } from 'aws-sdk/clients/dynamodb'; 4 | import DynamoDB = require('aws-sdk/clients/dynamodb'); 5 | 6 | export class QueryIterator extends ItemIterator { 7 | constructor(client: DynamoDB, input: QueryInput, limit?: number) { 8 | super(new QueryPaginator(client, input, limit)); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /packages/dynamodb-query-iterator/src/QueryPaginator.ts: -------------------------------------------------------------------------------- 1 | import { DynamoDbPaginator } from './DynamoDbPaginator'; 2 | import { DynamoDbResultsPage } from './DynamoDbResultsPage'; 3 | import { QueryInput } from 'aws-sdk/clients/dynamodb'; 4 | import DynamoDB = require('aws-sdk/clients/dynamodb'); 5 | 6 | export class QueryPaginator extends DynamoDbPaginator { 7 | private nextRequest?: QueryInput; 8 | 9 | constructor( 10 | private readonly client: DynamoDB, 11 | input: QueryInput, 12 | limit?: number 13 | ) { 14 | super(limit); 15 | this.nextRequest = {...input}; 16 | } 17 | 18 | protected getNext(): Promise> { 19 | if (this.nextRequest) { 20 | return this.client.query({ 21 | ...this.nextRequest, 22 | Limit: this.getNextPageSize(this.nextRequest.Limit) 23 | }) 24 | .promise() 25 | .then(output => { 26 | if (this.nextRequest && output.LastEvaluatedKey) { 27 | this.nextRequest = { 28 | ...this.nextRequest, 29 | ExclusiveStartKey: output.LastEvaluatedKey 30 | }; 31 | } else { 32 | this.nextRequest = undefined; 33 | } 34 | 35 | return Promise.resolve({ 36 | value: output, 37 | done: false 38 | }); 39 | }); 40 | } 41 | 42 | return Promise.resolve( 43 | {done: true} as IteratorResult 44 | ); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /packages/dynamodb-query-iterator/src/ScanIterator.ts: -------------------------------------------------------------------------------- 1 | import { ItemIterator } from './ItemIterator'; 2 | import { ScanPaginator } from './ScanPaginator'; 3 | import { ScanInput } from 'aws-sdk/clients/dynamodb'; 4 | import DynamoDB = require('aws-sdk/clients/dynamodb'); 5 | 6 | export class ScanIterator extends ItemIterator { 7 | constructor(client: DynamoDB, input: ScanInput, limit?: number) { 8 | super(new ScanPaginator(client, input, limit)); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /packages/dynamodb-query-iterator/src/ScanPaginator.ts: -------------------------------------------------------------------------------- 1 | import { DynamoDbPaginator } from './DynamoDbPaginator'; 2 | import { DynamoDbResultsPage } from './DynamoDbResultsPage'; 3 | import { ScanInput } from 'aws-sdk/clients/dynamodb'; 4 | import DynamoDB = require('aws-sdk/clients/dynamodb'); 5 | 6 | export class ScanPaginator extends DynamoDbPaginator { 7 | private nextRequest?: ScanInput; 8 | 9 | constructor( 10 | private readonly client: DynamoDB, 11 | input: ScanInput, 12 | limit?: number 13 | ) { 14 | super(limit); 15 | this.nextRequest = { 16 | ...input, 17 | Limit: this.getNextPageSize(input.Limit), 18 | }; 19 | } 20 | 21 | protected getNext(): Promise> { 22 | if (this.nextRequest) { 23 | return this.client.scan({ 24 | ...this.nextRequest, 25 | Limit: this.getNextPageSize(this.nextRequest.Limit) 26 | }) 27 | .promise() 28 | .then(output => { 29 | if (this.nextRequest && output.LastEvaluatedKey) { 30 | this.nextRequest = { 31 | ...this.nextRequest, 32 | ExclusiveStartKey: output.LastEvaluatedKey 33 | }; 34 | } else { 35 | this.nextRequest = undefined; 36 | } 37 | 38 | return Promise.resolve({ 39 | value: output, 40 | done: false 41 | }); 42 | }); 43 | } 44 | 45 | return Promise.resolve( 46 | {done: true} as IteratorResult 47 | ); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /packages/dynamodb-query-iterator/src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './DynamoDbPaginatorInterface'; 2 | export * from './ParallelScanInput'; 3 | export * from './ParallelScanIterator'; 4 | export * from './ParallelScanPaginator'; 5 | export * from './QueryIterator'; 6 | export * from './QueryPaginator'; 7 | export * from './ScanIterator'; 8 | export * from './ScanPaginator'; 9 | -------------------------------------------------------------------------------- /packages/dynamodb-query-iterator/src/mergeConsumedCapacities.ts: -------------------------------------------------------------------------------- 1 | import { 2 | Capacity, 3 | ConsumedCapacity, 4 | SecondaryIndexesCapacityMap, 5 | } from 'aws-sdk/clients/dynamodb'; 6 | 7 | /** 8 | * @internal 9 | */ 10 | export function mergeConsumedCapacities( 11 | a?: ConsumedCapacity, 12 | b?: ConsumedCapacity 13 | ): ConsumedCapacity|undefined { 14 | if (a || b) { 15 | a = a || {}; 16 | b = b || {}; 17 | 18 | if ((a.TableName && b.TableName) && a.TableName !== b.TableName) { 19 | throw new Error( 20 | 'Consumed capacity reports may only be merged if they describe the same table' 21 | ); 22 | } 23 | 24 | return { 25 | TableName: a.TableName || b.TableName, 26 | CapacityUnits: (a.CapacityUnits || 0) + (b.CapacityUnits || 0), 27 | Table: mergeCapacities(a.Table, b.Table), 28 | LocalSecondaryIndexes: mergeCapacityMaps( 29 | a.LocalSecondaryIndexes, 30 | b.LocalSecondaryIndexes 31 | ), 32 | GlobalSecondaryIndexes: mergeCapacityMaps( 33 | a.GlobalSecondaryIndexes, 34 | b.GlobalSecondaryIndexes 35 | ), 36 | } 37 | } 38 | } 39 | 40 | function mergeCapacities(a?: Capacity, b?: Capacity): Capacity|undefined { 41 | if (a || b) { 42 | return { 43 | CapacityUnits: ((a && a.CapacityUnits) || 0) + 44 | ((b && b.CapacityUnits) || 0), 45 | }; 46 | } 47 | } 48 | 49 | function mergeCapacityMaps( 50 | a?: SecondaryIndexesCapacityMap, 51 | b?: SecondaryIndexesCapacityMap 52 | ): SecondaryIndexesCapacityMap|undefined { 53 | if (a || b) { 54 | const out: SecondaryIndexesCapacityMap = {}; 55 | 56 | a = a || {}; 57 | b = b || {}; 58 | const keys = new Set(); 59 | for (const map of [a, b]) { 60 | for (const indexName of Object.keys(map)) { 61 | keys.add(indexName); 62 | } 63 | } 64 | 65 | for (const key of keys) { 66 | out[key] = mergeCapacities(a[key], b[key])!; 67 | } 68 | 69 | return out; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /packages/dynamodb-query-iterator/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": [ 5 | "es5", 6 | "es2015.iterable", 7 | "es2015.promise", 8 | "es2015.collection", 9 | "es2015.symbol.wellknown", 10 | "esnext.asynciterable" 11 | ], 12 | "downlevelIteration": true, 13 | "importHelpers": true, 14 | "module": "commonjs", 15 | "noUnusedLocals": true, 16 | "strict": true, 17 | "declaration": true, 18 | "sourceMap": true, 19 | "rootDir": "./src", 20 | "outDir": "./build" 21 | }, 22 | "typedocOptions": { 23 | "mode": "file", 24 | "out": "../../docs/packages/dynamodb-query-iterator", 25 | "excludeNotExported": true, 26 | "excludePrivate": true, 27 | "hideGenerator": true 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /packages/dynamodb-query-iterator/tsconfig.test.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "compilerOptions": { 4 | "sourceMap": false, 5 | "inlineSourceMap": true, 6 | "inlineSources": true, 7 | "rootDir": "./src", 8 | "outDir": "./build" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/dynamodb-data-mapper-js/3ffe4bfb9187cf21bf87f72212526fa72172e555/src/index.ts -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": [ 5 | "dom", 6 | "es5" 7 | ], 8 | "module": "commonjs", 9 | "strict": true 10 | }, 11 | "typedocOptions": { 12 | "mode": "file", 13 | "out": "./docs", 14 | "excludeNotExported": true, 15 | "excludePrivate": true, 16 | "hideGenerator": true, 17 | "name": "Amazon DynamoDB DataMapper For JavaScript" 18 | } 19 | } 20 | --------------------------------------------------------------------------------