- Notifications
You must be signed in to change notification settings - Fork 5.8k
Add DDB Item Tracker app for AWS SDK for JS (v3) #2742
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits Select commit Hold shift + click to select a range
73e1907 add ddb item tracker
brmur 4e3ae3f add ddb item tracker
brmur 60026a7 add ddb item tracker
brmur aad4a55 add ddb item tracker
brmur 92dcdb2 add ddb item tracker
brmur 1429496 add ddb item tracker
brmur 9fff9aa add ddb item tracker
brmur b58cf22 add ddb item tracker
brmur File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
748 changes: 748 additions & 0 deletions 748 javascriptv3/example_code/cross-services/ddb-item-tracker/README.md
Large diffs are not rendered by default.
Oops, something went wrong.
256 changes: 256 additions & 0 deletions 256 javascriptv3/example_code/cross-services/ddb-item-tracker/app.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,256 @@ | ||
| /* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| SPDX-License-Identifier: Apache-2.0 | ||
| | ||
| Purpose: | ||
| app.js is the main file in the Amazon DynamoDB Item Tracker example app. | ||
| It interacts with the Express web framework to execute code. | ||
| | ||
| INPUTS: | ||
| - REGION | ||
| | ||
| */ | ||
| // snippet-start:[cross-service.JavaScript.ddb-item-tracker.app] | ||
| import express from 'express'; | ||
| import {v4 as uuidv4} from 'uuid'; | ||
| const port = process.env.PORT || 3000; | ||
| const app = express(); | ||
| import bodyParser from 'body-parser'; | ||
| import path from 'path'; | ||
| import {fileURLToPath} from 'url'; | ||
| const __filename = fileURLToPath(import.meta.url); | ||
| import { UpdateCommand, PutCommand, ScanCommand } from "@aws-sdk/lib-dynamodb"; | ||
| import { ddbDocClient } from "./public/libs/ddbDocClient.js"; | ||
| import { SendEmailCommand } from "@aws-sdk/client-ses"; | ||
| import { sesClient, REGION } from "./public/libs/sesClient.js"; | ||
| import { s3Client } from "./public/libs/s3Client.js"; | ||
| import { PutObjectCommand } from "@aws-sdk/client-s3"; | ||
| | ||
| // Setting path for public directory | ||
| const __dirname = path.dirname(__filename); | ||
| const static_path = path.join(__dirname, "/public"); | ||
| app.use(express.static(static_path)); | ||
| app.use(express.urlencoded({ extended: true })); | ||
| | ||
| // Variables to update. | ||
| const tableName = 'TABLE_NAME'; | ||
| const bucketName = 'BUCKET_NAME'; | ||
| const senderEmail = 'SENDER_EMAIL_ADDRESS' | ||
| | ||
| app.use(bodyParser.json()); | ||
| | ||
| app.post("/add", (req, res) => { | ||
| const body = req.body; | ||
| const id = uuidv4(); | ||
| const d = new Date(); | ||
| const month = d.getMonth() + 1; | ||
| const day = d.getDate(); | ||
| const todaydate = d.getFullYear() + '/' + | ||
| (month < 10 ? '0' : '') + month + '/' + | ||
| (day < 10 ? '0' : '') + day; | ||
| const params = { | ||
| TableName: tableName, | ||
| Item: { | ||
| id: id, | ||
| guide: body.guide, | ||
| description: body.description, | ||
| status: body.status, | ||
| date: todaydate | ||
| } | ||
| }; | ||
| const run = async () => { | ||
| try { | ||
| const data = await ddbDocClient.send(new PutCommand(params)); | ||
| console.log("Added item:", JSON.stringify(data, null, 2)); | ||
| console.log(data); | ||
| res.contentType = "application/json"; | ||
| res.send(data); | ||
| } catch (err) { | ||
| console.error("Unable to add item. Error JSON:", JSON.stringify(err, null, 2)); | ||
| } | ||
| }; | ||
| run(); | ||
| }); | ||
| | ||
| | ||
| app.post("/request", (req, res) => { | ||
| var params = { | ||
| TableName: tableName | ||
| }; | ||
| | ||
| const run = async () => { | ||
| try { | ||
| const data = await ddbDocClient.send(new ScanCommand(params)); | ||
| console.log('data', data); | ||
| res.send(data); | ||
| } catch (err) { | ||
| console.log("Error", err); | ||
| } | ||
| } | ||
| run(); | ||
| }); | ||
| | ||
| app.post("/changewi", (req, res) => { | ||
| const body = req.body; | ||
| console.log(req.body) | ||
| var params = { | ||
| TableName: tableName, | ||
| Key: { | ||
| "id": req.body.id | ||
| }, | ||
| UpdateExpression: 'set #description=:d, #status=:s', | ||
| ExpressionAttributeValues: { | ||
| ':d': req.body.description, | ||
| ':s': req.body.status | ||
| }, | ||
| ExpressionAttributeNames: { | ||
| '#description': "description", | ||
| '#status': "status" | ||
| } | ||
| }; | ||
| const run = async () => { | ||
| try { | ||
| const data = await ddbDocClient.send(new UpdateCommand(params)); | ||
| res.contentType = "application/json"; | ||
| res.send(data); | ||
| } catch (err) { | ||
| console.error(err); | ||
| } | ||
| }; | ||
| run(); | ||
| }); | ||
| | ||
| app.post("/uploadCSV", (req, res) => { | ||
| const d = new Date(); | ||
| const month = d.getMonth() + 1; | ||
| const day = d.getDate(); | ||
| const hour = d.getHours(); | ||
| const minute = d.getMinutes(); | ||
| const todaydate = d.getFullYear() + | ||
| (month < 10 ? '0' : '') + month + | ||
| (day < 10 ? '0' : '') + day + hour + minute; | ||
| const uploadParams = { | ||
| Bucket: bucketName, | ||
| Body: req.body.csv, | ||
| Key: "Items_" + todaydate +".csv" | ||
| }; | ||
| const run = async () => { | ||
| try { | ||
| const data = await s3Client.send(new PutObjectCommand(uploadParams)); | ||
| const linkToCSV = | ||
| "https://s3.console.aws.amazon.com/s3/buckets/" + | ||
| uploadParams.Bucket + | ||
| "?region=" + | ||
| REGION + | ||
| "&tab=objects" | ||
| console.log("Success. Report uploaded to " + linkToCSV + "."); | ||
| console.log('Email', req.body.email) | ||
| try{ | ||
| const emailParams = { | ||
| Destination: { | ||
| /* required */ | ||
| CcAddresses: [ | ||
| /* more items */ | ||
| ], | ||
| ToAddresses: [ | ||
| req.body.email, //RECEIVER_ADDRESS | ||
| /* more To-email addresses */ | ||
| ], | ||
| }, | ||
| Message: { | ||
| /* required */ | ||
| Body: { | ||
| /* required */ | ||
| Html: { | ||
| Charset: "UTF-8", | ||
| Data: | ||
| "<h1>Hello!</h1>" + | ||
| "<p> The CSV has been uploaded to <a href=" +linkToCSV + | ||
| ">here.</a></p>" | ||
| }, | ||
| }, | ||
| Subject: { | ||
| Charset: "UTF-8", | ||
| Data: "Report ready.", | ||
| }, | ||
| }, | ||
| Source: "brmur@amazon.com", | ||
| ReplyToAddresses: [ | ||
| /* more items */ | ||
| ], | ||
| }; | ||
| const data = await sesClient.send(new SendEmailCommand(emailParams)); | ||
| console.log("Success. Email sent.", data); | ||
| res.contentType = "application/json"; | ||
| res.send(data); | ||
| } catch (err) { | ||
| console.log("Error", err); | ||
| }; | ||
| } catch (err) { | ||
| console.log("Error", err); | ||
| } | ||
| } | ||
| run(); | ||
| }); | ||
| | ||
| app.post("/report", (req, res) => { | ||
| // Helper function to send an email to user. | ||
| // Set the parameters | ||
| console.log('This is the email address: ', req.body.email) | ||
| const params = { | ||
| Destination: { | ||
| /* required */ | ||
| CcAddresses: [ | ||
| /* more items */ | ||
| ], | ||
| ToAddresses: [ | ||
| req.body.email, //RECEIVER_ADDRESS | ||
| /* more To-email addresses */ | ||
| ], | ||
| }, | ||
| Message: { | ||
| /* required */ | ||
| Body: { | ||
| /* required */ | ||
| Html: { | ||
| Charset: "UTF-8", | ||
| Data: | ||
| "<h1>Hello!</h1>" + | ||
| "<p> The Amazon DynamoDB table " + | ||
| tableName + | ||
| " has been updated with PPE information <a href='https://" + | ||
| REGION + | ||
| ".console.aws.amazon.com/dynamodb/home?region=" + | ||
| REGION + | ||
| "#item-explorer?table=" + | ||
| tableName + | ||
| "'>here.</a></p>" | ||
| }, | ||
| }, | ||
| Subject: { | ||
| Charset: "UTF-8", | ||
| Data: "PPE image report ready.", | ||
| }, | ||
| }, | ||
| Source: senderEmail, | ||
| ReplyToAddresses: [ | ||
| /* more items */ | ||
| ], | ||
| }; | ||
| const run = async () => { | ||
| try { | ||
| const data = await sesClient.send(new SendEmailCommand(params)); | ||
| console.log("Success. Email sent.", data); | ||
| res.contentType = "application/json"; | ||
| res.send(data); | ||
| } catch (err) { | ||
| console.log("Error", err); | ||
| } | ||
| }; | ||
| run(); | ||
| }); | ||
| | ||
| | ||
| app.listen(port, () => { | ||
| console.log(`Listening on port ${port}`); | ||
| }) | ||
| // snippet-end:[cross-service.JavaScript.ddb-item-tracker.app] |
Binary file added BIN +31.7 KB javascriptv3/example_code/cross-services/ddb-item-tracker/images/additems.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added BIN +113 KB ...e_code/cross-services/ddb-item-tracker/images/cloud_formation_resources_tab.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added BIN +42.2 KB .../example_code/cross-services/ddb-item-tracker/images/cloud_formation_stacks.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added BIN +118 KB javascriptv3/example_code/cross-services/ddb-item-tracker/images/homepage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added BIN +85.3 KB javascriptv3/example_code/cross-services/ddb-item-tracker/images/homepage2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added BIN +66.6 KB ...iptv3/example_code/cross-services/ddb-item-tracker/images/identity_pool_ids.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added BIN +7.29 KB ...3/example_code/cross-services/ddb-item-tracker/images/item_tracker_projects.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added BIN +21.5 KB javascriptv3/example_code/cross-services/ddb-item-tracker/images/pic1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added BIN +13.1 KB javascriptv3/example_code/cross-services/ddb-item-tracker/images/pic10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added BIN +16.7 KB javascriptv3/example_code/cross-services/ddb-item-tracker/images/pic11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added BIN +110 KB javascriptv3/example_code/cross-services/ddb-item-tracker/images/pic12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added BIN +25 KB javascriptv3/example_code/cross-services/ddb-item-tracker/images/pic13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added BIN +15.5 KB javascriptv3/example_code/cross-services/ddb-item-tracker/images/pic14.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added BIN +19.6 KB javascriptv3/example_code/cross-services/ddb-item-tracker/images/pic1a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added BIN +8.66 KB javascriptv3/example_code/cross-services/ddb-item-tracker/images/pic2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added BIN +106 KB javascriptv3/example_code/cross-services/ddb-item-tracker/images/pic3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added BIN +89.7 KB javascriptv3/example_code/cross-services/ddb-item-tracker/images/pic3a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added BIN +45.1 KB javascriptv3/example_code/cross-services/ddb-item-tracker/images/pic4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added BIN +53.9 KB javascriptv3/example_code/cross-services/ddb-item-tracker/images/pic5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added BIN +24.9 KB javascriptv3/example_code/cross-services/ddb-item-tracker/images/pic6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added BIN +49.4 KB javascriptv3/example_code/cross-services/ddb-item-tracker/images/pic7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added BIN +10.8 KB javascriptv3/example_code/cross-services/ddb-item-tracker/images/pic8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added BIN +10.9 KB javascriptv3/example_code/cross-services/ddb-item-tracker/images/pic9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added BIN +11.2 KB javascriptv3/example_code/cross-services/ddb-item-tracker/images/project.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added BIN +14.6 KB javascriptv3/example_code/cross-services/ddb-item-tracker/images/project1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added BIN +24.9 KB javascriptv3/example_code/cross-services/ddb-item-tracker/images/project2.png
Oops, something went wrong.
Binary file added BIN +63.6 KB javascriptv3/example_code/cross-services/ddb-item-tracker/images/worktable.png
Oops, something went wrong.
25 changes: 25 additions & 0 deletions 25 javascriptv3/example_code/cross-services/ddb-item-tracker/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| { | ||
| "name": "Amazon DynamoDB Item Tracker cross-service example (AWS SDK for JavaScript)", | ||
| "version": "1.0.0", | ||
| "description": "", | ||
| "main": "app.js", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1", | ||
| "start": "node server.js" | ||
| }, | ||
| "dependencies": { | ||
| "body-parser": "^1.19.0", | ||
| "express": "^4.17.1", | ||
| "node-uuid": "^1.4.8", | ||
| "uuid": "^8.1.0", | ||
| "@aws-sdk/client-dynamodb": "^3.32.0", | ||
| "@aws-sdk/lib-dynamodb": "^3.32.0", | ||
| "@aws-sdk/client-ses": "^3.32.0", | ||
| "@aws-sdk/client-cognito-identity" : "^3.32.0", | ||
| "@aws-sdk/credential-provider-cognito-identity" : "^3.32.0", | ||
| "jest": "^26.6.3" | ||
| }, | ||
| "author": "", | ||
| "license": "Apache 2.0", | ||
| "type": "module" | ||
| } |
80 changes: 80 additions & 0 deletions 80 javascriptv3/example_code/cross-services/ddb-item-tracker/public/add.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| <!--Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| SPDX-License-Identifier: Apache-2.0 | ||
| | ||
| Purpose: | ||
| add.html is a HTML template for an Amazon DynamoDB item tracker app.--> | ||
| | ||
| <!--snippet-start:[cross-service.HTML.ddb-item-tracker.add]--> | ||
| | ||
| <html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> | ||
| <html> | ||
| <head> | ||
| <title>Modify Items</title> | ||
| <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script> | ||
| <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script> | ||
| <script src="https://cdn.datatables.net/v/dt/dt-1.10.20/datatables.min.js"></script> | ||
| <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"></script> | ||
| <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" ></script> | ||
| <script src="../js/items.js" ></script> | ||
| | ||
| <!-- CSS files --> | ||
| <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" > | ||
| <link rel="stylesheet" href="https://cdn.datatables.net/v/dt/dt-1.10.20/datatables.min.css"/> | ||
| <link rel="stylesheet" href="/css/styles.css" /> | ||
| <link rel="stylesheet" href="/css/col.css" /> | ||
| <link rel="stylesheet" href="/css/button.css" /> | ||
| <link rel="stylesheet" href="/css/common.css" /> | ||
| | ||
| </head> | ||
| <body> | ||
| <nav class="navbar navbar-expand-lg navbar-light bg-light"> | ||
| <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation"> | ||
| <span class="navbar-toggler-icon"></span> | ||
| </button> | ||
| <div class="collapse navbar-collapse" id="navbarNavAltMarkup"> | ||
| <div class="navbar-nav"> | ||
| <a class="nav-item nav-link active" href="index.html">Welcome</a> | ||
| <a class="nav-item nav-link" href="add.html">Add items <span class="sr-only">(current)</span></a> | ||
| <a class="nav-item nav-link" href="items.html">Get items</a> | ||
| </div> | ||
| </div> | ||
| </nav> | ||
| <div class="container" id="newItem"> | ||
| <h3>Welcome to AWS DynamoDB Item Tracker</h3> | ||
| <p>Add new items by filling in this table and clicking <i>Create Item</i></p> | ||
| | ||
| <div class="row"> | ||
| <div class="col-lg-8 mx-auto"> | ||
| | ||
| <form> | ||
| <div class="control-group"> | ||
| <div class="form-group floating-label-form-group controls mb-0 pb-2"> | ||
| <label>Guide</label> | ||
| <input class="form-control" id="guide" type="guide" placeholder="AWS Guide/AWS API" required="required" data-validation-required-message="Please enter the AWS Guide."> | ||
| <p class="help-block text-danger"></p> | ||
| </div> | ||
| </div> | ||
| <div class="control-group"> | ||
| <div class="form-group floating-label-form-group controls mb-0 pb-2"> | ||
| <label>Description</label> | ||
| <textarea class="form-control" id="description" rows="5" placeholder="Description" required="required" data-validation-required-message="Please enter a description."></textarea> | ||
| <p class="help-block text-danger"></p> | ||
| </div> | ||
| </div> | ||
| <div class="control-group"> | ||
| <div class="form-group floating-label-form-group controls mb-0 pb-2"> | ||
| <label>Status</label> | ||
| <textarea class="form-control" id="status" rows="5" placeholder="Status" required="required" data-validation-required-message="Please enter the status."></textarea> | ||
| <p class="help-block text-danger"></p> | ||
| </div> | ||
| </div> | ||
| <br> | ||
| </form> | ||
| <button onclick="addItem()">Create Item</button> | ||
| | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </body> | ||
| </html> | ||
| <!--snippet-end:[cross-service.HTML.ddb-item-tracker.add]--> | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.