Skip to content

Storage KeyValue DynamoDB Nodejs

Braden Hitchcock edited this page Jan 30, 2020 · 5 revisions

Node.js SDK for AWS DynamoDB

The Node.js SDK for AWS is automatically included in your deployment package when building Node.js applications. To use it, simply add a require statement at the top of your compute instance code:

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

You can create a new instance of the DynamoDB Document Client to interact with DynamoDB from your compute instance.

const client = new AWS.DynamoDB.DocumentClient();

Below are some simple examples of using the document client to interact with DynamoDB. You can find a more exhaustive documentation of the entire DynamoDB SDK provided by Amazon online. The examples below use the async/await syntax of JavaScript per the ES8 (ES2017) specification.

All of the examples assume that the reference to the storage instance in the Scootr runtime library is named StorageReferenceName.

Creating An Entry

You can add a new entry to DynamoDB using the put method:

const client = new AWS.DynamoDB.DocumentClient();

// We will generate a (non-secure) random numerical ID.
const id = Math.floor(Math.random() * 10000);

// Create the item we will add to the database.
const item = {
  id: id,
  name: 'Alice',
  username: 'alice',
  email: '[email protected]'
};

const params = {
  TableName: process.env.StorageReferenceName,
  Item: item
};

try {
  await client.put(params).promise();
  console.log('Successfully created item', item);
} catch (err) {
  console.error('Failed to create item', err);
}

Getting An Entry

You can retrieve a single entry using the get method:

const client = new AWS.DynamoDB.DocumentClient();

const id = 1234;

const params = {
  TableName: process.env.StorageReferenceName,
  Key: { id: id }
};

try {
  const response = await client.get(params).promise();
  console.log('Successfully got item', response.Item);
} catch (err) {
  console.error('Failed to get item', err);
}

The resulting entry will be available as the Item property of the response.

Getting All Entries

You can retrieve all of the entries from the database using the scan method:

const client = new AWS.DynamoDB.DocumentClient();

const params = {
  TableName: process.env.StorageReferenceName
};

try {
  const response = await client.scan(params).promise();
  console.log('Successfully got items', response.Items);
} catch (err) {
  console.error('Failed to get items', err);
}

The resulting entries will be available as the Items property of the response.

Updating An Entry

You can update an existing entry in the database using the update method. In the code example below, we assume the same structure of the item we created in the [put example above][#creating-an-entry]:

const client = new AWS.DynamoDB.DocumentClient();

const id = 1234;
const updatedUsername = 'alice121';
const updatedEmail = '[email protected]';

const params = {
  TableName: process.env.StorageReferenceName,
  Key: {
    id: id
  },
  UpdateExpression: 'set username = :u, email = :e',
  ExpressionAttributeValues: {
    ':u': updatedUsername,
    ':e': updatedEmail
  }
};

try {
  await client.update(params).promise();
  console.log('Successfully update the item');
} catch (err) {
  console.error('Failed to update the item', err);
}

Deleting An Entry

You can delete an existing entry in the database using the delete method:

const client = new AWS.DynamoDB.DocumentClient();

const params = {
  TableName: process.env.StorageReferenceName,
  Key: {
    id: 1234
  }
};

try {
  await client.delete(params).promise();
  console.log('Successfully deleted item');
} catch (err) {
  console.error('Failed to delete item', err);
}