Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: ordering by timestamp fields #57

Closed
endymion00 opened this issue Jan 14, 2017 · 2 comments
Closed

Question: ordering by timestamp fields #57

endymion00 opened this issue Jan 14, 2017 · 2 comments

Comments

@endymion00
Copy link

Hi,
I'm wondering how can I deal with sorting by using tiemstamp fields createdAt and updatedAt because there are strings and dynamoDB does not have a date type so numbers should be used.
Thank you in advance!

@M1chaelTran
Copy link

M1chaelTran commented Jan 14, 2017

hi @endymion00

It funny that you ask because we have to do the same thing just last week.
I spend a whole day to figure out how to do this... so hoped you didn't have to go through the same ordeal.

Yes, dynamoDB stores data type as string...
but they have RangeKey that works with dates, with the exception that the date string are stored in UTC.

so what you need to do is set the createdAt or updatedAt as the rangeKey
on insertion, you'll need to set the createdAt as part of the insertion (recommended to use momentjs moment.utc().toISOString())

then on query, you'll need to do something along the line of:

OrderTable.scan().loadAll()
        .filterExpression('CreatedAt BETWEEN :fromDate AND :toDate')
        .expressionAttributeValues({':fromDate': fromDate, ':toDate': toDate})
        .projectionExpression('OrderName, OrderId, OrderAddress') \\ optional

@endymion00
Copy link
Author

hey @M1chaelTran,

Thank you very much for help :)
I've started the migration from number dates to ISO string dates since it is more human readable for application data troubleshooting and also to avoid transformations when sending and receiving dates from an API (we use API Gateway over dynamoDB)

And yes, moment is the way to go. I feel very confident by using this module in all my node projects. In this case with one single of line of code it is possible to migrate from number dates to string dates easily.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants