@protobase/cli 0.22.13 → 0.23.1
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/main.js +2 -0
- package/dist/package.js +1 -1
- package/dist/skill/install.js +29 -0
- package/dist/skill/register.js +14 -0
- package/package.json +8 -7
- package/skills/protobase/SKILL.md +89 -0
- package/skills/protobase/references/getting-started.md +78 -0
- package/skills/protobase/references/guides/api-functions.md +240 -0
- package/skills/protobase/references/guides/billing-page.md +288 -0
- package/skills/protobase/references/guides/create-page.md +108 -0
- package/skills/protobase/references/guides/deploy.md +52 -0
- package/skills/protobase/references/guides/export-a-list.md +44 -0
- package/skills/protobase/references/guides/field-type.md +131 -0
- package/skills/protobase/references/guides/first-composed-page.md +230 -0
- package/skills/protobase/references/guides/first-plugin.md +166 -0
- package/skills/protobase/references/guides/import-records.md +43 -0
- package/skills/protobase/references/guides/plugin-build.md +64 -0
- package/skills/protobase/references/guides/record-card.md +70 -0
- package/skills/protobase/references/guides/remote-resources.md +185 -0
- package/skills/protobase/references/guides/table-to-admin.md +205 -0
- package/skills/protobase/references/guides/theme-an-app.md +87 -0
- package/skills/protobase/references/reference/access.md +115 -0
- package/skills/protobase/references/reference/ai.md +106 -0
- package/skills/protobase/references/reference/api.md +231 -0
- package/skills/protobase/references/reference/architecture.md +63 -0
- package/skills/protobase/references/reference/assistant.md +157 -0
- package/skills/protobase/references/reference/auth.md +462 -0
- package/skills/protobase/references/reference/blocks.md +341 -0
- package/skills/protobase/references/reference/bottom-bar.md +44 -0
- package/skills/protobase/references/reference/cli.md +493 -0
- package/skills/protobase/references/reference/client.md +38 -0
- package/skills/protobase/references/reference/comments.md +83 -0
- package/skills/protobase/references/reference/custom-components.md +287 -0
- package/skills/protobase/references/reference/data-config.md +266 -0
- package/skills/protobase/references/reference/email.md +166 -0
- package/skills/protobase/references/reference/events.md +121 -0
- package/skills/protobase/references/reference/files.md +291 -0
- package/skills/protobase/references/reference/forms.md +133 -0
- package/skills/protobase/references/reference/history.md +86 -0
- package/skills/protobase/references/reference/jobs.md +174 -0
- package/skills/protobase/references/reference/layouts.md +126 -0
- package/skills/protobase/references/reference/plugins.md +328 -0
- package/skills/protobase/references/reference/remote-resources.md +198 -0
- package/skills/protobase/references/reference/sidebar.md +51 -0
- package/skills/protobase/references/reference/theme.md +132 -0
- package/skills/protobase/references/reference/ui-config.md +880 -0
- package/skills/protobase/references/reference/user-menu.md +38 -0
- package/skills/protobase/references/reference/versioning.md +91 -0
- package/skills/protobase/references/reference/webhooks.md +136 -0
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
<!-- Generated by scripts/protobase-skill.ts from website/src/content/docs/guides/billing-page.mdx: run `bun run docs:skill`, do not edit. -->
|
|
2
|
+
|
|
3
|
+
# A billing page
|
|
4
|
+
|
|
5
|
+
Give your customers one page for their plan, billing details, cards and invoices, with forms, actions and a component of your own.
|
|
6
|
+
|
|
7
|
+
Northwind Labs hosts its apps on your platform. Ada, who looks after their money, signs in once a month with four questions: what are we on, who do you bill, which card do you charge, and what did we pay. Today those answers sit in four lists. This guide gives Ada one page with all four, plus the buttons to act on them.
|
|
8
|
+
|
|
9
|
+
It builds on [Your first composed page](first-composed-page.md)), so `page`, `Stat`, `Table` and `RecordCard` are familiar. New here: forms in a dialog, actions that delete records or open links, conditions on a record's own fields, and a component written in React. The data is a small billing schema: `plans`, `subscriptions` (with a `plan` relation), `billing_profiles`, `payment_methods` and `invoices`.
|
|
10
|
+
|
|
11
|
+
## 1. Your plan
|
|
12
|
+
|
|
13
|
+
```tsx title="config/billing/page.tsx"
|
|
14
|
+
/** @jsxImportSource @protobase/layout */
|
|
15
|
+
import { Action, Field, Grid, Page, Progress, RecordCard, page } from '@protobase/layout'
|
|
16
|
+
|
|
17
|
+
export const billing = page(
|
|
18
|
+
'billing',
|
|
19
|
+
<Page title="Billing" description="Your plan, how you pay, and what you paid.">
|
|
20
|
+
<RecordCard
|
|
21
|
+
title="Your plan"
|
|
22
|
+
resource="subscriptions"
|
|
23
|
+
filter="status = 'active'"
|
|
24
|
+
empty="You have no active plan."
|
|
25
|
+
actions={[<Action name="cancel" variant="danger" />, <Action name="upgrade" variant="primary" />]}
|
|
26
|
+
>
|
|
27
|
+
<Grid columns={3}>
|
|
28
|
+
<Field name="plan.name" label="Plan" />
|
|
29
|
+
<Field name="plan.price" label="Per month" />
|
|
30
|
+
<Field name="renews_at" />
|
|
31
|
+
</Grid>
|
|
32
|
+
<Progress label="Seats" value="seats_used" max="plan.seats" />
|
|
33
|
+
</RecordCard>
|
|
34
|
+
</Page>,
|
|
35
|
+
{ icon: 'credit-card' },
|
|
36
|
+
)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
The card shows the active subscription. `plan.name` and `plan.price` reach through the `plan` relation, and `Progress` draws `seats_used` out of `plan.seats`: a string names a field of the record, a number is used as is. The bar turns amber from 90%.
|
|
40
|
+
|
|
41
|
+
The two header buttons are named actions of the subscriptions view, each with its own kind of behaviour. The views of the billing schema declare every action this page uses:
|
|
42
|
+
|
|
43
|
+
```ts title="config/billing/views.ts"
|
|
44
|
+
import { view } from '@protobase/schema'
|
|
45
|
+
import type { billingProfiles, invoices, paymentMethods, plans, subscriptions } from './billing-config'
|
|
46
|
+
|
|
47
|
+
// The views of the billing schema: names, labels, and the named actions the billing page's buttons run.
|
|
48
|
+
|
|
49
|
+
export const plansView = view<typeof plans>('plans').title((r) => r.name).nav({ hidden: true }).fields((r) => ({ price: r.price.prefix('€').decimals(2) }))
|
|
50
|
+
|
|
51
|
+
export const subscriptionsView = view<typeof subscriptions>('subscriptions')
|
|
52
|
+
.nav({ hidden: true })
|
|
53
|
+
.fields((r) => ({ renews_at: r.renews_at.label('Renews on'), status: r.status.valueLabels({ active: 'Active', canceled: 'Canceled' }) }))
|
|
54
|
+
.actions((a) => [
|
|
55
|
+
a.update('cancel', { label: 'Cancel plan', set: { status: 'canceled' }, confirm: 'Your plan stays active until the end of the period.' }),
|
|
56
|
+
a.link('upgrade', { label: 'Upgrade', href: 'mailto:sales@example.com?subject=Upgrade%20from%20plan%20{plan}' }),
|
|
57
|
+
])
|
|
58
|
+
|
|
59
|
+
export const billingProfilesView = view<typeof billingProfiles>('billing_profiles').nav({ hidden: true }).fields((r) => ({ vat_number: r.vat_number.label('VAT number') }))
|
|
60
|
+
|
|
61
|
+
export const paymentMethodsView = view<typeof paymentMethods>('payment_methods')
|
|
62
|
+
.nav({ hidden: true })
|
|
63
|
+
.actions((a) => [
|
|
64
|
+
a.update('makeDefault', { label: 'Make default', set: { is_default: true } }),
|
|
65
|
+
a.remove('remove', { label: 'Remove', confirm: 'The card is removed from your account.' }),
|
|
66
|
+
])
|
|
67
|
+
|
|
68
|
+
export const invoicesView = view<typeof invoices>('invoices')
|
|
69
|
+
.title((r) => r.number)
|
|
70
|
+
.names({ singular: 'Invoice', plural: 'Invoices' })
|
|
71
|
+
.fields((r) => ({ total: r.total.prefix('€').decimals(2), issued_at: r.issued_at.label('Date'), status: r.status.valueLabels({ open: 'Open', paid: 'Paid' }) }))
|
|
72
|
+
.list((r) => ({ columns: [r.number, r.issued_at, r.total, r.status] }))
|
|
73
|
+
.actions((a) => [a.action('download', { label: 'Download PDF' })])
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
`a.update('cancel', ...)` sets `status` after its `confirm` question. `a.link` opens its `href`, with `{plan}` filled in from the record: a path in the app (`/plans`) stays in the app, anything else (`https:`, `mailto:`) leaves it. Cancel the plan in the story and the card says there is no active plan, because its `filter` no longer matches.
|
|
77
|
+
|
|
78
|
+
## 2. Who we bill
|
|
79
|
+
|
|
80
|
+
Ada's company moved offices. The details card gets an edit button:
|
|
81
|
+
|
|
82
|
+
```tsx title="config/billing/page.tsx"
|
|
83
|
+
/** @jsxImportSource @protobase/layout */
|
|
84
|
+
import { Action, Field, Grid, ModalForm, Page, Progress, RecordCard, page } from '@protobase/layout'
|
|
85
|
+
|
|
86
|
+
export const billing = page(
|
|
87
|
+
'billing',
|
|
88
|
+
<Page title="Billing" description="Your plan, how you pay, and what you paid.">
|
|
89
|
+
<Grid columns={2}>
|
|
90
|
+
<RecordCard
|
|
91
|
+
title="Your plan"
|
|
92
|
+
resource="subscriptions"
|
|
93
|
+
filter="status = 'active'"
|
|
94
|
+
empty="You have no active plan."
|
|
95
|
+
actions={[<Action name="cancel" variant="danger" />, <Action name="upgrade" variant="primary" />]}
|
|
96
|
+
>
|
|
97
|
+
<Grid columns={3}>
|
|
98
|
+
<Field name="plan.name" label="Plan" />
|
|
99
|
+
<Field name="plan.price" label="Per month" />
|
|
100
|
+
<Field name="renews_at" />
|
|
101
|
+
</Grid>
|
|
102
|
+
<Progress label="Seats" value="seats_used" max="plan.seats" />
|
|
103
|
+
</RecordCard>
|
|
104
|
+
<RecordCard
|
|
105
|
+
title="Billing details"
|
|
106
|
+
resource="billing_profiles"
|
|
107
|
+
actions={<ModalForm mode="edit" label="Edit" title="Edit billing details" fields={['company', 'vat_number', 'address', 'email']} />}
|
|
108
|
+
>
|
|
109
|
+
<Grid columns={2}>
|
|
110
|
+
<Field name="company" />
|
|
111
|
+
<Field name="vat_number" />
|
|
112
|
+
<Field name="address" />
|
|
113
|
+
<Field name="email" />
|
|
114
|
+
</Grid>
|
|
115
|
+
</RecordCard>
|
|
116
|
+
</Grid>
|
|
117
|
+
</Page>,
|
|
118
|
+
{ icon: 'credit-card' },
|
|
119
|
+
)
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
`ModalForm mode="edit"` in a RecordCard's `actions` edits that card's record: a dialog with the `fields` listed, the same editors as the record page, the changed fields sent as one update with the record's ETag. Errors from the server land on their fields. Change the company name in the story and save.
|
|
123
|
+
|
|
124
|
+
## 3. How they pay
|
|
125
|
+
|
|
126
|
+
```tsx title="config/billing/page.tsx"
|
|
127
|
+
/** @jsxImportSource @protobase/layout */
|
|
128
|
+
import { Action, CardRow, Field, Grid, ModalForm, Page, Progress, RecordCard, Show, page } from '@protobase/layout'
|
|
129
|
+
|
|
130
|
+
export const billing = page(
|
|
131
|
+
'billing',
|
|
132
|
+
<Page title="Billing" description="Your plan, how you pay, and what you paid.">
|
|
133
|
+
<Grid columns={2}>
|
|
134
|
+
<RecordCard
|
|
135
|
+
title="Your plan"
|
|
136
|
+
resource="subscriptions"
|
|
137
|
+
filter="status = 'active'"
|
|
138
|
+
empty="You have no active plan."
|
|
139
|
+
actions={[<Action name="cancel" variant="danger" />, <Action name="upgrade" variant="primary" />]}
|
|
140
|
+
>
|
|
141
|
+
<Grid columns={3}>
|
|
142
|
+
<Field name="plan.name" label="Plan" />
|
|
143
|
+
<Field name="plan.price" label="Per month" />
|
|
144
|
+
<Field name="renews_at" />
|
|
145
|
+
</Grid>
|
|
146
|
+
<Progress label="Seats" value="seats_used" max="plan.seats" />
|
|
147
|
+
</RecordCard>
|
|
148
|
+
<RecordCard
|
|
149
|
+
title="Billing details"
|
|
150
|
+
resource="billing_profiles"
|
|
151
|
+
actions={<ModalForm mode="edit" label="Edit" title="Edit billing details" fields={['company', 'vat_number', 'address', 'email']} />}
|
|
152
|
+
>
|
|
153
|
+
<Grid columns={2}>
|
|
154
|
+
<Field name="company" />
|
|
155
|
+
<Field name="vat_number" />
|
|
156
|
+
<Field name="address" />
|
|
157
|
+
<Field name="email" />
|
|
158
|
+
</Grid>
|
|
159
|
+
</RecordCard>
|
|
160
|
+
</Grid>
|
|
161
|
+
<CardRow
|
|
162
|
+
title="Payment methods"
|
|
163
|
+
resource="payment_methods"
|
|
164
|
+
sort="is_default desc, id"
|
|
165
|
+
actions={<ModalForm mode="create" label="Add card" resource="payment_methods" fields={['brand', 'last4', 'expires']} />}
|
|
166
|
+
>
|
|
167
|
+
<Field name="brand" label={false} />
|
|
168
|
+
<Field name="last4" label="Number" />
|
|
169
|
+
<Field name="expires" />
|
|
170
|
+
<Show when="is_default = true">Default card</Show>
|
|
171
|
+
<Show when="is_default = false">
|
|
172
|
+
<Action name="makeDefault" />
|
|
173
|
+
<Action name="remove" variant="danger" />
|
|
174
|
+
</Show>
|
|
175
|
+
</CardRow>
|
|
176
|
+
</Page>,
|
|
177
|
+
{ icon: 'credit-card' },
|
|
178
|
+
)
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
The cards sit side by side in a `CardRow`, the default card first (`sort="is_default desc, id"`). Two `Show` blocks split the card on its own `is_default` field: the default card says so, the others get "Make default" and "Remove". Those are again view actions, `a.update('makeDefault', ...)` and `a.remove('remove', ...)` in the views above.
|
|
182
|
+
|
|
183
|
+
Making a card the default unsets the others in the database (a trigger or a write hook); the row reloads after every action, so the order follows. "Add card" is a `ModalForm mode="create"` in the row's header. In the story, make the Mastercard the default, remove the Visa and add a card.
|
|
184
|
+
|
|
185
|
+
## 4. What they paid, and a component of your own
|
|
186
|
+
|
|
187
|
+
```tsx title="config/billing/page.tsx"
|
|
188
|
+
/** @jsxImportSource @protobase/layout */
|
|
189
|
+
import { Action, Card, CardRow, Field, Grid, Link, ModalForm, Page, Progress, RecordCard, Show, Stat, Table, component, page } from '@protobase/layout'
|
|
190
|
+
|
|
191
|
+
/** Seats in use over the last months; the app registers the React component under this name. */
|
|
192
|
+
export const SeatHistory = component<{ months: number }>('SeatHistory')
|
|
193
|
+
|
|
194
|
+
export const billing = page(
|
|
195
|
+
'billing',
|
|
196
|
+
<Page title="Billing" description="Your plan, how you pay, and what you paid.">
|
|
197
|
+
<Grid columns={2}>
|
|
198
|
+
<RecordCard
|
|
199
|
+
title="Your plan"
|
|
200
|
+
resource="subscriptions"
|
|
201
|
+
filter="status = 'active'"
|
|
202
|
+
empty="You have no active plan."
|
|
203
|
+
actions={[<Action name="cancel" variant="danger" />, <Action name="upgrade" variant="primary" />]}
|
|
204
|
+
>
|
|
205
|
+
<Grid columns={3}>
|
|
206
|
+
<Field name="plan.name" label="Plan" />
|
|
207
|
+
<Field name="plan.price" label="Per month" />
|
|
208
|
+
<Field name="renews_at" />
|
|
209
|
+
</Grid>
|
|
210
|
+
<Progress label="Seats" value="seats_used" max="plan.seats" />
|
|
211
|
+
<SeatHistory months={6} />
|
|
212
|
+
</RecordCard>
|
|
213
|
+
<RecordCard
|
|
214
|
+
title="Billing details"
|
|
215
|
+
resource="billing_profiles"
|
|
216
|
+
actions={<ModalForm mode="edit" label="Edit" title="Edit billing details" fields={['company', 'vat_number', 'address', 'email']} />}
|
|
217
|
+
>
|
|
218
|
+
<Grid columns={2}>
|
|
219
|
+
<Field name="company" />
|
|
220
|
+
<Field name="vat_number" />
|
|
221
|
+
<Field name="address" />
|
|
222
|
+
<Field name="email" />
|
|
223
|
+
</Grid>
|
|
224
|
+
</RecordCard>
|
|
225
|
+
</Grid>
|
|
226
|
+
<CardRow
|
|
227
|
+
title="Payment methods"
|
|
228
|
+
resource="payment_methods"
|
|
229
|
+
sort="is_default desc, id"
|
|
230
|
+
actions={<ModalForm mode="create" label="Add card" resource="payment_methods" fields={['brand', 'last4', 'expires']} />}
|
|
231
|
+
>
|
|
232
|
+
<Field name="brand" label={false} />
|
|
233
|
+
<Field name="last4" label="Number" />
|
|
234
|
+
<Field name="expires" />
|
|
235
|
+
<Show when="is_default = true">Default card</Show>
|
|
236
|
+
<Show when="is_default = false">
|
|
237
|
+
<Action name="makeDefault" />
|
|
238
|
+
<Action name="remove" variant="danger" />
|
|
239
|
+
</Show>
|
|
240
|
+
</CardRow>
|
|
241
|
+
<Grid columns={3}>
|
|
242
|
+
<Table title="Invoices" resource="invoices" sort="issued_at desc" limit={5} span={2} />
|
|
243
|
+
<Grid columns={1}>
|
|
244
|
+
<Stat label="Open invoices" resource="invoices" filter="status = 'open'" />
|
|
245
|
+
<RecordCard title="Latest invoice" resource="invoices" sort="issued_at desc" actions={<Action name="download" />}>
|
|
246
|
+
<Grid columns={2}>
|
|
247
|
+
<Field name="number" />
|
|
248
|
+
<Field name="total" />
|
|
249
|
+
</Grid>
|
|
250
|
+
</RecordCard>
|
|
251
|
+
<Card title="Questions about a bill?">
|
|
252
|
+
Mail <Link href="mailto:billing@example.com">billing@example.com</Link> and we answer within a day.
|
|
253
|
+
</Card>
|
|
254
|
+
</Grid>
|
|
255
|
+
</Grid>
|
|
256
|
+
</Page>,
|
|
257
|
+
{ icon: 'credit-card' },
|
|
258
|
+
)
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
Two things here are not built-in blocks.
|
|
262
|
+
|
|
263
|
+
`SeatHistory` is a **custom component**. `component('SeatHistory')` declares it for the layout, with its props typed, and the element stays data: `{ type: 'SeatHistory', props: { months: 6 }, custom: true }`. The React code lives in the app, exported under the same name from `protobase.ui.tsx` at the project root, which the admin bundles with itself. Inside a RecordCard, `useLayoutRecord()` gives it the record.
|
|
264
|
+
|
|
265
|
+
"Download PDF" is a named action without built-in behaviour (`a.action('download', ...)`), so it runs the handler registered under its name, with the record, the API client and the router. The ERP example has one of each, its component in a file of its own:
|
|
266
|
+
|
|
267
|
+
```tsx title="protobase.ui.tsx"
|
|
268
|
+
import { defineUi, type ActionContext } from '@protobase/ui'
|
|
269
|
+
|
|
270
|
+
// The custom components, each in a file of its own, exported by the name their layouts declare.
|
|
271
|
+
export { OrdersPerDay } from './components/orders-per-day'
|
|
272
|
+
|
|
273
|
+
/** "Send reminder" on an invoice: opens a mail to the customer, and marks a draft invoice as sent. */
|
|
274
|
+
const send = async ({ record, recordKey, etag, client }: ActionContext) => {
|
|
275
|
+
const company = await client.get('companies', String(record!.company_id))
|
|
276
|
+
const subject = encodeURIComponent(`Invoice ${String(record!.number)}`)
|
|
277
|
+
window.location.assign(`mailto:${String(company.record.email ?? '')}?subject=${subject}`)
|
|
278
|
+
if (record!.status === 'draft') await client.update('invoices', recordKey!, { status: 'sent' }, etag ?? '*')
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
export default defineUi({ actions: { send } })
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
The rest is familiar: a paged `Table` of invoices spanning two of the three columns, a count of open invoices, and a `Link` inside the text of a card.
|
|
285
|
+
|
|
286
|
+
If the app has no component registered under a name, the page says so where the component would be, and an action without a handler is disabled:
|
|
287
|
+
|
|
288
|
+
Next: [Custom components](../reference/custom-components.md)) for the details of `protobase.ui.tsx`, and [Layouts](../reference/layouts.md)) for named actions, conditions and access.
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
<!-- Generated by scripts/protobase-skill.ts from website/src/content/docs/guides/create-page.md: run `bun run docs:skill`, do not edit. -->
|
|
2
|
+
|
|
3
|
+
# A create page of your own
|
|
4
|
+
|
|
5
|
+
Replace a resource's create form with a page of the app's own, built from the app's form parts, that creates records through an API function.
|
|
6
|
+
|
|
7
|
+
Pip's bike shop books repairs at the desk, and a booking is more than a row: the workshop wants to know what the bike needs, in words a customer understands, and the shop's API function plans the job. This guide gives the repairs a page of their own for that, opened by **New** on their list, and lets the API refuse plain creates. It uses the bike shop of the [first guide](table-to-admin.md)).
|
|
8
|
+
|
|
9
|
+
## 1. The page
|
|
10
|
+
|
|
11
|
+
A create page is a [composed page](../reference/layouts.md)) like any other, with a [component](../reference/custom-components.md#components)) of the project's own in it:
|
|
12
|
+
|
|
13
|
+
```tsx
|
|
14
|
+
// config/pages/book-repair.tsx
|
|
15
|
+
/** @jsxImportSource @protobase/layout */
|
|
16
|
+
import { Page, component, page } from '@protobase/layout'
|
|
17
|
+
|
|
18
|
+
export const BookRepair = component('BookRepair')
|
|
19
|
+
|
|
20
|
+
export const bookRepair = page(
|
|
21
|
+
'book-repair',
|
|
22
|
+
<Page title="Book a repair" description="What the bike needs, so the workshop can plan it.">
|
|
23
|
+
<BookRepair />
|
|
24
|
+
</Page>,
|
|
25
|
+
{ roles: ['desk', 'admin'] },
|
|
26
|
+
)
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Its `roles` say who may book: they are the people who get the page, and so **New**.
|
|
30
|
+
|
|
31
|
+
## 2. The component
|
|
32
|
+
|
|
33
|
+
The component asks its questions with the [form parts](../reference/custom-components.md#form-parts)) of `@protobase/ui`, so it looks like the app's own forms, and keeps the answers in the address, so a reload keeps them:
|
|
34
|
+
|
|
35
|
+
```tsx
|
|
36
|
+
// components/book-repair.tsx, exported from protobase.ui.tsx: export { BookRepair } from './components/book-repair'
|
|
37
|
+
import { Choices, Field, Form, Input, Section, useAppAddress, useClient } from '@protobase/ui'
|
|
38
|
+
import { useState } from 'react'
|
|
39
|
+
|
|
40
|
+
const kinds = [
|
|
41
|
+
{ value: 'service', label: 'Service', description: 'The whole bike checked: brakes, gears, tyres and chain.' },
|
|
42
|
+
{ value: 'flat', label: 'Flat tyre', description: 'A new inner tube, and the tyre checked for what caused it.' },
|
|
43
|
+
{ value: 'other', label: 'Something else', description: 'Say what is wrong at the desk.' },
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
export const BookRepair = () => {
|
|
47
|
+
const { address, go } = useAppAddress()
|
|
48
|
+
const client = useClient()
|
|
49
|
+
const [saving, setSaving] = useState(false)
|
|
50
|
+
const [error, setError] = useState<unknown>()
|
|
51
|
+
const values = address?.kind === 'create' ? (address.values ?? {}) : {}
|
|
52
|
+
const answer = (patch: Record<string, string>) => go({ kind: 'create', resource: 'repairs', values: { ...values, ...patch } }, { replace: true })
|
|
53
|
+
|
|
54
|
+
const book = () => {
|
|
55
|
+
setSaving(true)
|
|
56
|
+
client.invoke('book-repair', { body: values }).then(
|
|
57
|
+
(repair) => go({ kind: 'record', resource: 'repairs', key: (repair as { id: number }).id }),
|
|
58
|
+
(failure) => {
|
|
59
|
+
setSaving(false)
|
|
60
|
+
setError(failure)
|
|
61
|
+
},
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return (
|
|
66
|
+
<Form submit="Book repair" disabled={!values.bike || !values.kind} saving={saving} error={error} errorTitle="Not booked" onSubmit={book}>
|
|
67
|
+
<Section title="The bike">
|
|
68
|
+
<div className="flex flex-col gap-5">
|
|
69
|
+
<Field label="Bike" required help="Make and model, as on the frame.">
|
|
70
|
+
<Input value={values.bike ?? ''} onChange={(event) => answer({ bike: event.target.value })} />
|
|
71
|
+
</Field>
|
|
72
|
+
<Field label="What it needs" required>
|
|
73
|
+
<Choices name="kind" label="What it needs" options={kinds} value={values.kind} onChange={(kind) => answer({ kind })} />
|
|
74
|
+
</Field>
|
|
75
|
+
</div>
|
|
76
|
+
</Section>
|
|
77
|
+
</Form>
|
|
78
|
+
)
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Without a `title`, the `Form` sits under the page's heading, with its button after the sections.
|
|
83
|
+
|
|
84
|
+
## 3. Point the view at it
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
// config/repairs/ui.ts
|
|
88
|
+
export const repairsView = view<typeof repairs>('repairs')
|
|
89
|
+
// ...
|
|
90
|
+
.create({ page: 'book-repair' })
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
**New** on the repairs list now opens the page at `/repairs/new`, and so does every link to a new repair, such as one on a customer's page with `values: { customer_id: '{id}' }`. Someone the page leaves out gets no **New**, and `/repairs/new` tells them they cannot create repairs; the plain create form no longer shows. The server refuses a page name it does not know when it starts.
|
|
94
|
+
|
|
95
|
+
## 4. Create through a function
|
|
96
|
+
|
|
97
|
+
With its own page, the shop can make the [API function](api-functions.md)) `book-repair` the only way in: it checks the answers, plans the job and inserts the repair with `db`, which skips the access rules. The resource's API then refuses creates:
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
// config/repairs/data.ts
|
|
101
|
+
export const repairs = resource('repairs')
|
|
102
|
+
// ...
|
|
103
|
+
.access({ create: () => false })
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
**New** follows the page, not this rule, so it stays for the desk. **Import** on the list follows the rule, and goes.
|
|
107
|
+
|
|
108
|
+
The [UI config](../reference/ui-config.md#create-pages)) has the details.
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
<!-- Generated by scripts/protobase-skill.ts from website/src/content/docs/guides/deploy.md: run `bun run docs:skill`, do not edit. -->
|
|
2
|
+
|
|
3
|
+
# Deploy
|
|
4
|
+
|
|
5
|
+
Ship a project as one bundle folder and run it with the serve runtime.
|
|
6
|
+
|
|
7
|
+
A project ships as one bundle folder; the host runs protobase around it.
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
bun run --cwd examples/erp protobase build # the bundle to ship: examples/erp/dist/
|
|
11
|
+
bun run build:serve # the runtime the host carries: dist/protobase-serve.js
|
|
12
|
+
bun --no-install /opt/protobase/protobase-serve.js /app/protobase.config.js # on the host: the API
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Both builds write plain JavaScript that Node and Bun load as it is. For a host that runs Bun, as the images below do, add `--bun` to either: Bun's bundler then writes the final file, the form Bun loads without transpiling, and the build needs `bun` on `PATH` ([details](../reference/cli.md#the-config-module))).
|
|
16
|
+
|
|
17
|
+
The bundle holds three things, four when the config uses native add-ons:
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
dist/
|
|
21
|
+
protobase.config.js the config module, run by the serve runtime
|
|
22
|
+
node_modules/ only with native add-ons: those packages, with linux x64 and arm64 binaries
|
|
23
|
+
public/ the admin app (index.html and content-hashed assets/), the project's UI as plugins in
|
|
24
|
+
plugins/, its theme, protobase.theme.css, and its favicon, protobase.favicon.svg
|
|
25
|
+
protobase.bundle.json the manifest: which paths go to the server, which Protobase version built it, the
|
|
26
|
+
plugins and the app's version
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
The host serves `public/` itself and sends only the manifest's `api` paths to the runtime (and `/.well-known/webauthn`, for an app on [several origins](../reference/auth.md#several-origins))), so loading the UI never needs a running container. The runtime serves the API alone; `protobase serve dist` serves the whole folder the same way, to check a bundle locally. The host passes `DATABASE_URL`, `PORT` (default 8787) and optionally `PROTOBASE_DB_CONNECTIONS` (the connections the app may open, so its functions and jobs leave one to requests, see [`serve`](../reference/cli.md#serve))), `REQUEST_LOG`, `PROTOBASE_SMTP_URL` and `PROTOBASE_MAIL_FROM` (the mail server and sender for [password reset](../reference/auth.md#password-reset)) and emailed sign-in codes) `PROTOBASE_APP_NAME` (the app's name in that mail, see [`appName`](../reference/auth.md#configuration))) `PROTOBASE_SIGN_IN_*` (a provider people with an account sign in with, the [platform sign-in provider](../reference/auth.md#platform-sign-in-provider))) `PROTOBASE_OPERATOR_*` (the provider its staff sign in with for [staff sign-in](../reference/auth.md#staff-sign-in))), `PROTOBASE_ORIGINS` and `PROTOBASE_PASSKEY_DOMAIN` (the app's [other origins](../reference/auth.md#several-origins)) and its passkey domain) `PROTOBASE_CORS_ORIGINS` (the websites that may call its API, see [CORS](../reference/api.md#cors))) and `PROTOBASE_SENTRY_DSN` (the app's [error tracker](../reference/api.md#error-reporting))), plus the project's own settings (`BETTER_AUTH_SECRET`, ...). The runtime and the config module are plain JavaScript for Bun: no install, no TypeScript, no writes. The runtime supplies protobase and the dependencies it runs on the server (`pg`, `postgres`, `kysely`, `better-auth`, ...), so the config module carries only the project's code and the other packages it uses. The runtime serves only bundles built by a compatible Protobase version, below 1.0 the same minor (see [Versioning](../reference/versioning.md#bundles-and-runtimes))), so rebuild the bundle when the host's runtime moves to another one. Nothing in the bundle or the runtime changes the database: run the project's `db:migrate`, which applies its migrations and those of the auth schema, before a new version serves it, with no other writes while it runs. The runtime refuses to start while the auth schema lacks a table or column the new version needs (see [migrations](../reference/auth.md#migrations))). The manifest format and the routing rules are in the CLI reference: [`build`](../reference/cli.md#build)), [`build-serve`](../reference/cli.md#build-serve)) and [`serve`](../reference/cli.md#serve)).
|
|
30
|
+
|
|
31
|
+
## Images
|
|
32
|
+
|
|
33
|
+
Every release pushes two public images to GitHub's registry, for `linux/amd64` and `linux/arm64`, tagged with its version:
|
|
34
|
+
|
|
35
|
+
| Image | What it holds |
|
|
36
|
+
| --- | --- |
|
|
37
|
+
| `ghcr.io/prototeamhq/protobase:<version>` | the serve runtime, compiled with Bun into `/opt/protobase/protobase-serve`, which serves the API of the bundle mounted at `/app`; the release's prebuilt admin app at `/opt/protobase/shell`, which a host may serve for the bundle's [shell paths](../reference/plugins.md#the-shell)) instead of the bundle's copy; Bun for its healthcheck on `/health`, and the C++ runtime native add-ons link. It runs as `nonroot` (65532) and writes nothing, so it runs with a read-only root filesystem |
|
|
38
|
+
| `ghcr.io/prototeamhq/protobase-dev:<version>` | Bun and the presets at `/opt/protobase/presets/<name>` (`erp`, `real-estate`, `scratch`), with the dependencies they share installed in `/workspace` and Bun's cache in `/opt/bun-cache`. It has no Node: the presets' scripts and the `protobase` command run on Bun. It runs as `bun` (1000) |
|
|
39
|
+
|
|
40
|
+
```sh
|
|
41
|
+
docker run -v "$PWD/dist:/app:ro" -p 8787:8787 -e DATABASE_URL -e BETTER_AUTH_SECRET ghcr.io/prototeamhq/protobase:0.2.0
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
In `protobase-dev`, copy a preset into `/workspace` and `bun install --frozen-lockfile` has nothing to do; installed anywhere else, it comes from the cache, without the network.
|
|
45
|
+
|
|
46
|
+
The Dockerfiles are `docker/protobase/Dockerfile` and `docker/protobase-dev/Dockerfile`. To build them from a clone, at the version in `package.json` (whose `@protobase` packages must be on npm: the serve runtime and the shell come from `@protobase/cli` and `@protobase/ui` installed from npm, and the presets' `bun.lock` files resolve the packages there):
|
|
47
|
+
|
|
48
|
+
```sh
|
|
49
|
+
bun run images:context # dist/images/protobase and dist/images/protobase-dev
|
|
50
|
+
docker build -f docker/protobase/Dockerfile -t protobase dist/images/protobase
|
|
51
|
+
docker build -f docker/protobase-dev/Dockerfile -t protobase-dev dist/images/protobase-dev
|
|
52
|
+
```
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
<!-- Generated by scripts/protobase-skill.ts from website/src/content/docs/guides/export-a-list.md: run `bun run docs:skill`, do not edit. -->
|
|
2
|
+
|
|
3
|
+
# Exporting a list
|
|
4
|
+
|
|
5
|
+
Let users save a list as an Excel or CSV file, limit how many rows a file holds, or take Export off a list.
|
|
6
|
+
|
|
7
|
+
Every list in the admin can be saved as a file: Rijnland's finance team wants this month's invoices in Excel, and the warehouse wants a CSV of the stock moves for a supplier. Nothing needs setting up for that; this guide shows what users get, and how a view limits it or takes it away. It uses the ERP example (`examples/erp`).
|
|
8
|
+
|
|
9
|
+
## 1. Export a list
|
|
10
|
+
|
|
11
|
+
Run `bun run protobase dev`, open the orders and filter them, by status for instance. **Export** beside the title opens a dialog:
|
|
12
|
+
|
|
13
|
+
- **Format**: an Excel workbook (`.xlsx`) or a CSV file.
|
|
14
|
+
- **Rows**: the orders the list finds, with its filters, search and sort. With orders selected in the table, **Export N** beside **Delete N** opens the same dialog with the selected ones.
|
|
15
|
+
- **Values**: **As shown** writes what the list shows, such as "Shipped" and the company's name; **Raw values** writes what is stored, such as `shipped` and the company's key.
|
|
16
|
+
- **Columns**: the list's columns, in its order. Tick other fields to add them, and move any column up or down.
|
|
17
|
+
|
|
18
|
+
The dialog says how many rows that is. **Export** asks where to save the file in Chrome and Edge, which then write it as it is built, and downloads it in other browsers. The browser builds the file while it reads the orders page by page, so a large export costs the server no more than scrolling through the list would; **Cancel** stops it.
|
|
19
|
+
|
|
20
|
+
## 2. Limit or take it off
|
|
21
|
+
|
|
22
|
+
A view can limit how many rows one file holds, or take Export off its list:
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
// config/orders/ui.ts
|
|
26
|
+
export const ordersView = view<typeof orders>('orders')
|
|
27
|
+
// ...
|
|
28
|
+
.export({ maxRows: 50_000 })
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
// config/stock_moves/ui.ts
|
|
33
|
+
export const stockMovesView = view<typeof stockMoves>('stock_moves')
|
|
34
|
+
// ...
|
|
35
|
+
.export(false)
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
With a limit, the dialog says when the list finds more rows than a file holds, and the file holds the first ones in the list's order.
|
|
39
|
+
|
|
40
|
+
## 3. What a file holds
|
|
41
|
+
|
|
42
|
+
A file holds what the user could read in the list, and no more: the rows come from the same list API, as that user, so their role's [row filters and field lists](../reference/access.md#capabilities)) apply, and a [sensitive field](../reference/data-config.md#sensitive-fields)) is never offered. To keep a field out of someone's exports, keep it out of what their role may read. A view's limit only shapes the page; someone who may read the orders can still read them all through the API.
|
|
43
|
+
|
|
44
|
+
The [UI config](../reference/ui-config.md#exports)) has the details: how cells are typed in a workbook, how a CSV file is quoted, and how files are named.
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
<!-- Generated by scripts/protobase-skill.ts from website/src/content/docs/guides/field-type.md: run `bun run docs:skill`, do not edit. -->
|
|
2
|
+
|
|
3
|
+
# Adding a field type
|
|
4
|
+
|
|
5
|
+
Show and edit a field your own way everywhere the app shows it, with a field type a plugin registers and a view picks.
|
|
6
|
+
|
|
7
|
+
An order's status is one of six values, and each order moves through them in turn: draft, confirmed, picking, shipped, delivered. The app shows it as a badge and edits it with a select. Rijnland's warehouse wants to see at a glance how far along an order is, and to move it with one click. That is a field type: a component that shows the value, one that edits it, and the storage types it works for. This guide adds one, `steps`, to the ERP example (`examples/erp`). [Field types](../reference/plugins.md#field-types)) has the contract.
|
|
8
|
+
|
|
9
|
+
## 1. Show the value
|
|
10
|
+
|
|
11
|
+
A field type's display gets the field's model, its value as the API gives it, and the props the view passes:
|
|
12
|
+
|
|
13
|
+
```tsx
|
|
14
|
+
// components/order-steps.tsx
|
|
15
|
+
import type { FieldWidgetProps } from '@protobase/ui'
|
|
16
|
+
|
|
17
|
+
const label = (value: string) => value.charAt(0).toUpperCase() + value.slice(1)
|
|
18
|
+
|
|
19
|
+
/** An enum's value as its place among the values: a dot per step, filled up to it, and its name. */
|
|
20
|
+
export const StepsDisplay = ({ field, value, props }: FieldWidgetProps<string, { last?: string }>) => {
|
|
21
|
+
const steps = (field.enumValues ?? []).filter((step) => step !== 'cancelled' || value === 'cancelled')
|
|
22
|
+
const at = value === null ? -1 : steps.indexOf(value)
|
|
23
|
+
return (
|
|
24
|
+
<span className="inline-flex items-center gap-1.5">
|
|
25
|
+
{steps.map((step, index) => (
|
|
26
|
+
<span key={step} className={index <= at ? 'size-2 rounded-full bg-primary' : 'size-2 rounded-full bg-border-strong'} />
|
|
27
|
+
))}
|
|
28
|
+
<span className="text-xs text-muted-foreground">
|
|
29
|
+
{value === null ? '—' : label(value)}
|
|
30
|
+
{props.last === value ? ' ✓' : ''}
|
|
31
|
+
</span>
|
|
32
|
+
</span>
|
|
33
|
+
)
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
`field.enumValues` are the steps, in the order the data config lists them, so the same field type works for any enum. `value` is `null` for an empty field. `props.last` is whatever the view says it is; here, the step that ends an order.
|
|
38
|
+
|
|
39
|
+
## 2. Edit it
|
|
40
|
+
|
|
41
|
+
An editor gets the same props, and `onChange` to set a new value, and `invalid` when the value it holds is refused:
|
|
42
|
+
|
|
43
|
+
```tsx
|
|
44
|
+
// components/order-steps.tsx
|
|
45
|
+
import type { FieldWidgetEditorProps } from '@protobase/ui'
|
|
46
|
+
|
|
47
|
+
/** The steps as buttons: the current one marked, a click moves to another. */
|
|
48
|
+
export const StepsEditor = ({ field, value, onChange }: FieldWidgetEditorProps<string>) => (
|
|
49
|
+
<div role="radiogroup" aria-label={field.name} className="flex flex-wrap gap-1">
|
|
50
|
+
{(field.enumValues ?? []).map((step) => (
|
|
51
|
+
<button
|
|
52
|
+
key={step}
|
|
53
|
+
type="button"
|
|
54
|
+
role="radio"
|
|
55
|
+
aria-checked={step === value}
|
|
56
|
+
onClick={() => onChange(step)}
|
|
57
|
+
className={step === value ? 'rounded-md border border-primary bg-primary-soft px-2 py-1 text-xs text-primary-text' : 'rounded-md border border-border px-2 py-1 text-xs hover:bg-muted'}
|
|
58
|
+
>
|
|
59
|
+
{label(step)}
|
|
60
|
+
</button>
|
|
61
|
+
))}
|
|
62
|
+
</div>
|
|
63
|
+
)
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
`onChange` changes the record on the page, as typing in an input does: the page shows "Unsaved changes", and Save sends it, with the page's checks and conflict handling. A field type has no save of its own.
|
|
67
|
+
|
|
68
|
+
## 3. Register it
|
|
69
|
+
|
|
70
|
+
`protobase.ui.tsx` registers the field type under a name, with the storage types it shows:
|
|
71
|
+
|
|
72
|
+
```tsx
|
|
73
|
+
// protobase.ui.tsx
|
|
74
|
+
import { definePlugin } from '@protobase/ui'
|
|
75
|
+
import { StepsDisplay, StepsEditor } from './components/order-steps'
|
|
76
|
+
|
|
77
|
+
export { OrdersPerDay } from './components/orders-per-day'
|
|
78
|
+
|
|
79
|
+
export default definePlugin({
|
|
80
|
+
actions: { send },
|
|
81
|
+
fields: { steps: { of: ['enum'], display: StepsDisplay, editor: StepsEditor } },
|
|
82
|
+
})
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
`of: ['enum']` says it shows enum fields. A table cell draws `display` too, unless the field type gives a `cell` of its own, for instance a more compact one.
|
|
86
|
+
|
|
87
|
+
## 4. Pick it in the view
|
|
88
|
+
|
|
89
|
+
The view chooses the field type for a field, with props:
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
// config/orders/ui.ts
|
|
93
|
+
.fields((r) => ({
|
|
94
|
+
status: r.status.help('Where the order is in its life.').widget('steps', { last: 'delivered' }),
|
|
95
|
+
}))
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Run `bun run protobase dev` and open the orders. The status column shows the dots, and so does every place that shows an order's status: its page when it may not be changed, the summary in its sidebar, cards and page elements. On an order you may change, the status is a row of buttons; click one and save.
|
|
99
|
+
|
|
100
|
+
## 5. What the app checks
|
|
101
|
+
|
|
102
|
+
A field type shows the storage types it names, and nothing else. Pick it for a text field by mistake, say the order's number:
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
number: r.number.widget('steps'),
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
`protobase build` reads which storage types each field type of the bundle shows, and the server refuses to start:
|
|
109
|
+
|
|
110
|
+
```
|
|
111
|
+
error: The views do not match the resources:
|
|
112
|
+
- View "orders": field "number" is text, and its field type "steps" shows only enum
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
In `protobase dev`, the app checks the same as it draws, and shows the number as it would without a field type.
|
|
116
|
+
|
|
117
|
+
A field type that throws while drawing takes only its own field with it: the field shows as it would without the field type, and the rest of the page draws. So does every field of a field type whose plugin is off.
|
|
118
|
+
|
|
119
|
+
## 6. One field type for many apps
|
|
120
|
+
|
|
121
|
+
The `steps` field type above is the project's own, built with its bundle. To give it to other apps too, move `components/order-steps.tsx` into a [plugin](first-plugin.md)) of its own and register it there:
|
|
122
|
+
|
|
123
|
+
```tsx
|
|
124
|
+
// workshop-tools/protobase.ui.tsx
|
|
125
|
+
import { definePlugin } from '@protobase/ui'
|
|
126
|
+
import { StepsDisplay, StepsEditor } from './components/steps'
|
|
127
|
+
|
|
128
|
+
export default definePlugin({ name: 'workshop-tools', fields: { steps: { of: ['enum'], display: StepsDisplay, editor: StepsEditor } } })
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Each app lists the plugin and picks `.widget('steps')` in its views. The app checks the storage types when it draws; the server checks only the field types of the bundle's own plugins, since it never loads remote code.
|