UpdateExpression: Add other attribute's value to list - amazon-web-services

Given the following DynamoDB document:
{
"myobject" : {"foo" : "bar"},
"mylist" : [{"some" : "stuff}]
}
My goal is to update this document to get the following result:
{
"myobject" : {"foo" : "bar"},
"mylist" : [{"some" : "stuff}, {"foo" : "bar"}]
}
My request's params look like this:
let params = {
TableName: doctorSlotsTable,
Key: {
hashKey: hash,
rangeKey: range
},
UpdateExpression: 'SET mylist = list_append(if_not_exists(mylist, :empty_list), [myobject])',
ExpressionAttributeValues : {
':empty_list' : []
},
ReturnValues : "UPDATED_NEW"
};
This obviously does not work because the [ in the list_append triggers a syntax error.
Is there any solution to achieve that without having to get the data in a previous request and add it manually to the list ?

Unfortunately you cannot use an attribute name as an operand to list_append(...) unless that attribute is itself a list. The best you can do I believe would be to store myobject in the proper type up front, and then update it as expected.
Since storage is cheap & network/compute are expensive here, you could even duplicate the data to have one of them in the right form.
Here's a full example, where createTable() and deleteTable() do exactly what you think:
const PK = 'the item';
async function createObjAndList() {
const docClient = new DocumentClient();
const myObject = { foo: "bar" };
const theItem = {
PK,
myObject,
myObjectAsList: [ myObject ],
myList: [ { some : "stuff" } ],
};
const putParams = {
TableName,
Item: theItem
}
await docClient.put(putParams).promise();
console.log(`Put item ${util.inspect(theItem)}`);
}
async function updateListWithObject() {
const docClient = new DocumentClient();
const updateParams = {
TableName,
Key: { PK },
UpdateExpression: `SET #myList = list_append(if_not_exists(#myList, :emptyList), #myObjectAsList)`,
ExpressionAttributeNames: {
'#myList': 'myList',
'#myObjectAsList': 'myObjectAsList',
},
ExpressionAttributeValues: {
':emptyList': [],
}
}
await docClient.update(updateParams).promise();
console.log(`Updated list to include object`);
}
async function getObjAndList() {
const docClient = new DocumentClient();
const results = await docClient.get({ TableName, Key: { PK }}).promise();
console.log(`Item is now: ${util.inspect(results.Item)}`);
}
if (module === require.main) {
(async () => {
try {
await createTable();
await createObjAndList()
await updateListWithObject();
await getObjAndList();
} catch (err) {
console.log(`Error: ${err.message}`);
} finally {
await deleteTable();
}
})();
}
The output from this is:
Put item {
PK: 'the item',
myObject: { foo: 'bar' },
myObjectAsList: [ { foo: 'bar' } ],
myList: [ { some: 'stuff' } ]
}
Updated list to include object
Item is now: {
myList: [ { some: 'stuff' }, { foo: 'bar' } ],
myObject: { foo: 'bar' },
PK: 'the item',
myObjectAsList: [ { foo: 'bar' } ]
}

Related

BatchWriteItemCommand with AWS.DynamoDB class using AWS SDK V3 in Nodejs

I have been trying for hours to perform a DynamoDB DeleteRequest using BatchWriteItemCommand but I keep getting the following error:
Error ValidationException: 1 validation error detected: Value null at 'requestItems.td_notes_sdk.member.1.member.deleteRequest.key' failed to satisfy constraint: Member must not be null
This is what my table looks like:
Partition key: user_id (string)
Sort key: timestamp (number)
DynamoDB Screenshot
This is what my code looks like:
// Import required AWS SDK clients and commands for Node.js
import {
DynamoDBClient,
BatchWriteItemCommand,
} from "#aws-sdk/client-dynamodb";
// Set the parameters
export const params = {
RequestItems: {
"td_notes_sdk": [
{
DeleteRequest: {
Item: {
Key: {
user_id: { S : "bb" },
timestamp: { N : 2 },
},
},
},
},
],
},
};
export const run = async () => {
const ddbClient = new DynamoDBClient({ region: "us-east-2" });
try {
const data = await ddbClient.send(new BatchWriteItemCommand(params));
console.log("Success, items inserted", data);
return data;
} catch (err) {
console.log("Error", err);
}
};
run();
Here are some resources that I've been trying to follow along with:
Resource 1: Writing items in Batch Example
Resource 2: AWS Javascript SDK v3 Documentation
Update: BatchWrite PutRequest work with the code below, so I know that the structure of my keys/attributes is closer to being correct. Still does not work for DeleteRequest.
export const params = {
RequestItems: {
"td_notes_sdk": [
{
PutRequest: {
Item: {
user_id: { "S": "bb" },
timestamp: { "N": "5" },
},
},
},
],
},
};
You don't supply an Item when deleting an item. You supply a Key.
Here is a working example:
const params_delete = {
RequestItems: {
"td_notes_sdk": [
{
DeleteRequest: {
Key: {
user_id: { S: "bb" },
timestamp: { N: "2" },
},
},
},
],
},
};
const delete_batch = async () => {
const ddbClient = new DynamoDBClient({ region: "us-east-2" });
try {
const data = await ddbClient.send(new BatchWriteItemCommand(params_delete));
console.log("Success, item deleted");
return data;
} catch (err) {
console.log("Error", err);
}
};
delete_batch();

Apollo Server - how to create and add object in resolver when type isn't available in namespace

In the following example, I am attempting to create a post and add it to the Dictionary 'post'. How is the Mutation expected to create, add to the hash, and return the type of the item created when the item type isn't available to the namespace of the resolver?
mutation createPost {
createPost(input: {name: "Post Name"}){
name
}
}
index.js:
const { ApolloServer, gql } = require('apollo-server');
const dictionary = {};
const typeDefs = gql`
input PostSpecInput {
name: String
}
type PostSpec {
id: ID!
name: String
}
type Mutation {
createPost(input: PostSpecInput): PostSpec
}
type Query {
post_specs: [PostSpec]
}
`;
const resolvers = {
Query: {
post_specs: () => Object.keys(dictionary).map(function(key){
return dictionary[key];
})
},
Mutation: {
createPost(parent, args, context, info) {
var id = require('crypto').randomBytes(10).toString('hex');
const postSpec = new PostSpec(id, args.input);
posts_mock_database[id] = args.input;
return postSpec;
}
}
}
const server = new ApolloServer({typeDefs, resolvers})
server.listen().then(({url}) => {
console.log(`Server Ready at ${url}`);
})
Error:
{
"errors": [
{
"message": "PostSpec is not defined",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"createPost"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"stacktrace": [
"ReferenceError: PostSpec is not defined",
" at createPost (index.js:38:34)",
Type definitions are not classes nor object instances, they are just for enforcing type. Even if you were in the namespace, calling 'new' would not work. Here is the solution for your mock database:
Mutation: {
createPost(parent, args, context, info) {
var id = require('crypto').randomBytes(10).toString('hex');
const newPostSpec = { id: id, name: args.input.name }
posts_mock_database[id] = newPostSpec;
return newPostSpec;
}
}

Transient transition that sets a value only on condition passing

Take the following code:
const isWarning = () => { ... }
const setWarning = () => { ... }
const machine = Machine({
initial: "foo",
context: {
warning: null
},
states: {
foo: {
on: {
"": [
target: "bar",
action: "setWarning",
cond: "isWarning",
]
}
},
bar: {
on: {
FOO: "foo,
}
}
}
}, {
actions: {
setWarning
}
guards: {
isWarning
}
});
Is this the best way to go to "bar" and set a warning based on some quantitative data in "foo"?
Given the posted code example, I am not sure what you mean by "quantitative data in foo". Data relevant for machine's behavior can be stored in machine's context or state's meta property.
For getting into bar state and set a warning you might need something like:
const sm = Machine({
initial: 'baz',
context: { wasWarned: false },
on: {
'WARNING': {
target: 'bar',
action: 'setWarning'
}
},
states: {
baz: {},
bar: {}
}
}, {
actions: {
setWarning: assign({ warning: true })
}
})
This means: When machine gets 'WARNING' event, go into bar state AND immediately, before anything else update the context.
Actions are not immediately triggered. Instead, the State object returned from machine.transition(...) will declaratively provide an array of .actions that an interpreter can then execute.
The transition will be enabled after the guards are passed.
Other code example that might prove useful depending on what you want to achieve:
const sm = Machine({
initial: 'pending',
context: { wasWarned: null },
states: {
pending: {
on: {
'': [
{target: 'bar', cond:'wasWarned'},
{target: 'baz', cond: 'otherCond'}
]
}
},
bar: {},
baz: {}
},
guards: {
wasWarned: (ctx) => ctx.wasWarned
}
})

DynamoDB retrieve only attribute values

I have a table that has userId as the PK and a single attribute called userToken.
I have written a batchGet() function to return all the userTokens for specific userIds, however it returns it like this:
[ { userToken: '1234' },
{ userToken: '5678' } ]
I'd like it to just return the values since I already know what the attribute name will be:
['1234', '5678']
How would I go about doing so?
const params = {
RequestItems: {
UserTokens: {
Keys: userIds,
AttributesToGet: [
'userToken'
]
}
}
};
db.batchGet(params, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log(data.Responses);
sendNotifications(data.Responses);
}
});
DynamoDB always returns the attribute name and value.
You can easily filter this on the client side.
val = [ { userToken: '1234' }, { userToken: '5678' } ];
reducer = (accumulator, currentVal) => {
accumulator.push(currentVal.userToken);
return accumulator;
}
console.log(val.reduce(reducer, []));

DynamoDB update list of list items

I need your help to update/add item in a list of list of DynamoDB.
I want to add and update a list that is composed by a list.
How can i do that? Thanks for your help
Here my example:
DATABASE
Item{1}
idProject:"15azeze-55ze"
dateCreationProjectString: 08/01/2018 14:6:32
environnements: List[1]
0 MAP {3}
idEnvironnement: "11-aa",
name:"Exemple Environnement"
tasks[0]
NodeJS code :
let newTask = {
authorTask : "Toto",
dateCreationTask: "01/01/1960",
idTask: "154-141-aa41",
nameTask: "Task name ..."
};
dynamodbdc.update({
TableName: "projects",
Key: { idProject: "15azeze-55ze", idEnvironnement:"11-aa" },
ReturnValues: 'ALL_NEW',
UpdateExpression: 'set #environnements.#tasks = list_append(if_not_exists(#environnements.#tasks, :empty_list), :newTask)',
ExpressionAttributeNames: {
'#environnements' : 'environnements',
'#tasks' : 'tasks',
},
ExpressionAttributeValues: {
':newTask': [newTask],
':empty_list': []
}
}, function(error, stdout) {
if(error){
console.log("error==", error)
else {
console.log("Nice thank you !!")
}
});
I would suggest to keep the tasks attribute as SET ('SS') which will simplify to NOT add the duplicate values. Otherwise, it would be difficult to fulfil the scenario.
Also, you may the index of the environments attribute list to form the update expression correctly. There is no options in the dynamodb api to workout the index automatically.
The below code should work if you define the tasks attribute as SET.
Insert item code:-
var params = {
TableName : table,
Item : {
"idProject" : "15azeze-55ze",
"dateCreationProjectString" : Date(),
"environnements" : [{
"idEnvironnement" : "11-aa",
"name" : "Example Environments",
"tasks" : docClient.createSet(["1"])
}]
}
};
console.log("Adding a new item...");
docClient.put(params, function(err, data) {
if (err) {
console.error("Unable to add item. Error JSON:", JSON.stringify(err,
null, 2));
} else {
console.log("Added item:", JSON.stringify(data, null, 2));
}
});
Sample update item working code:-
The ADD operation will ensure that it doesn't add duplicates to the SET
var docClient = new AWS.DynamoDB.DocumentClient();
var params = {
TableName: "projects",
Key: {
"idProject" : "15azeze-55ze"
},
UpdateExpression: 'ADD environnements['+0+'].tasks :tasksVal',
ExpressionAttributeValues: {
":tasksVal": docClient.createSet(["2"])
},
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));
}
});