Skip to content

DynamoDB with the AWS SDK for JavaScript v3

Uses @aws-sdk/client-dynamodb and @aws-sdk/lib-dynamodb 3.1160 with endpoint. The code is an ordinary application for the original database; nothing in it knows about MachNoSQL.

https://dynamodb.eu-central-1.amazonaws.com
https://machnosql.example.com:8000

The address comes from the environment variable DYNAMODB_ENDPOINT, so switching to MachNoSQL changes configuration only.

Terminal window
export DYNAMODB_ENDPOINT='https://machnosql.example.com:8000'
npm install
npm start

Credentials are the usual AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY (an access key created in MachNoSQL) and AWS_REGION. Instead of passing the endpoint in code, current AWS SDKs and the AWS CLI also read AWS_ENDPOINT_URL_DYNAMODB from the environment; then the application needs no change at all.

The program as it is kept and tested in the Mach Database NoSQL repository; highlighted lines read the connection setting.

app.js
// Orders table on DynamoDB, unchanged on MachNoSQL.
import { CreateTableCommand, DynamoDBClient, ResourceInUseException, waitUntilTableExists } from "@aws-sdk/client-dynamodb";
import {
BatchWriteCommand,
DynamoDBDocumentClient,
GetCommand,
QueryCommand,
UpdateCommand,
} from "@aws-sdk/lib-dynamodb";
const client = new DynamoDBClient({ endpoint: process.env.DYNAMODB_ENDPOINT });
const docs = DynamoDBDocumentClient.from(client);
try {
await client.send(
new CreateTableCommand({
TableName: "Orders",
KeySchema: [
{ AttributeName: "customer", KeyType: "HASH" },
{ AttributeName: "orderId", KeyType: "RANGE" },
],
AttributeDefinitions: [
{ AttributeName: "customer", AttributeType: "S" },
{ AttributeName: "orderId", AttributeType: "S" },
],
BillingMode: "PAY_PER_REQUEST",
}),
);
await waitUntilTableExists({ client, maxWaitTime: 60 }, { TableName: "Orders" });
} catch (error) {
if (!(error instanceof ResourceInUseException)) throw error;
}
await docs.send(
new BatchWriteCommand({
RequestItems: {
Orders: [
{ PutRequest: { Item: { customer: "ada", orderId: "1001", item: "keyboard", total: 49 } } },
{ PutRequest: { Item: { customer: "ada", orderId: "1002", item: "mouse", total: 39 } } },
{ PutRequest: { Item: { customer: "linus", orderId: "1003", item: "monitor", total: 199 } } },
],
},
}),
);
const { Item } = await docs.send(new GetCommand({ TableName: "Orders", Key: { customer: "ada", orderId: "1001" } }));
console.log(Item);
const { Items } = await docs.send(
new QueryCommand({
TableName: "Orders",
KeyConditionExpression: "customer = :c",
ExpressionAttributeValues: { ":c": "ada" },
}),
);
console.log(Items);
await docs.send(
new UpdateCommand({
TableName: "Orders",
Key: { customer: "ada", orderId: "1002" },
UpdateExpression: "SET #s = :shipped",
ConditionExpression: "attribute_not_exists(#s)",
ExpressionAttributeNames: { "#s": "status" },
ExpressionAttributeValues: { ":shipped": "shipped" },
}),
);
package.json
{
"name": "machnosql-example-dynamodb-node",
"private": true,
"type": "module",
"scripts": { "start": "node app.js" },
"dependencies": {
"@aws-sdk/client-dynamodb": "^3.1160.0",
"@aws-sdk/lib-dynamodb": "^3.1160.0"
}
}