Skip to content

Commit 268ba2d

Browse files
brmurford-at-awsirenepsmith
authored
Update scenario readme (#3835)
* updates to dynamodb_basics.js scenario * updates to dynamodb_basics.js scenario * updates to dynamodb_basics.js scenario * updates to dynamodb_basics.js scenario * updates to dynamodb_basics.js scenario * updates to dynamodb_basics.js scenario * updates to dynamodb_basics.js scenario * updates to dynamodb_basics.js scenario * updates to dynamodb_basics.js scenario Co-authored-by: ford prior <[email protected]> Co-authored-by: Irene Smith <[email protected]>
1 parent 92df9da commit 268ba2d

File tree

9 files changed

+103610
-125
lines changed

9 files changed

+103610
-125
lines changed

javascriptv3/example_code/README.md

+38-21
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,52 @@
1-
# AWS SDK for JavaScript v3 examples
21

3-
These are examples for the AWS SDK for JavaScript version 3 (v3) public documentation.
2+
# AWS SDK for JavaScript (v3) code examples
3+
## Overview
4+
The code examples in this topic show you how to use the AWS SDK for JavaScript (v3) with AWS.
45

5-
## Prerequisites
6-
To build and run these examples, you need to:
6+
The AWS SDK for JavaScript (v3) provides a JavaScript API for AWS infrastructure services. Using the SDK, you can build applications on top of Amazon S3, Amazon EC2, Amazon DynamoDB, and more.
77

8-
* Set up the project environment to run these TypeScript examples with Node.js, and install the required AWS SDK for JavaScript and third-party modules. Follow the instructions in the README.md in the folder with the examples you're running; for example, [instructions for Amazon S3 examples](https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/javascriptv3/example_code/s3/README.md).
9-
* Create a shared configurations file with your user credentials. See the [AWS SDK for JavaScript v3 Developer Guide](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/loading-node-credentials-shared.html) for more information.
8+
## Types of code examples
9+
* **Single-service actions** - Code examples that show you how to call individual service functions.
1010

11-
## Running the examples
12-
Examples are written for either execution in a browser script or in Node.js, usually depending on the use case for each. The two scenarios typically differ in how you supply credentials to the code. For information on the differences, see the [AWS SDK for JavaScript v3 Developer Guide](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/setting-credentials.html).
11+
* **Single-service scenarios** - Code examples that show you how to accomplish a specific task by calling multiple functions within the same service.
1312

14-
The Node.js examples are all run from the command line.
13+
* **Cross-service examples** - Sample applications that work across multiple AWS services.
1514

16-
## Unit tests
17-
[Unit tests](./tests) are provided for many examples, using the [Jest](https://jestjs.io/) framework.
15+
### Find code examples
16+
Single-service actions and scenarios are organized by AWS service in this folder. A README in each folder lists and describes how to run the examples.
1817

19-
To run tests, navigate to the **tests** folder.
18+
Cross-service examples are located in the [*cross-services folder*](./cross-services). A README in each folder describes how to run the example.
2019

21-
For example, to run tests on the S3 folder, enter the following sequence of commands at the command prompt:
20+
## ⚠️ Important
21+
* Running this code might result in charges to your AWS account.
22+
* Running the tests might result in charges to your AWS account.
23+
* We recommend that you grant your code the least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege).
24+
* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services).
2225

