@softr/sdk 0.0.26-alpha-0 → 0.0.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +618 -2
- package/dist/codegen/softr-env.js +2 -1
- package/dist/package.js +1 -1
- package/package.json +10 -32
- package/skills/softr-sdk/SKILL.md +11 -11
- package/skills/softr-sdk/auth/SKILL.md +1 -1
- package/skills/softr-sdk/current-user/SKILL.md +1 -1
- package/skills/softr-sdk/data-mutations/SKILL.md +56 -76
- package/skills/softr-sdk/integrations/SKILL.md +140 -0
- package/skills/softr-sdk/reading-records/SKILL.md +93 -77
- package/skills/softr-sdk/reading-records/references/field-types.md +48 -41
- package/skills/softr-sdk/reading-records/references/query-builder.md +70 -66
- package/skills/softr-sdk/routes-and-visibility/SKILL.md +1 -1
- package/dist/codegen/fetch-metadata.js +0 -46
- package/skills/softr-sdk/charts-and-metrics/SKILL.md +0 -178
- package/skills/softr-sdk/charts-and-metrics/references/aggregations.md +0 -76
|
@@ -1,178 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: charts-and-metrics
|
|
3
|
-
description: >-
|
|
4
|
-
Aggregate Softr datasource records inside a server function: ds.chart with
|
|
5
|
-
groupBy, metrics, where and orderBy for chart rows, ds.metric for a single
|
|
6
|
-
headline number, date bucket intervals, which aggregations each kind of field
|
|
7
|
-
offers, the two-group-column ceiling, and distinct values for filter
|
|
8
|
-
controls. Aggregation only. Listing and fetching records is reading-records.
|
|
9
|
-
metadata:
|
|
10
|
-
type: sub-skill
|
|
11
|
-
library: softr-sdk
|
|
12
|
-
library_version: 0.0.22
|
|
13
|
-
---
|
|
14
|
-
|
|
15
|
-
This skill builds on softr-sdk, and on reading-records for declaring the action a chart aggregates over.
|
|
16
|
-
|
|
17
|
-
# Charts and metrics
|
|
18
|
-
|
|
19
|
-
`ds.chart` groups records and aggregates each group into one row: the data behind a bar chart, a line, a pie. `ds.metric` aggregates everything into one value: the number on a stat tile. Both live on `@softr/sdk/server` and run inside a `createServerFn` handler, and both take the same read action `ds.list` takes, with the same `where` filters.
|
|
20
|
-
|
|
21
|
-
## Setup
|
|
22
|
-
|
|
23
|
-
```ts
|
|
24
|
-
import { createServerFn } from '@tanstack/react-start'
|
|
25
|
-
import { defineAction } from '@softr/sdk'
|
|
26
|
-
import { ds } from '@softr/sdk/server'
|
|
27
|
-
|
|
28
|
-
const sales = defineAction({
|
|
29
|
-
id: 'sales',
|
|
30
|
-
datasource: '8f14e45f-ceea-467a-9d0b-2c1a0e3b5d77',
|
|
31
|
-
operation: 'read',
|
|
32
|
-
fields: {
|
|
33
|
-
amount: 'Vk9cXm2pSz6Ld4', // CURRENCY
|
|
34
|
-
region: 'fZk3Qm9pLw2Xa1', // SELECT
|
|
35
|
-
status: 'Ru7bVx2cNq0Ed9', // SELECT, choice ids 'paid' | 'open' | 'void'
|
|
36
|
-
closedAt: 'Xp2dLh6vKz9Fa3', // DATETIME
|
|
37
|
-
rep: 'Gq5wNt8rJy3Hb1', // LINKED_RECORD
|
|
38
|
-
},
|
|
39
|
-
permission: { userGroup: 'LOGGED_IN_USERS' },
|
|
40
|
-
})
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
A chart only sees the action's declared fields, so a field to group, aggregate or filter by must be in `fields`.
|
|
44
|
-
|
|
45
|
-
## Core Patterns
|
|
46
|
-
|
|
47
|
-
### A bar chart
|
|
48
|
-
|
|
49
|
-
```ts
|
|
50
|
-
const ordersByRegion = createServerFn().handler(async () => {
|
|
51
|
-
const { data } = await ds
|
|
52
|
-
.chart(sales)
|
|
53
|
-
.groupBy((q) => ({ region: q.region }))
|
|
54
|
-
.metrics((q) => ({ orders: q.$count }))
|
|
55
|
-
.orderBy((q) => q.orders.desc)
|
|
56
|
-
return data
|
|
57
|
-
})
|
|
58
|
-
// data: Array<{ region: string | null; orders: number }>
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
One row per group, keyed by the names you chose in `groupBy` and `metrics`. `orders` is `number`, not `number | null`: a group that exists holds at least one record. The `null` region is the bucket of records with no value. The result also carries `executionTimestamp: Date`, when the numbers were computed.
|
|
62
|
-
|
|
63
|
-
A SELECT or LINKED_RECORD group column comes back as the choice's display label, not its id. Labels are user-editable and a `where` on the same field compares ids, so a clicked bar's value cannot be fed straight back into a filter.
|
|
64
|
-
|
|
65
|
-
### A time series
|
|
66
|
-
|
|
67
|
-
```ts
|
|
68
|
-
const { data } = await ds
|
|
69
|
-
.chart(sales)
|
|
70
|
-
.groupBy((q) => ({ month: q.closedAt.byMonth }))
|
|
71
|
-
.metrics((q) => ({ revenue: q.amount.sum }))
|
|
72
|
-
.where((q) => q.status.is('paid'))
|
|
73
|
-
.orderBy((q) => q.month.asc)
|
|
74
|
-
// data: Array<{ month: string | null; revenue: number | null }>
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
`q.closedAt` bare groups by the field's value. The bucket leaves hanging off it group a date field by the bucket: `byDay`, `byWeek`, `byMonth`, `byQuarter`, `byYear`, and the cyclic `byHourOfDay`, `byDayOfWeek`, `byMonthOfYear`, `byQuarterOfYear`. What each one holds is in [references/aggregations.md](./references/aggregations.md); `byMonth` reads as `"2025-06"`.
|
|
78
|
-
|
|
79
|
-
**Sort the time axis ascending yourself.** Left unsorted, rows come back by the first bucket ascending when there is one, else by the first metric descending. A time axis that misses the fallback arrives in value order, which draws a scrambled line rather than failing.
|
|
80
|
-
|
|
81
|
-
### Spread, not just totals
|
|
82
|
-
|
|
83
|
-
```ts
|
|
84
|
-
const { data } = await ds
|
|
85
|
-
.chart(sales)
|
|
86
|
-
.groupBy((q) => ({ region: q.region }))
|
|
87
|
-
.metrics((q) => ({
|
|
88
|
-
smallest: q.amount.min,
|
|
89
|
-
typical: q.amount.avg,
|
|
90
|
-
biggest: q.amount.max,
|
|
91
|
-
}))
|
|
92
|
-
.orderBy((q) => q.typical.desc)
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
Any number of metrics in one request, several over the same field included. Each row carries all of them, so one request can feed two panels. A metric key may not reuse a group key, and two metrics may not ask for the same value. `avg` comes back rounded to two decimals.
|
|
96
|
-
|
|
97
|
-
### Two group columns, which is the ceiling
|
|
98
|
-
|
|
99
|
-
```ts
|
|
100
|
-
const { data } = await ds
|
|
101
|
-
.chart(sales)
|
|
102
|
-
.groupBy((q) => ({ month: q.closedAt.byMonth, region: q.region }))
|
|
103
|
-
.metrics((q) => ({ revenue: q.amount.sum }))
|
|
104
|
-
.orderBy((q) => [q.month.asc, q.revenue.desc])
|
|
105
|
-
// data: Array<{ month: string | null; region: string | null; revenue: number | null }>
|
|
106
|
-
```
|
|
107
|
-
|
|
108
|
-
The result is long format, one row per month and region pair, so the app pivots into one array per series before charting. **A chart groups by at most two columns**, each a different field, and a field cannot be grouped and aggregated in the same chart. A third group column is a compile error.
|
|
109
|
-
|
|
110
|
-
### Distinct values, for a filter control
|
|
111
|
-
|
|
112
|
-
```ts
|
|
113
|
-
const { data } = await ds.chart(sales).groupBy((q) => ({ region: q.region }))
|
|
114
|
-
// data: Array<{ region: string | null }>
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
Skip `metrics` entirely and the rows hold the distinct group values, one each. This is the cheapest way to fill a dropdown from a real column.
|
|
118
|
-
|
|
119
|
-
### A headline number
|
|
120
|
-
|
|
121
|
-
```ts
|
|
122
|
-
const { value } = await ds
|
|
123
|
-
.metric(sales, (q) => q.amount.sum)
|
|
124
|
-
.where((q) => q.status.is('paid'))
|
|
125
|
-
// value: number | null
|
|
126
|
-
```
|
|
127
|
-
|
|
128
|
-
One aggregation, no grouping, one value. Whenever the answer is a single number, a stat tile or a count, reach for `ds.metric` rather than a one-row chart: there is nothing to group by and nothing to unpack out of `data`. The same measure handles apply, `q.$count` included:
|
|
129
|
-
|
|
130
|
-
```ts
|
|
131
|
-
const { value: open } = await ds.metric(sales, (q) => q.$count).where((q) => q.status.is('open'))
|
|
132
|
-
// open: number
|
|
133
|
-
```
|
|
134
|
-
|
|
135
|
-
### Top N
|
|
136
|
-
|
|
137
|
-
```ts
|
|
138
|
-
const { data } = await ds
|
|
139
|
-
.chart(sales)
|
|
140
|
-
.groupBy((q) => ({ rep: q.rep }))
|
|
141
|
-
.metrics((q) => ({ revenue: q.amount.sum }))
|
|
142
|
-
.orderBy((q) => q.revenue.desc)
|
|
143
|
-
|
|
144
|
-
const top5 = data.slice(0, 5)
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
A chart has no row limit to set, so a top-N is a client-side slice of the fully sorted rows.
|
|
148
|
-
|
|
149
|
-
### A dashboard load
|
|
150
|
-
|
|
151
|
-
```ts
|
|
152
|
-
const loadDashboard = createServerFn().handler(async () => {
|
|
153
|
-
const [mtd, series] = await Promise.all([
|
|
154
|
-
ds
|
|
155
|
-
.metric(sales, (q) => q.amount.sum)
|
|
156
|
-
.where((q) => q.$and(q.status.is('paid'), q.closedAt.gte('2026-09-01'))),
|
|
157
|
-
ds
|
|
158
|
-
.chart(sales)
|
|
159
|
-
.groupBy((q) => ({ month: q.closedAt.byMonth }))
|
|
160
|
-
.metrics((q) => ({ revenue: q.amount.sum }))
|
|
161
|
-
.where((q) => q.status.is('paid'))
|
|
162
|
-
.orderBy((q) => q.month.asc),
|
|
163
|
-
])
|
|
164
|
-
return { mtd: mtd.value, series: series.data }
|
|
165
|
-
})
|
|
166
|
-
```
|
|
167
|
-
|
|
168
|
-
Each tile and panel is its own request, fired in parallel from one handler.
|
|
169
|
-
|
|
170
|
-
## Order of calls
|
|
171
|
-
|
|
172
|
-
`groupBy` comes first and exactly once; a chart cannot be awaited before it. `metrics` follows, at most once, or not at all for distinct values. `where` can be chained anywhere, repeatedly, and calling it more than once ands the filters together. `orderBy` sorts by the chart's own keys, group and metric alike, and only sees the keys declared so far, so call it last.
|
|
173
|
-
|
|
174
|
-
## References
|
|
175
|
-
|
|
176
|
-
- [references/aggregations.md](./references/aggregations.md): every bucket interval and its value type, which aggregations each kind of field offers, and the chart rules
|
|
177
|
-
- [reading-records/SKILL.md](../reading-records/SKILL.md): declaring the action, and the filter operators `where` accepts
|
|
178
|
-
- [reading-records/references/query-builder.md](../reading-records/references/query-builder.md): the full filter matrix
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
# Buckets, aggregations and chart rules
|
|
2
|
-
|
|
3
|
-
## Group columns
|
|
4
|
-
|
|
5
|
-
A bare handle in `groupBy` groups by the field's value. What the column reads back as depends on the field's declared type:
|
|
6
|
-
|
|
7
|
-
| Field type | Column value |
|
|
8
|
-
| --- | --- |
|
|
9
|
-
| SELECT, LINKED_RECORD, CREATED_BY, UPDATED_BY | `string \| null`, the display label, not the id |
|
|
10
|
-
| Text fields | `string \| null` |
|
|
11
|
-
| Number fields | `number \| null` |
|
|
12
|
-
| CHECKBOX | `boolean \| null` |
|
|
13
|
-
| DATE, DATETIME, CREATED_AT, UPDATED_AT | `string \| null`, or a bucket leaf below |
|
|
14
|
-
| Multi-value (`SELECT[]` and friends), ATTACHMENT, DATE_RANGE | Cannot group |
|
|
15
|
-
|
|
16
|
-
`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.
|
|
17
|
-
|
|
18
|
-
## Date buckets
|
|
19
|
-
|
|
20
|
-
Leaves on every date and datetime handle, `q.closedAt.byMonth` style:
|
|
21
|
-
|
|
22
|
-
| Leaf | Groups by | Column value |
|
|
23
|
-
| --- | --- | --- |
|
|
24
|
-
| `.byDay` | Calendar day | `"2025-06-14"` |
|
|
25
|
-
| `.byWeek` | Calendar week | The ISO date of the week's Monday |
|
|
26
|
-
| `.byMonth` | Calendar month | `"2025-06"` |
|
|
27
|
-
| `.byQuarter` | Calendar quarter | `"Q2 2025"`, a display string, not a parseable date |
|
|
28
|
-
| `.byYear` | Calendar year | The year as a `number` |
|
|
29
|
-
| `.byHourOfDay` | Hour of day | `0`–`23`. DATETIME fields only, not DATE |
|
|
30
|
-
| `.byDayOfWeek` | Day of week | `1`–`7`, 1 is Sunday |
|
|
31
|
-
| `.byMonthOfYear` | Month of year | `1`–`12` |
|
|
32
|
-
| `.byQuarterOfYear` | Quarter of year | `1`–`4` |
|
|
33
|
-
|
|
34
|
-
Every bucket value is `| null` for the empty bucket. The cyclic buckets come back as bare numbers; the app supplies the labels.
|
|
35
|
-
|
|
36
|
-
`byWeek` is Monday-based while `byDayOfWeek` counts from Sunday, so the two cannot be lined up by arithmetic alone.
|
|
37
|
-
|
|
38
|
-
## Aggregations
|
|
39
|
-
|
|
40
|
-
Handles in `metrics`, and the second argument of `ds.metric`:
|
|
41
|
-
|
|
42
|
-
| Handle | Available on | Comes back as |
|
|
43
|
-
| --- | --- | --- |
|
|
44
|
-
| `.sum` | Number fields | `number \| null` |
|
|
45
|
-
| `.avg` | Number fields | `number \| null`, rounded to two decimals |
|
|
46
|
-
| `.min` / `.max` | Number fields | `number \| null` |
|
|
47
|
-
| `.min` / `.max` | Date fields | `string \| null`, as `"2025-06-14"` |
|
|
48
|
-
| `.distinct` | Number and date fields | `number`, the count of distinct values |
|
|
49
|
-
| `q.$count` | No field: counts rows | `number` |
|
|
50
|
-
|
|
51
|
-
Number fields: NUMBER, PERCENT, CURRENCY, PROGRESS, RATING, DURATION, COUNT, AUTONUMBER. Date fields: DATE, DATETIME, CREATED_AT, UPDATED_AT.
|
|
52
|
-
|
|
53
|
-
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.
|
|
54
|
-
|
|
55
|
-
`sum`, `avg`, `min` and `max` are `null` for a group with no values. `$count` and `distinct` are plain `number`.
|
|
56
|
-
|
|
57
|
-
## Rules
|
|
58
|
-
|
|
59
|
-
- `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.
|
|
60
|
-
- Each group column is a different field, and a field cannot be grouped and aggregated in the same chart.
|
|
61
|
-
- `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.
|
|
62
|
-
- `orderBy` names the chart's own keys, group and metric alike. Call it last: it only sees keys already declared.
|
|
63
|
-
- 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.
|
|
64
|
-
- There is no row limit. A top-N is a client-side `slice` of the sorted rows.
|
|
65
|
-
|
|
66
|
-
## Errors
|
|
67
|
-
|
|
68
|
-
| Message | Cause |
|
|
69
|
-
| --- | --- |
|
|
70
|
-
| `a chart needs at least one column to group by` | Awaiting a chart whose `groupBy` produced no columns |
|
|
71
|
-
| `a chart may group by at most two columns` | Three or more group columns, on an untyped action |
|
|
72
|
-
| `a chart may group by a field only once` | The same field twice in `groupBy` |
|
|
73
|
-
| `a field cannot be grouped and aggregated in one chart` | One field in both `groupBy` and `metrics` |
|
|
74
|
-
| `two metrics ask for the same value` | The same aggregation of the same field under two keys |
|
|
75
|
-
| `"..." is not a column of this chart` | An `orderBy` key that is neither a group nor a metric key |
|
|
76
|
-
| `a chart column must come from the callback a chart hands you` | A hand-built object where a handle belongs |
|