I am attempting to batchWrite items to my users
table, but I am receiving a ValidationException. I can't identify why The provided key element does not match the schema
. I created a new table, and the partition key is id
.
batchWrite function
const batchWriteUserData = (data) => {
const input = {
RequestItems: {
"users": data.map((item) => ({
PutRequest: {
Item: {
id: { S: item.id },
name: { S: item.name },
username: { S: item.username },
email: { S: item.email },
password: { S: item.password },
blogs: {
L: item.blogs.map((blog) => ({
M: {
id: { S: blog.id.toString() },
title: { S: blog.title },
content: { S: blog.content },
},
})),
},
},
},
})),
},
}
console.log(JSON.stringify(input, null, 2))
dynamoDB.batchWrite(input, (err, data) => {
if (err) console.error('Error:', err);
else return 'Sucess';
});
}
functions that create the data
const generateBlogData = () => {
const paragraphAmount = Math.floor((Math.random() * 5) + 1);
const titleAmount = Math.floor((Math.random() * 10) + 1);
const blogCount = Math.floor(Math.random() * 9 + 1);
const userBlogs = [];
for (let i = 0; i < blogCount; i++) {
const blogTitle = lorem.generateWords(titleAmount);
const blogContent = lorem.generateParagraphs(paragraphAmount);
const blogData = {
id: i + 1,
title: blogTitle,
content: blogContent,
};
userBlogs.push(blogData);
};
return userBlogs;
};
const generateUserData = () => {
let maxUsers = 100;
let count = 0;
let userData = []
while (maxUsers > 0) {
const prefix = lorem.generateWords(1);
const suffix = lorem.generateWords(1);
const loremPassword = lorem.generateWords(1).concat(Math.floor(Math.random() * 900) + 100);
const data = {
id: generateUniqueId(),
name: generateUniqueName(),
username: generateUniqueUsername(),
email: `${prefix}@${suffix}.com`,
password: loremPassword
};
data['blogs'] = generateBlogData();
if (count < 25) {
userData.push(data);
}
else if (count === 25) {
batchWriteUserData(userData);
}
count++;
maxUsers--;
}
};
Can someone identify why this is happening? I appreciate any feedback/perspective.
I've tried writing only the user data, even down to just the id to determine if the issues was the blogs data, I also tried storing the blog.id
without .toString()
and with the type as N
but the issue persists.
You're using the document client which takes native JSON not DDB JSON which you use on your request.
const input = {
RequestItems: {
"users": data.map((item) => ({
PutRequest: {
Item: {
id: item.id ,
name: item.name ,
username: item.username ,
email: item.email ,
password: item.password ,
blogs:
item.blogs.map((blog) => ({
id: blog.id.toString() ,
title: blog.title ,
content: blog.content ,
})),
},
},
})),
},
}
Learn more here: https://aws.amazon.com/blogs/database/exploring-amazon-dynamodb-sdk-clients/