I am not getting data from dynamodb using scan() nodejs - alexa-skills-kit

I am trying to get the data where the product name is same and implementing the code by using nodejs.`
AWS.config.update({region : "us-east-1"});
var docClient = new AWS.DynamoDB.DocumentClient();
var params = {
TableName: "ProductLocation",
FilterExpression: "#pn = :a",
ExpressionAttributeNames: {
"#pn": "Product_Name",
},
ExpressionAttributeValues: {
":a" : "rava"
}
};
console.log("Scanning Product_Name table.");
docClient.scan(params, onScan);
function onScan(err, data) {
if (err) {
console.error("Unable to scan the table. Error JSON:", JSON.stringify(err, null, 2));
} else {
// print all the movies
console.log("Scan succeeded.");
console.log(data);
}
}
Below is my dynamodb data

Your ExpressionAttributeValues should be:
{":a" : {S:"rava"}}
i.e. include the data type
Reference: see the complete example

Related

How to make DynamoDB Table calls to different location than lambda endpoint?

I am trying to access a table in DynamoDB which is hosted in Europe (Ireland) eu-west-1, from a lambda endpoint which is hosted in US East (N. Virginia) us-east-1 - however, I am always getting the error:
{
"message": "Requested resource not found",
"code": "ResourceNotFoundException",
"time": "2020-07-05T22:13:17.145Z",
"requestId": "6SGPGDFVN6AE4QMC5A8LG2RJ0JVV4KQNSO5AEMVJF66Q9ASUAAJG",
"statusCode": 400,
"retryable": false,
"retryDelay": 27.110475966612935
}
this is my code my code to call the DB:
class DynamoDB {
constructor() {
this.region = 'eu-west-1';
this.endpoint = 'https://dynamodb.eu-west-1.amazonaws.com';
this.tableName = 'MyTableName';
this.docClient = new AWS.DynamoDB.DocumentClient();
}
getUser(userId) {
return new Promise((resolve, reject) => {
const params = {
TableName: this.tableName,
Key: {
"id": userId,
}
}
this.docClient.get(params, function(err, data) {
if (err || !data.Item) {
console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
return reject(JSON.stringify(err, null, 2))
} else {
console.log("GetItem data succeeded:", JSON.stringify(data, null, 2));
resolve(data.Item);
}
});
});
}
}
const DynamoDB = require("../Libraries/DynamoDB");
const $ = new DynamoDB;
const Interceptor = {
async process(session) {
return $.getUser(session.id).then(data => {
console.log(`User gotten! ${JSON.stringify(data, null, 2)}`);
}).catch(err => {
console.log(`Failure getting user... ${err}`);
};
}).finally(async () => {
await InitGameSettings();
});
}
When I attempt this using an endpoint that is hosted in the same region as the DynamoDB table - it works fine! Only when I try this using different regions does it break. Why is this?
Looks like the SDK is not using the DDB region and endpoint that you configured. Could you try replacing the constructor() with the following code snippet?
constructor() {
this.region = 'eu-west-1';
this.endpoint = 'https://dynamodb.eu-west-1.amazonaws.com';
this.tableName = 'MyTableName';
this.ddbService = new AWS.DynamoDB({
apiVersion: '2012-08-10',
endpoint: this.endpoint,
region: this.region
});
this.docClient = new AWS.DynamoDB.DocumentClient({
service: this.ddbService
});
}

DynamoDB code not working inside of Mocha test case?

Background:
I've created a Dynamo DB table and populated it. I have some code to read an item in a file called readItem.js which works when I run it stand-alone but does NOT work when I run it as a Mocha test case.
Any ideas why the code is not running properly inside of an it() test case?
For standalone, I run the app like this:
node readItem.js
readItem.js:
var AWS = require("aws-sdk");
AWS.config.update({
region: "us-west-2",
endpoint: "http://localhost:8000"
});
var docClient = new AWS.DynamoDB.DocumentClient();
var table = "Movies";
var year = 2015;
var title = "The Big New Movie";
var params = {
TableName: table,
Key:{
"year": year,
"title": title
}
};
docClient.get(params, function(err, data) {
if (err) {
console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
}
});
However, when I run it inside of a Mocha test case, the callback function on docClient.get() call is not being invoke, i.e.:
docClient.get(params, function(err, data) { ...// Not being invoked });
Here is how I have the code structured as a test case. I invoke the test with:
npm run test
Where "test" is defined in my package.json as:
"scripts": {
"test": "./node_modules/.bin/mocha --recursive --reporter spec --ui bdd test/"
}
Test Case code:
const AWS = require('aws-sdk');
AWS.config.update({
region: "us-west-2",
endpoint: "http://localhost:8000"
});
var docClient = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10'});
let table = "Movies";
let year = 2015;
let title = "The Big New Movie";
let params = {
TableName: table,
Key:{
"year": year,
"title": title
}
};
describe('DynamoDB Tests', function() {
it("Read Item", function() {
try {
docClient.get(params, function (err,data) {
if (err) {
console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
}
});
} catch (err) {
console.log("ERROR:"+ err);
}
});
});
I was using older version of mocha:
"mocha": "^3.5.3"
Upgrading fixed it
"mocha": "^6.1.4"

How to dynamically update an attribute in a dynamodb item?

I created an item in dynamodb using Node js, the item has multiple attributes such as brand, category, discount, validity, etc. I am using uuid to generate ids for each item. Now let's say I want to update the validity attribute of the item, in which case I am currently sending the entire json object with the value of validity modified to the new value.
This is definitely not optimal, please help me find an optimal solution.
const params = {
TableName: process.env.PRODUCT_TABLE,
Key: {
id: event.pathParameters.id,
},
ExpressionAttributeNames: {
'#discount': 'discount',
},
ExpressionAttributeValues: {
':brand': data.brand,
':category': data.category,
':discount': data.discount,
':denominations': data.denominations,
":validity": data.validity,
":redemption": data.redemption
},
UpdateExpression: 'SET #discount = :discount, denominations = :denominations, brand = :brand, category = :category, validity = :validity, redemption = :redemption',
ReturnValues: 'ALL_NEW',
};
I want to send just the attribute I want to update with the new value, if I want to change the validity from 6 months to 8 months, I should just send something like:
{
"validity": "8 months"
}
And it should update the validity attribute of the item.
Same should apply to any other attribute of the item.
'use strict';
const AWS = require('aws-sdk');
const dynamoDb = new AWS.DynamoDB.DocumentClient();
module.exports.update = (event, context, callback) => {
const data = JSON.parse(event.body);
let attr = {};
let nameobj = {};
let exp = 'SET #';
let arr = Object.keys(data);
let attrname = {};
arr.map((key) => {attr[`:${key}`]=data[key]});
arr.map((key) => {
exp += `${key} = :${key}, `
});
arr.map((key) => {nameobj[`#${key}`]=data[key]});
attrname = {
[Object.keys(nameobj)[0]] : nameobj[Object.keys(nameobj)[0]]
}
const params = {
TableName: process.env.PRODUCT_TABLE,
Key: {
id: event.pathParameters.id,
},
ExpressionAttributeNames: attrname,
ExpressionAttributeValues: attr,
UpdateExpression: exp,
ReturnValues: 'ALL_NEW',
};
// update the todo in the database
dynamoDb.update(params, (error, result) => {
// handle potential errors
if (error) {
console.error(error);
callback(null, {
statusCode: error.statusCode || 501,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t update the card',
});
return;
}
// create a response
const response = {
statusCode: 200,
body: JSON.stringify(result.Attributes),
};
callback(null, response);
});
};
Contrary to others comments, this is very possible, use the UpdateItem action.
Language agnostic API docs
JavaScript specific API docs
If you want to dynamically create the query, try something like this:
const generateUpdateQuery = (fields) => {
let exp = {
UpdateExpression: 'set',
ExpressionAttributeNames: {},
ExpressionAttributeValues: {}
}
Object.entries(fields).forEach(([key, item]) => {
exp.UpdateExpression += ` #${key} = :${key},`;
exp.ExpressionAttributeNames[`#${key}`] = key;
exp.ExpressionAttributeValues[`:${key}`] = item
})
exp.UpdateExpression = exp.UpdateExpression.slice(0, -1);
return exp
}
let data = {
'field' : { 'subfield': 123 },
'other': '456'
}
let expression = generateUpdateQuery(data)
let params = {
// Key, Table, etc..
...expression
}
console.log(params)
Output:
{
UpdateExpression: 'set #field = :field, #other = :other',
ExpressionAttributeNames: {
'#field': 'field',
'#other': 'other'
},
ExpressionAttributeValues: {
':field': {
'subfield': 123
},
':other': '456'
}
}
Using Javascript SDK V3:
Import from the right package:
import { DynamoDBClient PutItemCommandInput, UpdateItemCommandInput, UpdateItemCommand } from '#aws-sdk/client-dynamodb';
Function to dynamically do partial updates to the item:
(the code below is typescript can be easily converted to Javascript, just remove the types!)
function updateItem(id: string, item: any) {
const dbClient = new DynamoDBClient({region: 'your-region-here });
let exp = 'set ';
let attNames: any = { };
let attVal: any = { };
for(const attribute in item) {
const valKey = `:${attribute}`;
attNames[`#${attribute}`] = attribute;
exp += `#${attribute} = ${valKey}, `;
const val = item[attribute];
attVal[valKey] = { [getDynamoType(val)]: val };
}
exp = exp.substring(0, exp.length - 2);
const params: UpdateItemCommandInput = {
TableName: 'your-table-name-here',
Key: { id: { S: id } },
UpdateExpression: exp,
ExpressionAttributeValues: attVal,
ExpressionAttributeNames: attNames,
ReturnValues: 'ALL_NEW',
};
try {
console.debug('writing to db: ', params);
const command = new UpdateItemCommand(params);
const res = await dbClient.send(command);
console.debug('db res: ', res);
return true;
} catch (err) {
console.error('error writing to dynamoDB: ', err);
return false;
}
}
And to use it (we can do partial updates as well):
updateItem('some-unique-id', { name: 'some-attributes' });
What i did is create a helper class.
Here is a simple function : Add all the attribute and values that goes into, if the value is null or undefined it won't be in the expression.
I recommande to create a helper class with typescript and add more functions and other stuff like generator of expressionAttributeValues , expressionAttributeNames ... , Hope this help.
function updateExpression(attributes, values) {
const expression = attributes.reduce((res, attribute, index) => {
if (values[index]) {
res += ` #${attribute}=:${attribute},`;
}
return res;
}, "SET ");
return expression.slice(0, expression.length - 1)
}
console.log(
updateExpression(["id", "age", "power"], ["e8a8da9a-fab0-55ba-bae3-6392e1ebf624", 28, undefined])
);
You can use code and generate the params object based on the object you provide. It's just a JavaScript object, you walk through the items so that the update expression only contains the fields you have provided.
This is not really a DynamoDB question in that this is more a general JS coding question.
You can use UpdateItem; to familiarize yourself with DynamoDb queries I would suggest you DynamoDb NoSQL workbench:
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/workbench.settingup.html
It can generate snippets for you based on your queries.
DynamoDb NoSQL workbench screenshot query

Best way to add a new key-value pair in an item of items list in DynamoDB

I have a sample Table info as -
items: [{
"key1":"value1",
"key2":"value2"
}
]
I have to update this like below -
items: [{
"key1":"value1",
"key2":"value2",
"key3": [{"subkey1":"value1"}, {"subkey2:"value2"}]
}
]
You can use Update API with SET operator.
In the below example, categoryList has the key3 value mentioned in OP.
Sample Code:-
var docClient = new AWS.DynamoDB.DocumentClient();
var categoryList = [{"subkey1":"value1"}, {"subkey2" : "value2"}];
var params = {
TableName : "Movies",
Key : {
"yearkey" : 2016,
"title" : "The Big New Movie 1"
},
UpdateExpression : "set #category = :categoryList",
ExpressionAttributeNames: {
'#category' : 'category'
},
ExpressionAttributeValues: {':categoryList' : categoryList},
ReturnValues: 'UPDATED_NEW'
};
console.log("Updating the item...");
docClient.update(params, function(err, data) {
if (err) {
console.error("Unable to update item. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("UpdateItem succeeded:", JSON.stringify(data));
}
});

How do I write an item to a DynamoDb with the AWS DynamoDB DocumentClient?

I'm having trouble with the AWS DynamoDb JS SDK v2.4.9. I want to use the DocumentClient class as opposed to the lower level DynamoDb class, but can't get it working.
This works:
function testPutItem( callback ) {
var tableName = 'todos';
var params = {
TableName: tableName,
Item: {
user_id: { S : userId },
id: { N : msFromEpoch }, // ms from epoch
title: { S : makeRandomStringWithLength(16) },
completed: { BOOL: false }
}
};
var dynamodb = new AWS.DynamoDB();
dynamodb.putItem(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else {
console.log(data); // successful response
if (callback) callback(data);
}
});
}
This does not work and gives the error InvalidParameterType: Expected params.Item[attribute] to be a structure for each attribute--as if DocumentClient is expecting the same input as DynamoDb:
function testPutItem( callback ) {
var tableName = 'todos';
var params = {
TableName: tableName,
Item: {
user_id: userId,
id: msFromEpoch,
title: makeRandomStringWithLength(16),
completed: false
}
};
console.log(params);
var docClient = new AWS.DynamoDB.DocumentClient({region: 'us-east-1'});
docClient.put(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else {
console.log(data); // successful response
if (callback) callback(data);
}
});
}
Does anyone have any idea what I am doing wrong?
I used to have the same issue,
please try with a simple object first, cause it's due to some special characters in your attributes, see my example :
this generates the error
InvalidParameterType: Expected params.Item[attribute] to be a structure
var Item = {
domain: "knewtone.com",
categorie: "<some HTML Object stuff>",
title: "<some HTML stuff>",
html: "<some HTML stuff>""
};
but when i replace the HTML stuff with a formated Html, simple characters , it works
var Item = {
domain: "knewtone.com",
categorie: $(categorie).html(),
title: $(title).html(),
html: $(html).html()
};

Resources