How to Defer Commands


  • The defer method allows you to register server commands via the session.

  • All the deferred requests will be stored in the session and sent to the server in a single batch when save_changes is called, along with any other changes/operations made on the session.
    Thus, all deferred commands are executed as part of the session's save_changes transaction.

  • In this page:


Defer commands example

# Defer is available in the session's advanced methods
session.advanced.defer(
    # Define commands to be executed:
    # i.e. Put a new document
    PutCommandDataBase(
        "products/999-A",
        None,
        {"Name": "My Product", "Supplier": "suppliers/1-A", "@metadata": {"@collection": "Products"}},
    ),
    # Patch document
    PatchCommandData("products/999-A", None, PatchRequest("this.Supplier = 'suppliers/2-A'"), None),
    # Force a revision to be created
    ForceRevisionCommandData("products/999-A"),
    # Delete a document
    DeleteCommandData("products/1-A", None),
)

# All deferred commands will be sent to the server upon calling save_changes
session.save_changes()

Commands that can be deferred

The following commands implement the ICommandData interface and can be deferred:

Syntax

def defer(self, *commands: CommandData) -> None: ...
Parameter Type Description
*commands CommandData An array of commands to be executed