@stacksjs/defaults 0.75.552 → 0.75.555
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/ai/skills/stacks-actions/SKILL.md +1 -1
- package/ai/skills/stacks-dashboard/SKILL.md +347 -14
- package/ai/skills/stacks-desktop/SKILL.md +19 -0
- package/ai/skills/stacks-models/SKILL.md +19 -0
- package/ai/skills/stacks-native/CATALOG.md +5 -5
- package/app/Actions/Dashboard/Content/file-manager.ts +9 -3
- package/app/Actions/Dashboard/Data/SubscriberIndexAction.ts +6 -3
- package/app/Actions/Dashboard/Data/TeamIndexAction.ts +6 -3
- package/app/Actions/Dashboard/Data/UserIndexAction.ts +6 -3
- package/app/Actions/Dashboard/Data/data-records.ts +12 -0
- package/app/Actions/Dashboard/Models/ModelActionRunAction.ts +77 -0
- package/app/Actions/Dashboard/Models/ModelMarkdownAction.ts +42 -0
- package/app/Actions/Dashboard/Models/ModelShowAction.ts +63 -21
- package/app/Actions/Dashboard/Models/ModelUploadAction.ts +83 -0
- package/app/Actions/Dashboard/Models/model-actions.ts +393 -0
- package/app/Actions/Dashboard/Models/model-fields.ts +198 -0
- package/app/Actions/Dashboard/Models/model-forms.ts +36 -0
- package/app/Actions/Dashboard/Models/model-related.ts +62 -11
- package/app/Actions/Dashboard/Models/model-upload.ts +81 -0
- package/app/Actions/Dashboard/Models/model-write.ts +92 -5
- package/app/Actions/Dashboard/Users/UserPasswordResetAction.ts +50 -0
- package/app/Actions/Desktop/UpdateArtifactAction.ts +37 -0
- package/app/Models/CardAttachment.ts +5 -0
- package/app/Models/Deployment.ts +5 -0
- package/app/Models/QueryLog.ts +5 -0
- package/app/Models/Request.ts +4 -0
- package/app/Models/Team.ts +4 -0
- package/app/Models/User.ts +18 -0
- package/functions/dashboard-markdown.ts +132 -0
- package/functions/dashboard-model-bulk.ts +224 -0
- package/functions/dashboard-model-format.ts +201 -0
- package/functions/dashboard-model-record.test.ts +53 -30
- package/functions/dashboard-model-record.ts +267 -19
- package/functions/data.ts +9 -0
- package/ide/vscode/package.json +1 -1
- package/package.json +3 -3
- package/resources/components/Dashboard/Data/ActivityLog.stx +212 -0
- package/resources/components/Dashboard/Data/DataModelRecords.stx +89 -0
- package/resources/components/Dashboard/Models/ModelActionForm.stx +85 -0
- package/resources/components/Dashboard/Models/ModelBadge.stx +22 -0
- package/resources/components/Dashboard/Models/ModelFieldInputs.stx +43 -9
- package/resources/components/Dashboard/Models/ModelMarkdownInput.stx +139 -0
- package/resources/components/Dashboard/Models/ModelRecordActions.stx +32 -1
- package/resources/components/Dashboard/Models/ModelRecordCreate.stx +1 -1
- package/resources/components/Dashboard/Models/ModelRecordDetail.stx +114 -7
- package/resources/components/Dashboard/Models/ModelRecordEdit.stx +1 -1
- package/resources/components/Dashboard/Models/ModelRecordsDashboard.stx +387 -20
- package/resources/components/Dashboard/Models/ModelRelatedPanels.stx +197 -27
- package/resources/components/Dashboard/Models/ModelRelationPicker.stx +3 -0
- package/resources/components/Dashboard/Models/ModelUploadInput.stx +97 -0
- package/routes/dashboard-api.ts +9 -0
- package/routes/desktop-updates.ts +7 -0
- package/views/dashboard/data/activity/index.stx +1 -1
- package/views/dashboard/data/subscribers.stx +1 -3
- package/views/dashboard/data/teams.stx +1 -3
- package/views/dashboard/data/users.stx +1 -3
- package/resources/components/Dashboard/Data/DataRecords.stx +0 -427
|
@@ -6,6 +6,14 @@
|
|
|
6
6
|
* is all a panel needs: nothing is declared for the dashboard. Each panel is
|
|
7
7
|
* a short table of the newest few with a count, so the page answers "what
|
|
8
8
|
* belongs to this?" without a trip to another list and a filter.
|
|
9
|
+
*
|
|
10
|
+
* Where the related model lets the dashboard write its rows, the panel is
|
|
11
|
+
* also where they are managed: it carries that model's form fields and each
|
|
12
|
+
* listed row's values, so a row is added, edited and deleted from this page.
|
|
13
|
+
* Each of those is a write of its own through the related model's own
|
|
14
|
+
* endpoints. Saving a record and its rows together, all or nothing, is not
|
|
15
|
+
* offered: a write through a model takes no part in a transaction
|
|
16
|
+
* (stacksjs/stacks#3269).
|
|
9
17
|
*/
|
|
10
18
|
import type { ModelHasRelation } from './model-relations'
|
|
11
19
|
import { safeGet } from '../../../../resources/functions/dashboard/data'
|
|
@@ -13,7 +21,9 @@ import { resolveDashboardModel } from '../../../../resources/functions/dashboard
|
|
|
13
21
|
import { hiddenModelColumns } from '../../../../resources/functions/dashboard/model-columns'
|
|
14
22
|
import { recordNames } from './model-options'
|
|
15
23
|
import { belongsToRelations, hasRelations, modelLabelColumn, pluralizeLabel, relatedForeignKey } from './model-relations'
|
|
16
|
-
import {
|
|
24
|
+
import type { ModelFieldDisplay } from './model-fields'
|
|
25
|
+
import { modelFormFields } from './model-forms'
|
|
26
|
+
import { humanizeIdentifier, type ModelCreateField, modelColumnDisplay, modelColumnLabel, modelListColumns, modelSchemaColumns, modelWriteCapabilities } from './model-write'
|
|
17
27
|
|
|
18
28
|
/** How many of a panel's records the page shows before "View all". */
|
|
19
29
|
export const RELATED_PREVIEW = 5
|
|
@@ -23,6 +33,8 @@ export interface RelatedPanelColumn {
|
|
|
23
33
|
label: string
|
|
24
34
|
/** A name or value, a status-like word, or a moment. */
|
|
25
35
|
type: 'text' | 'badge' | 'datetime'
|
|
36
|
+
/** How the related model says the value is shown (`dashboard.fields`). Wins over `type`. */
|
|
37
|
+
format?: ModelFieldDisplay
|
|
26
38
|
}
|
|
27
39
|
|
|
28
40
|
export interface RelatedPanel {
|
|
@@ -44,6 +56,18 @@ export interface RelatedPanel {
|
|
|
44
56
|
rows: Array<Record<string, unknown>>
|
|
45
57
|
/** A new one can be added from the dashboard, already pointing at this record. */
|
|
46
58
|
canCreate: boolean
|
|
59
|
+
/** The related model lets the dashboard edit its rows. */
|
|
60
|
+
canUpdate: boolean
|
|
61
|
+
/** The related model lets the dashboard delete its rows. */
|
|
62
|
+
canDestroy: boolean
|
|
63
|
+
/**
|
|
64
|
+
* What "Add" and the pencil ask for: the related model's own form fields,
|
|
65
|
+
* without the link back to this record, which is not a choice here. Empty
|
|
66
|
+
* when the panel offers neither.
|
|
67
|
+
*/
|
|
68
|
+
fields: ModelCreateField[]
|
|
69
|
+
/** What an edit starts from: each listed row's stored values for those fields, by row id. */
|
|
70
|
+
values: Record<string, Record<string, unknown>>
|
|
47
71
|
}
|
|
48
72
|
|
|
49
73
|
interface SummaryColumn extends RelatedPanelColumn {
|
|
@@ -68,6 +92,13 @@ const SUMMARY_LIMIT = 5
|
|
|
68
92
|
*/
|
|
69
93
|
export function relatedSummaryColumns(Related: any, foreignKey: string, reachable: ReadonlySet<string>): SummaryColumn[] {
|
|
70
94
|
const hidden = hiddenModelColumns(Related)
|
|
95
|
+
// Called what the related model calls it, and shown the way it says: its
|
|
96
|
+
// `dashboard.fields`, as its own list does. `label` going in is the name
|
|
97
|
+
// of the record a column points at, when it points at one.
|
|
98
|
+
const named = (column: SummaryColumn): SummaryColumn => {
|
|
99
|
+
const format = modelColumnDisplay(Related, column.key)
|
|
100
|
+
return { ...column, label: modelColumnLabel(Related, column.key, column.label || undefined), ...(format ? { format } : {}) }
|
|
101
|
+
}
|
|
71
102
|
|
|
72
103
|
// The model's own choice of list columns, when it made one: the panel is
|
|
73
104
|
// that list, less the column every row here shares.
|
|
@@ -78,25 +109,25 @@ export function relatedSummaryColumns(Related: any, foreignKey: string, reachabl
|
|
|
78
109
|
return chosen.columns.filter(column => column !== foreignKey).map((column) => {
|
|
79
110
|
const relation = relations.find(candidate => candidate.column === column)
|
|
80
111
|
if (relation)
|
|
81
|
-
return reachable.has(relation.model) ? { key: column, label: humanizeIdentifier(relation.model), type: 'text'
|
|
112
|
+
return reachable.has(relation.model) ? named({ key: column, label: humanizeIdentifier(relation.model), type: 'text', relation: relation.model }) : named({ key: column, label: '', type: 'text' })
|
|
82
113
|
const allowed = attributes[column]?.validation?.rule?.allowedValues
|
|
83
114
|
if (Array.isArray(allowed) && allowed.length > 0)
|
|
84
|
-
return { key: column, label:
|
|
115
|
+
return named({ key: column, label: '', type: 'badge' })
|
|
85
116
|
if (/_(?:at|on)$/.test(column))
|
|
86
|
-
return { key: column, label:
|
|
87
|
-
return { key: column, label:
|
|
117
|
+
return named({ key: column, label: '', type: 'datetime' })
|
|
118
|
+
return named({ key: column, label: '', type: 'text' })
|
|
88
119
|
})
|
|
89
120
|
}
|
|
90
121
|
|
|
91
122
|
const columns: SummaryColumn[] = []
|
|
92
123
|
const label = modelLabelColumn(Related)
|
|
93
124
|
if (label && !hidden.has(label))
|
|
94
|
-
columns.push({ key: label, label:
|
|
125
|
+
columns.push(named({ key: label, label: '', type: 'text' }))
|
|
95
126
|
|
|
96
127
|
for (const relation of belongsToRelations(Related)) {
|
|
97
128
|
if (relation.column === foreignKey || hidden.has(relation.column) || !reachable.has(relation.model))
|
|
98
129
|
continue
|
|
99
|
-
columns.push({ key: relation.column, label: humanizeIdentifier(relation.model), type: 'text', relation: relation.model })
|
|
130
|
+
columns.push(named({ key: relation.column, label: humanizeIdentifier(relation.model), type: 'text', relation: relation.model }))
|
|
100
131
|
}
|
|
101
132
|
|
|
102
133
|
// Neither a name of its own nor a record it points at: the id is all there
|
|
@@ -109,13 +140,13 @@ export function relatedSummaryColumns(Related: any, foreignKey: string, reachabl
|
|
|
109
140
|
const allowed = definition?.validation?.rule?.allowedValues
|
|
110
141
|
if (!Array.isArray(allowed) || allowed.length === 0 || hidden.has(column) || columns.some(entry => entry.key === column))
|
|
111
142
|
continue
|
|
112
|
-
columns.push({ key: column, label:
|
|
143
|
+
columns.push(named({ key: column, label: '', type: 'badge' }))
|
|
113
144
|
}
|
|
114
145
|
|
|
115
146
|
const traits = Related?.traits ?? {}
|
|
116
147
|
const kept = columns.slice(0, SUMMARY_LIMIT - 1)
|
|
117
148
|
if ((traits.useTimestamps ?? traits.timestampable) !== false)
|
|
118
|
-
kept.push({ key: 'created_at', label: '
|
|
149
|
+
kept.push(named({ key: 'created_at', label: '', type: 'datetime' }))
|
|
119
150
|
return kept
|
|
120
151
|
}
|
|
121
152
|
|
|
@@ -136,6 +167,7 @@ export async function relatedPanel(parentName: string, parentId: number, relatio
|
|
|
136
167
|
reachable.add(other.model)
|
|
137
168
|
}
|
|
138
169
|
const columns = relatedSummaryColumns(Related, foreignKey, reachable)
|
|
170
|
+
const hidden = hiddenModelColumns(Related)
|
|
139
171
|
|
|
140
172
|
const total = Number(await Related.query().where(foreignKey, parentId).count()) || 0
|
|
141
173
|
const found = total === 0
|
|
@@ -160,12 +192,28 @@ export async function relatedPanel(parentName: string, parentId: number, relatio
|
|
|
160
192
|
shown[column.key] = stored === null ? '' : names.get(column.key)?.get(String(stored)) ?? `${column.label} #${String(stored)}`
|
|
161
193
|
else
|
|
162
194
|
shown[column.key] = stored ?? ''
|
|
195
|
+
// An amount is read in its row's own currency, so that travels too.
|
|
196
|
+
const currency = column.format?.currencyColumn
|
|
197
|
+
if (currency && !hidden.has(currency) && !(currency in shown))
|
|
198
|
+
shown[currency] = safeGet(row, currency, null) ?? ''
|
|
163
199
|
}
|
|
164
200
|
return shown
|
|
165
201
|
})
|
|
166
202
|
|
|
167
203
|
const label = humanizeIdentifier(relation.model)
|
|
168
204
|
const field = belongsToRelations(Related).find(candidate => candidate.column === foreignKey)?.field ?? null
|
|
205
|
+
const capabilities = modelWriteCapabilities(Related)
|
|
206
|
+
const form = modelFormFields(Related, reachable)
|
|
207
|
+
// Only when the form can be told which record it starts from.
|
|
208
|
+
const canCreate = Boolean(field) && capabilities.create && form.some(entry => entry.name === field)
|
|
209
|
+
// The link back to this record is every row's, and moving a row to another
|
|
210
|
+
// record is not what a panel on this one is for.
|
|
211
|
+
const fields = canCreate || capabilities.update ? form.filter(entry => entry.name !== field && snakeCase(entry.name) !== foreignKey) : []
|
|
212
|
+
const values: Record<string, Record<string, unknown>> = {}
|
|
213
|
+
if (capabilities.update) {
|
|
214
|
+
for (const row of list)
|
|
215
|
+
values[String(safeGet(row, 'id', ''))] = Object.fromEntries(fields.map(entry => [snakeCase(entry.name), safeGet(row, snakeCase(entry.name), null)]))
|
|
216
|
+
}
|
|
169
217
|
return {
|
|
170
218
|
kind: relation.kind,
|
|
171
219
|
model: relation.model,
|
|
@@ -178,8 +226,11 @@ export async function relatedPanel(parentName: string, parentId: number, relatio
|
|
|
178
226
|
// The id column shows "#12", not a bare number in a text cell.
|
|
179
227
|
columns: columns.map(({ relation: _named, ...column }) => column.key === 'id' ? { ...column, key: 'id_label' } : column),
|
|
180
228
|
rows,
|
|
181
|
-
|
|
182
|
-
|
|
229
|
+
canCreate,
|
|
230
|
+
canUpdate: capabilities.update && fields.length > 0,
|
|
231
|
+
canDestroy: capabilities.destroy,
|
|
232
|
+
fields,
|
|
233
|
+
values,
|
|
183
234
|
}
|
|
184
235
|
}
|
|
185
236
|
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A file chosen for a model's field (`dashboard.fields.<name>.input:
|
|
3
|
+
* 'upload'`): whether it is one the field takes, and where it is kept.
|
|
4
|
+
*/
|
|
5
|
+
import { detectMimeFromMagicBytes } from '@stacksjs/storage'
|
|
6
|
+
|
|
7
|
+
/** The largest file a field's upload takes. The same limit the Files page has. */
|
|
8
|
+
export const MODEL_UPLOAD_BYTES = 10 * 1024 * 1024
|
|
9
|
+
|
|
10
|
+
/** The image types an extension stands for, so `.png` is held to the same check `image/png` is. */
|
|
11
|
+
const IMAGE_EXTENSIONS: Record<string, string> = {
|
|
12
|
+
'.avif': 'image/avif',
|
|
13
|
+
'.gif': 'image/gif',
|
|
14
|
+
'.heic': 'image/heic',
|
|
15
|
+
'.jpeg': 'image/jpeg',
|
|
16
|
+
'.jpg': 'image/jpeg',
|
|
17
|
+
'.png': 'image/png',
|
|
18
|
+
'.webp': 'image/webp',
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* What a file is, as far as it can be told.
|
|
23
|
+
*
|
|
24
|
+
* Its bytes say, for the formats that have a signature: that is what it is,
|
|
25
|
+
* whatever it was sent as. A file sent as an image whose bytes are no image
|
|
26
|
+
* this can recognise is not taken to be one: the type on a request is the
|
|
27
|
+
* sender's word, and an image is where a wrong word matters (it is shown in
|
|
28
|
+
* a page). An SVG is text and has no signature, so it is an image only to a
|
|
29
|
+
* field that names it.
|
|
30
|
+
*/
|
|
31
|
+
export function uploadedType(file: { type?: string, bytes?: Uint8Array }): string {
|
|
32
|
+
const detected = file.bytes ? detectMimeFromMagicBytes(file.bytes) : null
|
|
33
|
+
if (detected)
|
|
34
|
+
return detected
|
|
35
|
+
const claimed = (file.type ?? '').split(';')[0]!.trim().toLowerCase()
|
|
36
|
+
return claimed.startsWith('image/') && claimed !== 'image/svg+xml' ? '' : claimed
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Whether a file is of a kind the field accepts.
|
|
41
|
+
*
|
|
42
|
+
* `accept` is spelled as a file input spells it: a type (`image/png`), a
|
|
43
|
+
* family (`image/*`), or an extension (`.pdf`). The browser's file chooser
|
|
44
|
+
* reads the same list, but it is a hint there and a rule here: a request can
|
|
45
|
+
* be made without the chooser, so the file is judged by what it is
|
|
46
|
+
* ({@link uploadedType}) and not by what it was sent as. With no `accept`,
|
|
47
|
+
* any file is one.
|
|
48
|
+
*/
|
|
49
|
+
export function uploadAccepts(accept: string | undefined, file: { type?: string, name?: string, bytes?: Uint8Array }): boolean {
|
|
50
|
+
const entries = (accept ?? '').split(',').map(entry => entry.trim().toLowerCase()).filter(Boolean)
|
|
51
|
+
if (entries.length === 0)
|
|
52
|
+
return true
|
|
53
|
+
const type = uploadedType(file)
|
|
54
|
+
const name = (file.name ?? '').toLowerCase()
|
|
55
|
+
return entries.some((entry) => {
|
|
56
|
+
if (entry.startsWith('.')) {
|
|
57
|
+
// An image's extension is its type; any other is the name's to say,
|
|
58
|
+
// unless the bytes turn out to be an image after all.
|
|
59
|
+
return IMAGE_EXTENSIONS[entry] ? type === IMAGE_EXTENSIONS[entry] : name.endsWith(entry) && !type.startsWith('image/')
|
|
60
|
+
}
|
|
61
|
+
// A family takes what is of it. An SVG can carry script, so it is taken
|
|
62
|
+
// only where it is named.
|
|
63
|
+
if (entry.endsWith('/*'))
|
|
64
|
+
return type.startsWith(entry.slice(0, -1)) && type !== 'image/svg+xml'
|
|
65
|
+
return type === entry
|
|
66
|
+
})
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** What an `accept` reads as in a sentence: "image/*" is "an image", a list is itself. */
|
|
70
|
+
export function describeAccept(accept: string): string {
|
|
71
|
+
const entries = accept.split(',').map(entry => entry.trim()).filter(Boolean)
|
|
72
|
+
if (entries.length === 1 && entries[0]!.toLowerCase() === 'image/*')
|
|
73
|
+
return 'an image'
|
|
74
|
+
return `one of: ${entries.join(', ')}`
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Where a field's files are kept on the disk: together, by table and column. */
|
|
78
|
+
export function uploadDirectory(table: string, column: string): string {
|
|
79
|
+
const segment = (value: string) => value.toLowerCase().replace(/[^a-z\d_-]+/g, '-').replace(/^-+|-+$/g, '') || 'file'
|
|
80
|
+
return `uploads/${segment(table)}/${segment(column)}`
|
|
81
|
+
}
|
|
@@ -10,6 +10,7 @@ import { GDPR_SUBJECT_MODEL } from '@stacksjs/orm'
|
|
|
10
10
|
import { resolveDashboardModel } from '../../../../resources/functions/dashboard/model-access'
|
|
11
11
|
import { dashboardOperationalIssue } from '../dashboard-response'
|
|
12
12
|
import { modelApiConfiguration } from './model-api'
|
|
13
|
+
import { type ModelFieldDisplay, modelFieldOverrides } from './model-fields'
|
|
13
14
|
import { belongsToRelations } from './model-relations'
|
|
14
15
|
|
|
15
16
|
/** Never writable from a generic admin table, whatever the model declares. */
|
|
@@ -80,7 +81,10 @@ export interface ModelWriteCapabilities {
|
|
|
80
81
|
* own time zone; `relation` is another model's record, picked by name and
|
|
81
82
|
* stored as its id.
|
|
82
83
|
*/
|
|
83
|
-
export type ModelFieldType = 'boolean' | 'date' | 'datetime' | 'email' | 'enum' | 'json' | 'longtext' | 'number' | 'relation' | 'text'
|
|
84
|
+
export type ModelFieldType = 'boolean' | 'date' | 'datetime' | 'email' | 'enum' | 'json' | 'longtext' | 'markdown' | 'number' | 'relation' | 'text' | 'upload' | 'url'
|
|
85
|
+
|
|
86
|
+
/** The kinds whose value is text, and may be given another input that produces text. */
|
|
87
|
+
const TEXT_KINDS: ReadonlySet<ModelFieldType> = new Set(['email', 'longtext', 'markdown', 'text', 'upload', 'url'])
|
|
84
88
|
|
|
85
89
|
export interface ModelCreateField {
|
|
86
90
|
name: string
|
|
@@ -91,6 +95,10 @@ export interface ModelCreateField {
|
|
|
91
95
|
options: string[]
|
|
92
96
|
/** For a `relation`: the model whose records the picker lists. */
|
|
93
97
|
relation?: { model: string, slug: string, label: string }
|
|
98
|
+
/** What the model says under the input: its `dashboard.fields` help. */
|
|
99
|
+
help?: string
|
|
100
|
+
/** For an `upload`: the kinds of file that may be chosen. */
|
|
101
|
+
accept?: string
|
|
94
102
|
}
|
|
95
103
|
|
|
96
104
|
export interface PreparedModelFields {
|
|
@@ -140,6 +148,36 @@ export function humanizeIdentifier(name: string): string {
|
|
|
140
148
|
|
|
141
149
|
const humanize = humanizeIdentifier
|
|
142
150
|
|
|
151
|
+
/**
|
|
152
|
+
* What a column is called when the model does not say: its name in words,
|
|
153
|
+
* with a moment read as what happened ("Created", not "Created At").
|
|
154
|
+
*/
|
|
155
|
+
function defaultColumnLabel(column: string): string {
|
|
156
|
+
if (column === 'updated_at')
|
|
157
|
+
return 'Last Updated'
|
|
158
|
+
const label = humanize(column)
|
|
159
|
+
return /_(?:at|on)$/.test(column) ? label.replace(/\s+(?:At|On)$/, '') : label
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* What a field is called, on every page that names it: a list's heading, a
|
|
164
|
+
* record's label, a form's input, the message under it.
|
|
165
|
+
*
|
|
166
|
+
* The model's own word for it (`dashboard.fields.<name>.label`), else the
|
|
167
|
+
* record it points at when it is a relation the page can name, else the
|
|
168
|
+
* name in words. One function, so a field is not "Joined" in the list and
|
|
169
|
+
* "Created At" in the form.
|
|
170
|
+
*/
|
|
171
|
+
export function modelColumnLabel(Model: any, name: string, relationLabel?: string): string {
|
|
172
|
+
const column = snakeCase(name)
|
|
173
|
+
return modelFieldOverrides(Model).fields[column]?.label ?? relationLabel ?? defaultColumnLabel(column)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** How the model says a column's value is shown, when it says. */
|
|
177
|
+
export function modelColumnDisplay(Model: any, name: string): ModelFieldDisplay | undefined {
|
|
178
|
+
return modelFieldOverrides(Model).fields[snakeCase(name)]?.display
|
|
179
|
+
}
|
|
180
|
+
|
|
143
181
|
/**
|
|
144
182
|
* Derive the physical columns declared by a model when its table has no rows
|
|
145
183
|
* to inspect. This mirrors model-driven migrations closely enough for the
|
|
@@ -302,7 +340,7 @@ export function duplicateFieldErrors(Model: any, columns: string[]): Record<stri
|
|
|
302
340
|
const errors: Record<string, string[]> = {}
|
|
303
341
|
for (const [name, definition] of Object.entries((Model?.attributes ?? {}) as Record<string, any>)) {
|
|
304
342
|
if (definition?.unique === true && columns.includes(snakeCase(name)))
|
|
305
|
-
errors[name] = [`${
|
|
343
|
+
errors[name] = [`${modelColumnLabel(Model, name)} is already in use.`]
|
|
306
344
|
}
|
|
307
345
|
return errors
|
|
308
346
|
}
|
|
@@ -350,21 +388,28 @@ export function modelFieldType(name: string, definition: any): ModelFieldType {
|
|
|
350
388
|
export function modelCreateFields(Model: any): ModelCreateField[] {
|
|
351
389
|
const fields: ModelCreateField[] = []
|
|
352
390
|
const attributes = Model?.attributes ?? {}
|
|
391
|
+
const overrides = modelFieldOverrides(Model).fields
|
|
353
392
|
|
|
354
393
|
for (const [name, definition] of Object.entries(attributes as Record<string, any>)) {
|
|
355
394
|
if (!definition?.fillable || definition.hidden || isProtectedField(name))
|
|
356
395
|
continue
|
|
357
396
|
const rule = definition.validation?.rule
|
|
358
397
|
const options = Array.isArray(rule?.allowedValues) ? rule.allowedValues.map(String) : []
|
|
359
|
-
const
|
|
398
|
+
const override = overrides[snakeCase(name)]
|
|
399
|
+
const derived = modelFieldType(name, definition)
|
|
400
|
+
// Another input is only ever another way to type text: what a value is
|
|
401
|
+
// stays the rule's to say, so a number is never asked for as prose.
|
|
402
|
+
const type = override?.input && TEXT_KINDS.has(derived) ? override.input : derived
|
|
360
403
|
|
|
361
404
|
fields.push({
|
|
362
405
|
name,
|
|
363
|
-
label:
|
|
406
|
+
label: modelColumnLabel(Model, name),
|
|
364
407
|
type,
|
|
365
408
|
required: definition.required === true || rule?.isRequired === true,
|
|
366
409
|
defaultValue: definition.default ?? (type === 'boolean' ? false : ''),
|
|
367
410
|
options,
|
|
411
|
+
...(override?.help ? { help: override.help } : {}),
|
|
412
|
+
...(type === 'upload' && override?.accept ? { accept: override.accept } : {}),
|
|
368
413
|
})
|
|
369
414
|
}
|
|
370
415
|
|
|
@@ -374,7 +419,13 @@ export function modelCreateFields(Model: any): ModelCreateField[] {
|
|
|
374
419
|
for (const relation of belongsToRelations(Model)) {
|
|
375
420
|
if (isProtectedField(relation.field))
|
|
376
421
|
continue
|
|
377
|
-
const
|
|
422
|
+
const help = overrides[relation.column]?.help
|
|
423
|
+
const asRelation = {
|
|
424
|
+
type: 'relation' as const,
|
|
425
|
+
label: modelColumnLabel(Model, relation.column, humanize(relation.model)),
|
|
426
|
+
relation: { model: relation.model, slug: relation.slug, label: humanize(relation.model) },
|
|
427
|
+
...(help ? { help } : {}),
|
|
428
|
+
}
|
|
378
429
|
const declared = fields.find(field => field.name === relation.field)
|
|
379
430
|
if (declared) {
|
|
380
431
|
Object.assign(declared, asRelation, { options: [] })
|
|
@@ -390,6 +441,42 @@ export function modelCreateFields(Model: any): ModelCreateField[] {
|
|
|
390
441
|
})
|
|
391
442
|
}
|
|
392
443
|
|
|
444
|
+
/**
|
|
445
|
+
* What is wrong with a model's `dashboard.fields`, in words that name the
|
|
446
|
+
* field: a setting that cannot be read, a name the model does not have, an
|
|
447
|
+
* input asked of a field that is not text. `tableColumns` are the columns a
|
|
448
|
+
* row of the table was seen to carry, when one was read.
|
|
449
|
+
*/
|
|
450
|
+
export function modelFieldProblems(Model: any, tableColumns: string[] = []): string[] {
|
|
451
|
+
const { fields, problems } = modelFieldOverrides(Model)
|
|
452
|
+
const found = [...problems]
|
|
453
|
+
// The model's own, and the ones its table is seen to have: a trait can add
|
|
454
|
+
// a column the definition does not list (`email_verified_at`).
|
|
455
|
+
const columns = new Set([...modelSchemaColumns(Model), ...tableColumns])
|
|
456
|
+
const attributes = Object.fromEntries(Object.entries((Model?.attributes ?? {}) as Record<string, any>).map(([name, definition]) => [snakeCase(name), [name, definition] as const]))
|
|
457
|
+
for (const [column, override] of Object.entries(fields)) {
|
|
458
|
+
const called = `'${override.declared}'`
|
|
459
|
+
if (!columns.has(column)) {
|
|
460
|
+
found.push(`${called} is not a field of the model`)
|
|
461
|
+
continue
|
|
462
|
+
}
|
|
463
|
+
if (override.input) {
|
|
464
|
+
const attribute = attributes[column]
|
|
465
|
+
const derived = attribute ? modelFieldType(attribute[0], attribute[1]) : null
|
|
466
|
+
// A column a trait or a relation adds is not one of the model's own
|
|
467
|
+
// attributes, so no form asks for it as text.
|
|
468
|
+
if (!derived)
|
|
469
|
+
found.push(`${called} asks for the '${override.input}' input, but it is not an attribute the forms ask for`)
|
|
470
|
+
else if (!TEXT_KINDS.has(derived))
|
|
471
|
+
found.push(`${called} asks for the '${override.input}' input, but it is not a field of text (it is asked for as ${derived})`)
|
|
472
|
+
}
|
|
473
|
+
const currency = override.display?.currencyColumn
|
|
474
|
+
if (currency && !columns.has(currency))
|
|
475
|
+
found.push(`${called} names '${currency}' as its currency field, which the model does not have`)
|
|
476
|
+
}
|
|
477
|
+
return found
|
|
478
|
+
}
|
|
479
|
+
|
|
393
480
|
/** Physical database columns accepted by the model's generic update API. */
|
|
394
481
|
export function modelWritableColumns(Model: any): string[] {
|
|
395
482
|
return modelCreateFields(Model).map(field => snakeCase(field.name))
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { RequestInstance } from '@stacksjs/types'
|
|
2
|
+
import { Action } from '@stacksjs/actions/runtime'
|
|
3
|
+
import { passwordResets } from '@stacksjs/auth'
|
|
4
|
+
import { response } from '@stacksjs/router'
|
|
5
|
+
import { safeGet } from '../../../../resources/functions/dashboard/data'
|
|
6
|
+
import { dashboardOperationalError } from '../dashboard-response'
|
|
7
|
+
import { parseRowId, resolveWritableModel } from '../Models/model-write'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* "Send password reset" on a user: mails the account's holder the same link
|
|
11
|
+
* "Forgot password" would, for the person who cannot sign in and wrote to
|
|
12
|
+
* support instead, or who lost the invitation they were sent.
|
|
13
|
+
*
|
|
14
|
+
* It has no route of its own. The User model lists it under
|
|
15
|
+
* `dashboard.actions`, and the dashboard runs it for the record whose button
|
|
16
|
+
* was pressed, which arrives as the `id` route parameter. It is the first
|
|
17
|
+
* action wired that way, and is what one looks like.
|
|
18
|
+
*
|
|
19
|
+
* Nothing about the account changes until the link is used: the current
|
|
20
|
+
* password keeps working, and no session is ended.
|
|
21
|
+
*/
|
|
22
|
+
export default new Action({
|
|
23
|
+
name: 'Dashboard User Password Reset',
|
|
24
|
+
description: 'Emails a user a link to choose a new password.',
|
|
25
|
+
method: 'POST',
|
|
26
|
+
apiResponse: true,
|
|
27
|
+
async handle(request: RequestInstance) {
|
|
28
|
+
const resolved = await resolveWritableModel('user')
|
|
29
|
+
if ('error' in resolved)
|
|
30
|
+
return response.json({ message: resolved.error }, resolved.status)
|
|
31
|
+
const id = parseRowId(request.getParam('id'))
|
|
32
|
+
const user = id === null ? null : await resolved.Model.find(id)
|
|
33
|
+
if (!user)
|
|
34
|
+
return response.json({ message: 'This user was not found.' }, 404)
|
|
35
|
+
|
|
36
|
+
const email = String(safeGet(user, 'email', '') ?? '')
|
|
37
|
+
let sent: boolean
|
|
38
|
+
try {
|
|
39
|
+
sent = await passwordResets(email).sendEmail()
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
return dashboardOperationalError(error, 'The email could not be sent. Check the mail settings and try again.', 'UserPasswordResetAction.deliver', 502)
|
|
43
|
+
}
|
|
44
|
+
// The account exists, so nothing sent means one went out moments ago:
|
|
45
|
+
// links are throttled per address (`config.auth.passwordReset.throttle`).
|
|
46
|
+
if (!sent)
|
|
47
|
+
return response.json({ message: `A link was sent to ${email} moments ago. Wait a little before sending another.` }, 429)
|
|
48
|
+
return { message: `${email} was sent a link to choose a new password.` }
|
|
49
|
+
},
|
|
50
|
+
})
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Action } from '@stacksjs/actions/runtime'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A published release artifact of the desktop app this project builds:
|
|
5
|
+
* `GET /api/updates/files/{channel}/{target}/{version}/{file}`, where
|
|
6
|
+
* `target` is `<platform>-<arch>` (`darwin-arm64`).
|
|
7
|
+
*
|
|
8
|
+
* It is where `buddy release:desktop` points a manifest and release.json when
|
|
9
|
+
* `webview.updates.artifactBaseUrl` is not set, so a release on any disk, the
|
|
10
|
+
* private local one included, can be downloaded with no bucket or CDN. The
|
|
11
|
+
* file is streamed from the updates disk with its length and byte ranges, so
|
|
12
|
+
* a large installer is never held in memory and a download can resume.
|
|
13
|
+
*
|
|
14
|
+
* Only a key of the artifact shape under `webview.updates.prefix` is read: no
|
|
15
|
+
* path here reaches another folder or file on the disk. Public on purpose,
|
|
16
|
+
* like the feed: an installer is downloaded before anyone has an account.
|
|
17
|
+
*
|
|
18
|
+
* Mounted by the framework beside the feed. The URL a release writes follows
|
|
19
|
+
* `feedUrl`, so to move it, mount this action at `<feedUrl path>/files/...`.
|
|
20
|
+
*/
|
|
21
|
+
export default new Action({
|
|
22
|
+
name: 'DesktopUpdateArtifactAction',
|
|
23
|
+
description: 'Streams a published desktop release artifact from the updates disk.',
|
|
24
|
+
method: 'GET',
|
|
25
|
+
|
|
26
|
+
async handle(request: RequestInstance) {
|
|
27
|
+
const { answerUpdateArtifact } = await import('@stacksjs/desktop-build/update-feed')
|
|
28
|
+
return answerUpdateArtifact({
|
|
29
|
+
channel: String(request.getParam('channel') ?? ''),
|
|
30
|
+
target: String(request.getParam('target') ?? ''),
|
|
31
|
+
version: String(request.getParam('version') ?? ''),
|
|
32
|
+
file: String(request.getParam('file') ?? ''),
|
|
33
|
+
range: request.headers?.get('range') ?? null,
|
|
34
|
+
method: request.method,
|
|
35
|
+
})
|
|
36
|
+
},
|
|
37
|
+
})
|
|
@@ -110,5 +110,10 @@ export default defineModel({
|
|
|
110
110
|
|
|
111
111
|
dashboard: {
|
|
112
112
|
columns: ['path', 'card', 'mimeType', 'sizeBytes', 'createdAt'],
|
|
113
|
+
// A count of bytes reads as a size: "1.5 MB".
|
|
114
|
+
fields: {
|
|
115
|
+
sizeBytes: { label: 'Size', format: 'bytes' },
|
|
116
|
+
mimeType: { label: 'Type' },
|
|
117
|
+
},
|
|
113
118
|
},
|
|
114
119
|
} as const)
|
package/app/Models/Deployment.ts
CHANGED
|
@@ -113,6 +113,11 @@ export default defineModel({
|
|
|
113
113
|
|
|
114
114
|
dashboard: {
|
|
115
115
|
columns: ['commitMessage', 'branch', 'environment', 'status', 'duration', 'createdAt'],
|
|
116
|
+
// A deploy is timed in seconds, and its address is somewhere to go.
|
|
117
|
+
fields: {
|
|
118
|
+
duration: { format: 'duration', unit: 's' },
|
|
119
|
+
url: { label: 'URL', format: 'link' },
|
|
120
|
+
},
|
|
116
121
|
highlight: true,
|
|
117
122
|
},
|
|
118
123
|
} as const)
|
package/app/Models/QueryLog.ts
CHANGED
|
@@ -192,5 +192,10 @@ export default defineModel({
|
|
|
192
192
|
|
|
193
193
|
dashboard: {
|
|
194
194
|
columns: ['query', 'duration', 'status', 'connection', 'model', 'executedAt'],
|
|
195
|
+
// SQL reads as code, and a query is timed in milliseconds.
|
|
196
|
+
fields: {
|
|
197
|
+
query: { format: 'code' },
|
|
198
|
+
duration: { format: 'duration' },
|
|
199
|
+
},
|
|
195
200
|
},
|
|
196
201
|
} as const)
|
package/app/Models/Request.ts
CHANGED
|
@@ -147,5 +147,9 @@ export default defineModel({
|
|
|
147
147
|
|
|
148
148
|
dashboard: {
|
|
149
149
|
columns: ['method', 'path', 'statusCode', 'durationMs', 'ipAddress', 'createdAt'],
|
|
150
|
+
// Stored in milliseconds: "634 ms", "1.2 s".
|
|
151
|
+
fields: {
|
|
152
|
+
durationMs: { label: 'Duration', format: 'duration' },
|
|
153
|
+
},
|
|
150
154
|
},
|
|
151
155
|
} as const)
|
package/app/Models/Team.ts
CHANGED
|
@@ -76,6 +76,10 @@ export default defineModel({
|
|
|
76
76
|
|
|
77
77
|
dashboard: {
|
|
78
78
|
columns: ['name', 'memberCount', 'status', 'createdAt'],
|
|
79
|
+
// A count of members is called what it counts.
|
|
80
|
+
fields: {
|
|
81
|
+
memberCount: { label: 'Members' },
|
|
82
|
+
},
|
|
79
83
|
highlight: true,
|
|
80
84
|
},
|
|
81
85
|
} as const)
|
package/app/Models/User.ts
CHANGED
|
@@ -163,11 +163,29 @@ export default defineModel({
|
|
|
163
163
|
// The list is for telling accounts apart. The two-factor step, the
|
|
164
164
|
// billing id and the rest are on the account's own page.
|
|
165
165
|
columns: ['name', 'email', 'emailVerifiedAt', 'createdAt'],
|
|
166
|
+
// An avatar is a picture, not the address of one.
|
|
167
|
+
fields: {
|
|
168
|
+
avatar: { format: 'image' },
|
|
169
|
+
},
|
|
166
170
|
// Operators add, edit and remove accounts from the dashboard. Not through
|
|
167
171
|
// `useApi` above: a generated PATCH would let any signed-in caller change
|
|
168
172
|
// their own email and password outside the flows that verify them.
|
|
169
173
|
// `store` is by invitation: no form can supply a password, so the account
|
|
170
174
|
// is created and its holder is mailed a link to choose one.
|
|
171
175
|
writes: ['store', 'update', 'destroy'],
|
|
176
|
+
// What support is asked for most: the person cannot sign in, or lost the
|
|
177
|
+
// invitation. The button runs an action; the model only names it.
|
|
178
|
+
actions: [
|
|
179
|
+
{
|
|
180
|
+
name: 'send-password-reset',
|
|
181
|
+
label: 'Send password reset',
|
|
182
|
+
action: 'Actions/Dashboard/Users/UserPasswordResetAction',
|
|
183
|
+
icon: 'i-hugeicons-reset-password',
|
|
184
|
+
confirm: 'They are emailed a link to choose a new password. Their current password keeps working until they use it.',
|
|
185
|
+
// On the account's own page, where who it is can be read first. Not
|
|
186
|
+
// one press away on every row of a list.
|
|
187
|
+
on: ['record'],
|
|
188
|
+
},
|
|
189
|
+
],
|
|
172
190
|
},
|
|
173
191
|
} as const)
|