AWS DynamoDB Mapper Typescript
It is very painful to map the Dynamo DB data as per the parameters that is standardized by the AWS.
And that grows to be more complex when there is a scenario of complex structure of column data like a List, Object, etc.
Doing that with plain code is really hard and that way maintenance goes very hauntingly weird and the best part about AWS is that they do provide us a kind of library/ utility to overcome that.
And this article is going to cover most of the integration with Dynamo DB using typescript and mapper provided by AWSLabs - dynamodb-data-mapper-js.
Prerequisites
- Some knowledge about the AWS Dynamo DB.
- Some knowledge about the AWS IAM and mainly the credentials setup part.
- Good knowledge and hands on for working with NodeJS and Typescript.
- Some urge to do cleaner code rather than complex stuff.
Project(boilerplate code) structure
The Code repository is located at — neuw/aws-dynamodb-mapper-ts (github.com)
Insights on the code
First of all create an IAM user and create the AWS keyId and AWS secret with Full DynamoDB access.
Replace the region, ACCESS_KEY_ID and SECRET_ACCESS_KEY for the appropriate environment default environment is local add that to the app.config.json in the place as below:-
The main file is the Runner.ts and that will be main point to run the code. If you see the package.json, it has the main scripts also can be seen below in the gist.
It calls the Runner file only but for various scenarios there are different scripts (commands starting with ts:* are using ts-node and calling the typescript file directly):-
- ts:testUserMapper:createTable is for the creating table
- ts:testUserMapper:createAndSave is for the create And Save entry
- ts:testUserMapper:get is for getting the entry
- ts:testUserMapper:delete is for deleting the entry
- ts:testUserMapper:update is for updating the entry
Same the scripts with js:* are for making the same calls via transpiled Javascript file. That can be done using the transpile script command.
Replace the user-id’s values where ever required and applicable in the commands.
The ts:testUserMapper:createTable creates the table while appending the prefix that was configured in the for example test_dev_users in the example as test_dev_ was set as prefix in the AwsClientsManager.
After running the command the table will show up as below:-
Similarly you can call the other commands and get to understand the most logics around the same. Most of the code is quite clearly done, if any issues, please feel free to contact over Linkedin or reply over the article it self.