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

adding dataclass to record model #33

Merged
merged 1 commit into from
Apr 9, 2023
Merged

Conversation

dsikes
Copy link
Contributor

@dsikes dsikes commented Mar 1, 2023

By adding data class support to the record model, you can simply use data class.asdict to have a JSON serializable object.
Here is a simple example:

import json
import dataclasses
from pocketbase import PocketBase 

client = PocketBase('http://127.0.0.1:8090')

client.admins.auth_with_password('[email protected]', 'fakepassword123')

# get a list of things from a collection
report = client.collection("users").get_list()

j = dataclasses.asdict(report)

print(json.dumps(j, indent=4))

Which gives

{
    "page": 1,
    "per_page": 30,
    "total_items": 2,
    "total_pages": 1,
    "items": [
        {
            "collection_id": {
                "avatar": "",
                "collectionId": "_pb_users_auth_",
                "collectionName": "users",
                "created": "2023-02-27 05:42:17.112Z",
                "email": "[email protected]",
                "emailVisibility": false,
                "id": "49lzk2x2ta1v60h",
                "name": "Dan Sikes",
                "phone": 9194023592,
                "reports_to": "sr8397188ya59y9",
                "role": "Crew Leader",
                "updated": "2023-03-01 21:28:05.160Z",
                "username": "dsikes",
                "verified": true
            },
            "collection_name": "",
            "expand": {}
        },
        {
            "collection_id": {
                "avatar": "",
                "collectionId": "_pb_users_auth_",
                "collectionName": "users",
                "created": "2023-02-27 07:02:45.896Z",
                "email": "[email protected]",
                "emailVisibility": false,
                "id": "sr8397188ya59y9",
                "name": "Blueberry",
                "phone": 4785758205,
                "reports_to": "",
                "role": "CEO",
                "updated": "2023-02-27 16:47:27.613Z",
                "username": "blueberry",
                "verified": true
            },
            "collection_name": "",
            "expand": {}
        }
    ]
}

This would close #27

@dsikes
Copy link
Contributor Author

dsikes commented Mar 3, 2023

@vaphes any thoughts on the proposed changes?

@ronnyandre
Copy link

This is a great addition to Pocketbase!

Copy link
Owner

@vaphes vaphes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, I think it would improve a lot the usability of the lib.
Aproved

@vaphes vaphes merged commit c0afb20 into vaphes:master Apr 9, 2023
abyesilyurt added a commit to abyesilyurt/pocketbase that referenced this pull request May 29, 2023
Record class was broken after this commit. See vaphes#37
This reverts commit c0afb20.
vaphes pushed a commit that referenced this pull request May 31, 2023
Record class was broken after this commit. See #37
This reverts commit c0afb20.
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

Successfully merging this pull request may close these issues.

add json serialization support for various types
3 participants