crud icon indicating copy to clipboard operation
crud copied to clipboard

[Question] How to use unix epoch as format for dates?

Open alexmantaut opened this issue 4 years ago • 3 comments

Hi,

I have a few entities with attributes of time Date(). I would like to handle all of my date objects on Unix epoch format, I was wondering what is the best way to do that.

For the output of the requests I have an interceptor on the output of controllers, which checks the type of every attribute, and if it detects a date converts to unix epoch.

But I am not sure on how to change the format for the input of the controllers (this is the body of a POST, PATCH, etc) Right now I am doing that conversion manually. Also I am not sure on how to convert the attributes passed to filters. I would like for filter=event.date::gt::1584018000000 to work.

So, what would be the best way to use unix epochs with the crud controllers?

Thanks for reading,

alexmantaut avatar Mar 12 '20 22:03 alexmantaut

You can use TypeORM column transformer: transformer: { from(value: DatabaseType): EntityType, to(value: EntityType): DatabaseType }

or maybe just timestamp column type?

hakimio avatar Mar 21 '20 12:03 hakimio

Thanks for answering, sorry for the delay,

I think that transformer and interceptors allow me to deal with most of the cases, but it does not seem to deal with the query parameters... It might be an issue with how typeorm applies the transformers before executing the query builder, I need to look into it a bit more...

The timestamp column type didn't seem to make a difference though...

alexmantaut avatar Mar 25 '20 03:03 alexmantaut

I also met your problem ~ it seems that there is no good way at present.

jinpy666 avatar Dec 17 '21 08:12 jinpy666