Is there a way to opt out of time parsing for those rare occasions when precision matters?
I'm using timestamptz in a compound unique key (user_id, created_at)
to store time series values. However i can't paginate because javascript truncates created_at
field to milliseconds, so when there's two created within the same millisecond, i get duplicates.
-- record --
id: 'c2ed61d5-ffab-4b0e-a957-c184b3f33132',
created_at: '2016-02-02 15:46:17.681601-05'
precise ^
With a knex query:
knex("series").where("created_at", ">", last.created_at).toString()
select * from series
where created_at > '2016-02-02T15:46:17.681-05:00'
truncated ^
Opting out of timestamp parsing is a feature we might like to have. A customizable 'parseTime' function or similar perhaps.
However, I'm not sure about your specific case. There is a limit to the granularity of the computer clock, so requiring unique created_at
values is not going to scale.
@aj0strow Do you mean that you would like to prevent knex
from converting timestamp
to Javascript Date object when rows are read from DB?
If so, that parsing is done at least for PostgreSQL by pg
driver. You can setup driver to parse types differently with a code something like this:
var pgTypes = require('pg').types;
// Don't parse dates to js Date() objects
pgTypes.setTypeParser(1082, 'text');
// Don't parse timestamps to js Date() objects
pgTypes.setTypeParser(1184, 'text');
You can also give driver your own parser function instead of text
parameter.
@rhys-vdw it's unique on user + time, so will be fine.
@elhigu thanks!
That's exactly what i was looking for. It's also possible to cast to ::text
in the query but it's error prone for me, so better to turn off parsing and parse new Date
when required. Thanks again.
Most helpful comment
@aj0strow Do you mean that you would like to prevent
knex
from convertingtimestamp
to Javascript Date object when rows are read from DB?If so, that parsing is done at least for PostgreSQL by
pg
driver. You can setup driver to parse types differently with a code something like this:You can also give driver your own parser function instead of
text
parameter.