Skip to content

Access Provided Codecs at Runtime #915

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
seandstewart opened this issue May 16, 2022 · 1 comment
Closed

Access Provided Codecs at Runtime #915

seandstewart opened this issue May 16, 2022 · 1 comment

Comments

@seandstewart
Copy link

  • asyncpg version: 0.25.0
  • PostgreSQL version: 12
  • Do you use a PostgreSQL SaaS? If so, which? Can you reproduce
    the issue with a local PostgreSQL install?
    : n/a
  • Python version: 3.9
  • Platform: MacOS and Linux
  • Do you use pgbouncer?: no
  • Did you install asyncpg with pip?: yes
  • If you built asyncpg locally, which version of Cython did you use?: n/a
  • Can the issue be reproduced under both asyncio and
    uvloop?
    : n/a

Hello - I was wondering if it's possible to access the provided codecs for types such as Range at runtime?

Use-case: I'd like to pass my objects in as a JSON array rather than an array of composite types. However, one field is a Range type, which isn't json-encodable by default. I was hoping to use your optimized encoders when we encounter such types.

@ljluestc
Copy link

import asyncpg
from asyncpg import range as asyncpg_range

# Define a custom encoder function for the Range type
def encode_range(range_obj):
    return f"[{range_obj.lower},{range_obj.upper})"

# Register the custom codec
asyncpg_range.Range._binary_encoder = encode_range

# Create a connection pool with the custom codec
async def create_db_pool():
    pool = await asyncpg.create_pool(
        dsn="postgres://....",
        host="....amazonaws.com",
        user="xxx",
        database="yyy",
        port="5432",
        password="12345"
    )
    return pool

@seandstewart seandstewart closed this as not planned Won't fix, can't repro, duplicate, stale May 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants