Metadata-Version: 2.4
Name: notion_cascade_insert
Version: 0.0.1
Summary: Cascade inserting for Notion Database Automation (as log)
Home-page: https://github.com/amezaikupan/notion_cascade_insert
Author: Solveit
Author-email: nobody@fast.ai
License: Apache-2.0
Keywords: nbdev jupyter notebook python
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Natural Language :: English
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: fastcore
Requires-Dist: fastapi
Requires-Dist: notion_client
Provides-Extra: dev
Dynamic: author
Dynamic: author-email
Dynamic: classifier
Dynamic: description
Dynamic: description-content-type
Dynamic: home-page
Dynamic: keywords
Dynamic: license
Dynamic: license-file
Dynamic: provides-extra
Dynamic: requires-dist
Dynamic: requires-python
Dynamic: summary

# Notion Cascade Insert


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

## Installing

``` python
! pip install notion_cascade_insert
```

    Requirement already satisfied: notion_cascade_insert in /app/data/.local/lib/python3.12/site-packages (0.0.1)
    Requirement already satisfied: fastcore in /usr/local/lib/python3.12/site-packages (from notion_cascade_insert) (1.12.2)
    Requirement already satisfied: fastapi in /usr/local/lib/python3.12/site-packages (from notion_cascade_insert) (0.128.0)
    Requirement already satisfied: notion_client in /app/data/.local/lib/python3.12/site-packages (from notion_cascade_insert) (2.7.0)
    Requirement already satisfied: starlette<0.51.0,>=0.40.0 in /app/data/.local/lib/python3.12/site-packages (from fastapi->notion_cascade_insert) (0.50.0)
    Requirement already satisfied: pydantic>=2.7.0 in /usr/local/lib/python3.12/site-packages (from fastapi->notion_cascade_insert) (2.12.5)
    Requirement already satisfied: typing-extensions>=4.8.0 in /usr/local/lib/python3.12/site-packages (from fastapi->notion_cascade_insert) (4.15.0)
    Requirement already satisfied: annotated-doc>=0.0.2 in /usr/local/lib/python3.12/site-packages (from fastapi->notion_cascade_insert) (0.0.4)
    Requirement already satisfied: anyio<5,>=3.6.2 in /usr/local/lib/python3.12/site-packages (from starlette<0.51.0,>=0.40.0->fastapi->notion_cascade_insert) (4.12.1)
    Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.12/site-packages (from anyio<5,>=3.6.2->starlette<0.51.0,>=0.40.0->fastapi->notion_cascade_insert) (3.11)
    Requirement already satisfied: annotated-types>=0.6.0 in /usr/local/lib/python3.12/site-packages (from pydantic>=2.7.0->fastapi->notion_cascade_insert) (0.7.0)
    Requirement already satisfied: pydantic-core==2.41.5 in /usr/local/lib/python3.12/site-packages (from pydantic>=2.7.0->fastapi->notion_cascade_insert) (2.41.5)
    Requirement already satisfied: typing-inspection>=0.4.2 in /usr/local/lib/python3.12/site-packages (from pydantic>=2.7.0->fastapi->notion_cascade_insert) (0.4.2)
    Requirement already satisfied: packaging in /usr/local/lib/python3.12/site-packages (from fastcore->notion_cascade_insert) (25.0)
    Requirement already satisfied: httpx>=0.23.0 in /usr/local/lib/python3.12/site-packages (from notion_client->notion_cascade_insert) (0.28.1)
    Requirement already satisfied: certifi in /usr/local/lib/python3.12/site-packages (from httpx>=0.23.0->notion_client->notion_cascade_insert) (2026.1.4)
    Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.12/site-packages (from httpx>=0.23.0->notion_client->notion_cascade_insert) (1.0.9)
    Requirement already satisfied: h11>=0.16 in /usr/local/lib/python3.12/site-packages (from httpcore==1.*->httpx>=0.23.0->notion_client->notion_cascade_insert) (0.16.0)

## How to use

### Building blocks

This package is built to emulate the `Notion Automation` function but
with one to many relation. I built this to track the transactions (hence
the log) that involves calculating in case where there is an big object
that map to many other objects, like when you have a recipe and you want
to get the ingredients, or when you have a lists of guests and you want
to get their details… This helps you manage the ammoun of stuff that
relate to that big object.

There are 4 of the pipelines:

1.  **TriggerDB**: monitors a database for status changes  
2.  **JunctionDB**: looks up related items and amounts  
3.  **LogDB**: writes transaction logs  
4.  **AutoLogger**: orchestrates the flow

We can than use them to connect to `Notion Webhook` and create the
functions that we want.

### Example

<!-- <img src="img/mermaid-diagram-2026-01-24-213107.png"> -->

<img src="img/mermaid-diagram-2026-01-24-213107.png" style="width: 100%; height: auto; max-height: none;">

Let’s say we’re building a Bakery Inventory management database, and we
want our `Production Plan` database to automatically log the used
ingredients in a recipe that we want to make. This is a one-to-many
behavior, which Notion don’t support at the momment. For this, we would
do something like:

``` python
from notion_cascade_insert.core import TriggerDB, JunctionDB, LogDB, AutoLogger
from notion_cascade_insert.webhook import NotionWebhook 
from fastapi import FastAPI, Request 
from notion_client import Client 
import os
```

``` python
notion = Client(auth=os.getenv("NOTION_TOKEN"))

trigger = TriggerDB(os.getenv("PRODUCTION_PLAN_DB_ID"), notion, "Status", "Recipes", "Batches to make")
junction = JunctionDB(os.getenv("RECIPE_INGREDIENTS_DB_ID"), notion, "Recipes", "Ingredient Inventory", "Amount per batch")
log = LogDB(os.getenv("INGREDIENT_TRANSACTION_DB"), notion, "Ingredient", "Amount", "Production Plan", "Reason")
db_logger = AutoLogger(trigger, junction, log, "In Process", -1)
```

This will create your Ingredient Logger! Then you can set up your server
like so:

``` python
app = FastAPI()

@app.post("/webhook")
async def webhook(request: Request):
    hook = NotionWebhook(await request.json())
    if hook.parent_db_id == os.getenv("PRODUCTION_PLAN_DB_ID"):
        if hook.type == 'page.created': return {"result": db_logger.process(hook.entity_id)}
    return {"status": "received"}
```
