@romedb/studio 0.0.0-stage → 0.1.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.
Files changed (38) hide show
  1. package/LICENSE.md +20 -0
  2. package/README.md +15 -2
  3. package/package.json +36 -4
  4. package/src/components/CodeEditor.ts +298 -0
  5. package/src/components/studio/BrowseTab.ts +457 -0
  6. package/src/components/studio/CellEditorSheet.ts +165 -0
  7. package/src/components/studio/DatabaseTab.ts +148 -0
  8. package/src/components/studio/GraphCanvas.ts +90 -0
  9. package/src/components/studio/GraphTab.ts +110 -0
  10. package/src/components/studio/InsertRowSheet.ts +180 -0
  11. package/src/components/studio/MapCanvas.ts +177 -0
  12. package/src/components/studio/MapTab.ts +121 -0
  13. package/src/components/studio/NewObjectSheet.ts +235 -0
  14. package/src/components/studio/QueryHistorySheet.ts +111 -0
  15. package/src/components/studio/RowSheets.ts +226 -0
  16. package/src/components/studio/SqlConsole.ts +281 -0
  17. package/src/components/studio/StructureTab.ts +209 -0
  18. package/src/components/studio/icon-button.ts +21 -0
  19. package/src/lib/filters.ts +44 -0
  20. package/src/lib/graph.ts +214 -0
  21. package/src/lib/romeql.ts +106 -0
  22. package/src/lib/spatial.ts +518 -0
  23. package/src/lib/sql-text.ts +208 -0
  24. package/src/lib/studio-types.ts +268 -0
  25. package/src/lib/values.ts +305 -0
  26. package/src/monaco-standalone.d.ts +27 -0
  27. package/src/schema-draft.ts +41 -0
  28. package/src/schema-store.ts +8 -0
  29. package/src/server/backend.ts +77 -0
  30. package/src/server/data.ts +142 -0
  31. package/src/server/edits.ts +92 -0
  32. package/src/server/graph.ts +66 -0
  33. package/src/server/queries.ts +108 -0
  34. package/src/server/schema.ts +272 -0
  35. package/src/server/spatial.ts +74 -0
  36. package/src/studio-page.ts +84 -0
  37. package/src/workspace/StudioWorkspace.ts +1970 -0
  38. package/src/workspace/workspace.css +38 -0
@@ -0,0 +1,226 @@
1
+ // Adapted from Cossack Studio's RowSheets (MIT) for typed RomeDB values.
2
+ import { Component, Cossack } from '@cossackframework/core';
3
+ import { component, html } from '@cossackframework/renderer';
4
+ import { ExportIcon } from '@cossackframework/solar-icons/export';
5
+ import { Alert, Button, Checkbox, Icon, Sheet } from '@cossackframework/ui';
6
+ import type { StudioObject } from '../../lib/studio-types';
7
+ import { VALUE_KINDS } from '../../lib/values';
8
+ import type { BatchUpdateState, CellSelection, ExportFormat, ExportSheetState, RowEditorState } from '../../studio-page';
9
+ import { CodeEditor } from '../CodeEditor';
10
+ import { valueEditor } from './CellEditorSheet';
11
+
12
+ interface RowEditorSheetProps {
13
+ object?: StudioObject;
14
+ editor: RowEditorState | null;
15
+ theme: 'light' | 'dark';
16
+ saving: boolean;
17
+ onClose: () => void;
18
+ onChange: (value: string) => void;
19
+ onSave: () => void;
20
+ [key: string]: unknown;
21
+ }
22
+
23
+ @Component()
24
+ export class RowEditorSheet extends Cossack {
25
+ declare props: RowEditorSheetProps;
26
+
27
+ render() {
28
+ const { editor, object } = this.props;
29
+ const isDocument = object?.model === 'document';
30
+ return component(Sheet, {
31
+ open: Boolean(editor && object),
32
+ side: 'right',
33
+ size: 'min(48rem, 94vw)',
34
+ onOpenChange: this.props.onClose,
35
+ 'data-testid': 'row-editor-sheet',
36
+ 'aria-label': isDocument ? 'Edit document' : 'Edit row',
37
+ }, html`
38
+ <header class="flex shrink-0 items-start justify-between border-b p-5">
39
+ <div>
40
+ <h2 class="font-semibold">${isDocument ? 'Edit document as JSON' : 'Edit row as JSON'}</h2>
41
+ <p class="mt-1 text-sm text-muted-foreground">
42
+ ${object?.name ?? ''} · ${isDocument ? 'saving replaces the document content' : 'only changed properties are written'}
43
+ </p>
44
+ </div>
45
+ ${component(Button, { variant: 'ghost', size: 'sm', '@click': this.props.onClose }, 'Close')}
46
+ </header>
47
+ <div class="flex min-h-0 flex-1 flex-col gap-4 p-5">
48
+ ${editor?.error ? component(Alert, { variant: 'destructive', 'data-testid': 'row-editor-error' }, editor.error) : ''}
49
+ ${component(CodeEditor, {
50
+ class: 'min-h-[24rem] flex-1',
51
+ value: editor?.value ?? '{}',
52
+ language: 'json',
53
+ theme: this.props.theme,
54
+ enabled: Boolean(editor),
55
+ ariaLabel: `JSON ${isDocument ? 'document' : 'row'} for ${object?.name ?? 'table'}`,
56
+ 'data-testid': 'row-json-editor',
57
+ onChange: this.props.onChange,
58
+ onRun: this.props.onSave,
59
+ })}
60
+ <p class="text-xs text-muted-foreground">
61
+ Ctrl/Cmd+Enter saves. ${isDocument
62
+ ? 'Remove a property to remove that field. The id cannot change.'
63
+ : 'Remove a property to leave that column unchanged; use null to write NULL. Primary keys cannot change.'}
64
+ Typed values use ${'{"$record": "users:tan"}'}, ${'{"$number": 3}'} and ${'{"$binary": "0a0b"}'}.
65
+ The save fails if the row changed since it was loaded.
66
+ </p>
67
+ </div>
68
+ <footer class="flex shrink-0 justify-end gap-2 border-t p-4">
69
+ ${component(Button, { variant: 'ghost', '@click': this.props.onClose }, 'Cancel')}
70
+ ${component(Button, { disabled: this.props.saving, 'data-testid': 'save-row-editor', '@click': this.props.onSave }, this.props.saving ? 'Saving…' : isDocument ? 'Save document' : 'Save row')}
71
+ </footer>
72
+ `);
73
+ }
74
+ }
75
+
76
+ interface ExportSheetProps {
77
+ object?: StudioObject;
78
+ state: ExportSheetState | null;
79
+ browseColumns: string[];
80
+ onClose: () => void;
81
+ onFormatChange: (format: ExportFormat) => void;
82
+ onToggleColumn: (name: string, checked: boolean) => void;
83
+ onDownload: () => void;
84
+ [key: string]: unknown;
85
+ }
86
+
87
+ @Component()
88
+ export class ExportSheet extends Cossack {
89
+ declare props: ExportSheetProps;
90
+
91
+ render() {
92
+ const { state, object } = this.props;
93
+ const columns = this.props.browseColumns.length
94
+ ? this.props.browseColumns
95
+ : object?.columns.filter(column => !column.hidden).map(column => column.name) ?? [];
96
+ return component(Sheet, {
97
+ open: Boolean(state && object),
98
+ side: 'right',
99
+ size: 'min(32rem, 92vw)',
100
+ onOpenChange: this.props.onClose,
101
+ 'data-testid': 'export-sheet',
102
+ 'aria-label': 'Export rows',
103
+ }, html`
104
+ <header class="border-b p-5">
105
+ <h2 class="font-semibold">Export ${state?.rowIndexes.length ?? 0} row${state?.rowIndexes.length === 1 ? '' : 's'}</h2>
106
+ <p class="mt-1 text-sm text-muted-foreground">
107
+ Choose a format and the columns to include. JSON keeps RomeDB types; CSV contains display text.
108
+ </p>
109
+ </header>
110
+ <div class="min-h-0 flex-1 overflow-auto p-5">
111
+ <label class="grid gap-1 text-sm font-medium">
112
+ Format
113
+ <select
114
+ class="h-10 rounded-md border border-input bg-background px-3 font-normal"
115
+ data-testid="export-format"
116
+ .value="${state?.format ?? 'json'}"
117
+ @change="${(event: Event) => this.props.onFormatChange((event.target as HTMLSelectElement).value as ExportFormat)}"
118
+ >
119
+ <option value="json">JSON</option>
120
+ <option value="csv">CSV</option>
121
+ </select>
122
+ </label>
123
+ <fieldset class="mt-5 grid gap-3">
124
+ <legend class="mb-2 text-sm font-medium">Columns</legend>
125
+ ${columns.map(column => component(Checkbox, {
126
+ checked: state?.columns.includes(column) ?? false,
127
+ '@change': (event: Event) => this.props.onToggleColumn(column, (event.currentTarget as HTMLInputElement).checked),
128
+ }, column))}
129
+ </fieldset>
130
+ </div>
131
+ <footer class="flex justify-end gap-2 border-t p-4">
132
+ ${component(Button, { variant: 'ghost', '@click': this.props.onClose }, 'Cancel')}
133
+ ${component(Button, {
134
+ class: 'gap-2',
135
+ disabled: !state?.columns.length,
136
+ 'data-testid': 'download-export',
137
+ '@click': this.props.onDownload,
138
+ }, html`${component(Icon, { entry: ExportIcon, size: 16 })}Export`)}
139
+ </footer>
140
+ `);
141
+ }
142
+ }
143
+
144
+ interface BatchUpdateSheetProps {
145
+ object?: StudioObject;
146
+ state: BatchUpdateState | null;
147
+ selectedCount: number;
148
+ saving: boolean;
149
+ theme: 'light' | 'dark';
150
+ onClose: () => void;
151
+ onColumnChange: (column: string) => void;
152
+ onSelectionChange: (selection: CellSelection) => void;
153
+ onValueChange: (value: string) => void;
154
+ onSubmit: () => void;
155
+ [key: string]: unknown;
156
+ }
157
+
158
+ @Component()
159
+ export class BatchUpdateSheet extends Cossack {
160
+ declare props: BatchUpdateSheetProps;
161
+
162
+ render() {
163
+ const { state, object } = this.props;
164
+ const column = object?.columns.find(candidate => candidate.name === state?.column);
165
+ const editable = object?.columns.filter(candidate => !candidate.hidden && !candidate.readonly && !object.keyFields.includes(candidate.name)) ?? [];
166
+ return component(Sheet, {
167
+ open: Boolean(state && object),
168
+ side: 'right',
169
+ size: 'min(32rem, 92vw)',
170
+ onOpenChange: this.props.onClose,
171
+ 'data-testid': 'batch-update-sheet',
172
+ 'aria-label': 'Update rows',
173
+ }, html`
174
+ <header class="border-b p-5">
175
+ <h2 class="font-semibold">Update ${this.props.selectedCount} selected row${this.props.selectedCount === 1 ? '' : 's'}</h2>
176
+ <p class="mt-1 text-sm text-muted-foreground">
177
+ Set one ${object?.model === 'document' ? 'field' : 'column'} to the same value for every selected row, in one atomic write.
178
+ </p>
179
+ </header>
180
+ <div class="flex min-h-0 flex-1 flex-col gap-4 p-5">
181
+ ${state?.error ? component(Alert, { variant: 'destructive', 'data-testid': 'batch-error' }, state.error) : ''}
182
+ <label class="grid gap-1 text-sm font-medium">
183
+ ${object?.model === 'document' ? 'Field' : 'Column'}
184
+ ${object?.model === 'document' ? html`
185
+ <input
186
+ list="batch-update-fields"
187
+ class="h-10 rounded-md border border-input bg-background px-3 font-normal"
188
+ data-testid="batch-column"
189
+ .value="${state?.column ?? ''}"
190
+ @input="${(event: InputEvent) => this.props.onColumnChange((event.target as HTMLInputElement).value)}"
191
+ />
192
+ <datalist id="batch-update-fields">${editable.map(candidate => html`<option value="${candidate.name}"></option>`)}</datalist>
193
+ ` : html`
194
+ <select
195
+ class="h-10 rounded-md border border-input bg-background px-3 font-normal"
196
+ data-testid="batch-column"
197
+ .value="${state?.column ?? ''}"
198
+ @change="${(event: Event) => this.props.onColumnChange((event.target as HTMLSelectElement).value)}"
199
+ >
200
+ ${editable.map(candidate => html`<option value="${candidate.name}" ?selected="${candidate.name === state?.column}">${candidate.name}</option>`)}
201
+ </select>
202
+ `}
203
+ </label>
204
+ <label class="grid gap-1 text-sm font-medium">
205
+ Value type
206
+ <select
207
+ class="h-10 rounded-md border border-input bg-background px-3 font-normal"
208
+ data-testid="batch-kind"
209
+ .value="${state?.mode === 'null' ? 'null' : state?.kind ?? 'text'}"
210
+ @change="${(event: Event) => this.props.onSelectionChange((event.target as HTMLSelectElement).value as CellSelection)}"
211
+ >
212
+ ${VALUE_KINDS.map(kind => html`<option value="${kind.value}" ?selected="${state?.mode === 'value' && state.kind === kind.value}">${kind.label}</option>`)}
213
+ ${column?.nullable !== false ? html`<option value="null" ?selected="${state?.mode === 'null'}">NULL</option>` : ''}
214
+ </select>
215
+ </label>
216
+ <div class="min-h-0 flex-1 ${state?.mode === 'null' ? 'pointer-events-none opacity-50' : ''}">
217
+ ${state ? valueEditor(state, state.column, { theme: this.props.theme, onValueChange: this.props.onValueChange, onSelectionChange: this.props.onSelectionChange, onSave: this.props.onSubmit }) : ''}
218
+ </div>
219
+ </div>
220
+ <footer class="flex justify-end gap-2 border-t p-4">
221
+ ${component(Button, { variant: 'ghost', '@click': this.props.onClose }, 'Cancel')}
222
+ ${component(Button, { disabled: this.props.saving, 'data-testid': 'submit-batch-update', '@click': this.props.onSubmit }, this.props.saving ? 'Updating…' : 'Update selected')}
223
+ </footer>
224
+ `);
225
+ }
226
+ }
@@ -0,0 +1,281 @@
1
+ // Adapted from Cossack Studio's SqlConsole (MIT) for RomeDB reads, atomic writes and typed results.
2
+ import { Component, Cossack, Shared } from '@cossackframework/core';
3
+ import { component, html } from '@cossackframework/renderer';
4
+ import { GraphIcon } from '@cossackframework/solar-icons/graph';
5
+ import { HistoryIcon } from '@cossackframework/solar-icons/history';
6
+ import { PlayIcon } from '@cossackframework/solar-icons/play';
7
+ import { SettingsMinimalisticIcon } from '@cossackframework/solar-icons/settings-minimalistic';
8
+ import { StopIcon } from '@cossackframework/solar-icons/stop';
9
+ import { Button, Input } from '@cossackframework/ui';
10
+ import type { PortableValue } from '@romedb/client';
11
+ import type { MapFeature, SqlRun, StudioSchema } from '../../lib/studio-types';
12
+ import { displayValue } from '../../lib/values';
13
+ import { resultGraph } from '../../lib/graph';
14
+ import { cellText, geometryOf, resultFeatures } from '../../lib/spatial';
15
+ import { GraphCanvas } from './GraphCanvas';
16
+ import { MapCanvas } from './MapCanvas';
17
+ import type { SqlOutput } from '../../studio-page';
18
+ import { CodeEditor } from '../CodeEditor';
19
+ import { iconButton } from './icon-button';
20
+
21
+ interface SqlConsoleProps {
22
+ active: boolean;
23
+ sql: string;
24
+ theme: 'light' | 'dark';
25
+ schema: StudioSchema;
26
+ output: SqlOutput;
27
+ run: SqlRun | null;
28
+ explainRun: SqlRun | null;
29
+ resultIndex: number;
30
+ running: boolean;
31
+ explaining: boolean;
32
+ showOptions: boolean;
33
+ parameters: string;
34
+ revision: string;
35
+ /** False hides the revision in the summary and the "Expected revision" option. Default true. */
36
+ showRevision?: boolean;
37
+ rowsRead: string;
38
+ vmSteps: string;
39
+ onSqlChange: (value: string) => void;
40
+ onRun: () => void;
41
+ onExplain: () => void;
42
+ onCancel: () => void;
43
+ onOpenHistory: () => void;
44
+ onToggleOptions: () => void;
45
+ onOptionChange: (name: 'parameters' | 'revision' | 'rowsRead' | 'vmSteps', value: string) => void;
46
+ onOutputChange: (output: SqlOutput) => void;
47
+ onResultIndexChange: (index: number) => void;
48
+ mapSelected: string;
49
+ onSelectMapFeature: (feature: MapFeature) => void;
50
+ onOpenRecord: (record: PortableValue) => void;
51
+ [key: string]: unknown;
52
+ }
53
+
54
+ @Component()
55
+ export class SqlConsole extends Cossack {
56
+ declare props: SqlConsoleProps;
57
+
58
+ render() {
59
+ const run = this.props.output === 'explain' ? this.props.explainRun : this.props.run;
60
+ const index = Math.min(this.props.resultIndex, Math.max(0, (run?.results.length ?? 1) - 1));
61
+ const result = run?.results[index];
62
+ const busy = this.props.running || this.props.explaining;
63
+ return html`
64
+ <div class="${this.props.active ? 'grid' : 'hidden'} min-h-0 flex-1 ${this.props.showOptions ? 'grid-rows-[25rem_minmax(0,1fr)]' : 'grid-rows-[17rem_minmax(0,1fr)]'}">
65
+ <div class="border-b bg-muted/20 p-4">
66
+ ${component(CodeEditor, {
67
+ class: 'h-[13rem]',
68
+ value: this.props.sql,
69
+ language: 'sql',
70
+ theme: this.props.theme,
71
+ schema: this.props.schema,
72
+ enabled: this.props.active,
73
+ ariaLabel: 'RomeQL statements',
74
+ 'data-testid': 'sql-editor',
75
+ onChange: this.props.onSqlChange,
76
+ onRun: this.props.onRun,
77
+ })}
78
+ ${this.props.showOptions ? html`
79
+ <div class="mt-2 grid gap-2 rounded-md border bg-card p-3 md:grid-cols-[minmax(0,2fr)_repeat(3,minmax(0,1fr))]" data-testid="sql-options">
80
+ <label class="grid gap-1 text-xs font-medium">
81
+ Parameters (JSON object)
82
+ ${component(Input, {
83
+ class: 'font-mono',
84
+ placeholder: '{"$id": 1, "$owner": {"$record": "users:tan"}}',
85
+ 'data-testid': 'sql-parameters',
86
+ '.value': this.props.parameters,
87
+ '@input': (event: InputEvent) => this.props.onOptionChange('parameters', (event.target as HTMLInputElement).value),
88
+ })}
89
+ </label>
90
+ ${this.props.showRevision === false ? '' : html`<label class="grid gap-1 text-xs font-medium">
91
+ Expected revision
92
+ ${component(Input, {
93
+ inputmode: 'numeric',
94
+ placeholder: 'Any',
95
+ '.value': this.props.revision,
96
+ '@input': (event: InputEvent) => this.props.onOptionChange('revision', (event.target as HTMLInputElement).value),
97
+ })}
98
+ </label>`}
99
+ <label class="grid gap-1 text-xs font-medium">
100
+ Rows read budget
101
+ ${component(Input, {
102
+ inputmode: 'numeric',
103
+ '.value': this.props.rowsRead,
104
+ '@input': (event: InputEvent) => this.props.onOptionChange('rowsRead', (event.target as HTMLInputElement).value),
105
+ })}
106
+ </label>
107
+ <label class="grid gap-1 text-xs font-medium">
108
+ VM step budget
109
+ ${component(Input, {
110
+ inputmode: 'numeric',
111
+ '.value': this.props.vmSteps,
112
+ '@input': (event: InputEvent) => this.props.onOptionChange('vmSteps', (event.target as HTMLInputElement).value),
113
+ })}
114
+ </label>
115
+ <p class="text-xs text-muted-foreground md:col-span-4">
116
+ Parameters apply to each statement that references them.${this.props.showRevision === false ? '' : ` An expected revision makes a write fail
117
+ instead of overwriting changes made since that revision.`}
118
+ </p>
119
+ </div>
120
+ ` : ''}
121
+ <div class="mt-2 flex items-center justify-between gap-2">
122
+ <span class="text-xs text-muted-foreground">
123
+ Ctrl/Cmd+Enter to execute · separate statements with ; · writes run as one atomic request after confirmation
124
+ </span>
125
+ <div class="flex items-center gap-1">
126
+ ${iconButton('sql-options-tip', SettingsMinimalisticIcon, this.props.showOptions ? 'Hide request options' : 'Request options', this.props.onToggleOptions, {
127
+ 'data-testid': 'toggle-sql-options',
128
+ variant: this.props.showOptions ? 'secondary' : 'ghost',
129
+ })}
130
+ ${iconButton('sql-history-tip', HistoryIcon, 'Query history', this.props.onOpenHistory, { 'data-testid': 'open-query-history' })}
131
+ ${iconButton('sql-explain-tip', GraphIcon,
132
+ this.props.explaining ? 'Generating query plan' : 'Explain query',
133
+ this.props.onExplain,
134
+ {
135
+ disabled: Boolean(busy || !this.props.sql.trim()),
136
+ 'data-testid': 'explain-sql',
137
+ variant: this.props.output === 'explain' ? 'secondary' : 'ghost',
138
+ },
139
+ )}
140
+ ${busy ? iconButton('sql-cancel-tip', StopIcon, 'Cancel request', this.props.onCancel, {
141
+ 'data-testid': 'cancel-sql',
142
+ variant: 'outline',
143
+ }) : ''}
144
+ ${iconButton('sql-run-tip', PlayIcon,
145
+ this.props.running ? 'Running query' : 'Run query',
146
+ this.props.onRun,
147
+ { disabled: busy, 'data-testid': 'run-sql', variant: 'default' },
148
+ )}
149
+ </div>
150
+ </div>
151
+ </div>
152
+ <div class="flex min-h-0 flex-col overflow-hidden">
153
+ <div class="flex h-10 shrink-0 items-center gap-1 border-b px-3">
154
+ ${component(Button, {
155
+ variant: this.props.output === 'results' ? 'secondary' : 'ghost',
156
+ size: 'sm',
157
+ 'data-testid': 'sql-output-results',
158
+ '@click': () => this.props.onOutputChange('results'),
159
+ }, 'Results')}
160
+ ${component(Button, {
161
+ variant: this.props.output === 'graph' ? 'secondary' : 'ghost',
162
+ size: 'sm',
163
+ 'data-testid': 'sql-output-graph',
164
+ '@click': () => this.props.onOutputChange('graph'),
165
+ }, 'Graph')}
166
+ ${component(Button, {
167
+ variant: this.props.output === 'map' ? 'secondary' : 'ghost',
168
+ size: 'sm',
169
+ 'data-testid': 'sql-output-map',
170
+ '@click': () => this.props.onOutputChange('map'),
171
+ }, 'Map')}
172
+ ${component(Button, {
173
+ variant: this.props.output === 'explain' ? 'secondary' : 'ghost',
174
+ size: 'sm',
175
+ 'data-testid': 'sql-output-explain',
176
+ '@click': () => this.props.onOutputChange('explain'),
177
+ }, 'Explain')}
178
+ ${run && run.results.length > 1 ? html`
179
+ <span class="ml-auto flex items-center gap-1" data-testid="statement-results">
180
+ ${run.results.map((_, position) => component(Button, {
181
+ variant: position === index ? 'outline' : 'ghost',
182
+ size: 'sm',
183
+ '@click': () => this.props.onResultIndexChange(position),
184
+ }, `Statement ${position + 1}`))}
185
+ </span>
186
+ ` : ''}
187
+ </div>
188
+ <div class="min-h-0 flex-1 overflow-auto">
189
+ ${run?.error ? html`
190
+ <div class="m-3 rounded-md border border-destructive/30 bg-destructive/10 p-3 text-sm text-destructive" role="alert" data-testid="sql-error">
191
+ <strong class="font-mono">${run.error.code}</strong> · ${outcomeLabel(run.error.outcome)}
192
+ <p class="mt-1">${run.error.message}</p>
193
+ </div>
194
+ ` : ''}
195
+ ${result && this.props.output === 'graph' ? this.renderGraph(result) : ''}
196
+ ${result && this.props.output === 'map' ? this.renderMap(result) : ''}
197
+ ${result && this.props.output !== 'graph' && this.props.output !== 'map' ? html`
198
+ <table class="w-full text-sm" data-testid="${this.props.output === 'explain' ? 'explain-results' : 'sql-results'}">
199
+ <thead class="sticky top-0 bg-muted"><tr>
200
+ ${result.columns.map(column => html`<th class="border-b border-r p-2 text-left">${column}</th>`)}
201
+ </tr></thead>
202
+ <tbody>${result.rows.map(row => html`<tr>
203
+ ${result.columns.map(column => html`
204
+ <td class="studio-cell border-b border-r p-2 font-mono">
205
+ <span class="studio-cell-value ${row[column]?.type === 'Null' ? 'italic text-muted-foreground' : ''}" title="${geometryOf(row[column]) ? displayValue(row[column]).slice(0, 2000) : ''}">${cellText(row[column])}</span>
206
+ </td>
207
+ `)}
208
+ </tr>`)}</tbody>
209
+ </table>
210
+ ` : ''}
211
+ ${run ? html`
212
+ <div class="p-3 text-xs text-muted-foreground" data-testid="sql-summary">
213
+ ${this.props.output === 'explain'
214
+ ? `Plan generated in ${run.durationMs.toFixed(1)} ms`
215
+ : result
216
+ ? `${result.rows.length} returned row${result.rows.length === 1 ? '' : 's'} · ${result.affectedRows} affected row${result.affectedRows === 1 ? '' : 's'} · ${run.mode === 'write' ? 'atomic write' : 'read'}${this.props.showRevision === false ? '' : ` · revision ${run.sequence}`} · ${run.durationMs.toFixed(1)} ms`
217
+ : `${run.durationMs.toFixed(1)} ms`}
218
+ </div>
219
+ ` : html`
220
+ <div class="p-6 text-sm text-muted-foreground">Run a statement to see its results.</div>
221
+ `}
222
+ </div>
223
+ </div>
224
+ </div>
225
+ `;
226
+ }
227
+
228
+ @Shared()
229
+ renderGraph(result: Parameters<typeof resultGraph>[0]) {
230
+ const graph = resultGraph(result);
231
+ if (!graph) return graphHelp();
232
+ return html`<div class="p-3">${component(GraphCanvas, { nodes: graph.nodes, edges: graph.edges, 'data-testid': 'sql-graph' })}</div>`;
233
+ }
234
+
235
+ @Shared()
236
+ renderMap(result: Parameters<typeof resultFeatures>[0]) {
237
+ const { features, truncated } = resultFeatures(result);
238
+ if (!features.length) return mapHelp();
239
+ return html`<div class="p-3">
240
+ ${component(MapCanvas, {
241
+ features,
242
+ selected: this.props.mapSelected,
243
+ onSelectFeature: this.props.onSelectMapFeature,
244
+ onOpenRecord: this.props.onOpenRecord,
245
+ 'data-testid': 'sql-map',
246
+ })}
247
+ ${truncated ? html`<p class="mt-2 text-xs text-muted-foreground" data-testid="sql-map-truncated">Only the first ${features.length} features are drawn.</p>` : ''}
248
+ </div>`;
249
+ }
250
+ }
251
+
252
+ function mapHelp() {
253
+ return html`
254
+ <div class="p-6 text-sm text-muted-foreground" data-testid="sql-map-empty">
255
+ This result has no geometry. Select Point, Polygon or MultiPolygon values, for example
256
+ <code class="rounded bg-muted px-1">SELECT id, location FROM places</code>, rows from
257
+ <code class="rounded bg-muted px-1">search::bbox</code>, or <code class="rounded bg-muted px-1">geo::cell_boundary(cell)</code>.
258
+ </div>
259
+ `;
260
+ }
261
+
262
+ function graphHelp() {
263
+ return html`
264
+ <div class="p-6 text-sm text-muted-foreground" data-testid="sql-graph-empty">
265
+ This result has no graph shape. Select <code class="rounded bg-muted px-1">nodes</code> and
266
+ <code class="rounded bg-muted px-1">edges</code> from a <code class="rounded bg-muted px-1">GRAPH(... WITH PATH)</code> source,
267
+ <code class="rounded bg-muted px-1">id, source, target</code> from an edge table, or
268
+ <code class="rounded bg-muted px-1">root, node</code> from a GRAPH source.
269
+ </div>
270
+ `;
271
+ }
272
+
273
+ function outcomeLabel(outcome: string): string {
274
+ switch (outcome) {
275
+ case 'not-sent': return 'not sent, nothing changed';
276
+ case 'rolled-back': return 'rolled back, nothing changed';
277
+ case 'read-failed': return 'read failed';
278
+ case 'unknown': return 'outcome unknown, recover the saved request before another write';
279
+ default: return outcome;
280
+ }
281
+ }