-
Notifications
You must be signed in to change notification settings - Fork 2
Description
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 storedquery: SQL string with{{ }}template bindings for reactive parametersconnection: database URL or connection referencettl: cache duration — results are reused until TTL expiresrefresh: 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— aDataSourcecompiles to aComputedaction 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
DataSourcesupport 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)?