Closed jeremydaly closed 4 years ago
+1
+1
@jeremydaly we needed datetime support in our project so I implemented an initial version. See here for the changes: https://github.com/cklam2/data-api-client/tree/aws-timestamp-support
Changes in a nutshell:
Two new configuration options. Defaults to false, ensuring backwards compatibility. Values can be set as global setting or passed per query:
deserializeDate
(default false
)
Converts string values from db with a certain typeName to Date objectstreatAsLocalDate
(default false
) 1
Assumes local TZ when handling date/time values.When saving data To database and value
is of type Date, it is stored as string with TIMESTAMP
as typeHint (as described in the AWS SQLParameter page).
If treatAsLocalDate=true
the local timezone is used for the conversion to string, otherwise UTC timezone. In both cases the string format is yyyy-MM-dd hh:mm:ss[.fff], as per the specification from the aforementioned AWS page.
Note: If passing a string with format "yyyy-MM-dd hh:mm:ss" it's just stored as plain string like any other string, without any type hint.
When reading data From database and the typeName value from columnMetaData
is either 'DATE', 'TIMESTAMP', 'DATETIME' or 'TIMESTAMP WITH TIME ZONE' and deserializeDate=true
then value is converted to Date object. If treatAsLocalDate=true
the date is converted to local date, otherwise UTC.
Note that the readme is not yet updated. Can do that once we agree upon the implementation. And probably will also add additional unit tests for the date conversion methods.
Any feedback would be appreciated.
1 AWS services run in UTC time zone so in AWS the flag does nothing special. However when running in serverless offline mode (or when code is not running on AWS) the datetime value follows the server's time zone. For ppl who want to ensure that value is always stored as UTC (like in our case), they can use this option.
Thanks for this, @cklam2! I'm working on wrapping up a major new version to another project, then I'll turn my attention back here.
@jeremydaly it's July... can you merge and publish this?
An original implementation of this was supplied in #20, but it needs some more thought.
Handling date input should be straightforward. If a native JavaScript
Date
object is passed as a parameter, we could automatically cast it to a stringValue (assuming MySQL and Postgres could handle it).Parsing it back into a
Date
object might be a little more tricky. First, this would need to be optional. So we would likely add anotherconfig
parameter likeparseDates
. This could be passed on instantiation or as a query configuration parameter (likedatabase
) and override the global setting. Second, we'd need to inspect thecolumnMetaData
and whitelist data types that can be coerced.Looking for feedback on this feature.