2

I'm trying to save a timestamptz with microsecond precision like this one:

2016-10-18T13:36:38.431555

I have tested PostgreSQL alone and it works fine:

CREATE TABLE test(mytime timestamptz);
INSERT INTO test('2016-10-18T13:36:38.431555');
SELECT * FROM test;

the select returns the correct precision:

2016-10-18T13:36:38.431555

Then I tested with nodejs and the pg connector using the same sql queries.

Passing the timestamp as a string works but if I want to pass a Date object then I loose the microsecond precision. This is a JavaScript limitation since the Date object has a maximum resolution of milliseconds. To bypass that I converted the timestamp to a bigint with this function:

function convertToUnixTS(timestamp) {
    return new Date(timestamp).getTime() * 1000 + parseInt(timestamp.substr(timestamp.length - 3));
}

the bigint is transparent to postgres and the select query returns the right timestamp

2016-10-18T13:36:38.431555

Now to Waterline, I have created a collection with the postgres adapter and define my timestamp as a datetime

Persistence.prototype.collections.Value = Waterline.Collection.extend({
    identity: 'value',
    connection: 'default',
    attributes: {
        mytimestamptz: {
            type: 'datetime',
            required: true
        },
        myvalue: {
            type: 'float',
            required: true
        }
    }
});

This will save my timestamps with resolution to seconds, there are possible patches proposed on the waterline github to support milliseconds (which I have tested and it is working) but I am still missing the 3 extra digits for microsecond precision. As I have demonstrated, the only way in JavaScript to handle this is to convert it to a bigint. Passing a bigint to my current model would throw an exception so I tried to change my model like this

mytimestamptz: {
    type: 'integer',
    size: 64,
    required: true
}

and

mytimestamptz: {
    type: 'bigint',
    required: true
}

but now the waterline framework throws an invalid attribute error.

(node:12964) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 4): [Error (E_VALIDATION) 1 attribute is invalid] Invalid attributes sent to event:
 • di_timestamptz
   • [object Object]

while as a string I see:

1477488110007650

Plus, I would really prefer to have it as a timestamptz.

Is it possible?

Thanks

Remz1337
  • 105
  • 8

0 Answers0