Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can AWS DynamoDB DocumentClient handle native Javascript Date objects?

I'm using DocumentClient (http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html) to make working with DynamoDB easier. However, it seems like it is having trouble with Date objects. I know that DynamoDB wants dates formatted specifically Date (as ISO8601 millisecond-precision string, shifted to UTC).

Does DocumentClient just not handle this, or is there something that needs to be set on the Date object?

For now, I've just been converting the value to a string via toString(). The expires_at value is the value specifically:

This one does not include expires_at in the DyanmoDB Item.

{ 
  Item:
  { 
    id: 'session',
    credentials: 
    { 
      access_token: '',
      refresh_token: '',
      token_type: 'Bearer',
      expires_in: 3599,
      expires_at: 2017-04-17T18:48:03.608Z 
    } 
  },
  TableName: 'table' 
}

And this one will include it:

{ 
  Item:
  { 
    id: 'session',
    credentials: 
    { 
      access_token: '',
      refresh_token: '',
      token_type: 'Bearer',
      expires_in: 3599,
      expires_at: 'Mon Apr 17 2017 18:50:24 GMT+0000 (UTC)' 
    } 
  },
  TableName: 'table' 
}
like image 427
CLo Avatar asked Apr 17 '17 17:04

CLo


People also ask

Which data models are supported natively by DynamoDB?

DynamoDB supports both key-value and document data models. This enables DynamoDB to have a flexible schema, so each row can have any number of columns at any point in time.

What languages does DynamoDB support?

Languages and frameworks with a DynamoDB binding include Java, JavaScript, Node. js, Go, C# . NET, Perl, PHP, Python, Ruby, Rust, Haskell, Erlang, Django, and Grails.

What type of data does DynamoDB store?

DynamoDB supports three data types (number, string, and binary), in both scalar and multi-valued sets. It supports document stores such as JSON, XML, or HTML in these data types. Tables do not have a fixed schema, so each data item can have a different number of attributes.

Can you store an object in DynamoDB?

You can store them as an object in Amazon S3 and then store the object identifier in your DynamoDB item. You can also use the object metadata support in Amazon S3 to provide a link back to the parent item in DynamoDB. Store the primary key value of the item as Amazon S3 metadata of the object in Amazon S3.


1 Answers

DocumentClient is just an abstraction layer for DynamoDB. So, if the Date data type is not supported in DynamoDB, it won't be supported in DocumentClient. (See DynamoDB Data Types)

What you can do is pass the ISO 8601 string using the toISOString() method. For example:

var expires = new Date();
expires.setTime(expires.getTime() + (60*60*1000)); // Add 1 hour.

{ 
  Item:
  { 
    id: 'session',
    credentials: 
    { 
      access_token: '',
      refresh_token: '',
      token_type: 'Bearer',
      expires_in: 3599,
      expires_at: expires.toISOString() 
    } 
  },
  TableName: 'table' 
}
like image 183
Khalid T. Avatar answered Oct 12 '22 15:10

Khalid T.