How to use the DynamoDB function from aws-sdk

Find comprehensive JavaScript aws-sdk.DynamoDB code examples handpicked from public code repositorys.

aws-sdk.DynamoDB is a class in the AWS SDK for JavaScript that provides methods for interacting with DynamoDB, a fully managed NoSQL database service.

4
5
6
7
8
9
10
11
12
13
14
15
const fs = require('fs');
const { v4: uuidv4 } = require('uuid');
const { metricScope } = require('aws-embedded-metrics');


// Store meetings in a DynamoDB table so attendees can join by meeting title
const ddb = new AWS.DynamoDB();


// Set the AWS SDK Chime endpoint. The Chime endpoint is https://service.chime.aws.amazon.com.
const endpoint = process.env.CHIME_ENDPOINT;
const currentRegion = process.env.REGION;
fork icon450
star icon638
watch icon0

+ 5 other calls in file

23
24
25
26
27
28
29
30
31
32
 * @returns
 */
function init(setRegion) {
    // connect to dynamo if we haven't already
    if (!dynamoDB) {
        dynamoDB = new aws.DynamoDB({
            apiVersion: '2012-08-10',
            region: setRegion
        });
    }
fork icon171
star icon593
watch icon50

+ 8 other calls in file

How does aws-sdk.DynamoDB work?

The aws-sdk.DynamoDB is a JavaScript module that provides an interface to interact with Amazon Web Services (AWS) DynamoDB, a NoSQL database service that can be used to store and retrieve any amount of data, and serves any level of request traffic. It can be used to create tables, retrieve or store items, query or scan tables, and perform various other operations on the database using the AWS SDK for JavaScript.

179
180
181
182
183
184
185
186
187
188
const port = listener.address().port
AWS.config.update({
  endpoint: `http://localhost:${port}`
})
agent.startTransaction('myTransaction')
var ddb = new AWS.DynamoDB({ apiVersion: '2012-08-10' })
var params = {
  TableName: 'fixture-table',
  KeyConditionExpression: 'id = :foo',
  ExpressionAttributeValues: {
fork icon211
star icon536
watch icon296

+ 83 other calls in file

32
33
34
35
36
37
38
39
40
41

## Database operations

The main things you will be doing are interacting with the database in one of the following ways:

[put](https://github.com/dabit3/dynamodb-documentclient-cheat-sheet#put---creating-a-new-item--replacing-an-old-item-with-a-new-item) - [docs](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#put-property) - Creates a new item, or replaces an old item with a new item by delegating to AWS.DynamoDB.putItem().  
[scan](https://github.com/dabit3/dynamodb-documentclient-cheat-sheet#scan---scanning-and-returning-all-of-the-items-in-the-database) - [docs](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#scan-property) - Returns one or more items and item attributes by accessing every item in a table or a secondary index (limit of 1 MB of data).  
[get](https://github.com/dabit3/dynamodb-documentclient-cheat-sheet#get---getting-a-single-item-by-primary-key) - [docs](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#get-property) - Returns a single item given the primary key of that item  
[query](https://github.com/dabit3/dynamodb-documentclient-cheat-sheet#query---access-items-from-a-table-by-primary-key-or-a-secondary-index--gsi) - [docs](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#query-property) - Returns one or more items and item attributes by accessing every item in a table or a secondary index (maximum of 1 MB of data).  
[delete](https://github.com/dabit3/dynamodb-documentclient-cheat-sheet#delete---delete-a-single-item) - [docs](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#delete-property) - Deletes a single item in a table by primary key by delegating to AWS.DynamoDB.deleteItem().  
fork icon61
star icon440
watch icon7

+ 87 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const AWS = require("aws-sdk");
const dynamoDB = new AWS.DynamoDB();

const params = {
  TableName: "my-table",
  Item: {
    id: { S: "my-id" },
    name: { S: "John" },
    age: { N: "30" },
  },
};

dynamoDB.putItem(params, function (err, data) {
  if (err) console.log(err);
  else console.log(data);
});

In this example, we create a new instance of AWS.DynamoDB and then define a set of parameters to create a new item in the my-table table. The Item object contains attribute-value pairs, where the key is the name of the attribute and the value is an object that defines the value of the attribute and its data type. We then call the putItem method with the params object to create the new item in the DynamoDB table.

32
33
34
35
36
37
38
39
40
41
var AWS = require('aws-sdk');
// Set the region 
AWS.config.update({region: 'REGION'});

// Create DynamoDB service object
var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'});

var params = {
  RequestItems: {
    'TABLE_NAME': {
fork icon80
star icon72
watch icon0

+ 7 other calls in file

29
30
31
32
33
34
35
36
37
38

exports.handler = async function (event: any) {
    console.log("request:", JSON.stringify(event, undefined, 2));

    // create AWS SDK clients
    const dynamo = new DynamoDB();

    // update dynamo entry for "path" with hits++
    await dynamo.updateItem({
        TableName: process.env.HITS_TABLE_NAME,
fork icon105
star icon200
watch icon33

13
14
15
16
17
18
19
20
21
22
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

const AWS = require('aws-sdk');

const ddb = new AWS.DynamoDB.DocumentClient({ apiVersion: '2012-08-10', region: process.env.AWS_REGION });

exports.handler = async event => {
  const putParams = {
    TableName: process.env.TABLE_NAME,
fork icon12
star icon80
watch icon6

204
205
206
207
208
209
210
211
212
213

        return Promise.resolve();
}

return ddb.putItem({
        Item: aws.DynamoDB.Converter.marshall(status),
        TableName: "warcannon_progress",
        ReturnConsumedCapacity: "NONE",
        ReturnValues: "NONE"
}).promise();
fork icon31
star icon212
watch icon3

+ 5 other calls in file

14
15
16
17
18
19
20
21
22
23
const { CognitoIdentityServiceProvider, DynamoDB } = require('aws-sdk');
const { getUserPoolId, getVersion } = require('../util');
const { search } = require('../../src/persistence/elasticsearch/searchClient');

const cognito = new CognitoIdentityServiceProvider({ region: 'us-east-1' });
const dynamo = new DynamoDB({ region: process.env.REGION });

/**
 * Creates a cognito user
 *
fork icon35
star icon72
watch icon19

35
36
37
38
39
40
41
42
43
var prefix = process.argv[4];

if (!thisBatchId || !prefix) {
        usage();
}
var dynamoDB = new aws.DynamoDB({
        apiVersion : '2012-08-10',
        region : setRegion
});
fork icon7
star icon4
watch icon10

+ 5 other calls in file

1
2
3
4
5
6
7
8
9
10

const shortid = require('shortid')
const AWS = require('aws-sdk')

const tableName = process.env.DYNAMODB_TABLE
const dynamoDb = new AWS.DynamoDB.DocumentClient()

function buildBatchItems(items) {
  let results = []
  const timestamp = new Date().getTime()
fork icon5
star icon41
watch icon3

11
12
13
14
15
16
17
18
19
 *********************************************************************************************************************/

const aws = require('aws-sdk');

const wafv2 = process.env.METRICS == "true" ? new aws.WAFV2({ region: 'us-east-1', customUserAgent: process.env.SOLUTION_IDENTIFIER }) : new aws.WAFV2({ region: 'us-east-1' });
const dynamodb = process.env.METRICS == "true" ? new aws.DynamoDB({customUserAgent: process.env.SOLUTION_IDENTIFIER}) : new aws.DynamoDB();


const crypto = require("crypto");
fork icon5
star icon12
watch icon15

2
3
4
5
6
7
8
9
10
11
const AWS = require('aws-sdk');
const CodeDeploy = new AWS.CodeDeploy();
const Lambda = new AWS.Lambda();
const CloudWatch = new AWS.CloudWatch();
const CloudFormation = new AWS.CloudFormation();
const DynamoDB = new AWS.DynamoDB();
const S3 = new AWS.S3();
const ConfigService = new AWS.ConfigService();

const stackId = process.env.StackId;
fork icon3
star icon37
watch icon2

18
19
20
21
22
23
24
25
26
27
      done();
    });
  });

const nested = async () => {
  const ddb = new AWS.DynamoDB.DocumentClient({ region: 'us-west-3' });
  return await ddb.get({ Key: { key: 'value' } }).promise();
};

describe('stubbing', () => {
fork icon3
star icon19
watch icon79

+ 19 other calls in file

2
3
4
5
6
7
8
9
10
11

// default imports
const AWSXRay = require('aws-xray-sdk-core')
const AWS = AWSXRay.captureAWS(require('aws-sdk'))
const { metricScope, Unit } = require("aws-embedded-metrics")
const DDB = new AWS.DynamoDB({ apiVersion: "2012-10-08" })


// environment variables
const { TABLE_NAME, ENDPOINT_OVERRIDE, REGION } = process.env
fork icon874
star icon0
watch icon0

+ 3 other calls in file

96
97
98
99
100
101
102
103
104
105
    console.log('DynamoDB', 'getItem', 'mock called');
    callback(null, {pk: 'foo', sk: 'bar'});
  })

  const input:GetItemInput = { TableName: '', Key: {} };
  const dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
  expect(await dynamodb.getItem(input).promise()).toStrictEqual({ pk: 'foo', sk: 'bar' });

  AWSMock.restore('DynamoDB');
});
fork icon108
star icon0
watch icon12

+ 11 other calls in file

11
12
13
14
15
16
17
18
19
} = Config

AWS.config.update({ region: 'us-east-1' })
sgMail.setApiKey(SENDGRID_API_KEY)

const dynamoDb = new AWS.DynamoDB.DocumentClient({ convertEmptyValues: true })

function notify (to, options = {}) {
  const { uri, text } = options
fork icon18
star icon211
watch icon9

11
12
13
14
15
16
17
18
19
20
'use AWS.S3'
'use AWS.DynamoDB'
const assert = require('assert');
const AWS = require('aws-sdk');
const util = require('util');
const dynamodb = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10'});
const tablename = process.env.FOQUS_DYNAMO_TABLE_NAME;
const s3_bucket_name = process.env.SESSION_BUCKET_NAME;
const log_topic_name = process.env.FOQUS_LOG_TOPIC_NAME;
const s3 = new AWS.S3();
fork icon46
star icon36
watch icon17

+ 9 other calls in file

5
6
7
8
9
10
11
12
13
14
15
16
17
 */


'use strict';


const AWS = require('aws-sdk');
const DynamoDB = new AWS.DynamoDB.DocumentClient();
const SSM = new AWS.SSM();
const APIGatewayManagement = new AWS.ApiGatewayManagementApi({apiVersion: '2018-11-29'});


const readSessionFromSSM = function (callback) {
fork icon7
star icon22
watch icon0