Closed thoean closed 7 years ago
+1
+1
+1
+1
+1
+1
this is the single most requested thing I saw in my life.
Hi @superandrew,
You might want to check out the DynamoDB Data Mapper for JavaScript, which is now in developer preview. That project allows you to define your table schema up front so that empty strings can be saved as a sigil value compatible with DynamoDB's data model (null
) and then deserialized back to an empty string when the item is loaded.
If you don't want to define a schema and don't mind transforming empty strings and buffers into null
, you can also use the convertEmptyValues
option on the Document client as discussed above.
thanks @jeskew as others have pointed out, this is not a problem of the js sdk but of dynamodb itself. I was just noticing how many people continue to request a dynamodb feature here because it's not possible to do it nowhere else.
+1
+1
+1
+1
lol
Trying to insert the data directly from a third party service which has almost 234 fields, some of them are empty. +1
+1
+1
+1
+1
+1
+1
+1
+1
+1
+100000
+1
+1
+1
+9001
+1
const dynamodb = new AWS.DynamoDB.DocumentClient({
convertEmptyValues: true,
});
convertEmptyValues changes them into null values not empty strings https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html
@chrisburkejr Empty string values will be rejected by DynamoDB; null
is the only emptiness sigil that can be persisted in a DynamoDB table. If a particular field must always be a string, you could try using a schema-driven abstraction like the DynamoDB DataMapper, which can transparently handle empty strings.
This has been an issue for 5 years and has virtually no response on the DynamoDb forum, which is why you're getting hassled about it here. Maybe it's possible for you to raise this internally with the DynamoDb team so they can at the very least jump into the forum thread and address it with the users?
+1
+1
+1
convertEmptyValues: true
was enough to fix it for me.
+1
+1
+1
+1
@fptavares solution works just fine. It's documented here. Here is a quick example on usage:
module.exports.postback = (event, context, callback) => {
const body = qs.parse(event['body']) || {}
const params = {
TableName: "conversions",
Item: {
"gclid": body.gclid,
"leadid": body.leadid,
"affid": body.affid,
"campid": body.campid,
"cid": body.cid,
"tid": body.tid,
"s1": body.s1,
"s2": body.s2,
"s3": body.s3,
"s4": body.s4,
"s5": body.s5,
"price": body.price,
"udid": body.udid,
}
}
const dynamo = new AWS.DynamoDB.DocumentClient({
convertEmptyValues: true
})
dynamo.put(params, (err, data) => {
if (err) {
Raven.captureException(err)
return callback(err)
}
callback(null, {
statusCode: 200,
body: JSON.stringify({ "message": "success" })
})
})
}
As noted by @synergiclabs this solution will not store empty strings. Instead it will convert an empty string to null. If you require empty strings this solution is not for you.
@JamesTheHacker That is not a viable solution. The option convertEmptyValues
converts a string into a NULL value. There are instances you really need EMPTY STRING and not a null data type.
In JavaScript, "" !== null;
@synergiclabs Of course, but in my instance and maybe for others, the solution is fine as I do not require empty strings to be stored. This was likely the decision amazon took too which is why they've provided no support.
+1
+1
+1
+1
+1
When the object contains a property with an empty string, it results in an exception instead of for example silently removing this property or replacing with a user-provided constant.
To be specific, the following example request:
Results in the following error on DynamoDB:
Which is returned from the DocumentClient as: