I am designing a simple serverless app in AWS: API Gateway-> Lambda (node.js) -> DynamoDB.
I want to keep number of customers in the same table and coresponding items e.g.
Client_id, no, item, price
0001, 1, box, 10
0001, 2, table, 100
0002, 1, chair, 20
0003, 1, lamp, 15
I choose Primary Key = Client_id and Sort Key = no and currently I am able to get single item using "dynamodb.get" function, but have problems with getting all items for one client. I tried to use "dynomodb.batchget", but I need to know how many items are in table and do some loops perhaps. Maybe there is different, batter way to to get all items from single client?
const AWS = require('aws-sdk');
const dynamoDb = new AWS.DynamoDB.DocumentClient();
module.exports.get2 = (event, context, callback) => {
var params = {
"RequestItems": {
"items": {
"Keys": [{
id: Number(event.pathParameters.id),
no: 1
},
{
id: Number(event.pathParameters.id),
no: 2
}
]
}
},
"ReturnConsumedCapacity": "TOTAL"
};
dynamoDb.batchGet(params, function(err, data) {
if (err) {
console.error(err);
callback(null, {
statusCode: err.statusCode || 501,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t fetch the item.',
});
return;
}
const response = {
statusCode: 200,
body: JSON.stringify(data),
};
callback(null, response);
});
};```