clarkie / dynogels

DynamoDB data mapper for node.js. Originally forked from https://github.com/ryanfitz/vogels
Other
490 stars 110 forks source link

Question: ordering by timestamp fields #57

Closed endymion00 closed 7 years ago

endymion00 commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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.