UNPKG

@softr/sdk

Version:

The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.

77 lines (57 loc) • 4.17 kB
# Buckets, aggregations and chart rules ## Group columns A bare handle in `groupBy` groups by the field's value. What the column reads back as depends on the field's declared type: | Field type | Column value | | --- | --- | | SELECT, LINKED_RECORD, CREATED_BY, UPDATED_BY | `string \| null`, the display label, not the id | | Text fields | `string \| null` | | Number fields | `number \| null` | | CHECKBOX | `boolean \| null` | | DATE, DATETIME, CREATED_AT, UPDATED_AT | `string \| null`, or a bucket leaf below | | Multi-value (`SELECT[]` and friends), ATTACHMENT, DATE_RANGE | Cannot group | `null` is always the bucket of records with no value. A multi-value field can neither group nor aggregate: exploding it would count each record once per value and inflate every metric. ## Date buckets Leaves on every date and datetime handle, `q.closedAt.byMonth` style: | Leaf | Groups by | Column value | | --- | --- | --- | | `.byDay` | Calendar day | `"2025-06-14"` | | `.byWeek` | Calendar week | The ISO date of the week's Monday | | `.byMonth` | Calendar month | `"2025-06"` | | `.byQuarter` | Calendar quarter | `"Q2 2025"`, a display string, not a parseable date | | `.byYear` | Calendar year | The year as a `number` | | `.byHourOfDay` | Hour of day | `0`–`23`. DATETIME fields only, not DATE | | `.byDayOfWeek` | Day of week | `1`–`7`, 1 is Sunday | | `.byMonthOfYear` | Month of year | `1`–`12` | | `.byQuarterOfYear` | Quarter of year | `1`–`4` | Every bucket value is `| null` for the empty bucket. The cyclic buckets come back as bare numbers; the app supplies the labels. `byWeek` is Monday-based while `byDayOfWeek` counts from Sunday, so the two cannot be lined up by arithmetic alone. ## Aggregations Handles in `metrics`, and the second argument of `ds.metric`: | Handle | Available on | Comes back as | | --- | --- | --- | | `.sum` | Number fields | `number \| null` | | `.avg` | Number fields | `number \| null`, rounded to two decimals | | `.min` / `.max` | Number fields | `number \| null` | | `.min` / `.max` | Date fields | `string \| null`, as `"2025-06-14"` | | `.distinct` | Number and date fields | `number`, the count of distinct values | | `q.$count` | No field: counts rows | `number` | Number fields: NUMBER, PERCENT, CURRENCY, PROGRESS, RATING, DURATION, COUNT, AUTONUMBER. Date fields: DATE, DATETIME, CREATED_AT, UPDATED_AT. Every other field offers no aggregations at all. `q.$count` is the only measure that touches no field, so a text or choice column is measured by grouping on it and counting rows. `sum`, `avg`, `min` and `max` are `null` for a group with no values. `$count` and `distinct` are plain `number`. ## Rules - `groupBy` takes one or two columns, is required, and is called once. A third column, and a chart with no group columns, are compile errors. - Each group column is a different field, and a field cannot be grouped and aggregated in the same chart. - `metrics` is optional and called at most once. A metric key may not reuse a group key, and two metrics may not ask for the same aggregation of the same field. - `orderBy` names the chart's own keys, group and metric alike. Call it last: it only sees keys already declared. - Left unsorted, rows come back by the first bucket column ascending; with no bucket, by the first metric descending; with neither, by the first group ascending. - There is no row limit. A top-N is a client-side `slice` of the sorted rows. ## Errors | Message | Cause | | --- | --- | | `a chart needs at least one column to group by` | Awaiting a chart whose `groupBy` produced no columns | | `a chart may group by at most two columns` | Three or more group columns | | `a chart may group by a field only once` | The same field twice in `groupBy` | | `a field cannot be grouped and aggregated in one chart` | One field in both `groupBy` and `metrics` | | `two metrics ask for the same value` | The same aggregation of the same field under two keys | | `"..." is not a column of this chart` | An `orderBy` key that is neither a group nor a metric key | | `a chart column must come from the callback a chart hands you` | A hand-built object where a handle belongs |