New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to set intervalStyle globally? #154
Comments
You would have to override the default parser for that, using I don't really know enough about the intervals to be more specific. |
Hmmm, isn't there any way to just issue a I am switching over from knex, and there I used to be able to do this:
I was assuming that |
Knex and this library use the exact same driver underneath. You can do the same with this library, but it is a terrible hack, which means you are executing a query on every single connection, slowing down your communications. Are you sure you want to do something like this? What's even worse, it would execute the command on every virtual connection, since both libraries use the connection pool. It would be a lot more efficient if you needed it within a separate task. |
A similar ugly hack via var pgOptions = {
connect: function(client){
client.query('SET intervalStyle = iso_8601');
}
}; I'm not sure it would work, but you can try. The only proper way to do it is via the connection parameters: https://www.postgresql.org/docs/9.5/static/runtime-config-client.html, but their support by the driver is too limited. |
Thanks, Vitaly. I see that the feature described above is at the knex level - see here. However, can you please clarify why this will slow down the communications? If I understand it correctly, Another consideration is that by default Postgres is sending intervals like this: Would love to hear your thoughts. |
That code in Can you not use it within a separate task? Are you sure you need it for the entire application? |
By the way, this gave me an idea about adding a flag into event |
You just read my mind :-). I was going to ask you how connection pooling works. From your answer, I am assuming that a connection obtained from the pool can either be a fresh connection or a previously used connection. I would have to run |
Yes. Can you try that hack in the meantime - to see if that even works? |
Just tried it. It is not working. Investigating. Here's my code:
When I
However the same query in psql returns correctly:
Also used to work in knex. |
Ah, I think I know what it is. In my knex implementation, I had to override pg-type for this.
|
Does this mean that it now works for you? This was the solution I suggested from the beginning ;) |
It is working now, but with both pieces of code applied (which is my intent):
The combination works like a charm! Now if I could only have the fresh connection flag :-) |
COOL. I am working on it now ;) |
This seems to be possible to implement only for the JS client, but not for the Native Bindings. This is quite a bummer. |
It is a bummer. I am using the JS client though. |
I've got it working :) |
@nareshbhatia released in v.4.4.3 ;) |
I have figured out how to make this work for Native Bindings also. It ain't pretty, and not as efficient as for JS Bindings, but it works. I will release it later. |
@vitaly-t, thanks so much! I am having bit of an issue compiling with TypeScript:
I am getting this error:
Are the type definitions updated? I tried installing fresh type definitions, but I get this error:
|
Ouch, in a rush, I forgot to update the typescript. replace this line: connect?:(client:pg.Client, dc:any) => void; with this one: connect?:(client:pg.Client, dc:any, fresh:boolean) => void; I will fix it later ;) |
No worries. |
@nareshbhatia All done, see v.4.4.4 |
I have completely refactored it in 4.4.5, making it much simpler, and with much better performance. |
@vitaly-t, thanks so much for all your help on this issue. All working smoothly now. |
Maybe I missed something, but I think it should be as simple as returning pgp.pg.types.setTypeParser(INTERVAL_OID, stringVal => PostgresInterval(stringVal).toISO()); Then the database can send any string that postgres-interval understands and your application gets an ISO string without needing to init the connection. |
@tmtron It may be that simple, I don't know. But the issue did open a good possibility to configure fresh connections automatically. This API was updated since though, and the correct syntax now is like this: connect(client, dc, useCount) {
// For fresh connections, ask Postgres to return intervals as ISO 8601 strings:
const isFreshConnection = useCount === 0;
if (isFreshConnection) {
client.query('SET intervalStyle = iso_8601');
}
} |
@vitaly-t this has the same effect as using the pg-pool connect event |
@tmtron I can't say for sure, those are two separate things. This library does not use the |
I would like to tell Postgres to return intervals as ISO 8601 strings always and on all connections. To do this I must set
intervalStyle
as follows:What's the best way to do this? I tried
db.none('SET intervalStyle = iso_8601');
but that's not working. I suspect it is setting the value on only one connection.The text was updated successfully, but these errors were encountered: