Skip to content
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

Add ability to set the Record factory on the connection #40

Closed
elprans opened this issue Oct 31, 2016 · 7 comments · Fixed by #599
Closed

Add ability to set the Record factory on the connection #40

elprans opened this issue Oct 31, 2016 · 7 comments · Fixed by #599

Comments

@elprans
Copy link
Member

elprans commented Oct 31, 2016

In order to provide for alternative ways of accessing fields, or adding custom functionality, make it possible to specify a Record factory on a connection.

See #38 and #13 for related discussions.

@pmart123
Copy link

@elprans nice. Incorporating this issue is invaluable.

@Congee
Copy link

Congee commented Mar 22, 2019

This is necessary not only for dot accessor wanted in #38 and #13. I don't know any way to elegantly convert json, uuid objects from postgres to python objects except thru monkey patching.

What I am doing now is ugly IMO.

[
    {
        **record,
        'task_id': str(record['task_id']),  # uuid to str
        'rates': json.loads(t['rates']),  # jsonb to dict
    }
    for record in fetched_records
]

The ability to use customized Record is a must, dude. It's been more than 2 years

@1st1
Copy link
Member

1st1 commented Mar 22, 2019

The ability to use customized Record is a must, dude. It's been more than 2 years

Dear "dude", you're welcome to make a PR. Please make sure that the performance of asyncpg code that uses the builtin asyncpg.Record won't be impacted by it.

@elprans elprans mentioned this issue Jun 5, 2020
elprans added a commit that referenced this issue Jul 19, 2020
Add the new `record_class` parameter to the `create_pool()` and
`connect()` functions, as well as to the `cursor()`, `prepare()`,
`fetch()` and `fetchrow()` connection methods.

This not only allows adding custom functionality to the returned
objects, but also assists with typing (see #577 for discussion).

Fixes: #40.
elprans added a commit that referenced this issue Jul 19, 2020
Add the new `record_class` parameter to the `create_pool()` and
`connect()` functions, as well as to the `cursor()`, `prepare()`,
`fetch()` and `fetchrow()` connection methods.

This not only allows adding custom functionality to the returned
objects, but also assists with typing (see #577 for discussion).

Fixes: #40.
elprans added a commit that referenced this issue Jul 19, 2020
Add the new `record_class` parameter to the `create_pool()` and
`connect()` functions, as well as to the `cursor()`, `prepare()`,
`fetch()` and `fetchrow()` connection methods.

This not only allows adding custom functionality to the returned
objects, but also assists with typing (see #577 for discussion).

Fixes: #40.
elprans added a commit that referenced this issue Aug 8, 2020
Add the new `record_class` parameter to the `create_pool()` and
`connect()` functions, as well as to the `cursor()`, `prepare()`,
`fetch()` and `fetchrow()` connection methods.

This not only allows adding custom functionality to the returned
objects, but also assists with typing (see #577 for discussion).

Fixes: #40.
elprans added a commit that referenced this issue Aug 8, 2020
Add the new `record_class` parameter to the `create_pool()` and
`connect()` functions, as well as to the `cursor()`, `prepare()`,
`fetch()` and `fetchrow()` connection methods.

This not only allows adding custom functionality to the returned
objects, but also assists with typing (see #577 for discussion).

Fixes: #40.
elprans added a commit that referenced this issue Aug 14, 2020
Add the new `record_class` parameter to the `create_pool()` and
`connect()` functions, as well as to the `cursor()`, `prepare()`,
`fetch()` and `fetchrow()` connection methods.

This not only allows adding custom functionality to the returned
objects, but also assists with typing (see #577 for discussion).

Fixes: #40.
@Pirulax
Copy link

Pirulax commented Sep 28, 2022

Hey,
Is there any related docs on how to exactly use these custom classes?
What are they for exactly?
How do they work?

I have a similar issue the guy above (the "dude"), so I wonder what the solution to it would be using record_class.
So far I understand that I need to subclass Record and pass that to every function as record_class, but that's it.
I'd like to have namedtuple-like access (so instead of rec['id']to haverec.id`) + type hinting.

Thanks!

@elprans
Copy link
Member Author

elprans commented Sep 28, 2022

You need to subclass Record, e.g.:

class MyRecord(asyncpg.Record):
    def __getattr__(self, name):
        return self[name]

and then

conn = await asyncpg.connect(..., record_class=MyRecord)

You can also specify a record_class per-query.

@Pirulax
Copy link

Pirulax commented Sep 28, 2022

Okay I see.

I thought I could easily add typing to it too, but that doesn't seem to be the case.
So I'll just stick to the dataclass way I think.

(I'm using a connection pool btw)

@elprans
Copy link
Member Author

elprans commented Sep 28, 2022

You can actually add typing, though we need to land #577 to actually enable type checking.

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

Successfully merging a pull request may close this issue.

5 participants