aws / aws-sdk-js

AWS SDK for JavaScript in the browser and Node.js
https://aws.amazon.com/developer/language/javascript/
Apache License 2.0
7.6k stars 1.55k forks source link

DynamoDB.DocumentClient should support empty string properties #833

Closed thoean closed 7 years ago

thoean commented 8 years ago

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:

var params = {
    TableName: 'my-test-table',
    Item: { Key:"foo", MyData: ""}
};

Results in the following error on DynamoDB:

[AWS dynamodb 400 0.588s 0 retries] putItem({ TableName: 'my-test-table',
  Item: { Key: { S: 'foo' }, MyData: { S: '' } } })

Which is returned from the DocumentClient as:

Unable to store request. Error was {
  "message": "One or more parameter values were invalid: An AttributeValue may not contain an empty string",
  "code": "ValidationException",
  "time": "2015-12-10T09:19:17.204Z",
  "statusCode": 400,
  "retryable": false,
  "retryDelay": 0
}
JamesDorrian commented 7 years ago

+1

rocedavor commented 7 years ago

+1

kunalpatilabg commented 7 years ago

+1

atulrawat commented 7 years ago

+1

josepapaianni commented 7 years ago

+1

esteban-uo commented 6 years ago

+1

superandrew commented 6 years ago

this is the single most requested thing I saw in my life.

jeskew commented 6 years ago

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.

superandrew commented 6 years ago

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.

adimoraret commented 6 years ago

+1

M1chaelTran commented 6 years ago

+1

jhoel commented 6 years ago

+1

sowmitranalla commented 6 years ago

+1

invisibleroads commented 6 years ago

lol

mymail4blogs commented 6 years ago

Trying to insert the data directly from a third party service which has almost 234 fields, some of them are empty. +1

a1exwang commented 6 years ago

+1

Mnilko commented 6 years ago

+1

stephenfowler commented 6 years ago

+1

chrisburkejr commented 6 years ago

+1

bvanloocke commented 6 years ago

+1

yzhong52 commented 6 years ago

+1

rtatiparthy commented 6 years ago

+1

XBeg9 commented 6 years ago

+1

frankjl commented 6 years ago

+1

nfons commented 6 years ago

+100000

mlensment commented 6 years ago

+1

Kivylius commented 6 years ago

+1

devalpcyb commented 6 years ago

+1

alopes commented 6 years ago

+9001

synergiclabs commented 6 years ago

+1

gesposito commented 6 years ago
const dynamodb = new AWS.DynamoDB.DocumentClient({
    convertEmptyValues: true,
});
chrisburkejr commented 6 years ago

convertEmptyValues changes them into null values not empty strings https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html

jeskew commented 6 years ago

@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.

nickcoad commented 6 years ago

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?

Aristocles commented 6 years ago

+1

raaone7 commented 6 years ago

+1

menkveldj commented 6 years ago

+1

fptavares commented 6 years ago

convertEmptyValues: true

was enough to fix it for me.

BetaMee commented 6 years ago

+1

tobiaszheller commented 6 years ago

+1

hcho112 commented 6 years ago

+1

chauhanprateek89 commented 6 years ago

+1

JamesTheHacker commented 6 years ago

@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.

synergiclabs commented 6 years ago

@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;

JamesTheHacker commented 6 years ago

@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.

Hanslen commented 6 years ago

+1

seckcoder commented 6 years ago

+1

Antommyj commented 6 years ago

+1

spolyak commented 6 years ago

+1

jianingYu commented 6 years ago

+1