Querying many-to-many in DynamoDB with Node.js - amazon-web-services

I was reading about how to model many-to-many relationships in DynamoDB from this article: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-adjacency-graphs.html
Let's say that the requirement is to display a list of all Bills for a given Invoice. But you need to display all attributes for each bill (red circles in the image).
I could query all the Bills for Invoice-92551 as follow:
var params = {
TableName: "some-table",
KeyConditionExpression: "#pk = :pk",
ExpressionAttributeNames: {
"#pk":"pk",
},
ExpressionAttributeValues: {
":pk": "Invoice-92551"
},
ProjectionExpression: "pk, sk, isPaid, Amount, created...etc"
};
docClient.query(params, function(error, billsForGivenInvoice) {...});
Ok, I have now the bills but I need more attributes for each of them. I could do the following:
var params = {
RequestItems: {
"some-table": {
Keys: ["Bill-4224663", "Bill-4224687"],
ProjectionExpression: "...other attributes needed..."
}
}
};
docClient.batchGet(params, function(error, bills) {});
Is it possible to query both results in one go?. Instead of calling first to query() and them a batchGet().

Related

Querying with FilterExpression CONTAINS method

I want to query a specific row from a table based on a track's genre and release year.
Task: Query rows with release_year=2018 AND genre="pop"
Tracks Table:
trackId----------------release_year----------------genres----------------count
trackId: "7sT7kZEYd1MrmzLLIRVZas", release_year: 2018, genres: ["pop","rap", "hip-hop"], count: 7
Below code is how I am attempting to make this query as shown in FilterExpression. Currently I'm checking if the Table's attribute: genres (Array), contains :genre, which is a String sent from the client in the request. How would one set up a GSI in a way to make this query?
let queryParams = {
TableName: tableName,
IndexName: req.query.index,
KeyConditionExpression: 'release_year = :release_year and genres = :genre',
ExpressionAttributeValues: { ':release_year': parseInt(req.query.year), ':genre': req.query.genre},
FilterExpression : "contains (genres, :genre)",
ScanIndexForward: false,
Limit: 50
}
dynamodb.query(queryParams, (err, data) => {
if (err) {
res.statusCode = 500;
res.json({error: 'Could not load items: ' + err});
} else {
res.json(data.Items);
}
});
GSI should be created with release_year as the PK. Generes is part of the FilterExpression only.
let queryParams = {
TableName: tableName,
IndexName: req.query.index,
KeyConditionExpression: 'release_year = :release_year',
ExpressionAttributeValues: { ':release_year': parseInt(req.query.year), ':genre': req.query.genre},
FilterExpression : "contains (genres, :genre)",
ScanIndexForward: false,
Limit: 50
}
dynamodb.query(queryParams, (err, data) => {
if (err) {
res.statusCode = 500;
res.json({error: 'Could not load items: ' + err});
} else {
res.json(data.Items);
}
});

Query key condition not supported in DynamoDB

I'm attempting to set up my DynamoDB table so I can query the data via a rest API. My table has a partition key (id) which is a randomly generated ID, a sort key (name) and a List of strings (domain).
I have also set up a global secondary index for the "name" field.
I'm attempting to write a lambda that will search for items using both the name and possible search the domain array also. Is this possible? If so how do I set up the table as I'm currently getting this error:
Query key condition not supported
Here is my Lambda query code:
async function query(tableName, searchTerm) {
return new Promise((resolve, reject) => {
const params = {
IndexName: "nameIndex",
ExpressionAttributeNames: {
"#name": "name",
},
ExpressionAttributeValues: {
":topic": { S: searchTerm },
},
KeyConditionExpression: "begins_with(#name, :topic)",
TableName: tableName,
};
ddb.query(params, (err, data) => {
if (err) {
reject(err);
} else {
console.log("Success", data.Items);
resolve(data.Items);
}
});
});
}
A PK must be provided as a value. You can't do expressions like begins with on a PK.
Here are a lot of example programs doing queries with Node:
https://github.com/aws-samples/aws-dynamodb-examples/tree/master/DynamoDB-SDK-Examples/node.js/WorkingWithQueries

DynamoDB returning null nextToken when there are still results in database

