|
| 1 | +# NERDS Stack - NodeJS Express React DynamoDB and SAM |
| 2 | + |
| 3 | +## Enhanced example |
| 4 | + |
| 5 | +This example includes additional DynamoDB functionality, you will work with a table with a Partition Key and a Sort Key that will allow you to run more elegant queries. |
| 6 | + |
| 7 | +We will be working with an ehnahced task example where you will retrieve the tasks for a given user, and optional by priority. |
| 8 | + |
| 9 | +This project contains source code and supporting files for a sample application running on Express and NodeJS with Amazon DynamoDB local. It includes the following files and folders: |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +- `src` - Code for the express application and the APIs. |
| 14 | +- `package.json` - Includes all the scripts to work on this project and the pre-requisites. |
| 15 | + |
| 16 | +This application will create a couple of API methods to add and list the ToDos. |
| 17 | + |
| 18 | +## Requirements |
| 19 | + |
| 20 | +- [Docker Desktop](https://www.docker.com/products/docker-desktop/) |
| 21 | +- [DynamoDB Local Docker Image](https://hub.docker.com/r/amazon/dynamodb-local) |
| 22 | +- [Node v22.3.0](https://nodejs.org/en/blog/release/v22.3.0) |
| 23 | +- [AWS JavaScript SDK v3](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/dynamodb/) |
| 24 | + |
| 25 | +## Usage |
| 26 | + |
| 27 | +To start working with this project you need to first run `npm install`. |
| 28 | + |
| 29 | +### Running the tool in conjuntion with the front end. |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +### DynamoDB local |
| 34 | + |
| 35 | +This application will store all the information in an Amazon DynamoDB local instance, you will experience all the benefits from Amazon DynamoDB in your workstation. |
| 36 | + |
| 37 | +Execute the command `npm run start-db`. If this is the first time running the command your output should look like the one below. Docker will obtain the latest version for you! |
| 38 | + |
| 39 | +```shell |
| 40 | +❯ npm run start-db |
| 41 | + |
| 42 | +> basic-backend@0.0.1 start-db |
| 43 | +> echo "CID=$(docker run -p $npm_package_config_ddbport:$npm_package_config_ddbport -d amazon/dynamodb-local -jar DynamoDBLocal.jar -sharedDb)" > .ddb_cid |
| 44 | + |
| 45 | +Unable to find image 'amazon/dynamodb-local:latest' locally |
| 46 | +latest: Pulling from amazon/dynamodb-local |
| 47 | +decbb28a26fa: Pull complete |
| 48 | +4b968a8d55bd: Pull complete |
| 49 | +4f4fb700ef54: Pull complete |
| 50 | +16d147f186fa: Pull complete |
| 51 | +Digest: sha256:d7ebddeb60fa418bcda218a6c6a402a58441b2a20d54c9cb1d85fd5194341753 |
| 52 | +Status: Downloaded newer image for amazon/dynamodb-local:latest |
| 53 | +``` |
| 54 | + |
| 55 | +If you want to stop DynamoDB local execution you just need to run the command `npm run stop-db`. The long number at the end represents your docker container ID. |
| 56 | + |
| 57 | +```shell |
| 58 | +❯ npm run stop-db |
| 59 | + |
| 60 | +> basic-backend@0.0.1 stop-db |
| 61 | +> source .ddb_cid && docker stop $CID && rm .ddb_cid |
| 62 | + |
| 63 | +904e9a494f0f09acd027e33d275d3a51a05716f09cc4eace87b778bc3e65e650 |
| 64 | +``` |
| 65 | + |
| 66 | +### Working with DynamoDB tables |
| 67 | + |
| 68 | +This project consists of a To-Do application and you will be storing all the items in a DynamoDB table. First you need to create a `Notes` table, where you will store all your to-dos. |
| 69 | + |
| 70 | +Run the command `npm run create-table`. |
| 71 | + |
| 72 | +```shell |
| 73 | +❯ npm run create-table |
| 74 | + |
| 75 | +> basic-backend@0.0.1 create-table |
| 76 | +> aws dynamodb create-table --table-name $npm_package_config_ddbtable --attribute-definitions AttributeName=PK,AttributeType=S --key-schema AttributeName=PK,KeyType=HASH --billing-mode PAY_PER_REQUEST --endpoint-url http://$npm_package_config_ddbhost:$npm_package_config_ddbport --no-cli-page |
| 77 | + |
| 78 | +{ |
| 79 | + "TableDescription": { |
| 80 | + "AttributeDefinitions": [ |
| 81 | + { |
| 82 | + "AttributeName": "PK", |
| 83 | + "AttributeType": "S" |
| 84 | + } |
| 85 | + ], |
| 86 | + "TableName": "Notes", |
| 87 | + "KeySchema": [ |
| 88 | + { |
| 89 | + "AttributeName": "PK", |
| 90 | + "KeyType": "HASH" |
| 91 | + } |
| 92 | + ], |
| 93 | + "TableStatus": "ACTIVE", |
| 94 | + "CreationDateTime": "2024-07-29T10:40:09.344000-04:00", |
| 95 | + "ProvisionedThroughput": { |
| 96 | + "LastIncreaseDateTime": "1969-12-31T19:00:00-05:00", |
| 97 | + "LastDecreaseDateTime": "1969-12-31T19:00:00-05:00", |
| 98 | + "NumberOfDecreasesToday": 0, |
| 99 | + "ReadCapacityUnits": 0, |
| 100 | + "WriteCapacityUnits": 0 |
| 101 | + }, |
| 102 | + "TableSizeBytes": 0, |
| 103 | + "ItemCount": 0, |
| 104 | + "TableArn": "arn:aws:dynamodb:ddblocal:000000000000:table/Notes", |
| 105 | + "BillingModeSummary": { |
| 106 | + "BillingMode": "PAY_PER_REQUEST", |
| 107 | + "LastUpdateToPayPerRequestDateTime": "2024-07-29T10:40:09.344000-04:00" |
| 108 | + }, |
| 109 | + "DeletionProtectionEnabled": false |
| 110 | + } |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +If you run the command twice by mistake, the second time you will get an error saying that you already have a table called `Notes`. |
| 115 | + |
| 116 | +```shell |
| 117 | +❯ npm run create-table |
| 118 | + |
| 119 | +> basic-backend@0.0.1 create-table |
| 120 | +> aws dynamodb create-table --table-name $npm_package_config_ddbtable --attribute-definitions AttributeName=PK,AttributeType=S --key-schema AttributeName=PK,KeyType=HASH --billing-mode PAY_PER_REQUEST --endpoint-url http://$npm_package_config_ddbhost:$npm_package_config_ddbport --no-cli-page |
| 121 | + |
| 122 | + |
| 123 | +An error occurred (ResourceInUseException) when calling the CreateTable operation: Cannot create preexisting table |
| 124 | +``` |
| 125 | + |
| 126 | +### List all the existing tables |
| 127 | + |
| 128 | +To idenfity which tables you have created execute the command `npm run show-tables`. |
| 129 | + |
| 130 | +```shell |
| 131 | +❯ npm run show-tables |
| 132 | + |
| 133 | +> basic-backend@0.0.1 show-tables |
| 134 | +> aws dynamodb list-tables --endpoint-url http://$npm_package_config_ddbhost:$npm_package_config_ddbport |
| 135 | + |
| 136 | +{ |
| 137 | + "TableNames": [ |
| 138 | + "Notes" |
| 139 | + ] |
| 140 | +} |
| 141 | +``` |
| 142 | + |
| 143 | +### Retrieve all the items from one table |
| 144 | + |
| 145 | +To return all the elements from the `Notes` table you can run the command `npm run scan-table`. However if your table is empty you will get this output. |
| 146 | + |
| 147 | +```shell |
| 148 | +❯ npm run scan-table |
| 149 | + |
| 150 | +> basic-backend@0.0.1 scan-table |
| 151 | +> aws dynamodb scan --table-name $npm_package_config_ddbtable --endpoint-url http://$npm_package_config_ddbhost:$npm_package_config_ddbport |
| 152 | + |
| 153 | +{ |
| 154 | + "Items": [], |
| 155 | + "Count": 0, |
| 156 | + "ScannedCount": 0, |
| 157 | + "ConsumedCapacity": null |
| 158 | +} |
| 159 | +``` |
| 160 | + |
| 161 | +### Initialize the backend |
| 162 | + |
| 163 | +Finally to work with the sample express application, included in this project you will need to execute the instruction `npm run start-backend`, this process will start the express application that will be listening at port 3000. |
| 164 | + |
| 165 | +Tip: execute this command in a new terminal |
| 166 | + |
| 167 | +```shell |
| 168 | +❯ npm run start-backend |
| 169 | + |
| 170 | +> basic-backend@0.0.1 start-backend |
| 171 | +> node src/server.js |
| 172 | + |
| 173 | +Server is running on port 3000 |
| 174 | +``` |
| 175 | + |
| 176 | +## Contributing |
| 177 | + |
| 178 | +As soon as you clone this repository you need to run `pre-commit install` and before pushing any commit you need to run `detect-secrets scan > .secrets.baseline`. Failing to run this command will result in an invalid commit and it will be rejected. |
0 commit comments