Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions docs/contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const dataApp = defineDataApp({
appearance: { theme: 'system' },
queries: {
products: {
statement: 'SELECT * FROM products WHERE org_id = $orgId LIMIT $limit',
statement:
'SELECT name, unit_price AS "unitPrice" FROM products WHERE org_id = $orgId LIMIT $limit',
params: { orgId: {}, limit: { defaultValue: 10 } },
},
},
Expand All @@ -39,14 +40,17 @@ const products = dataApp.defineOperation({
policy: { maxQueryRows: 100, maxDurationMs: 15000 },
async run({ query }, input) {
const result = await query('products', input);
return parseProducts(rowsAsRecords(result, ['name', 'price']));
return parseProducts(rowsAsRecords(result, ['name', 'unitPrice']));
},
});
```

`parseProductInput()` and `parseProducts()` are app-owned example functions, not
package exports. Validate filter values in the input parser. Parse query rows once, in
`run`: the value it returns is the operation's data and type, sent to the view as is. `{}` declares a required parameter; `{ defaultValue }` supplies
`run`: the value it returns is the operation's data and type, sent to the view as is.
`rowsAsRecords()` keys each record by the exact SQL column names and rejects results
missing a listed column. It does not convert `snake_case`; alias columns in SQL, as with
`"unitPrice"`, or list and read the returned names. `{}` declares a required parameter; `{ defaultValue }` supplies
a fallback. `query(name, params, { limit })` executes only registered queries and
inherits the operation's row limit and cancellation signal.
Declare at least one valid input in `checks`; use a nonempty tuple for checks
Expand Down