26+
27+
### Prerequisites
28+
* Latest stable version of Node.js.
29+
* A shared configuration file with your user credentials. For more information, see the [AWS SDK for JavaScript (v3) Developer Guide](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/loading-node-credentials-shared.html).
30+
31+
## Tests
32+
**Note**: Running the tests might result in charges to your AWS account.
33+
34+
1. Where tests are provided, navigate to the 'tests' folder located in the root of the service folder.
35+
36+
**Note**: In some cases scenarios and cross-service examples have their own test folder, so navigate to that instead.
37+
38+
2. Run the following:
2339
```
24-
npm install node -g
25-
cd javascriptv3/example_code/s3/tests
40+
cd javascriptv3/example_code/[service folder name]
2641
npm install
2742
npm test
2843
```
29-
## Resources
30-
* [AWS SDK for JavaScript v3](https://github.com/aws/aws-sdk-js-v3)
31-
* [AWS SDK for JavaScript v3 Developer Guide](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/)
32-
* [AWS SDK for JavaScript v3 API Reference](http://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/index.html)
3344

34-
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
35-
SPDX-License-Identifier: Apache-2.0
45+
## Additional resources
46+
* [AWS SDK for JavaScript (v3)](https://github.com/aws/aws-sdk-js-v3)
47+
* [AWS SDK for JavaScript (v3) Developer Guide](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/)
48+
* [AWS SDK for JavaScript (v3) API Reference](http://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/index.html)
49+
50+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0
51+
52+
+40-38
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
1-
# Amazon DynamoDB JavaScript SDK v3 code examples
1+
# Amazon DynamoDB code examples for AWS SDK for JavaScript (v3)
22
The code examples in this directory demonstrate how to work with Amazon DynamoDB using the AWS SDK for JavaScript version 3 (v3).
33

4-
Amazon DynamoDB is a key-value and document database that delivers single-digit millisecond performance at any scale. It's a fully managed, multiregion, multimaster, durable database with built-in security, backup and restore, and in-memory caching for internet-scale applications.
4+
Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability. You can use Amazon DynamoDB to create a database table that can store and retrieve any amount of data, and serve any level of request traffic. Amazon DynamoDB automatically spreads the data and traffic for the table over a sufficient number of servers to handle the request capacity specified by the customer and the amount of data stored, while maintaining consistent and fast performance.
5+
###Table of contents
6+
- [Important information](#-important-information)
7+
- [Code examples](#code-examples)
8+
- [Single actions](#single-actions)
9+
- [Scenarios](#scenarios)
10+
- [Run the examples](#run-the-examples)
11+
- [Prerequisites](#prerequisites)
12+
- [Steps](#steps)
13+
- [Unit tests](#unit-tests)
14+
- [Additional resources](#-A)
515

6-
## Code examples
7-
This is a workspace where you can find the following AWS SDK for JavaScript version 3 (v3) Amazon DynamoDB examples:
16+
## ⚠️ Important information
17+
* Running the tests might result in charges to your AWS account.
18+
* We recommend that you grant your code the least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege).
19+
* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services).
820

9-
### Scenario examples
21+
## The code examples
1022

11-
- [DynamoDB_basics](scenarios/dynamodb_basics/src/dynamodb_basics.js)
12-
13-
#### Scenario examples using PartiQL
14-
15-
- [PartiQL_basics](scenarios/partiQL_basics/src/partiQL_basics.js)
16-
- [PartiQL_batch_basics](scenarios/partiQL_basics/src/partiQL_batch_basics.js)
17-
18-
### API Examples - DynamoDB
23+
### Single actions
24+
Code excerpts that show you how to call individual service functions.
1925

2026
- [Create a DynamoDB table](src/ddb_createtable.js) (CreateTableCommand)
2127
- [Create a DynamoDB table - TV example](src/QueryExample/ddb_createtable_tv.js)
@@ -33,8 +39,6 @@ This is a workspace where you can find the following AWS SDK for JavaScript vers
3339
- [Update a DynamoDB table using Document Client](src/movies/updateItem.js) (UpdateCommand)
3440
- [Write batch items using Document Client](src/movies/writeData.js) (BatchGetWriteCommand)
3541
- [Write batch items - TV example](src/QueryExample/ddb_batchwriteitem_tv.js)
36-
37-
#### API Examples - DynamoDB using PartiQL
3842
- [Add an item to a table using PartiQL](src/partiQL_examples/src/partiql_putItem.js) (ExecuteStatementCommand)
3943
- [Add items to a table by batch using PartiQL](src/partiQL_examples/src/partiql_batch_putItems.js) (BatchExecuteStatementCommand)
4044
- [Delete an item from a table using PartiQL](src/partiQL_examples/src/partiql_deleteItem.js) (ExecuteStatementCommand)
@@ -44,37 +48,35 @@ This is a workspace where you can find the following AWS SDK for JavaScript vers
4448
- [Update an item in a table using PartiQL](src/partiQL_examples/src/partiql_updateItem.js) (ExecuteStatementCommand)
4549
- [Update items in a table by batch using PartiQL](src/partiQL_examples/src/partiql_batch_updateItems.js) (BatchExecuteStatementCommand)
4650

47-
**Note**: All code examples are written in ECMAscript 6 (ES6). For guidelines on converting to CommonJS, see
48-
[JavaScript ES6/CommonJS syntax](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/sdk-examples-javascript-syntax.html).
4951

50-
## Getting started
52+
### Scenarios
53+
Code examples that show you how to accomplish a specific task by calling multiple functions within the same service.
54+
55+
- [DynamoDB basics](scenarios/dynamodb_basics/src/dynamodb_basics.js)
56+
- [PartiQL basics](scenarios/partiQL_basics/src/partiQL_basics.js)
57+
- [PartiQL batch_basics](scenarios/partiQL_basics/src/partiQL_batch_basics.js)
58+
59+
## Run the examples
5160

52-
1. Clone the [AWS Code Samples repo](https://github.com/awsdocs/aws-doc-sdk-examples) to your local environment.
53-
See [the Github documentation](https://docs.github.com/en/github/creating-cloning-and-archiving-repositories/cloning-a-repository) for
54-
instructions.
61+
### Prerequisites
5562

56-
2. Install the dependencies listed in the package.json.
63+
- [Set up AWS SDK for JavaScript](../README.md#prerequisites)
64+
65+
### Instructions
66+
67+
1. Install the dependencies.
5768

58-
**Note**: These include the client modules for the AWS services required in these example,
59-
which are *@aws-sdk/client-dynamodb*, *@aws-sdk/lib-dynamodb*.
6069
```
61-
npm install node -g
6270
cd javascriptv3/example_code/dynamodb
6371
npm install
6472
```
65-
3. In your text editor, update user variables specified in the ```Inputs``` section of the sample file.
66-
67-
4. Run sample code:
68-
```
69-
cd src
70-
node [example name].js // For example, node ddb_batchgetitem.js
71-
```
73+
2. Follow the instructions at the top of the example.
7274

73-
## Unit tests
74-
For more information see, the [README](../README.rst).
75+
## Tests
76+
For more information, see the [README](../README.md).
7577

76-
## Resources
77-
- [AWS SDK for JavaScript v3](https://github.com/aws/aws-sdk-js-v3) is available.
78-
- [AWS SDK for JavaScript v3 Developer Guide](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/dynamodb-examples.html)
79-
- [AWS SDK for JavaScript v3 API Reference Guide - Amazon DynamoDB client module](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-dynamodb/index.html)
78+
## Additional resources
79+
- [AWS SDK for JavaScript (v3)](https://github.com/aws/aws-sdk-js-v3) is available.
80+
- [AWS SDK for JavaScript (v3) Developer Guide](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/dynamodb-examples.html)
81+
- [AWS SDK for JavaScript (v3) API Reference Guide - Amazon DynamoDB client module](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-dynamodb/index.html)
8082

Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
{
2-
"name": "aws-sdk-v3-dynamodb-examples",
2+
"name": "dynamo_basics",
33
"version": "1.0.0",
4+
"scripts": {
5+
"test": "echo \"Error: no test specified\" && exit 1"
6+
},
47
"main": "index.js",
58
"repository": "[email protected]/awsdocs/aws-doc-sdk-examples/tree/master/javascriptv3/example_code/iam.git",
6-
"author": "Brian Murray <[email protected]>, Alex Forsyth <[email protected]>",
9+
"author": "Brian Murray <[email protected]>",
710
"license": "Apache 2.0",
811
"scripts": {
912
"test": "jest"
1013
},
1114
"dependencies": {
1215
"@aws-sdk/client-dynamodb": "^3.32.0",
13-
"@aws-sdk/node-http-handler": "^3.32.0",
1416
"@aws-sdk/lib-dynamodb": "^3.32.0",
15-
"@aws-sdk/types": "^3.32.0",
16-
"node-fetch": "^2.6",
17+
"ramda": "^0.28.0",
1718
"jest": "^26.6.3"
1819
},
19-
"devDependencies": {
20-
"@types/node": "^14.0.23",
21-
"typescript": "^4.0.2"
22-
},
2320
"type": "module"
2421
}

javascriptv3/example_code/dynamodb/scenarios/dynamodb_basics/libs/ddbClient.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Purpose:
88
ddbClient.js is a helper function that creates an Amazon Simple Storage Service (Amazon S3) service client.
99
1010
Inputs (replace in code):
11-
11+
-REGION - replace with your AWS Region.
1212
*/
1313
// snippet-start:[dynamodb.JavaScript.scenario.basics.createclientv3]
1414
// Create the DynamoDB service client module using ES6 syntax.

javascriptv3/example_code/dynamodb/scenarios/dynamodb_basics/libs/ddbDocClient.js

-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ which is available at https://github.com/aws/aws-sdk-js-v3.
66
Purpose:
77
ddbDocClient.js is a helper function that creates an Amazon DynamoDB service document client.
88
9-
INPUTS:
10-
- REGION
11-
129
*/
1310
// snippet-start:[dynamodb.JavaScript.scenario.basics.createdocclientv3]
1411

javascriptv3/example_code/dynamodb/scenarios/dynamodb_basics/package.json

-18
This file was deleted.

javascriptv3/example_code/dynamodb/scenarios/dynamodb_basics/src/dynamodb_basics.js

+19-19
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ ABOUT THIS NODE.JS EXAMPLE: This example works with the AWS SDK for JavaScript v
55
which is available at https://github.com/aws/aws-sdk-js-v3.
66
77
Purpose:
8-
Scenario demonstrating how to:
8+
This scenario demonstrates how to:
99
- Create a table that can hold movie data.
1010
- Write movie data to the table from a sample JSON file.
1111
- Put, get, and update a single movie in the table.
@@ -15,11 +15,14 @@ Scenario demonstrating how to:
1515
- Delete a movie from the table.
1616
- Delete the table.
1717
18+
1819
Running the code:
19-
node dynamodb_basics.js <table_name> <newMovieName> <newMovieYear> <existingMovieName> <existingMovieYear> <newMovieRank> <newMoviePlot>
20+
1. Update the AWS Region in '../libs/ddbClient.js'.
21+
2. Run the following at the command line:
2022
21-
For example, node dynamodb_basics.js myNewTable myMovieName 2022 'This Is the End' 2013 200 'A coder cracks code...'
23+
node dynamodb_basics.js <table_name> <newMovieName> <newMovieYear> <existingMovieName> <existingMovieYear> <newMovieRank> <newMoviePlot>
2224
25+
For example, node dynamodb_basics.js myNewTable myMovieName 2022 'This Is the End' 2013 200 'A coder cracks code...'
2326
2427
// snippet-start:[javascript.dynamodb_scenarios.dynamodb_basics]
2528
*/
@@ -44,7 +47,7 @@ import {
4447

4548
if (process.argv.length < 6) {
4649
console.log(
47-
"Usage: node dynamodb_basics.js <tableNaame> <newMovieName> <newMovieYear> <existingMovieName> <existingMovieYear> <newMovieRank> <newMoviePlot>\n" +
50+
"Usage: node dynamodb_basics.js <tableName> <newMovieName> <newMovieYear> <existingMovieName> <existingMovieYear> <newMovieRank> <newMoviePlot>\n" +
4851
"Example: node dynamodb_basics.js newmoviesbrmur newmoviename 2025 200 'MOVIE PLOT DETAILS'"
4952
);
5053
}
@@ -124,10 +127,8 @@ export const run = async (
124127
await ddbDocClient.send(new PutCommand(params));
125128
console.log("Success - single movie added.");
126129
try {
127-
// Before you run this example, download 'movies.json' from https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.Js.02.html,
128-
// and put it in the same folder as the example.
129130
// Get the movie data parse to convert into a JSON object.
130-
const allMovies = JSON.parse(fs.readFileSync("moviedata.json", "utf8"));
131+
const allMovies = JSON.parse(fs.readFileSync("../../../../../../resources/sample_files/movies.json", "utf8"));
131132
// Split the table into segments of 25.
132133
const dataSegments = R.splitEvery(25, allMovies);
133134
// Loop batch write operation 10 times to upload 250 items.
@@ -161,15 +162,15 @@ export const run = async (
161162
TableName: tableName,
162163
Key: {
163164
title: existingMovieName,
164-
year: existingMovieYear,
165+
year: existingMovieYear
165166
},
166167
// Define expressions for the new or updated attributes.
167168
ProjectionExpression: "#r",
168169
ExpressionAttributeNames: { "#r": "rank" },
169170
UpdateExpression: "set info.plot = :p, info.#r = :r",
170171
ExpressionAttributeValues: {
171172
":p": newMoviePlot,
172-
":r": newMovieRank,
173+
":r": newMovieRank
173174
},
174175
ReturnValues: "ALL_NEW",
175176
};
@@ -217,11 +218,10 @@ export const run = async (
217218
console.log(
218219
"Scan successful. Items with rank of " +
219220
newMovieRank +
220-
" include\n" +
221-
"Year = " +
222-
data.Items[i].year +
223-
" Title = " +
224-
data.Items[i].title
221+
" include:\n" +
222+
data.Items[i].title +
223+
", released in " +
224+
data.Items[i].year
225225
);
226226
}
227227
try {
@@ -248,11 +248,10 @@ export const run = async (
248248
console.log(
249249
"Query successful. Items with rank of " +
250250
newMovieRank +
251-
" include\n" +
252-
"Year = " +
253-
data.Items[i].year +
254-
" Title = " +
255-
data.Items[i].title
251+
" include:\n" +
252+
data.Items[i].title +
253+
", released in " +
254+
data.Items[i].year
256255
);
257256
}
258257
try {
@@ -273,6 +272,7 @@ export const run = async (
273272
const params = {
274273
TableName: tableName,
275274
};
275+
wait(5000);
276276
await ddbDocClient.send(
277277
new DeleteTableCommand(params)
278278
);

javascriptv3/example_code/dynamodb/scenarios/dynamodb_basics/tests/dynamodb_basics.test.js

+12-16
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
1-
var tableName = "dynamodb-scenario-table-name";
2-
var newMovieName = "new-movie-name";
3-
var newMovieYear = 2022;
4-
var existingMovieName = "Rush";
5-
var existingMovieYear = 2013;
6-
var newMovieRank = 111;
7-
var newMoviePlot = "movie-plot";
8-
9-
const expected = "Run successfully";
1+
process.argv.push("node", "dynamodb_basics.js", "myNewTable", "myMovieName", 2022, "This Is the End", 2013, 200, "A coder cracks code...");
2+
console.log('argv', process.argv[4], process.argv[5], process.argv[6], process.argv[7], process.argv[8], process.argv[9], process.argv[10]);
103

114
import "regenerator-runtime/runtime";
125
import { run } from "../src/dynamodb_basics.js";
136
jest.setTimeout(50000);
7+
8+
const expected = "Run successfully";
9+
1410
describe("Test function runs", () => {
1511
it("should successfully run", async () => {
1612
const response = await run(
17-
tableName,
18-
newMovieName,
19-
newMovieYear,
20-
existingMovieName,
21-
existingMovieYear,
22-
newMovieRank,
23-
newMoviePlot
13+
process.argv[4],
14+
process.argv[5],
15+
process.argv[6],
16+
process.argv[7],
17+
process.argv[8],
18+
process.argv[9],
19+
process.argv[10]
2420
);
2521
console.log("Response ", Promise.resolve(response));
2622
expect(response).toEqual(expected);

0 commit comments

Comments
 (0)