Skip to content

Custom Codec across connection pool #221

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

Closed
egalpin opened this issue Nov 1, 2017 · 4 comments
Closed

Custom Codec across connection pool #221

egalpin opened this issue Nov 1, 2017 · 4 comments
Labels

Comments

@egalpin
Copy link

egalpin commented Nov 1, 2017

  • asyncpg version: 0.13.0
  • PostgreSQL version: 9.6.5 (Docker)
  • Do you use a PostgreSQL SaaS? If so, which? Can you reproduce
    the issue with a local PostgreSQL install?
    :
  • Python version: 3.6.0
  • Platform: Mac OS X
  • Do you use pgbouncer?: No
  • Did you install asyncpg with pip?: Yes
  • If you built asyncpg locally, which version of Cython did you use?:
  • Can the issue be reproduced under both asyncio and
    uvloop?
    : yes

Related to #140, I originally set out to set a custom codec for JSONB (which the thread of issue 140 solved for me).

The current solution for overriding builtin codecs for various types is great on a per connection basis. This works great if you're using a single connection object everywhere, but becomes troublesome when working with a connection pool.

Is there an abstraction layer that I'm misunderstanding (i.e. just use connection and a pool is managed), or is there a pleasant way to go about adding a custom codec to all the connections in my app's pool on start up?

Thanks!

@elprans
Copy link
Member

elprans commented Nov 1, 2017

You want to use the init argument to create_pool():

async def init_connection(conn):
    await conn.set_type_codec(...)

await create_pool(..., init=init_connection)

@elprans elprans closed this as completed Nov 1, 2017
@egalpin
Copy link
Author

egalpin commented Nov 1, 2017

@elprans beautiful. Thanks for the quick reply!

@golkedj
Copy link

golkedj commented Jul 10, 2020

I am trying to do this now and even when providing init_connection to create_pool I get a string type back for my jsonb column

@golkedj
Copy link

golkedj commented Jul 10, 2020

Sorry I actually was just making a silly mistake and setting up the codec for json and not jsonb. I have it working now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants