0

I studied dynamoDB in a different way. I wanted a AWS CLI version but the keywords and terms to search is very difficult.

So I decided to make a javascript version.

Can anyone convert this nodejs dynamodb querys to AWS CLI?

Create Table

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

const region = "us-west-2";

AWS.config.update({
  region
});
AWS.config.dynamodb = {
    endpoint: new AWS.Endpoint('http://localhost:8005'),
    region
}

const dynamodb = new AWS.DynamoDB() //low-level client

const tableName = "Services";

const params = {
    TableName : tableName,
    KeySchema: [       
        { AttributeName: "pk", KeyType: "HASH"},  //Partition key
        { AttributeName: "sk", KeyType: "RANGE" }  //Sort key
    ],
    AttributeDefinitions: [       
        { AttributeName: "pk", AttributeType: "S" },
        { AttributeName: "sk", AttributeType: "S" }
    ],
    ProvisionedThroughput: {       
        ReadCapacityUnits: 10, 
        WriteCapacityUnits: 10
    }
};

dynamodb.createTable(params, function(err, data) {
    if (err) {
        console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2));
    } else {
        console.log("Created table. Table description JSON:", JSON.stringify(data, null, 2));
    }
});

Insert 2 rows:

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

const region = "us-west-2";

AWS.config.update({
  region
});
AWS.config.dynamodb = {
    endpoint: new AWS.Endpoint('http://localhost:8005'),
    region
}
const ddbClient = new AWS.DynamoDB() 

const params = {
  RequestItems: {
    "Services": [
        {
          PutRequest: {
            Item: {
              "pk": { S:"1"},
              "sk": { S:"1"},
              "Service": {S:"Express"},
              "Range1": {N:"0000000"},
              "Range2": {N:"2000000"},
              "Counters": {N:"0"},
              "TriggerUpdate": {N:"1"}
            }
          }
        },
        {
          PutRequest: {
            Item: {
              "pk": { S:"2"},
              "sk": { S:"2"},
              "Service": {S:"Saver"},
              "Range1": {N:"2000001"},
              "Range2": {N:"3999999"},
              "Counters": {N:"0"},
              "TriggerUpdate": {N:"1"}
            }
          }
        }
    ]
  }
};
  

ddbClient.batchWriteItem(params, function(err, data) {
    if (err) {
        console.error("Unable to write data: ", JSON.stringify(err, null, 2));
    } else {
        console.log("PutItem succeeded");
    }
});

Update a specific item. I couldn't search what Im looking for, its supposed to increment the Counters column. Can anyone also teach me how to do that?

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

const region = "us-west-2";

AWS.config.update({
  region
});
AWS.config.dynamodb = {
    endpoint: new AWS.Endpoint('http://localhost:8005'),
    region
}

// const ddbClient = new AWS.DynamoDB()
const ddbClient = new AWS.DynamoDB.DocumentClient()

const params = {
    TableName: "Services",
    Key: {
        pk: "1",
        sk: "1"
    },
    UpdateExpression: "add Counters :value",
    ExpressionAttributeValues: {
      ":val": "1"
    },
    ReturnConsumedCapacity: 'TOTAL',
    ReturnValues: 'ALL_NEW',
};
return ddbClient.update(params).promise();

Example result would be: aws dynamodb create-table --cli-input-json file://create-table.json --endpoint=http://localhost:8003

I assure you this codes works. I can see it in my NoSQL Workbench DynamoDB Offline.

1 Answer 1

1

To increment a counter via update item, you can use the following update expression (via CLI per your request):

aws dynamodb update-item \
    --table-name Services \
    --key '{"pk":{"S":"1"}, "sk":{"S":"1"}}' \
    --update-expression "SET Price = Price + 1" \
    --return-values ALL_NEW

For your other JS samples, could you share what you attempted to do via CLI and what error you got?

Sign up to request clarification or add additional context in comments.

5 Comments

I had no error. Only for the update it doesnt add Counters :value
yea that update-item, doesnt work
this is the error when i run the update-item Error parsing parameter '--key': Invalid JSON: Expecting ',' delimiter: line 1 column 32 (char 31) JSON received: {"pk":{"S":"1"}, "sk":{"S":"1"} this data exist in the database
I believe I was missing a curly brace, updated my answer.
An error occurred (ValidationException) when calling the UpdateItem operation: Invalid UpdateExpression: Syntax error; token: "1", near: "+ 1"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.