Search code examples
postgresqltypeorm

stop further date conversion which is already in UTC


By default any date/timestamp stored in postgres is in standard UTC. Postgres has the DATE column type that stores only the date part of a full timestamp.

When using typeorm for postgres, and using the repositories the date is fetched as is. Yet, when you do something with a raw query like this:

const queryRunner = await this.connection.createQueryRunner();
await queryRunner.connect()
const response = await queryRunner.query('SELECT * FROM MY_VIEW WHERE AGE=23');

For the same date(only) column I receive something like this, which has its value as '1999-01-02'

{
  name: 'mleko',
  age : '23',
  dob : '1999-01-01:T22:00:00:000Z',
  address: 'xyz'
}  

I'm not sure where exactly is this conversion taking place, probably the underlying driver that typeorm uses, yet, how do I tell typeorm to not do this conversion for a date already in UTC into again a UTC.


Solution

  • So, the way I got around this was doing a cast on the raw query:

    const queryRunner = await this.connection.createQueryRunner();
    await queryRunner.connect()
    const response = await queryRunner.query('SELECT name, age, dob::VARCHAR, address FROM MY_VIEW WHERE AGE=23');  
    

    this would avoid any conversions