Skip to content

Declarative DataSource with reactive query bindings #155

@jlowin

Description

@jlowin

Summary

Add a DataSource component that declares a data query, caches results, and exposes them as a reactive state variable that other components can bind to.

Motivation

Today, data must be fetched in Python before building the UI, or fetched via CallTool/Fetch actions wired manually. A declarative DataSource would let developers express "this app needs this data, refreshed on this schedule, parameterized by these inputs" as part of the component tree.

Proposed API

from prefab import DataSource, DataTable, Slider

DataSource(
    name="sales",
    query="SELECT * FROM sales WHERE revenue >= {{ min_rev }}",
    connection=os.environ["DATABASE_URL"],
    ttl="5m",
)

Slider(name="min_rev", min=0, max=1000, default=500)
DataTable(data="{{ sales }}")

Key properties

  • name: state key where results are stored
  • query: SQL string with {{ }} template bindings for reactive parameters
  • connection: database URL or connection reference
  • ttl: cache duration — results are reused until TTL expires
  • refresh: optional binding to a manual refresh trigger (e.g., a button)

Reactive behavior

When a referenced variable ({{ min_rev }}) changes, the query re-executes (respecting debounce). Results are written to the named state key. All components bound to {{ sales }} update automatically.

Implementation considerations

  • Query execution happens server-side (Python process or MCP tool)
  • Could be implemented as sugar over Computed — a DataSource compiles to a Computed action targeting a built-in query-execution tool
  • Connection pooling, parameterized queries (SQL injection prevention), and error handling are critical
  • Should support pandas, SQLAlchemy, and raw DB-API connections

Priority

This is a post-launch enhancement that builds on Computed. Shipping DataFrame support and Computed first provides the foundation this depends on.

Open questions

  • Should DataSource support non-SQL sources (REST APIs, file paths, S3)?
  • How to handle schema discovery (column names/types) before the first query runs?
  • Should failed queries surface in the UI automatically (error state on bound components)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementImprovement to existing functionality or new capabilities.pythonRelated to the Python SDK: components, actions, serialization.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions