Knex: Opt out of timestamptz parsing?

Created on 16 Mar 2016  ·  3Comments  ·  Source: knex/knex

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 ^
discussion question

Most helpful comment

@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.

All 3 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

arconus picture arconus  ·  3Comments

lanceschi picture lanceschi  ·  3Comments

sandrocsimas picture sandrocsimas  ·  3Comments

rarkins picture rarkins  ·  3Comments

marianomerlo picture marianomerlo  ·  3Comments