Repository navigation
Description
Summary
Let SQLPage display a component wherever another component accepts rich content. A SQL query can return a new virtual data type, a component value. Component values contain a template name, and optionally parameters and children component values.
Motivation
SQLPage currently offers a rich but finite set of components, that cannot be nested. Customizing what appears inside a table cell, a card body, a list item, requires
- either forking the official sqlpage component
- using the
embedparameter (only in the card component). Embedded content is slow to load, and it requires an entire new sql file, and cannot easily share temporary data with the parent query
The goal is to make components reusable at both levels. An author should be able to keep a stock parent and change one part of it. Using stock components together should require only SQL. Writing a custom widget should require only its own template.
Proposal
sqlpage.component(name, properties[, rows]) constructs a component value.
nameselects a templatepropertiessupplies its top-level parameters- the optional
rowsarray supplies its row level parameters. The child renders in the same response using data the query already returned.
For example, use the built-in alert as the body of each card:
SELECT 'card' AS component, 2 AS columns;
SELECT service_name AS title,
sqlpage.component(
'alert',
json_object('description', status_message, 'color', status_color)
) AS description
FROM service_status;A custom template can accept a component value at an HTML content position with {{content "field"}}.
This either renders field as text, or renders another component when field comes from sqlpage.component.
Collections and nesting
A field can contain a component whose properties or rows contain other components. The number of children can come from a query. For example, group each person’s friends in SQL, then put a bulleted_list of image components in that person’s table row:
SELECT 'table' AS component;
WITH friends_by_person AS (
SELECT f.person_id,
json_group_array(
json_object('src', friend.avatar_url, 'alt', friend.name)
ORDER BY friend.name, friend.id
) AS avatars
FROM friendships AS f
JOIN people AS friend ON friend.id = f.friend_id
GROUP BY f.person_id
)
SELECT p.name AS "Person",
sqlpage.component(
'bulleted_list',
NULL,
sqlpage.component_rows('image', 'item', f.avatars)
) AS "Friends"
FROM people AS p
LEFT JOIN friends_by_person AS f ON f.person_id = p.id;In this SQLite example, component_rows is a proposed convenience for mapping each avatar object to a row with an item field containing an image component. The bulleted_list template wraps {{content "item"}} in an <li> for each row and works with any child component that fits there. A NULL avatar collection gives it zero rows. SQL determines membership and order; rendering does not issue another query per person or per friend.
Including shared SQL
This existing pattern must work unchanged, including when the included file returns component values:
SELECT 'dynamic' AS component,
sqlpage.run_sql('common_header.sql') AS properties;For example, moving the card example into common_header.sql must render the same nested alert as leaving it inline. SQLPage can pass the included rows to dynamic in memory without converting their component values to JSON. Existing uses of run_sql() that consume ordinary results as JSON keep the same behavior, and dynamic still accepts JSON as it does today. If a caller asks to serialize a result that contains component values, SQLPage reports an error rather than dropping the values or encoding them as instructions.
Semantics and limits
- Components go in supported display fields. A body can contain a widget. A URL, CSS class, form value, or accessibility label still takes a scalar. Not every child fits every display field: a linked card cannot contain another interactive control. Page-level components such as
shell,redirect, andhttp_headercannot be children. - A component value is distinct from text and JSON data. Strings keep their current escaping rules. SQLPage does not interpret a string or a JSON object as a component because it resembles one. The application chooses the template; its input data still gets normal escaping and URL validation where applicable. Rendering arbitrary HTML from data is a separate feature.
- Compose after relational operations. SQLPage can preserve component values inside
json_objectandjson_arrayexpressions it evaluates. A component value cannot be concatenated as text, passed through an arbitrary database function or aggregate, stored in a table, or exported as JSON or CSV. Aggregate ordinary data first, as in the example. Passing included SQL rows directly todynamicstays inside SQLPage and does not require serialization. - Preserve existing parent behavior. Adding a rich cell should not change sorting, search, or formatting in plain columns. A text-bearing rich cell can use its displayed text; a control-only cell has no meaningful default sort key. Custom sort and search keys are outside this proposal.
- Make errors actionable. Reject detectable misuses, such as string concatenation, before running the statement. A missing template or an invalid display position should report the enclosing component and field using the normal SQLPage error handling.
Alternatives
- Add more options to built-in components. This keeps simple cases concise, but each parent needs another option for each kind of content. It also gives no way to reuse an app’s own widget across parents.
- Accept sanitized HTML. Useful for limited markup, but it cannot reuse a component’s behavior. It also asks authors to construct markup when they already have structured data.
- Use
dynamicorembed. Keep both for their existing jobs: rendering a sequence of components or loading another page. This RFC covers displaying a component inside a field of the current page.
Initial scope and open questions
Prototype table cells and unlinked card bodies. Demonstrate a built-in child, a custom child, and a variable-length collection without a new request. Use the same insertion rule in built-in and custom parents. Verify that a component-valued field renders the same when its SQL is inline or included through dynamic and run_sql().
- Is a row-mapping helper like
component_rowsthe simplest way to turn query results into child components? What common collections would it fail to express? - Which additional display fields would remove a real template override or separate request? Which need tighter rules than a cell or card body?
Related use cases: #1641, #732, same-request dashboard layout, and inconsistent content options.
Activity
[-]RFC: Compose components inside table cells and other display fields[/-][+]RFC: Composable component values in SQLPage[/+]