I have a GraphQL API (AppSync) backed by a DynamoDB table keyed a specific id with timestamp as the range key. I want to retrieve all possible history for that id so I wrote a query in my GraphQL schema that would allow me to do so. Here's the request vtl:
{
"version": "2017-02-28",
"operation": "Query",
"query": {
"expression": "id = :id",
"expressionValues": {
":id": {
"S": "$context.args.id"
}
}
},
"nextToken": $util.toJson($util.defaultIfNullOrEmpty($context.args.nextToken, null))
}
There could be thousands of items in the ddb table for an id so I wrote a Lambda function to query for all of them and return the result in a list as such (I know the code can be simplified):
exports.handler = async function (event, context, callback) {
const graphqlClient = new appsync.AWSAppSyncClient({
url: process.env.APPSYNC_ENDPOINT,
region: process.env.AWS_REGION,
auth: {
type: 'AWS_IAM',
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
sessionToken: process.env.AWS_SESSION_TOKEN
}
},
disableOffline: true
});
const query = gql`query GetAllItemsById(
$id: String!
$nextToken: String
) {
getAllItemsById(
id: $id
nextToken: $nextToken
) {
exampleField {
subField1
subField2
subField3
}
nextToken
}
}
`;
const initialResult = await graphqlClient.query({
query,
variables: {
id: event.id
}
});
var finalResult = initialResult.data.getAllItemsById.exampleField;
var nextToken = initialResult.data.getAllItemsById.nextToken;
while (nextToken !== null) {
const result = await graphqlClient.query({
query,
variables: {
id: event.id,
nextToken: nextToken
}
});
finalResult = finalResult.concat(result.data.getAllItemsById.exampleField);
nextToken = result.data.getAllItemsById.nextToken;
}
console.log("Total Results: " + finalResult.length);
return callback(null, finalResult);
};
For some reason, not all items are being returned. nextToken is null before all results are returned. I know DDB has a 1MB limit for query which is why I'm paginating using nextToken but why is it still not returning all the items in the table? Also, if there's a better way to implement this, I'm open to it.

Dynamo DB - Is is possible to update/insert nested field

I have a document in the dynamo DB in the following format.
{
key1: "value1",
key2: {sKeya: "A", sKeyB: "B" }
}
Is it possible to update (via JavaScript) directly nested field in the dynamo DB document? BCOZ I don't want to update key2 with all values again.
Example: I tried the below query, but did not work. It rather created one more field key2.sKeya: "ABC"
UpdateExpression: "set #field = :value",
ExpressionAttributeNames: {
"#field": "key2.sKeya"
},
ExpressionAttributeValues: {
":value": "ABC",
}
Has any one faced similar issue?
You need to define the object key as separate Attribute names.
So one alias for "key2" and one alias for "sKeya".
const AWS = require("aws-sdk");
const DOCCLIENT = new AWS.DynamoDB.DocumentClient();
exports.handler = (event, context, callback) => {
let entry = {
TableName: "test",
Key: {
"key1": "value1",
},
UpdateExpression: "set #parent.#child = :value",
ExpressionAttributeNames: {
"#parent": "key2",
"#child": "sKeya"
},
ExpressionAttributeValues: {
":value": "ABC",
},
ReturnValues: "UPDATED_NEW"
};
DOCCLIENT.update(entry, function(err, data) {
callback(null, 'done');
});
};

AWS DynamoDB Attempting to ADD to a Set - Incorrect Operand

I am creating an API using Nodejs and DynamoDB as a back end. I am attempting to update an item to add to a set of "friends". When I update the user, I get the error, "Invalid UpdateExpression: Incorrect operand type for operator or function; operator: ADD, operand type: MAP". My understanding is that when adding to a set that does not exist, the set will be created. If it already exists, the new value should be added to the set. I do not understand why the set I attempt to ADD is being read as a map.
How users are created:
var params = {
TableName: "users",
Item:{
"id": Number(id),
"name": name,
"password": password
}
};
documentClient.put(params, function(err, data) {
if(err)
res.json(500, err);
else
res.json(200, data);
});
How friends are added:
var params = {
TableName: "users",
Key: {
"id": id
},
UpdateExpression: "ADD friends :friendId",
ExpressionAttributeValues: {
":friendId": { "NS": [friendId] }
},
ReturnValues: "UPDATED_NEW"
};
documentClient.update(params, function(err, data) {
if(err)
res.json(500, err);
else
res.json(200, data);
});
This question has an answer here
https://stackoverflow.com/a/38960676/4975772
Here's the relevant code formatted to fit your question
let AWS = require('aws-sdk');
let docClient = new AWS.DynamoDB.DocumentClient();
...
var params = {
TableName : 'users',
Key: {'id': id},
UpdateExpression : 'ADD #friends :friendId',
ExpressionAttributeNames : {
'#friends' : 'friends'
},
ExpressionAttributeValues : {
':friendId' : docClient.createSet([friendId])
},
ReturnValues: 'UPDATED_NEW'
};
docClient.update(params, callback);
If the set doesn't exist, then that code will create it for you. You can also run that code with a different set to update the set's elements. Super convenient.
Here is the working code. You don't need ADD here. Just use "set friends = :friendId" as friends attribute is not already present in the table (i.e. before the update you have only id, name and password in the table). The friend attribute is being added newly as part of the update.
var docClient = new AWS.DynamoDB.DocumentClient();
var table = "users";
var userid = 1;
var friendId = [123];
var params = {
TableName : table,
Key: {
"id" : userid
},
"UpdateExpression": "set friends = :friendId",
"ExpressionAttributeValues": {
":friendId": {"NS": friendId}
},
"ReturnValues" : "UPDATED_NEW"
};