Using luxon and node-pg: Dates are never equal

69 Views Asked by At

I made a simple script:

this.db
  .query(
    `SELECT * FROM message WHERE id='5960ac15-34d6-472a-abe4-aec164a7bc90'`,
  )
  .then(async ({ rows }) => {
    console.log(rows);
    const expected = rows[0].creation_date;
    const dateTime = DateTime.fromSQL(expected);
    const result = dateTime.toSQL();

    console.log(expected === result);
    console.log(expected, typeof expected);
    console.log(result);

    const { rows: test } = await this.db.query(
      `SELECT * FROM message WHERE creation_date=$1`,
      [result],
    );
    console.log(test);
  });

This.db correspond to a Pool object. So I select a particular object in my db using his ID. Next to it I try to get back the same object but this time using the creation_date field.

I've tried many result variable changes. Like toIso(), fromISO()... But none seems to work. I never get my object back in my second query.

I know that node-pg parse dates to JS dates. So I overrided the parsers:

types.setTypeParser(1114, function (value) {
  return value;
});
types.setTypeParser(1082, (value) => value);

Which is working because now, if I pass expected to the second query instead of result, I know get back my object. My creation_date field is timestamp in my db.

This script is a simulation of my frontend/backend communication. My frontend need to transform the original field to luxon DateTime and I send back this date. I can use less than or greater than operators but not equal.

Can someone help me?

The expected format is: '2023-09-04 01:34:44.522983'

1

There are 1 best solutions below

0
Splinteer On

Ok, so I finaly managed to found why. The precision of my timestamp in my db was in microseconds but luxon keep only milliseconds in my case.

I updated my db script from:

creation_date timestamp NOT NULL DEFAULT NOW()

To

creation_date timestamp(3) NOT NULL DEFAULT NOW()