@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.
- package/LICENSE.md +20 -0
- package/README.md +15 -2
- package/package.json +36 -4
- package/src/components/CodeEditor.ts +298 -0
- package/src/components/studio/BrowseTab.ts +457 -0
- package/src/components/studio/CellEditorSheet.ts +165 -0
- package/src/components/studio/DatabaseTab.ts +148 -0
- package/src/components/studio/GraphCanvas.ts +90 -0
- package/src/components/studio/GraphTab.ts +110 -0
- package/src/components/studio/InsertRowSheet.ts +180 -0
- package/src/components/studio/MapCanvas.ts +177 -0
- package/src/components/studio/MapTab.ts +121 -0
- package/src/components/studio/NewObjectSheet.ts +235 -0
- package/src/components/studio/QueryHistorySheet.ts +111 -0
- package/src/components/studio/RowSheets.ts +226 -0
- package/src/components/studio/SqlConsole.ts +281 -0
- package/src/components/studio/StructureTab.ts +209 -0
- package/src/components/studio/icon-button.ts +21 -0
- package/src/lib/filters.ts +44 -0
- package/src/lib/graph.ts +214 -0
- package/src/lib/romeql.ts +106 -0
- package/src/lib/spatial.ts +518 -0
- package/src/lib/sql-text.ts +208 -0
- package/src/lib/studio-types.ts +268 -0
- package/src/lib/values.ts +305 -0
- package/src/monaco-standalone.d.ts +27 -0
- package/src/schema-draft.ts +41 -0
- package/src/schema-store.ts +8 -0
- package/src/server/backend.ts +77 -0
- package/src/server/data.ts +142 -0
- package/src/server/edits.ts +92 -0
- package/src/server/graph.ts +66 -0
- package/src/server/queries.ts +108 -0
- package/src/server/schema.ts +272 -0
- package/src/server/spatial.ts +74 -0
- package/src/studio-page.ts +84 -0
- package/src/workspace/StudioWorkspace.ts +1970 -0
- package/src/workspace/workspace.css +38 -0
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
// Offline SVG map of points and regions: equirectangular, fitted to the data, no tiles or network requests.
|
|
2
|
+
import { Client, Component, Cossack } from '@cossackframework/core';
|
|
3
|
+
import { component, html, svg } from '@cossackframework/renderer';
|
|
4
|
+
import { Button } from '@cossackframework/ui';
|
|
5
|
+
import type { PortableValue } from '@romedb/client';
|
|
6
|
+
import { tableColor } from '../../lib/graph';
|
|
7
|
+
import { createProjection, degreeLabel, geometryPath, graticule, mapBounds, selectionBounds, type MapProjection } from '../../lib/spatial';
|
|
8
|
+
import type { MapFeature } from '../../lib/studio-types';
|
|
9
|
+
|
|
10
|
+
interface MapCanvasProps {
|
|
11
|
+
features: MapFeature[];
|
|
12
|
+
selected?: string;
|
|
13
|
+
onSelectFeature?: (feature: MapFeature) => void;
|
|
14
|
+
onOpenRecord?: (record: PortableValue) => void;
|
|
15
|
+
/** When set, dragging a rectangle reports it as `search::bbox` arguments. */
|
|
16
|
+
onSelectArea?: (bbox: [number, number, number, number]) => void;
|
|
17
|
+
'data-testid'?: string;
|
|
18
|
+
[key: string]: unknown;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const WIDTH = 960;
|
|
22
|
+
const HEIGHT = 540;
|
|
23
|
+
const DRAG_THRESHOLD = 6;
|
|
24
|
+
|
|
25
|
+
@Component()
|
|
26
|
+
export class MapCanvas extends Cossack {
|
|
27
|
+
declare props: MapCanvasProps;
|
|
28
|
+
|
|
29
|
+
private projection?: MapProjection;
|
|
30
|
+
private dragStart?: { x: number; y: number };
|
|
31
|
+
private suppressClick = false;
|
|
32
|
+
|
|
33
|
+
render() {
|
|
34
|
+
const { features } = this.props;
|
|
35
|
+
const bounds = mapBounds(features.map(feature => feature.geometry));
|
|
36
|
+
if (!bounds) return html``;
|
|
37
|
+
const projection = createProjection(bounds, WIDTH, HEIGHT);
|
|
38
|
+
this.projection = projection;
|
|
39
|
+
const grid = graticule(projection);
|
|
40
|
+
const top = Math.max(0, projection.y(90));
|
|
41
|
+
const bottom = Math.min(HEIGHT, projection.y(-90));
|
|
42
|
+
const tables = [...new Set(features.map(feature => feature.table))];
|
|
43
|
+
const regions = features.filter(feature => feature.geometry.type !== 'Point');
|
|
44
|
+
const points = features.filter(feature => feature.geometry.type === 'Point');
|
|
45
|
+
const selected = features.find(feature => feature.key === this.props.selected);
|
|
46
|
+
const select = (feature: MapFeature) => {
|
|
47
|
+
if (this.suppressClick) { this.suppressClick = false; return; }
|
|
48
|
+
this.props.onSelectFeature?.(feature);
|
|
49
|
+
};
|
|
50
|
+
const featureAttributes = (feature: MapFeature) => ({
|
|
51
|
+
label: `${feature.label} · ${feature.column} · ${feature.summary}`,
|
|
52
|
+
keydown: (event: KeyboardEvent) => { if (event.key === 'Enter' || event.key === ' ') { event.preventDefault(); this.props.onSelectFeature?.(feature); } },
|
|
53
|
+
});
|
|
54
|
+
return html`
|
|
55
|
+
<div class="grid gap-2" data-testid="${this.props['data-testid'] ?? 'map-canvas'}">
|
|
56
|
+
<svg
|
|
57
|
+
class="h-[min(64vh,40rem)] w-full touch-none select-none rounded-md border bg-card ${this.props.onSelectArea ? 'cursor-crosshair' : ''}"
|
|
58
|
+
viewBox="0 0 ${WIDTH} ${HEIGHT}"
|
|
59
|
+
role="img"
|
|
60
|
+
aria-label="Map of ${features.length} feature${features.length === 1 ? '' : 's'}"
|
|
61
|
+
@pointerdown="${(event: PointerEvent) => this.beginDrag(event)}"
|
|
62
|
+
@pointermove="${(event: PointerEvent) => this.moveDrag(event)}"
|
|
63
|
+
@pointerup="${(event: PointerEvent) => this.endDrag(event)}"
|
|
64
|
+
@pointerleave="${(event: PointerEvent) => this.cancelDrag(event)}"
|
|
65
|
+
>
|
|
66
|
+
${bottom > top ? svg`<rect x="0" y="${top}" width="${WIDTH}" height="${bottom - top}" fill="currentColor" class="text-muted" opacity="0.35"></rect>` : ''}
|
|
67
|
+
<g data-testid="map-graticule" class="text-border">
|
|
68
|
+
${grid.meridians.map(longitude => {
|
|
69
|
+
const x = projection.x(longitude);
|
|
70
|
+
return svg`<line x1="${x}" y1="${top}" x2="${x}" y2="${bottom}" stroke="currentColor" stroke-width="1"></line>
|
|
71
|
+
<text x="${x + 3}" y="${HEIGHT - 6}" class="fill-muted-foreground text-[10px]">${degreeLabel(longitude, 'lon')}</text>`;
|
|
72
|
+
})}
|
|
73
|
+
${grid.parallels.map(latitude => {
|
|
74
|
+
const y = projection.y(latitude);
|
|
75
|
+
return svg`<line x1="0" y1="${y}" x2="${WIDTH}" y2="${y}" stroke="currentColor" stroke-width="1"></line>
|
|
76
|
+
<text x="4" y="${y - 3}" class="fill-muted-foreground text-[10px]">${degreeLabel(latitude, 'lat')}</text>`;
|
|
77
|
+
})}
|
|
78
|
+
</g>
|
|
79
|
+
${regions.map(feature => {
|
|
80
|
+
const color = tableColor(feature.table);
|
|
81
|
+
const active = feature.key === this.props.selected;
|
|
82
|
+
const attributes = featureAttributes(feature);
|
|
83
|
+
return svg`<g data-testid="map-feature" data-feature="${feature.key}" data-geometry="${feature.geometry.type}" class="cursor-pointer" role="button" tabindex="0"
|
|
84
|
+
aria-label="${attributes.label}" @click="${() => select(feature)}" @keydown="${attributes.keydown}">
|
|
85
|
+
<path d="${geometryPath(feature.geometry, projection)}" fill="${color}" fill-opacity="${active ? 0.45 : 0.22}" fill-rule="evenodd"
|
|
86
|
+
stroke="${active ? 'var(--foreground)' : color}" stroke-width="${active ? 2.5 : 1.5}" stroke-linejoin="round"></path>
|
|
87
|
+
<title>${attributes.label}</title>
|
|
88
|
+
</g>`;
|
|
89
|
+
})}
|
|
90
|
+
${points.map(feature => {
|
|
91
|
+
const [x, y] = projection.project(feature.geometry.coordinates as [number, number]);
|
|
92
|
+
const active = feature.key === this.props.selected;
|
|
93
|
+
const attributes = featureAttributes(feature);
|
|
94
|
+
return svg`<g data-testid="map-feature" data-feature="${feature.key}" data-geometry="Point" class="cursor-pointer" role="button" tabindex="0"
|
|
95
|
+
aria-label="${attributes.label}" @click="${() => select(feature)}" @keydown="${attributes.keydown}">
|
|
96
|
+
<circle cx="${x}" cy="${y}" r="${active ? 7 : 5}" fill="${tableColor(feature.table)}" stroke="${active ? 'var(--foreground)' : 'var(--background)'}" stroke-width="2"></circle>
|
|
97
|
+
<title>${attributes.label}</title>
|
|
98
|
+
</g>`;
|
|
99
|
+
})}
|
|
100
|
+
<rect data-role="selection" x="0" y="0" width="0" height="0" fill="currentColor" fill-opacity="0.08" stroke="currentColor" stroke-dasharray="4 3"
|
|
101
|
+
class="pointer-events-none text-primary" visibility="hidden"></rect>
|
|
102
|
+
</svg>
|
|
103
|
+
<div class="flex flex-wrap items-center gap-3 text-xs text-muted-foreground" data-testid="map-legend">
|
|
104
|
+
${tables.map(table => html`<span class="inline-flex items-center gap-1.5"><span class="inline-block size-2.5 rounded-full" style="background:${tableColor(table)}"></span>${table}</span>`)}
|
|
105
|
+
<span>· ${features.length} feature${features.length === 1 ? '' : 's'} · graticule ${grid.step}°</span>
|
|
106
|
+
${this.props.onSelectArea ? html`<span>· Drag on the map to search an area</span>` : ''}
|
|
107
|
+
</div>
|
|
108
|
+
${selected ? html`
|
|
109
|
+
<div class="flex flex-wrap items-center gap-3 rounded-md border bg-card px-3 py-2 text-sm" data-testid="map-feature-info">
|
|
110
|
+
<span class="font-mono" data-testid="map-feature-id">${selected.label}</span>
|
|
111
|
+
<span class="text-muted-foreground">${selected.column}</span>
|
|
112
|
+
<span class="font-mono text-muted-foreground">${selected.summary}</span>
|
|
113
|
+
${selected.id?.type === 'Record' && this.props.onOpenRecord ? component(Button, {
|
|
114
|
+
size: 'sm',
|
|
115
|
+
variant: 'outline',
|
|
116
|
+
class: 'ml-auto',
|
|
117
|
+
'data-testid': 'map-open-row',
|
|
118
|
+
'@click': () => this.props.onOpenRecord?.(selected.id!),
|
|
119
|
+
}, 'Open row') : ''}
|
|
120
|
+
</div>
|
|
121
|
+
` : ''}
|
|
122
|
+
</div>
|
|
123
|
+
`;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
@Client()
|
|
127
|
+
private svgPoint(event: PointerEvent): { x: number; y: number } | null {
|
|
128
|
+
const element = event.currentTarget as SVGSVGElement | null;
|
|
129
|
+
const matrix = element?.getScreenCTM?.();
|
|
130
|
+
if (!matrix) return null;
|
|
131
|
+
const point = new DOMPoint(event.clientX, event.clientY).matrixTransform(matrix.inverse());
|
|
132
|
+
return { x: point.x, y: point.y };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
@Client()
|
|
136
|
+
private selectionRect(event: PointerEvent): SVGRectElement | null {
|
|
137
|
+
return (event.currentTarget as SVGSVGElement | null)?.querySelector<SVGRectElement>('[data-role="selection"]') ?? null;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
@Client()
|
|
141
|
+
private beginDrag(event: PointerEvent) {
|
|
142
|
+
this.suppressClick = false;
|
|
143
|
+
if (!this.props.onSelectArea || event.button !== 0) return;
|
|
144
|
+
this.dragStart = this.svgPoint(event) ?? undefined;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
@Client()
|
|
148
|
+
private moveDrag(event: PointerEvent) {
|
|
149
|
+
const start = this.dragStart;
|
|
150
|
+
const point = start && this.svgPoint(event);
|
|
151
|
+
const rect = this.selectionRect(event);
|
|
152
|
+
if (!start || !point || !rect) return;
|
|
153
|
+
if (Math.hypot(point.x - start.x, point.y - start.y) < DRAG_THRESHOLD) return;
|
|
154
|
+
rect.setAttribute('x', String(Math.min(start.x, point.x)));
|
|
155
|
+
rect.setAttribute('y', String(Math.min(start.y, point.y)));
|
|
156
|
+
rect.setAttribute('width', String(Math.abs(point.x - start.x)));
|
|
157
|
+
rect.setAttribute('height', String(Math.abs(point.y - start.y)));
|
|
158
|
+
rect.setAttribute('visibility', 'visible');
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
@Client()
|
|
162
|
+
private endDrag(event: PointerEvent) {
|
|
163
|
+
const start = this.dragStart;
|
|
164
|
+
const point = start && this.svgPoint(event);
|
|
165
|
+
this.cancelDrag(event);
|
|
166
|
+
if (!start || !point || !this.projection) return;
|
|
167
|
+
if (Math.hypot(point.x - start.x, point.y - start.y) < DRAG_THRESHOLD) return;
|
|
168
|
+
this.suppressClick = true;
|
|
169
|
+
this.props.onSelectArea?.(selectionBounds(this.projection, start.x, start.y, point.x, point.y));
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
@Client()
|
|
173
|
+
private cancelDrag(event: PointerEvent) {
|
|
174
|
+
this.dragStart = undefined;
|
|
175
|
+
this.selectionRect(event)?.setAttribute('visibility', 'hidden');
|
|
176
|
+
}
|
|
177
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// Map view of collections with a spatial index: a bounded page of points or regions on an offline SVG map.
|
|
2
|
+
import { Component, Cossack } from '@cossackframework/core';
|
|
3
|
+
import { component, html } from '@cossackframework/renderer';
|
|
4
|
+
import { PlayIcon } from '@cossackframework/solar-icons/play';
|
|
5
|
+
import { Alert, Button, Icon, Input } from '@cossackframework/ui';
|
|
6
|
+
import type { PortableValue } from '@romedb/client';
|
|
7
|
+
import { MAP_LIMITS, spatialIndexes } from '../../lib/spatial';
|
|
8
|
+
import type { MapFeature, MapView, StudioSchema } from '../../lib/studio-types';
|
|
9
|
+
import { MapCanvas } from './MapCanvas';
|
|
10
|
+
|
|
11
|
+
export type MapBoxField = 'west' | 'south' | 'east' | 'north' | 'lng' | 'lat';
|
|
12
|
+
|
|
13
|
+
export interface MapTabProps {
|
|
14
|
+
active: boolean;
|
|
15
|
+
schema: StudioSchema;
|
|
16
|
+
index: string;
|
|
17
|
+
box: Record<MapBoxField, string>;
|
|
18
|
+
limit: number;
|
|
19
|
+
view: MapView | null;
|
|
20
|
+
selected: string;
|
|
21
|
+
loading: boolean;
|
|
22
|
+
onIndexChange: (index: string) => void;
|
|
23
|
+
onBoxChange: (field: MapBoxField, value: string) => void;
|
|
24
|
+
onLimitChange: (limit: number) => void;
|
|
25
|
+
onLoad: () => void;
|
|
26
|
+
onSelectArea: (bbox: [number, number, number, number]) => void;
|
|
27
|
+
onSelectFeature: (feature: MapFeature) => void;
|
|
28
|
+
onOpenRecord: (record: PortableValue) => void;
|
|
29
|
+
onOpenSql: () => void;
|
|
30
|
+
[key: string]: unknown;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
@Component()
|
|
34
|
+
export class MapTab extends Cossack {
|
|
35
|
+
declare props: MapTabProps;
|
|
36
|
+
|
|
37
|
+
render() {
|
|
38
|
+
const { schema, view } = this.props;
|
|
39
|
+
const indexes = spatialIndexes(schema);
|
|
40
|
+
const current = indexes.find(entry => entry.index.name === this.props.index);
|
|
41
|
+
const geometry = current?.index.geometry;
|
|
42
|
+
const selectClass = 'h-9 rounded-md border border-input bg-background px-2 text-sm';
|
|
43
|
+
const field = (name: MapBoxField, label: string, placeholder: string) => html`
|
|
44
|
+
<label class="grid gap-1 text-sm font-medium">
|
|
45
|
+
${label}
|
|
46
|
+
${component(Input, {
|
|
47
|
+
class: 'font-mono',
|
|
48
|
+
inputmode: 'decimal',
|
|
49
|
+
placeholder,
|
|
50
|
+
'data-testid': `map-${name}`,
|
|
51
|
+
'.value': this.props.box[name],
|
|
52
|
+
'@input': (event: InputEvent) => this.props.onBoxChange(name, (event.target as HTMLInputElement).value),
|
|
53
|
+
})}
|
|
54
|
+
</label>
|
|
55
|
+
`;
|
|
56
|
+
return html`
|
|
57
|
+
<div class="${this.props.active ? 'flex' : 'hidden'} min-h-0 flex-1 flex-col overflow-auto p-5" data-testid="map-panel">
|
|
58
|
+
${indexes.length ? html`
|
|
59
|
+
<form
|
|
60
|
+
class="grid gap-3 md:grid-cols-[minmax(0,2fr)_repeat(4,minmax(0,1fr))_auto_auto] md:items-end"
|
|
61
|
+
@submit="${(event: Event) => { event.preventDefault(); this.props.onLoad(); }}"
|
|
62
|
+
>
|
|
63
|
+
<label class="grid gap-1 text-sm font-medium">
|
|
64
|
+
Spatial index
|
|
65
|
+
<select class="${selectClass}" data-testid="map-index" .value="${this.props.index}"
|
|
66
|
+
@change="${(event: Event) => this.props.onIndexChange((event.target as HTMLSelectElement).value)}">
|
|
67
|
+
${indexes.map(entry => html`<option value="${entry.index.name}" ?selected="${entry.index.name === this.props.index}">${entry.table}.${entry.index.columns.join(', ')} · ${entry.index.geometry ?? 'spatial'} (${entry.index.name})</option>`)}
|
|
68
|
+
</select>
|
|
69
|
+
</label>
|
|
70
|
+
${geometry === 'point' ? html`
|
|
71
|
+
${field('west', 'West', '-180')}${field('south', 'South', '-90')}${field('east', 'East', '180')}${field('north', 'North', '90')}
|
|
72
|
+
` : geometry === 'region' ? html`
|
|
73
|
+
${field('lng', 'Covers longitude', 'any')}${field('lat', 'Covers latitude', 'any')}
|
|
74
|
+
<span class="hidden md:block"></span><span class="hidden md:block"></span>
|
|
75
|
+
` : html`<span class="text-xs text-muted-foreground md:col-span-4">The index kind is not visible on this connection; the first rows are read.</span>`}
|
|
76
|
+
<label class="grid gap-1 text-sm font-medium">
|
|
77
|
+
Limit
|
|
78
|
+
<select class="${selectClass}" data-testid="map-limit" .value="${String(this.props.limit)}"
|
|
79
|
+
@change="${(event: Event) => this.props.onLimitChange(Number((event.target as HTMLSelectElement).value))}">
|
|
80
|
+
${MAP_LIMITS.map(limit => html`<option value="${limit}" ?selected="${limit === this.props.limit}">${limit}</option>`)}
|
|
81
|
+
</select>
|
|
82
|
+
</label>
|
|
83
|
+
${component(Button, { type: 'submit', class: 'gap-2', disabled: this.props.loading || !current, 'data-testid': 'map-load' },
|
|
84
|
+
html`${component(Icon, { entry: PlayIcon, size: 16 })}<span>${this.props.loading ? 'Loading…' : 'Load'}</span>`)}
|
|
85
|
+
</form>
|
|
86
|
+
<p class="mt-2 text-xs text-muted-foreground">
|
|
87
|
+
${geometry === 'region'
|
|
88
|
+
? html`Region indexes find the stored regions that cover a point with <code class="rounded bg-muted px-1">search::containing</code>. Leave the point empty to draw the first rows.`
|
|
89
|
+
: html`Point indexes read the viewport with <code class="rounded bg-muted px-1">search::bbox</code>; west greater than east crosses the antimeridian. Drag on the map to search an area.`}
|
|
90
|
+
Reads are limited to the chosen number of rows and the connection's work budgets.
|
|
91
|
+
</p>
|
|
92
|
+
` : html`
|
|
93
|
+
<div class="text-sm text-muted-foreground" data-testid="map-empty">
|
|
94
|
+
<p>This database has no spatial indexes.</p>
|
|
95
|
+
<p class="mt-2">Create one with <code class="rounded bg-muted px-1">CREATE SEARCH INDEX places_location ON places(location) USING SPATIAL</code>
|
|
96
|
+
or, for Polygon and MultiPolygon values, <code class="rounded bg-muted px-1">... USING SPATIAL WITH (geometry = 'region')</code>.
|
|
97
|
+
The Console tab's Map output draws geometry from any result.</p>
|
|
98
|
+
</div>
|
|
99
|
+
`}
|
|
100
|
+
${view?.error ? component(Alert, { variant: 'destructive', class: 'mt-4', title: 'Could not load the map', 'data-testid': 'map-error' }, view.error) : ''}
|
|
101
|
+
${view && !view.error ? html`
|
|
102
|
+
<div class="mt-4 grid gap-3">
|
|
103
|
+
${view.features.length ? component(MapCanvas, {
|
|
104
|
+
features: view.features,
|
|
105
|
+
selected: this.props.selected,
|
|
106
|
+
onSelectFeature: this.props.onSelectFeature,
|
|
107
|
+
onOpenRecord: this.props.onOpenRecord,
|
|
108
|
+
...(geometry === 'point' ? { onSelectArea: this.props.onSelectArea } : {}),
|
|
109
|
+
'data-testid': 'map-view-canvas',
|
|
110
|
+
}) : html`<p class="text-sm text-muted-foreground" data-testid="map-no-features">No geometry was found.</p>`}
|
|
111
|
+
<div class="flex flex-wrap items-center justify-between gap-2 text-xs text-muted-foreground">
|
|
112
|
+
<code class="min-w-0 flex-1 break-all rounded bg-muted px-2 py-1" data-testid="map-query">${view.query}</code>
|
|
113
|
+
<span data-testid="map-summary">${view.rows} row${view.rows === 1 ? '' : 's'}${view.truncated ? ` · limit reached, narrow the area or raise the limit` : ''} · ${view.durationMs.toFixed(1)} ms · revision ${view.sequence}</span>
|
|
114
|
+
${component(Button, { size: 'sm', variant: 'outline', 'data-testid': 'map-open-sql', '@click': this.props.onOpenSql }, 'Open in Console')}
|
|
115
|
+
</div>
|
|
116
|
+
</div>
|
|
117
|
+
` : ''}
|
|
118
|
+
</div>
|
|
119
|
+
`;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
// Creates a relational table or a document collection, in Cossack Studio's sheet layout.
|
|
2
|
+
import { Component, Cossack } from '@cossackframework/core';
|
|
3
|
+
import { component, html } from '@cossackframework/renderer';
|
|
4
|
+
import { TrashBinMinimalisticIcon } from '@cossackframework/solar-icons/trash-bin-minimalistic';
|
|
5
|
+
import { Alert, Button, Checkbox, Icon, Input, Sheet } from '@cossackframework/ui';
|
|
6
|
+
import type { NewColumnDraft } from '../../studio-page';
|
|
7
|
+
import { CodeEditor } from '../CodeEditor';
|
|
8
|
+
import { COLUMN_TYPES } from '../../schema-draft';
|
|
9
|
+
|
|
10
|
+
export type NewObjectKind = 'table' | 'collection' | 'edge' | 'spatial';
|
|
11
|
+
|
|
12
|
+
interface NewObjectSheetProps {
|
|
13
|
+
open: boolean;
|
|
14
|
+
name: string;
|
|
15
|
+
kind: NewObjectKind;
|
|
16
|
+
columns: NewColumnDraft[];
|
|
17
|
+
collections: string[];
|
|
18
|
+
edgeSource: string;
|
|
19
|
+
edgeTarget: string;
|
|
20
|
+
edgeCascade: boolean;
|
|
21
|
+
onEdgeChange: (source: string, target: string, cascade: boolean) => void;
|
|
22
|
+
spatialTable: string;
|
|
23
|
+
spatialField: string;
|
|
24
|
+
spatialRegion: boolean;
|
|
25
|
+
onSpatialChange: (table: string, field: string, region: boolean) => void;
|
|
26
|
+
sql: string;
|
|
27
|
+
error: string;
|
|
28
|
+
theme: 'light' | 'dark';
|
|
29
|
+
creating: boolean;
|
|
30
|
+
writesBlocked: boolean;
|
|
31
|
+
onClose: () => void;
|
|
32
|
+
onNameChange: (name: string) => void;
|
|
33
|
+
onKindChange: (kind: NewObjectKind) => void;
|
|
34
|
+
onColumnChange: (index: number, column: NewColumnDraft) => void;
|
|
35
|
+
onAddColumn: () => void;
|
|
36
|
+
onRemoveColumn: (index: number) => void;
|
|
37
|
+
onOpenInSql: () => void;
|
|
38
|
+
onCreate: () => void;
|
|
39
|
+
[key: string]: unknown;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
@Component()
|
|
43
|
+
export class NewObjectSheet extends Cossack {
|
|
44
|
+
declare props: NewObjectSheetProps;
|
|
45
|
+
|
|
46
|
+
render() {
|
|
47
|
+
const table = this.props.kind === 'table';
|
|
48
|
+
const edge = this.props.kind === 'edge';
|
|
49
|
+
const spatial = this.props.kind === 'spatial';
|
|
50
|
+
const noun = table ? 'table' : edge ? 'edge table' : spatial ? 'spatial index' : 'collection';
|
|
51
|
+
const selectClass = 'h-9 rounded-md border border-input bg-background px-2 text-sm';
|
|
52
|
+
return component(Sheet, {
|
|
53
|
+
open: this.props.open,
|
|
54
|
+
side: 'right',
|
|
55
|
+
size: 'min(46rem, 94vw)',
|
|
56
|
+
onOpenChange: this.props.onClose,
|
|
57
|
+
'data-testid': 'new-object-sheet',
|
|
58
|
+
'aria-label': 'Create table or collection',
|
|
59
|
+
}, html`
|
|
60
|
+
<header class="shrink-0 border-b p-5">
|
|
61
|
+
<h2 class="font-semibold">Create ${noun}</h2>
|
|
62
|
+
<p class="mt-1 text-sm text-muted-foreground">
|
|
63
|
+
Review the generated statement before it runs. Nothing changes until you confirm.
|
|
64
|
+
</p>
|
|
65
|
+
</header>
|
|
66
|
+
<div class="min-h-0 flex-1 overflow-auto p-5">
|
|
67
|
+
${this.props.error ? component(Alert, { variant: 'destructive', class: 'mb-4', title: 'Could not create object', 'data-testid': 'new-object-error' }, this.props.error) : ''}
|
|
68
|
+
<div class="grid gap-4">
|
|
69
|
+
<div class="grid grid-cols-2 gap-2 md:grid-cols-4" role="radiogroup" aria-label="Object kind">
|
|
70
|
+
${([['collection', 'Document collection', 'Schemaless JSON documents with record IDs'], ['table', 'Relational table', 'Typed columns with a primary key'], ['edge', 'Edge table', 'Directed graph edges between two collections'], ['spatial', 'Spatial index', 'Point or region search on a collection field']] as const).map(([kind, label, detail]) => html`
|
|
71
|
+
<button
|
|
72
|
+
type="button"
|
|
73
|
+
role="radio"
|
|
74
|
+
aria-checked="${this.props.kind === kind}"
|
|
75
|
+
data-testid="new-object-kind-${kind}"
|
|
76
|
+
class="rounded-lg border p-3 text-left ${this.props.kind === kind ? 'border-primary bg-primary/5' : 'hover:bg-accent'}"
|
|
77
|
+
@click="${() => this.props.onKindChange(kind)}"
|
|
78
|
+
>
|
|
79
|
+
<span class="block text-sm font-medium">${label}</span>
|
|
80
|
+
<span class="block text-xs text-muted-foreground">${detail}</span>
|
|
81
|
+
</button>
|
|
82
|
+
`)}
|
|
83
|
+
</div>
|
|
84
|
+
<label class="grid gap-1 text-sm font-medium">
|
|
85
|
+
${spatial ? 'Index name' : 'Name'}
|
|
86
|
+
${component(Input, {
|
|
87
|
+
'data-testid': 'new-object-name',
|
|
88
|
+
placeholder: table ? 'orders' : spatial ? `${this.props.spatialTable || 'places'}_${(this.props.spatialField || 'location').replaceAll('.', '_')}` : 'users',
|
|
89
|
+
maxlength: 128,
|
|
90
|
+
'.value': this.props.name,
|
|
91
|
+
'@input': (event: InputEvent) => this.props.onNameChange((event.target as HTMLInputElement).value),
|
|
92
|
+
})}
|
|
93
|
+
</label>
|
|
94
|
+
${table ? html`
|
|
95
|
+
<fieldset class="grid gap-2">
|
|
96
|
+
<legend class="mb-1 text-sm font-medium">Columns</legend>
|
|
97
|
+
<div class="grid grid-cols-[minmax(0,1fr)_8rem_auto_auto_auto_auto] items-center gap-2 text-xs text-muted-foreground">
|
|
98
|
+
<span>Name</span><span>Type</span><span>Primary key</span><span>Not null</span><span>Unique</span><span></span>
|
|
99
|
+
</div>
|
|
100
|
+
${this.props.columns.map((column, index) => html`
|
|
101
|
+
<div class="grid grid-cols-[minmax(0,1fr)_8rem_auto_auto_auto_auto] items-center gap-2" data-testid="new-column-${index}">
|
|
102
|
+
${component(Input, {
|
|
103
|
+
size: 'sm',
|
|
104
|
+
'aria-label': `Column ${index + 1} name`,
|
|
105
|
+
'.value': column.name,
|
|
106
|
+
'@input': (event: InputEvent) => this.props.onColumnChange(index, { ...column, name: (event.target as HTMLInputElement).value }),
|
|
107
|
+
})}
|
|
108
|
+
<select
|
|
109
|
+
class="h-8 rounded-md border border-input bg-background px-2 text-sm"
|
|
110
|
+
aria-label="Column ${index + 1} type"
|
|
111
|
+
.value="${column.type}"
|
|
112
|
+
@change="${(event: Event) => this.props.onColumnChange(index, { ...column, type: (event.target as HTMLSelectElement).value })}"
|
|
113
|
+
>
|
|
114
|
+
${COLUMN_TYPES.map(type => html`<option value="${type}" ?selected="${type === column.type}">${type}</option>`)}
|
|
115
|
+
</select>
|
|
116
|
+
<span class="flex justify-center">${component(Checkbox, { checked: column.primary, 'aria-label': `Column ${index + 1} primary key`, '@change': (event: Event) => this.props.onColumnChange(index, { ...column, primary: (event.currentTarget as HTMLInputElement).checked }) })}</span>
|
|
117
|
+
<span class="flex justify-center">${component(Checkbox, { checked: column.required, 'aria-label': `Column ${index + 1} not null`, '@change': (event: Event) => this.props.onColumnChange(index, { ...column, required: (event.currentTarget as HTMLInputElement).checked }) })}</span>
|
|
118
|
+
<span class="flex justify-center">${component(Checkbox, { checked: column.unique, 'aria-label': `Column ${index + 1} unique`, '@change': (event: Event) => this.props.onColumnChange(index, { ...column, unique: (event.currentTarget as HTMLInputElement).checked }) })}</span>
|
|
119
|
+
${component(Button, {
|
|
120
|
+
variant: 'ghost',
|
|
121
|
+
size: 'icon',
|
|
122
|
+
'aria-label': `Remove column ${index + 1}`,
|
|
123
|
+
disabled: this.props.columns.length <= 1,
|
|
124
|
+
'@click': () => this.props.onRemoveColumn(index),
|
|
125
|
+
}, component(Icon, { entry: TrashBinMinimalisticIcon, size: 15 }))}
|
|
126
|
+
</div>
|
|
127
|
+
`)}
|
|
128
|
+
<div>${component(Button, { variant: 'outline', size: 'sm', 'data-testid': 'add-column', '@click': this.props.onAddColumn }, 'Add column')}</div>
|
|
129
|
+
<p class="text-xs text-muted-foreground">Studio edits rows by primary key. Tables without one stay read only.</p>
|
|
130
|
+
</fieldset>
|
|
131
|
+
` : edge ? html`
|
|
132
|
+
<div class="grid gap-3 md:grid-cols-3">
|
|
133
|
+
<label class="grid gap-1 text-sm font-medium">
|
|
134
|
+
From collection
|
|
135
|
+
<select class="${selectClass}" data-testid="new-edge-source" .value="${this.props.edgeSource}"
|
|
136
|
+
@change="${(event: Event) => this.props.onEdgeChange((event.target as HTMLSelectElement).value, this.props.edgeTarget, this.props.edgeCascade)}">
|
|
137
|
+
<option value="" ?selected="${!this.props.edgeSource}">Choose…</option>
|
|
138
|
+
${this.props.collections.map(name => html`<option value="${name}" ?selected="${name === this.props.edgeSource}">${name}</option>`)}
|
|
139
|
+
</select>
|
|
140
|
+
</label>
|
|
141
|
+
<label class="grid gap-1 text-sm font-medium">
|
|
142
|
+
To collection
|
|
143
|
+
<select class="${selectClass}" data-testid="new-edge-target" .value="${this.props.edgeTarget}"
|
|
144
|
+
@change="${(event: Event) => this.props.onEdgeChange(this.props.edgeSource, (event.target as HTMLSelectElement).value, this.props.edgeCascade)}">
|
|
145
|
+
<option value="" ?selected="${!this.props.edgeTarget}">Choose…</option>
|
|
146
|
+
${this.props.collections.map(name => html`<option value="${name}" ?selected="${name === this.props.edgeTarget}">${name}</option>`)}
|
|
147
|
+
</select>
|
|
148
|
+
</label>
|
|
149
|
+
<label class="grid gap-1 text-sm font-medium">
|
|
150
|
+
When a node is deleted
|
|
151
|
+
<select class="${selectClass}" data-testid="new-edge-delete" .value="${this.props.edgeCascade ? 'cascade' : 'restrict'}"
|
|
152
|
+
@change="${(event: Event) => this.props.onEdgeChange(this.props.edgeSource, this.props.edgeTarget, (event.target as HTMLSelectElement).value === 'cascade')}">
|
|
153
|
+
<option value="restrict" ?selected="${!this.props.edgeCascade}">Restrict: block the deletion</option>
|
|
154
|
+
<option value="cascade" ?selected="${this.props.edgeCascade}">Cascade: delete its edges</option>
|
|
155
|
+
</select>
|
|
156
|
+
</label>
|
|
157
|
+
</div>
|
|
158
|
+
<p class="text-sm text-muted-foreground">
|
|
159
|
+
Each edge has an ID, an immutable <code class="rounded bg-muted px-1">source</code> and
|
|
160
|
+
<code class="rounded bg-muted px-1">target</code>, and any other properties. Both endpoints must exist.
|
|
161
|
+
Add edges with <code class="rounded bg-muted px-1">RELATE</code> or the insert form.
|
|
162
|
+
</p>
|
|
163
|
+
${this.props.collections.length ? '' : html`<p class="text-sm text-destructive">Create a document collection first.</p>`}
|
|
164
|
+
` : spatial ? html`
|
|
165
|
+
<div class="grid gap-3 md:grid-cols-3">
|
|
166
|
+
<label class="grid gap-1 text-sm font-medium">
|
|
167
|
+
Collection
|
|
168
|
+
<select class="${selectClass}" data-testid="new-spatial-table" .value="${this.props.spatialTable}"
|
|
169
|
+
@change="${(event: Event) => this.props.onSpatialChange((event.target as HTMLSelectElement).value, this.props.spatialField, this.props.spatialRegion)}">
|
|
170
|
+
<option value="" ?selected="${!this.props.spatialTable}">Choose…</option>
|
|
171
|
+
${this.props.collections.map(name => html`<option value="${name}" ?selected="${name === this.props.spatialTable}">${name}</option>`)}
|
|
172
|
+
</select>
|
|
173
|
+
</label>
|
|
174
|
+
<label class="grid gap-1 text-sm font-medium">
|
|
175
|
+
Field path
|
|
176
|
+
${component(Input, {
|
|
177
|
+
class: 'font-mono',
|
|
178
|
+
placeholder: 'location',
|
|
179
|
+
'data-testid': 'new-spatial-field',
|
|
180
|
+
'.value': this.props.spatialField,
|
|
181
|
+
'@input': (event: InputEvent) => this.props.onSpatialChange(this.props.spatialTable, (event.target as HTMLInputElement).value, this.props.spatialRegion),
|
|
182
|
+
})}
|
|
183
|
+
</label>
|
|
184
|
+
<label class="grid gap-1 text-sm font-medium">
|
|
185
|
+
Geometry
|
|
186
|
+
<select class="${selectClass}" data-testid="new-spatial-geometry" .value="${this.props.spatialRegion ? 'region' : 'point'}"
|
|
187
|
+
@change="${(event: Event) => this.props.onSpatialChange(this.props.spatialTable, this.props.spatialField, (event.target as HTMLSelectElement).value === 'region')}">
|
|
188
|
+
<option value="point" ?selected="${!this.props.spatialRegion}">Points: radius, viewport and nearest search</option>
|
|
189
|
+
<option value="region" ?selected="${this.props.spatialRegion}">Regions: Polygon and MultiPolygon zones</option>
|
|
190
|
+
</select>
|
|
191
|
+
</label>
|
|
192
|
+
</div>
|
|
193
|
+
<p class="text-sm text-muted-foreground">
|
|
194
|
+
Creating the index validates every existing value and rejects later writes with invalid geometry.
|
|
195
|
+
Missing and NULL fields are allowed. ${this.props.spatialRegion
|
|
196
|
+
? html`Find the zones that cover a point with <code class="rounded bg-muted px-1">search::containing</code>.`
|
|
197
|
+
: html`Search with <code class="rounded bg-muted px-1">search::near</code>, <code class="rounded bg-muted px-1">search::bbox</code>,
|
|
198
|
+
<code class="rounded bg-muted px-1">search::polygon</code> or <code class="rounded bg-muted px-1">search::nearest</code>.`}
|
|
199
|
+
Spatial indexes move the collection to catalog version 7, which RomeDB 2.4.0 and older cannot open.
|
|
200
|
+
</p>
|
|
201
|
+
${this.props.collections.length ? '' : html`<p class="text-sm text-destructive">Create a document collection first.</p>`}
|
|
202
|
+
` : html`
|
|
203
|
+
<p class="text-sm text-muted-foreground">
|
|
204
|
+
Collections start without fields. Insert documents with any shape, then add validation with
|
|
205
|
+
<code class="rounded bg-muted px-1">DEFINE FIELD</code> in the Console tab.
|
|
206
|
+
</p>
|
|
207
|
+
`}
|
|
208
|
+
<section class="grid gap-2">
|
|
209
|
+
<h3 class="text-sm font-medium">Statement</h3>
|
|
210
|
+
${component(CodeEditor, {
|
|
211
|
+
class: 'h-[9rem]',
|
|
212
|
+
value: this.props.sql || '-- Complete the form to preview the statement',
|
|
213
|
+
language: 'sql',
|
|
214
|
+
theme: this.props.theme,
|
|
215
|
+
enabled: this.props.open,
|
|
216
|
+
readOnly: true,
|
|
217
|
+
lineNumbers: 'off',
|
|
218
|
+
ariaLabel: 'Generated statement',
|
|
219
|
+
'data-testid': 'new-object-sql',
|
|
220
|
+
})}
|
|
221
|
+
</section>
|
|
222
|
+
</div>
|
|
223
|
+
</div>
|
|
224
|
+
<footer class="flex shrink-0 justify-end gap-2 border-t p-4">
|
|
225
|
+
${component(Button, { variant: 'ghost', '@click': this.props.onClose }, 'Cancel')}
|
|
226
|
+
${component(Button, { variant: 'outline', disabled: !this.props.sql, 'data-testid': 'open-new-object-sql', '@click': this.props.onOpenInSql }, 'Open in Console')}
|
|
227
|
+
${component(Button, {
|
|
228
|
+
disabled: !this.props.sql || this.props.creating || this.props.writesBlocked,
|
|
229
|
+
'data-testid': 'create-object',
|
|
230
|
+
'@click': this.props.onCreate,
|
|
231
|
+
}, this.props.creating ? 'Creating…' : `Create ${noun}`)}
|
|
232
|
+
</footer>
|
|
233
|
+
`);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// Adapted from Cossack Studio's QueryHistorySheet (MIT).
|
|
2
|
+
import { Component, Cossack } from '@cossackframework/core';
|
|
3
|
+
import { component, html } from '@cossackframework/renderer';
|
|
4
|
+
import { HistoryIcon } from '@cossackframework/solar-icons/history';
|
|
5
|
+
import { PlayIcon } from '@cossackframework/solar-icons/play';
|
|
6
|
+
import { StarIcon } from '@cossackframework/solar-icons/star';
|
|
7
|
+
import { Badge, Button, Icon, Sheet } from '@cossackframework/ui';
|
|
8
|
+
import type { QueryHistoryEntry } from '../../studio-page';
|
|
9
|
+
|
|
10
|
+
interface QueryHistorySheetProps {
|
|
11
|
+
open: boolean;
|
|
12
|
+
connectionLabel: string;
|
|
13
|
+
history: QueryHistoryEntry[];
|
|
14
|
+
onClose: () => void;
|
|
15
|
+
onClear: () => void;
|
|
16
|
+
onToggleFavorite: (id: string) => void;
|
|
17
|
+
onLoad: (entry: QueryHistoryEntry) => void;
|
|
18
|
+
onRun: (entry: QueryHistoryEntry) => void;
|
|
19
|
+
[key: string]: unknown;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
@Component()
|
|
23
|
+
export class QueryHistorySheet extends Cossack {
|
|
24
|
+
declare props: QueryHistorySheetProps;
|
|
25
|
+
|
|
26
|
+
render() {
|
|
27
|
+
const history = [...this.props.history].sort((left, right) =>
|
|
28
|
+
Number(right.favorite) - Number(left.favorite) ||
|
|
29
|
+
right.executedAt - left.executedAt);
|
|
30
|
+
return component(Sheet, {
|
|
31
|
+
open: this.props.open,
|
|
32
|
+
side: 'right',
|
|
33
|
+
size: 'min(36rem, 94vw)',
|
|
34
|
+
onOpenChange: this.props.onClose,
|
|
35
|
+
'data-testid': 'query-history',
|
|
36
|
+
'aria-label': 'Query history',
|
|
37
|
+
}, html`
|
|
38
|
+
<header class="flex shrink-0 items-start justify-between gap-4 border-b p-5">
|
|
39
|
+
<div>
|
|
40
|
+
<h2 class="font-semibold">Query history</h2>
|
|
41
|
+
<p class="mt-1 text-sm text-muted-foreground">
|
|
42
|
+
Stored only in this browser for ${this.props.connectionLabel}.
|
|
43
|
+
</p>
|
|
44
|
+
</div>
|
|
45
|
+
${component(Button, {
|
|
46
|
+
variant: 'outline',
|
|
47
|
+
size: 'sm',
|
|
48
|
+
disabled: !history.some((entry) => !entry.favorite),
|
|
49
|
+
'@click': this.props.onClear,
|
|
50
|
+
}, 'Clear history')}
|
|
51
|
+
</header>
|
|
52
|
+
<div class="min-h-0 flex-1 overflow-auto p-4">
|
|
53
|
+
${history.length ? html`
|
|
54
|
+
<div class="grid gap-3">
|
|
55
|
+
${history.map((entry) => html`
|
|
56
|
+
<article class="rounded-lg border bg-card p-3" data-testid="query-history-entry">
|
|
57
|
+
<div class="mb-2 flex items-center justify-between gap-3">
|
|
58
|
+
<div class="flex items-center gap-2 text-xs text-muted-foreground">
|
|
59
|
+
${component(Badge, {
|
|
60
|
+
variant: entry.error ? 'destructive' : 'secondary',
|
|
61
|
+
}, entry.source === 'browse' ? 'Browse' : 'Console')}
|
|
62
|
+
<span>${new Date(entry.executedAt).toLocaleString()}</span>
|
|
63
|
+
<span>${entry.durationMs.toFixed(1)} ms</span>
|
|
64
|
+
</div>
|
|
65
|
+
<button
|
|
66
|
+
type="button"
|
|
67
|
+
class="${entry.favorite ? 'text-warning' : 'text-muted-foreground'} rounded-md p-1.5 hover:bg-accent"
|
|
68
|
+
aria-label="${entry.favorite ? 'Remove from saved queries' : 'Save query'}"
|
|
69
|
+
@click="${() => this.props.onToggleFavorite(entry.id)}"
|
|
70
|
+
>
|
|
71
|
+
${component(Icon, {
|
|
72
|
+
entry: StarIcon,
|
|
73
|
+
variant: entry.favorite ? 'bold' : 'line',
|
|
74
|
+
size: 17,
|
|
75
|
+
})}
|
|
76
|
+
</button>
|
|
77
|
+
</div>
|
|
78
|
+
<pre class="max-h-32 overflow-auto whitespace-pre-wrap rounded-md bg-muted/50 p-2 text-xs">${entry.statement}</pre>
|
|
79
|
+
${entry.error ? html`
|
|
80
|
+
<p class="mt-2 line-clamp-2 text-xs text-destructive">${entry.error}</p>
|
|
81
|
+
` : ''}
|
|
82
|
+
<div class="mt-3 flex justify-end gap-2">
|
|
83
|
+
${component(Button, {
|
|
84
|
+
variant: 'outline',
|
|
85
|
+
size: 'sm',
|
|
86
|
+
'@click': () => this.props.onLoad(entry),
|
|
87
|
+
}, 'Load')}
|
|
88
|
+
${component(Button, {
|
|
89
|
+
size: 'sm',
|
|
90
|
+
class: 'gap-1.5',
|
|
91
|
+
'@click': () => this.props.onRun(entry),
|
|
92
|
+
}, html`${component(Icon, { entry: PlayIcon, size: 14 })}Run`)}
|
|
93
|
+
</div>
|
|
94
|
+
</article>
|
|
95
|
+
`)}
|
|
96
|
+
</div>
|
|
97
|
+
` : html`
|
|
98
|
+
<div class="grid min-h-64 place-items-center rounded-lg border border-dashed p-6 text-center">
|
|
99
|
+
<div>
|
|
100
|
+
${component(Icon, { entry: HistoryIcon, size: 28 })}
|
|
101
|
+
<p class="mt-2 text-sm font-medium">No query history yet</p>
|
|
102
|
+
<p class="mt-1 text-xs text-muted-foreground">
|
|
103
|
+
Queries executed from Browse or SQL will appear here.
|
|
104
|
+
</p>
|
|
105
|
+
</div>
|
|
106
|
+
</div>
|
|
107
|
+
`}
|
|
108
|
+
</div>
|
|
109
|
+
`);
|
|
110
|
+
}
|
|
111
|
+
}
|