vaphes / pocketbase

PocketBase client SDK for python
https://pypi.org/project/pocketbase/
MIT License
332 stars 38 forks source link

adding dataclass to record model #33

Closed dsikes closed 1 year ago

dsikes commented 1 year ago

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('foo@bar.com', '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": "foobar@gmail.com",
                "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": "foobar7@gmail.com",
                "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 commented 1 year ago

@vaphes any thoughts on the proposed changes?

ronnyandre commented 1 year ago

This is a great addition to Pocketbase!