surrealdb / surrealdb.py

SurrealDB SDK for Python
https://surrealdb.com
Apache License 2.0
170 stars 49 forks source link

Feature: add method 'merge' to the HTTP client #79

Closed mivinci closed 4 months ago

mivinci commented 7 months ago

Is your feature request related to a problem?

The current HTTP client has no method merge to partially update record(s) by performing a query like

UPDATE person:tobbie MERGE {
    settings: {
        active: true
    }
};

Describe the solution

According to the official documentation from https://surrealdb.com/docs/integration/http#modify-one, it can be easily solved by adding the following code to class SurrealHTTP

async def merge(self, thing: str, data: Any) -> Dict[str, Any]:
        """Partially update records in a table, or a specific record, in the database.

        This function partially replaces the current document / record data with the
        specified data.

        This function will run the following query in the database:
        update $thing merge $data

        Args:
            thing: The table or record ID.
            data: The document / record data to update.

        Examples:
            Update all records in a table
                person = await db.merge('person')

            Update a record with a specific ID
                record = await db.merge('person:tobie', {
                    'name': 'Tobie',
                    'settings': {
                        'active': true,
                        'marketing': true,
                        },
                })
        """
        table, record_id = thing.split(":") if ":" in thing else (thing, None)
        response = await self._request(
            method="PATCH",
            uri=f"/key/{table}/{record_id}" if record_id else f"/key/{table}",
            data=json.dumps(data, ensure_ascii=False),
        )
        return response[0]['result']

and I've tested that it works correctly.

Alternative methods

no yet

SurrealDB version

surreal 1.0.0 for Linux on x86_64

surrealdb.py version

surrealdb.py 0.3.1 for Linux on x86_64 using Python 3.10

Contact Details

mivinci@qq.com

Is there an existing issue for this?

Code of Conduct

maxwellflitton commented 5 months ago

Hey @mivinci thanks for being patient with us, we have been working on a big pull request over some time to get the backend of the python client to be powered by Rust which adds a lot more features. In this pull request, we are adding the merge as you can see here:

https://github.com/surrealdb/surrealdb.py/pull/76/files#diff-ee534fcaa73a2572af647e23bce8dc03112b00aee215a9a663f51c2809087b57

You will be able to call in async and blocking functions for the new python client

mivinci commented 4 months ago

Cool! That looks awesome and way better.