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

A simple implementation for update_multiple_by_id #550

Open
abse4411 opened this issue Jan 2, 2024 · 0 comments
Open

A simple implementation for update_multiple_by_id #550

abse4411 opened this issue Jan 2, 2024 · 0 comments

Comments

@abse4411
Copy link

abse4411 commented Jan 2, 2024

It seems that update_multiple() doesn't support updating data by the corresponding doc_id using QueryLike. So this is my simple implementation for it:

class MyTable(Table):
    def __init__(self, storage: Storage, name: str):
        super().__init__(storage, name)

    def update_multiple_by_id(
            self,
            updates: Iterable[
                Tuple[Union[Mapping, Callable[[Mapping], None]], int]
            ],
    ) -> List[int]:
        """
        Update all matching documents to have a given set of fields.

        :returns: a list containing the updated document's ID
        """

        # Define the function that will perform the update
        def perform_update(fields, table, doc_id):
            if callable(fields):
                # Update documents by calling the update function provided
                # by the user
                fields(table[doc_id])
            else:
                # Update documents by setting all fields from the provided
                # data
                table[doc_id].update(fields)

        # Perform the update operation for documents specified by a query

        # Collect affected doc_ids
        updated_ids = []

        def updater(table: dict):
            # We need to convert the keys iterator to a list because
            # we may remove entries from the ``table`` dict during
            # iteration and doing this without the list conversion would
            # result in an exception (RuntimeError: dictionary changed size
            # during iteration)
            for fields, doc_id in updates:
                if doc_id in table:
                    # Add ID to list of updated documents
                    updated_ids.append(doc_id)

                    # Perform the update (see above)
                    perform_update(fields, table, doc_id)

        # Perform the update operation (see _update_table for details)
        self._update_table(updater)

        return updated_ids

Then replace the TinyDB.table_class by the MyTable before creating a TinyDB instance.

TinyDB.table_class = MyTable
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

1 participant