Collection override

This is the official documentation of the forestadmin-agent-django and forestadmin-agent-flask Python agents.

Forest Admin allows customizing at a very low level the behavior of any given Collection via the usage of Collection Overrides.

Collection Overrides provide a powerful means to completely replace the default behavior of CUD operations (create, update, delete) for your Collections. This feature should be used with caution, as it directly affects the core operations on your data.

How it Works

In addition to the standard Collection functions:

  • create

  • update

  • delete

Collection Overrides allow you to define custom behavior that will entirely replace the default implementation of the create, update, and delete operations.

To define an Override for a Collection, you must specify:

  • The handler function that will be executed instead of the default operation.

The custom handler function will receive a context object containing relevant information for the operation, allowing for comprehensive control over the behavior of these CUD operations.

Setting Up Overrides

Overrides are declared similarly to hooks but are aimed at replacing an entire operation rather than augmenting its execution. However this can also be used to enrich the default behavior. Here's how to set up overrides in your Collection:

Custom Create Operation

To replace the default create operation, use override_create with your custom handler:

from forestadmin.datasource_toolkit.decorators.override.context import CreateOverrideCustomizationContext

async def create_handle(context: CreateOverrideCustomizationContext)
    # Custom logic to handle creation
    # context.data contains the data intended for creation
    # Return an array of created records

collection.override_create(create_handle)

Custom Update Operation

To replace the default update operation, use override_update with your custom handler:

from forestadmin.datasource_toolkit.decorators.override.context import UpdateOverrideCustomizationContext

async def update_handle(context: UpdateOverrideCustomizationContext)
    # Custom logic to handle update
    # context.filter to determine which records are targeted
    # context.patch contains the data for update
    # Perform update operation

collection.override_update(update_handle)

Custom Delete Operation

To replace the default delete operation, use override_delete with your custom handler:

from forestadmin.datasource_toolkit.decorators.override.context import DeleteOverrideCustomizationContext

async def delete_handle(context: DeleteOverrideCustomizationContext)
    # Custom logic to handle deletion
    # context.filter to determine which records are targeted
    # Perform deletion operation

collection.override_delete(delete_handle)

In theses examples, we use async function as handler, but you can also use sync or lambda function.

Overrides take precedence over the default operation. Ensure your custom handlers properly manage all necessary logic for the operation, as the default behavior will not be executed.

Basic Use Cases

Create over API

You might want to create the record with your custom API:

import requests
from forestadmin.datasource_toolkit.decorators.override.context import CreateOverrideCustomizationContext

def create_handle(context: CreateOverrideCustomizationContext)
    response = requests.post("https://my-product-api.com/products", json=context.data)
    return response.json()

collection.override_create(create_handle)

Modify data before update

You might want to modify payload data before update your record:

import requests
from forestadmin.datasource_toolkit.decorators.override.context import UpdateOverrideCustomizationContext

async def update_handle(context: UpdateOverrideCustomizationContext)
    # Execute data modification and validation only if one of name or slug was edited
    if "name" in context.patch or "slug" in context.patch:
        name = context.get("name", context["slug"].split("-")[0])
        response = requests.get("https://my-product-api.com/slug", json={"name": name})
        uuid = response.text

        context.patch["name"] = name
        context.patch["slug"] = f"{name}-{uuid}"

    await context.collection.update(context.filter, context.patch)

collection.override_update(update_handle)

Last updated