@antelopejs/dms-database 0.3.1 → 0.4.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/README.md +17 -10
- package/dist/automation/index.js.map +1 -1
- package/dist/automation/tableWatch.d.ts +8 -4
- package/dist/automation/tableWatch.js +35 -8
- package/dist/automation/tableWatch.js.map +1 -1
- package/dist/db/models/diagram_layouts.model.d.ts +7 -5
- package/dist/db/models/diagram_notes.model.d.ts +7 -5
- package/dist/db/models/query_history.model.d.ts +7 -5
- package/dist/db/models/saved_queries.model.d.ts +7 -5
- package/dist/db/tables/query_history.table.d.ts +2 -1
- package/dist/db/tables/query_history.table.js.map +1 -1
- package/dist/db/tables/saved_queries.table.d.ts +2 -1
- package/dist/db/tables/saved_queries.table.js.map +1 -1
- package/dist/i18n/messages.js +5 -2
- package/dist/i18n/messages.js.map +1 -1
- package/dist/module.js +5 -1
- package/dist/module.js.map +1 -1
- package/dist/pages/schemas.js.map +1 -1
- package/dist/routes/data.d.ts +4 -3
- package/dist/routes/data.js +37 -238
- package/dist/routes/data.js.map +1 -1
- package/dist/routes/diagram.js +64 -43
- package/dist/routes/diagram.js.map +1 -1
- package/dist/routes/query.js +51 -53
- package/dist/routes/query.js.map +1 -1
- package/dist/routes/tables.js +13 -7
- package/dist/routes/tables.js.map +1 -1
- package/dist/service/browse.d.ts +37 -0
- package/dist/service/browse.js +135 -0
- package/dist/service/browse.js.map +1 -0
- package/dist/service/fieldDescriptor.d.ts +6 -6
- package/dist/service/fieldDescriptor.js +56 -50
- package/dist/service/fieldDescriptor.js.map +1 -1
- package/dist/service/health.js.map +1 -1
- package/dist/service/introspect.js +45 -40
- package/dist/service/introspect.js.map +1 -1
- package/dist/service/overview.js.map +1 -1
- package/dist/service/queryInspection.d.ts +5 -4
- package/dist/service/queryInspection.js +28 -26
- package/dist/service/queryInspection.js.map +1 -1
- package/dist/service/references.d.ts +1 -1
- package/dist/service/references.js +3 -7
- package/dist/service/references.js.map +1 -1
- package/dist/service/rowEdit.d.ts +4 -0
- package/dist/service/rowEdit.js +80 -0
- package/dist/service/rowEdit.js.map +1 -0
- package/dist/service/schemaFilters.d.ts +1 -1
- package/dist/service/schemaFilters.js.map +1 -1
- package/dist/service/schemaLabels.js +1 -1
- package/dist/service/schemaLabels.js.map +1 -1
- package/dist/service/schemaRegistry.d.ts +1 -1
- package/dist/service/schemaRegistry.js +1 -1
- package/dist/service/schemaRegistry.js.map +1 -1
- package/dist/service/stagedSerialization.d.ts +7 -6
- package/dist/service/stagedSerialization.js +40 -32
- package/dist/service/stagedSerialization.js.map +1 -1
- package/dist/service/types.d.ts +3 -2
- package/dist/types/json.d.ts +8 -0
- package/dist/types/json.js +53 -0
- package/dist/types/json.js.map +1 -0
- package/dist/utils/guards.d.ts +8 -0
- package/dist/utils/guards.js +35 -0
- package/dist/utils/guards.js.map +1 -0
- package/dist/utils/query.d.ts +6 -7
- package/dist/utils/query.js +22 -30
- package/dist/utils/query.js.map +1 -1
- package/dist/utils/requestValidation.d.ts +1 -2
- package/dist/utils/requestValidation.js +4 -9
- package/dist/utils/requestValidation.js.map +1 -1
- package/frontend-vue/app/build/components/DataGridEmpty.vue +89 -0
- package/frontend-vue/app/build/components/DataGridFooter.vue +113 -0
- package/frontend-vue/app/build/components/DataGridReadOnlyBanner.vue +50 -0
- package/frontend-vue/app/build/components/DataGridSaveStatus.vue +73 -0
- package/frontend-vue/app/build/components/DataGridToolbar.vue +72 -0
- package/frontend-vue/app/build/components/DiagramLegend.vue +48 -0
- package/frontend-vue/app/build/components/DiagramSearchBar.vue +82 -0
- package/frontend-vue/app/build/components/DiagramStatusBar.vue +89 -0
- package/frontend-vue/app/build/components/DiagramToolbar.vue +102 -0
- package/frontend-vue/app/build/components/DiagramViewBar.vue +103 -0
- package/frontend-vue/app/build/components/diagramFloat.css +21 -0
- package/frontend-vue/app/build/composables/useDiagramDrag.ts +84 -0
- package/frontend-vue/app/build/composables/useDiagramNotes.ts +146 -0
- package/frontend-vue/app/build/composables/useDiagramSaveStatus.ts +50 -0
- package/frontend-vue/app/build/composables/useDiagramSearch.ts +77 -0
- package/frontend-vue/app/build/composables/useDiagramView.ts +125 -0
- package/frontend-vue/app/build/composables/useElsewhereInstances.ts +94 -0
- package/frontend-vue/app/build/composables/useTableSample.ts +176 -0
- package/frontend-vue/app/build/data/gridData.ts +66 -0
- package/frontend-vue/app/build/data/useStagedSave.ts +13 -20
- package/frontend-vue/app/build/diagram/autosave.ts +3 -3
- package/frontend-vue/app/build/diagram/graph.ts +67 -485
- package/frontend-vue/app/build/diagram/graphModel.ts +368 -0
- package/frontend-vue/app/build/diagram/layout.ts +135 -0
- package/frontend-vue/app/build/query/failures.ts +17 -0
- package/frontend-vue/app/components/DataGrid.vue +64 -358
- package/frontend-vue/app/components/QueryConsole.vue +27 -116
- package/frontend-vue/app/components/SchemaDiagram.vue +118 -678
- package/frontend-vue/app/components/TableInspector.vue +12 -136
- package/frontend-vue/build/backend-boundary.ts +122 -0
- package/frontend-vue/dms.frontend.build.ts +6 -0
- package/frontend-vue/package.json +46 -45
- package/frontend-vue/pnpm-lock.yaml +51 -16
- package/frontend-vue/tests/apiErrorPresentation.test.ts +83 -0
- package/frontend-vue/tests/backend-boundary.test.ts +134 -0
- package/frontend-vue/tests/clientNavigation.test.ts +5 -7
- package/frontend-vue/tests/fixtures/serverRender.ts +13 -1
- package/frontend-vue/tests/locales.test.ts +16 -89
- package/frontend-vue/tests/queryConsole.test.ts +20 -0
- package/frontend-vue/tests/registration.test.ts +0 -18
- package/frontend-vue/vitest.config.ts +5 -2
- package/package.json +28 -17
- package/skills/dms-database/cases.json +48 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { ComputedRef, Ref, ShallowRef } from 'vue'
|
|
2
|
+
import { expandForSearch, searchTables, tableNodeId } from '../diagram/graph'
|
|
3
|
+
import type { SchemaSummary } from './useDatabaseSchemas'
|
|
4
|
+
|
|
5
|
+
/** What the search reads and writes of the diagram page. */
|
|
6
|
+
interface SearchContext {
|
|
7
|
+
schema: ComputedRef<SchemaSummary | null>
|
|
8
|
+
schemaId: ComputedRef<string | null>
|
|
9
|
+
focusIds: ComputedRef<Set<string> | null>
|
|
10
|
+
focusId: Ref<string | null>
|
|
11
|
+
expanded: ShallowRef<Set<string>>
|
|
12
|
+
setExpanded: (next: Set<string>) => void
|
|
13
|
+
render: () => void
|
|
14
|
+
centerOn: (nodeId: string) => boolean
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** A table or a column; Enter goes from match to match (D-13). */
|
|
18
|
+
export function useDiagramSearch({
|
|
19
|
+
schema,
|
|
20
|
+
schemaId,
|
|
21
|
+
focusIds,
|
|
22
|
+
focusId,
|
|
23
|
+
expanded,
|
|
24
|
+
setExpanded,
|
|
25
|
+
render,
|
|
26
|
+
centerOn,
|
|
27
|
+
}: SearchContext) {
|
|
28
|
+
const search = ref('')
|
|
29
|
+
const searchIndex = ref(-1)
|
|
30
|
+
|
|
31
|
+
const searchMatches = computed(() =>
|
|
32
|
+
schema.value ? searchTables(schema.value, search.value) : [],
|
|
33
|
+
)
|
|
34
|
+
const searchMissed = computed(
|
|
35
|
+
() => search.value.trim() !== '' && searchMatches.value.length === 0,
|
|
36
|
+
)
|
|
37
|
+
const searchPosition = computed(() =>
|
|
38
|
+
searchIndex.value >= 0 && searchMatches.value.length > 1
|
|
39
|
+
? `${searchIndex.value + 1}/${searchMatches.value.length}`
|
|
40
|
+
: null,
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
// Synchronous: an Enter right after the text changed starts from the first
|
|
44
|
+
// match of the new text.
|
|
45
|
+
watch(
|
|
46
|
+
search,
|
|
47
|
+
() => {
|
|
48
|
+
searchIndex.value = -1
|
|
49
|
+
},
|
|
50
|
+
{ flush: 'sync' },
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
function runSearch() {
|
|
54
|
+
const matches = searchMatches.value
|
|
55
|
+
if (!matches.length || !schemaId.value) return
|
|
56
|
+
searchIndex.value = (searchIndex.value + 1) % matches.length
|
|
57
|
+
const tableName = matches[searchIndex.value] ?? ''
|
|
58
|
+
const id = tableNodeId(schemaId.value, tableName)
|
|
59
|
+
if (focusIds.value && !focusIds.value.has(id)) focusId.value = null
|
|
60
|
+
// A column found in a folded table unfolds it.
|
|
61
|
+
if (schema.value) {
|
|
62
|
+
const next = expandForSearch(
|
|
63
|
+
schema.value,
|
|
64
|
+
tableName,
|
|
65
|
+
search.value,
|
|
66
|
+
expanded.value,
|
|
67
|
+
)
|
|
68
|
+
if (next !== expanded.value) {
|
|
69
|
+
setExpanded(next)
|
|
70
|
+
render()
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
nextTick(() => centerOn(id))
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return { search, searchIndex, searchMissed, searchPosition, runSearch }
|
|
77
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import type { VueFlowStore } from '@vue-flow/core'
|
|
2
|
+
import type { ComputedRef, Ref, ShallowRef, ShallowUnwrapRef } from 'vue'
|
|
3
|
+
import { NODE_WIDTH, tableNodeId } from '../diagram/graph'
|
|
4
|
+
import { boundsOf, fitZoom, startViewport } from '../diagram/packing'
|
|
5
|
+
import type { SchemaSummary } from './useDatabaseSchemas'
|
|
6
|
+
|
|
7
|
+
export const RESET_ZOOM = 1
|
|
8
|
+
// Room around the tables for the floating toolbars.
|
|
9
|
+
const FIT_PADDING = 0.25
|
|
10
|
+
// Below this zoom a table cannot be read: a diagram too large to fit at it
|
|
11
|
+
// starts there, on its top-left corner, clear of the toolbars.
|
|
12
|
+
const START_MIN_ZOOM = 0.4
|
|
13
|
+
const START_INSET = { x: 32, y: 80 }
|
|
14
|
+
|
|
15
|
+
/** What the view reads and writes of the diagram page. */
|
|
16
|
+
interface ViewContext {
|
|
17
|
+
canvas: ShallowRef<ShallowUnwrapRef<VueFlowStore> | null>
|
|
18
|
+
canvasWrapper: Readonly<ShallowRef<HTMLElement | null>>
|
|
19
|
+
viewport: Ref<{ x: number; y: number; zoom: number }>
|
|
20
|
+
selectedId: Ref<string | null>
|
|
21
|
+
schema: ComputedRef<SchemaSummary | null>
|
|
22
|
+
route: ReturnType<typeof useDmsRoute>
|
|
23
|
+
router: ReturnType<typeof useDmsRouter>
|
|
24
|
+
warn: (title: string) => void
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Where the canvas looks: the first frame, a fit, a table centred. */
|
|
28
|
+
export function useDiagramView({
|
|
29
|
+
canvas,
|
|
30
|
+
canvasWrapper,
|
|
31
|
+
viewport,
|
|
32
|
+
selectedId,
|
|
33
|
+
schema,
|
|
34
|
+
route,
|
|
35
|
+
router,
|
|
36
|
+
warn,
|
|
37
|
+
}: ViewContext) {
|
|
38
|
+
const { t } = useI18n()
|
|
39
|
+
let fitPending = true
|
|
40
|
+
|
|
41
|
+
/** The next measured nodes frame the diagram again. */
|
|
42
|
+
function requestFit() {
|
|
43
|
+
fitPending = true
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// The nodes have been measured: the first fit, or the one a schema change or
|
|
47
|
+
// a focus asks for, can frame them.
|
|
48
|
+
function onNodesInitialized() {
|
|
49
|
+
if (!fitPending) return
|
|
50
|
+
fitPending = false
|
|
51
|
+
if (!centerOnQueryTable()) startView()
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function fitAll() {
|
|
55
|
+
canvas.value?.fitView({ padding: FIT_PADDING, maxZoom: RESET_ZOOM })
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// The whole diagram when it fits at a readable zoom; else its top-left
|
|
59
|
+
// corner at that zoom, the minimap and "Fit view" giving the overview.
|
|
60
|
+
function startView() {
|
|
61
|
+
const flow = canvas.value
|
|
62
|
+
const rect = canvasWrapper.value?.getBoundingClientRect()
|
|
63
|
+
if (!flow || !rect?.width || !rect.height) return fitAll()
|
|
64
|
+
const bounds = boundsOf(
|
|
65
|
+
flow.getNodes.map((node) => ({
|
|
66
|
+
x: node.position.x,
|
|
67
|
+
y: node.position.y,
|
|
68
|
+
width: node.dimensions.width,
|
|
69
|
+
height: node.dimensions.height,
|
|
70
|
+
})),
|
|
71
|
+
)
|
|
72
|
+
const view = startViewport(
|
|
73
|
+
bounds,
|
|
74
|
+
fitZoom(bounds, rect, FIT_PADDING),
|
|
75
|
+
START_MIN_ZOOM,
|
|
76
|
+
START_INSET,
|
|
77
|
+
)
|
|
78
|
+
if (view) flow.setViewport(view)
|
|
79
|
+
else fitAll()
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function centerOn(nodeId: string): boolean {
|
|
83
|
+
const node = canvas.value?.findNode(nodeId)
|
|
84
|
+
if (!node) return false
|
|
85
|
+
selectedId.value = nodeId
|
|
86
|
+
canvas.value?.setCenter(
|
|
87
|
+
node.position.x + NODE_WIDTH / 2,
|
|
88
|
+
node.position.y + 120,
|
|
89
|
+
{
|
|
90
|
+
zoom: Math.max(viewport.value.zoom, RESET_ZOOM),
|
|
91
|
+
duration: 300,
|
|
92
|
+
},
|
|
93
|
+
)
|
|
94
|
+
return true
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// The table the URL names; one that does not exist is reported and dropped
|
|
98
|
+
// from the URL (D-12).
|
|
99
|
+
function centerOnQueryTable(): boolean {
|
|
100
|
+
const table = route.query.table
|
|
101
|
+
const asked = route.query.schema
|
|
102
|
+
if (typeof table !== 'string' || !schema.value) return false
|
|
103
|
+
if (typeof asked === 'string' && asked !== schema.value.id) return false
|
|
104
|
+
if (!schema.value.tables.some((entry) => entry.name === table)) {
|
|
105
|
+
warn(
|
|
106
|
+
t('dms_database.diagram.not_found.table', {
|
|
107
|
+
schema: schema.value.id,
|
|
108
|
+
table,
|
|
109
|
+
}),
|
|
110
|
+
)
|
|
111
|
+
router.replace({ query: { ...route.query, table: undefined } })
|
|
112
|
+
return false
|
|
113
|
+
}
|
|
114
|
+
return centerOn(tableNodeId(schema.value.id, table))
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return {
|
|
118
|
+
RESET_ZOOM,
|
|
119
|
+
fitAll,
|
|
120
|
+
startView,
|
|
121
|
+
centerOn,
|
|
122
|
+
onNodesInitialized,
|
|
123
|
+
requestFit,
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import type { Ref } from 'vue'
|
|
2
|
+
import type { InstanceRows } from '../../components/QueryResultPanel.vue'
|
|
3
|
+
import type { QueryTarget } from '../query/target'
|
|
4
|
+
import type { SchemaSummary } from './useDatabaseSchemas'
|
|
5
|
+
import type { ExecuteResult } from './useQueryStore'
|
|
6
|
+
|
|
7
|
+
const BROWSE_COUNT = '/api/database/browse/count'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The named instances holding rows of the table a read found empty in the
|
|
11
|
+
* default instance (decision 11): counted after the run, in the browser.
|
|
12
|
+
*/
|
|
13
|
+
export function useElsewhereInstances(schemas: Ref<SchemaSummary[]>) {
|
|
14
|
+
// Named instances holding rows of a table the last read found empty in the
|
|
15
|
+
// default instance.
|
|
16
|
+
const elsewhere = ref<InstanceRows[]>([])
|
|
17
|
+
|
|
18
|
+
type CountRows = (query: Record<string, string>) => Promise<{ total: number }>
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The named instances of a table that hold rows, each with its count: asked
|
|
22
|
+
* only when a read found the table empty in the default instance (decision
|
|
23
|
+
* 11). An instance whose count fails is left out.
|
|
24
|
+
*/
|
|
25
|
+
async function instancesWithRows(
|
|
26
|
+
target: QueryTarget,
|
|
27
|
+
count: CountRows,
|
|
28
|
+
): Promise<InstanceRows[]> {
|
|
29
|
+
const instances =
|
|
30
|
+
schemas.value.find((schema) => schema.id === target.schema)?.instances ??
|
|
31
|
+
[]
|
|
32
|
+
const counts = await Promise.all(
|
|
33
|
+
instances.map((id) =>
|
|
34
|
+
count({
|
|
35
|
+
filter_schema: `is:${target.schema}`,
|
|
36
|
+
filter_table: `is:${target.table}`,
|
|
37
|
+
filter_instance: `is:${id}`,
|
|
38
|
+
}).then(
|
|
39
|
+
(res) => ({ id, count: res.total }),
|
|
40
|
+
() => ({ id, count: 0 }),
|
|
41
|
+
),
|
|
42
|
+
),
|
|
43
|
+
)
|
|
44
|
+
return counts.filter((entry) => entry.count > 0)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// The table the last read found empty, whose instances are counted: in the
|
|
48
|
+
// browser, after a run.
|
|
49
|
+
const emptyTarget = shallowRef<QueryTarget | null>(null)
|
|
50
|
+
const elsewhereRead = useApiData<InstanceRows[]>(
|
|
51
|
+
() =>
|
|
52
|
+
emptyTarget.value
|
|
53
|
+
? `dms-database:elsewhere:${JSON.stringify([
|
|
54
|
+
emptyTarget.value.schema,
|
|
55
|
+
emptyTarget.value.table,
|
|
56
|
+
])}`
|
|
57
|
+
: null,
|
|
58
|
+
({ $authFetch, signal }) =>
|
|
59
|
+
instancesWithRows(emptyTarget.value as QueryTarget, (query) =>
|
|
60
|
+
$authFetch<{ total: number }>(BROWSE_COUNT, { query, signal }),
|
|
61
|
+
),
|
|
62
|
+
{
|
|
63
|
+
immediate: false,
|
|
64
|
+
server: false,
|
|
65
|
+
watch: false,
|
|
66
|
+
refreshOnPageRefresh: false,
|
|
67
|
+
},
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
// Each run drops the instances found for the previous one; a slow answer
|
|
71
|
+
// for an older run is ignored.
|
|
72
|
+
let runId = 0
|
|
73
|
+
|
|
74
|
+
async function findElsewhere(
|
|
75
|
+
answer: Pick<ExecuteResult, 'mutation' | 'rows'>,
|
|
76
|
+
next: QueryTarget,
|
|
77
|
+
) {
|
|
78
|
+
const id = ++runId
|
|
79
|
+
elsewhere.value = []
|
|
80
|
+
if (
|
|
81
|
+
answer.mutation ||
|
|
82
|
+
answer.rows.length > 0 ||
|
|
83
|
+
next.instance !== '' ||
|
|
84
|
+
!next.schema ||
|
|
85
|
+
!next.table
|
|
86
|
+
)
|
|
87
|
+
return
|
|
88
|
+
emptyTarget.value = next
|
|
89
|
+
await elsewhereRead.execute()
|
|
90
|
+
if (id === runId) elsewhere.value = elsewhereRead.data.value ?? []
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return { elsewhere, findElsewhere }
|
|
94
|
+
}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import type { ComputedRef, Ref } from 'vue'
|
|
2
|
+
import type { SchemaSummary } from './useDatabaseSchemas'
|
|
3
|
+
|
|
4
|
+
const BROWSE_LIST = '/api/database/browse/list'
|
|
5
|
+
|
|
6
|
+
interface SamplePage {
|
|
7
|
+
results: Record<string, unknown>[]
|
|
8
|
+
total: number
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The row asked for: its offset in an instance. No instance at offset 0 reads
|
|
13
|
+
* the default one, or the first named instance holding rows when the default
|
|
14
|
+
* one holds none.
|
|
15
|
+
*/
|
|
16
|
+
interface SampleRequest {
|
|
17
|
+
instance: string | null
|
|
18
|
+
offset: number
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** A sample read, with the table and the instance it was read from. */
|
|
22
|
+
interface SampleRead {
|
|
23
|
+
table: string
|
|
24
|
+
instance: string | null
|
|
25
|
+
page: SamplePage
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** What the sample reads from: the inspected table, and where it is shown. */
|
|
29
|
+
interface SampleSource {
|
|
30
|
+
schemaId: ComputedRef<string>
|
|
31
|
+
tableName: ComputedRef<string>
|
|
32
|
+
tableKey: ComputedRef<string>
|
|
33
|
+
schema: ComputedRef<SchemaSummary | undefined>
|
|
34
|
+
tab: Ref<string>
|
|
35
|
+
loading: ComputedRef<boolean>
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The inspector's Sample tab: the latest row of the table, or the next one on
|
|
40
|
+
* demand.
|
|
41
|
+
*/
|
|
42
|
+
export function useTableSample({
|
|
43
|
+
schemaId,
|
|
44
|
+
tableName,
|
|
45
|
+
tableKey,
|
|
46
|
+
schema,
|
|
47
|
+
tab,
|
|
48
|
+
loading,
|
|
49
|
+
}: SampleSource) {
|
|
50
|
+
const { t } = useI18n()
|
|
51
|
+
|
|
52
|
+
const requested = shallowRef<SampleRequest>({ instance: null, offset: 0 })
|
|
53
|
+
|
|
54
|
+
type ReadSample = (instance: string | null) => Promise<SamplePage>
|
|
55
|
+
|
|
56
|
+
async function firstFilledInstance(
|
|
57
|
+
read: ReadSample,
|
|
58
|
+
): Promise<{ instance: string | null; page: SamplePage }> {
|
|
59
|
+
const page = await read(null)
|
|
60
|
+
if (page.total > 0) return { instance: null, page }
|
|
61
|
+
for (const instance of schema.value?.instances ?? []) {
|
|
62
|
+
const named = await read(instance)
|
|
63
|
+
if (named.total > 0) return { instance, page: named }
|
|
64
|
+
}
|
|
65
|
+
return { instance: null, page }
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Read once the Sample tab shows, in the browser only.
|
|
69
|
+
const sampleRead = useApiData<SampleRead>(
|
|
70
|
+
() =>
|
|
71
|
+
schemaId.value && tableName.value
|
|
72
|
+
? `dms-database:sample:${JSON.stringify([
|
|
73
|
+
tableKey.value,
|
|
74
|
+
requested.value.instance,
|
|
75
|
+
requested.value.offset,
|
|
76
|
+
])}`
|
|
77
|
+
: null,
|
|
78
|
+
async ({ $authFetch, signal }) => {
|
|
79
|
+
const table = tableKey.value
|
|
80
|
+
const { instance, offset } = requested.value
|
|
81
|
+
const read: ReadSample = (from) =>
|
|
82
|
+
$authFetch<SamplePage>(BROWSE_LIST, {
|
|
83
|
+
query: {
|
|
84
|
+
filter_schema: `is:${schemaId.value}`,
|
|
85
|
+
filter_table: `is:${tableName.value}`,
|
|
86
|
+
offset,
|
|
87
|
+
limit: 1,
|
|
88
|
+
...(from ? { filter_instance: `is:${from}` } : {}),
|
|
89
|
+
},
|
|
90
|
+
signal,
|
|
91
|
+
})
|
|
92
|
+
const found =
|
|
93
|
+
instance === null && offset === 0
|
|
94
|
+
? await firstFilledInstance(read)
|
|
95
|
+
: { instance, page: await read(instance) }
|
|
96
|
+
return { table, ...found }
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
immediate: false,
|
|
100
|
+
server: false,
|
|
101
|
+
watch: false,
|
|
102
|
+
refreshOnPageRefresh: false,
|
|
103
|
+
},
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
// Another table (a relation followed) shows none of the previous one's row.
|
|
107
|
+
const sampleShown = computed(() => {
|
|
108
|
+
const value = sampleRead.data.value
|
|
109
|
+
return value?.table === tableKey.value ? value : null
|
|
110
|
+
})
|
|
111
|
+
const sample = computed(() => sampleShown.value?.page.results?.[0] ?? null)
|
|
112
|
+
const sampleTotal = computed(() => sampleShown.value?.page.total ?? 0)
|
|
113
|
+
const sampleOffset = computed(() => requested.value.offset)
|
|
114
|
+
const sampleLoading = computed(() => sampleRead.pending.value)
|
|
115
|
+
const sampleFailed = computed(() => sampleRead.status.value === 'error')
|
|
116
|
+
// The instance the sample is read from.
|
|
117
|
+
const sampleInstance = computed(() => sampleShown.value?.instance ?? null)
|
|
118
|
+
|
|
119
|
+
const sampleInstanceLabel = computed(() =>
|
|
120
|
+
sampleInstance.value === null
|
|
121
|
+
? t('dms_database.inspector.sample.default_instance')
|
|
122
|
+
: t('dms_database.inspector.sample.named_instance', {
|
|
123
|
+
name: sampleInstance.value,
|
|
124
|
+
}),
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
function loadSample() {
|
|
128
|
+
if (schemaId.value && tableName.value) void sampleRead.execute()
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function nextSample() {
|
|
132
|
+
const total = sampleTotal.value
|
|
133
|
+
requested.value = {
|
|
134
|
+
instance: sampleInstance.value,
|
|
135
|
+
offset: total > 0 ? (requested.value.offset + 1) % total : 0,
|
|
136
|
+
}
|
|
137
|
+
loadSample()
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Another table (a relation followed) starts over from its latest row.
|
|
141
|
+
watch(tableKey, () => {
|
|
142
|
+
requested.value = { instance: null, offset: 0 }
|
|
143
|
+
if (tab.value === 'sample' && !loading.value) loadSample()
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
// The sample waits for the schemas: they name the instances to look in.
|
|
147
|
+
onMounted(() =>
|
|
148
|
+
watch(
|
|
149
|
+
() => tab.value === 'sample' && !loading.value,
|
|
150
|
+
(ready) => {
|
|
151
|
+
if (ready && !sample.value && !sampleLoading.value) loadSample()
|
|
152
|
+
},
|
|
153
|
+
{ immediate: true },
|
|
154
|
+
),
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
// The `_instance` tag the store adds to rows of named instances is not a
|
|
158
|
+
// column of the table.
|
|
159
|
+
const sampleJson = computed(() => {
|
|
160
|
+
if (!sample.value) return ''
|
|
161
|
+
const { _instance: _tag, ...row } = sample.value
|
|
162
|
+
return JSON.stringify(row, null, 2)
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
return {
|
|
166
|
+
sample,
|
|
167
|
+
sampleTotal,
|
|
168
|
+
sampleOffset,
|
|
169
|
+
sampleLoading,
|
|
170
|
+
sampleFailed,
|
|
171
|
+
sampleInstanceLabel,
|
|
172
|
+
sampleJson,
|
|
173
|
+
loadSample,
|
|
174
|
+
nextSample,
|
|
175
|
+
}
|
|
176
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { GridColumn } from '../composables/useDataBrowserGrid'
|
|
2
|
+
import type { TableSummary } from '../composables/useDatabaseSchemas'
|
|
3
|
+
|
|
4
|
+
/** Primary key assumed when the backend reports none (MongoDB's implicit id). */
|
|
5
|
+
export const DEFAULT_PRIMARY_KEY = '_id'
|
|
6
|
+
|
|
7
|
+
export interface ColumnsResponse {
|
|
8
|
+
columns: string[]
|
|
9
|
+
types: Record<string, string>
|
|
10
|
+
primaryKey: string | null
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface ListResponse {
|
|
14
|
+
results: Record<string, unknown>[]
|
|
15
|
+
total: number
|
|
16
|
+
primaryKey: string | null
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The grid's columns: the sampled ones, or the introspected fields when the
|
|
21
|
+
* sample is empty or failed.
|
|
22
|
+
*/
|
|
23
|
+
export function columnsOf(
|
|
24
|
+
meta: ColumnsResponse | null,
|
|
25
|
+
primaryKey: string | null,
|
|
26
|
+
table: TableSummary | undefined,
|
|
27
|
+
): GridColumn[] {
|
|
28
|
+
const sampled = meta?.columns ?? []
|
|
29
|
+
const names = sampled.length > 0 ? sampled : Object.keys(table?.fields ?? {})
|
|
30
|
+
return names.map((name) => ({
|
|
31
|
+
name,
|
|
32
|
+
type: meta?.types[name] ?? 'string',
|
|
33
|
+
isPrimaryKey: name === (primaryKey ?? DEFAULT_PRIMARY_KEY),
|
|
34
|
+
}))
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface GridCell {
|
|
38
|
+
r: number
|
|
39
|
+
c: number
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const MOVES: Record<string, (cell: GridCell) => GridCell> = {
|
|
43
|
+
ArrowUp: ({ r, c }) => ({ r: r - 1, c }),
|
|
44
|
+
ArrowDown: ({ r, c }) => ({ r: r + 1, c }),
|
|
45
|
+
ArrowLeft: ({ r, c }) => ({ r, c: c - 1 }),
|
|
46
|
+
ArrowRight: ({ r, c }) => ({ r, c: c + 1 }),
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The cell an arrow key moves the focus to, kept inside a grid of `rows` by
|
|
51
|
+
* `columns`; null for any other key.
|
|
52
|
+
*/
|
|
53
|
+
export function moveFocus(
|
|
54
|
+
key: string,
|
|
55
|
+
from: GridCell,
|
|
56
|
+
rows: number,
|
|
57
|
+
columns: number,
|
|
58
|
+
): GridCell | null {
|
|
59
|
+
const move = MOVES[key]
|
|
60
|
+
if (!move) return null
|
|
61
|
+
const next = move(from)
|
|
62
|
+
return {
|
|
63
|
+
r: Math.max(0, Math.min(rows - 1, next.r)),
|
|
64
|
+
c: Math.max(0, Math.min(columns - 1, next.c)),
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -22,7 +22,7 @@ const UNDO_WINDOW_MS = 10_000
|
|
|
22
22
|
const UNDONE_NOTICE_MS = 3_000
|
|
23
23
|
|
|
24
24
|
/** What the save bar says once a save is written, then once it is undone. */
|
|
25
|
-
interface SaveNotice {
|
|
25
|
+
export interface SaveNotice {
|
|
26
26
|
state: 'saved' | 'undoing' | 'undone'
|
|
27
27
|
changes: StagedChange[]
|
|
28
28
|
scope: string
|
|
@@ -43,15 +43,6 @@ interface StagedSaveOptions {
|
|
|
43
43
|
onSaved: (saved: SavedChanges) => void
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
/** The server's reason when it gave one, else the error's own message. */
|
|
47
|
-
function messageOf(error: unknown): string {
|
|
48
|
-
const data = (error as { data?: unknown } | null)?.data
|
|
49
|
-
if (typeof data === 'string' && data) return data
|
|
50
|
-
const message = (data as { message?: unknown } | null)?.message
|
|
51
|
-
if (typeof message === 'string' && message) return message
|
|
52
|
-
return error instanceof Error ? error.message : String(error)
|
|
53
|
-
}
|
|
54
|
-
|
|
55
46
|
function fieldsOf(changes: StagedChange[]): string[] {
|
|
56
47
|
return [...new Set(changes.map((change) => change.field))]
|
|
57
48
|
}
|
|
@@ -61,6 +52,7 @@ export function useStagedSave(options: StagedSaveOptions) {
|
|
|
61
52
|
const { $authFetch } = useAuthFetch()
|
|
62
53
|
const { confirm } = useConfirm()
|
|
63
54
|
const toast = useToast()
|
|
55
|
+
const { presentError, resolveError } = useApiErrorPresenter()
|
|
64
56
|
const staged = useStagedEdits()
|
|
65
57
|
const saving = ref(false)
|
|
66
58
|
const notice = ref<SaveNotice | null>(null)
|
|
@@ -152,7 +144,10 @@ export function useStagedSave(options: StagedSaveOptions) {
|
|
|
152
144
|
}
|
|
153
145
|
|
|
154
146
|
/** Shows a row written as the server answered it. */
|
|
155
|
-
function showWritten(
|
|
147
|
+
function showWritten(
|
|
148
|
+
tab: BrowserTab,
|
|
149
|
+
row: Record<string, unknown> | undefined,
|
|
150
|
+
) {
|
|
156
151
|
if (row) options.onWritten(tab, row)
|
|
157
152
|
}
|
|
158
153
|
|
|
@@ -197,10 +192,13 @@ export function useStagedSave(options: StagedSaveOptions) {
|
|
|
197
192
|
)
|
|
198
193
|
if (outcome.error !== null) {
|
|
199
194
|
const left = changesOf(changes, outcome.left).length
|
|
195
|
+
const notice = resolveError(outcome.error, {
|
|
196
|
+
fallbackKey: 'error.500.description',
|
|
197
|
+
})
|
|
200
198
|
details.push(
|
|
201
199
|
t(
|
|
202
200
|
'dms_database.data.save.not_written',
|
|
203
|
-
{ count: n(left), error:
|
|
201
|
+
{ count: n(left), error: notice?.message ?? '' },
|
|
204
202
|
left,
|
|
205
203
|
),
|
|
206
204
|
)
|
|
@@ -237,14 +235,9 @@ export function useStagedSave(options: StagedSaveOptions) {
|
|
|
237
235
|
// The rows the failure left keep their undo, for a retry.
|
|
238
236
|
if (here)
|
|
239
237
|
show({ ...current, state: 'saved', changes: left }, UNDO_WINDOW_MS)
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
{ count: n(left.length + gone.length) },
|
|
244
|
-
left.length + gone.length,
|
|
245
|
-
),
|
|
246
|
-
description: messageOf(outcome.error),
|
|
247
|
-
color: 'error',
|
|
238
|
+
presentError(outcome.error, {
|
|
239
|
+
fallbackKey: 'dms_database.data.save.undo_partial',
|
|
240
|
+
params: { count: left.length + gone.length },
|
|
248
241
|
})
|
|
249
242
|
} else if (here) {
|
|
250
243
|
show(
|
|
@@ -29,7 +29,7 @@ export class DiagramAutosave {
|
|
|
29
29
|
/** The state to write, read when the write is asked for. */
|
|
30
30
|
private readonly read: () => DiagramState,
|
|
31
31
|
private readonly delayMs: number,
|
|
32
|
-
private readonly onStatus: (status: SaveStatus) => void,
|
|
32
|
+
private readonly onStatus: (status: SaveStatus, error?: unknown) => void,
|
|
33
33
|
) {}
|
|
34
34
|
|
|
35
35
|
/** Saves once edits pause. */
|
|
@@ -65,8 +65,8 @@ export class DiagramAutosave {
|
|
|
65
65
|
try {
|
|
66
66
|
await this.writer.save(state)
|
|
67
67
|
this.onStatus('saved')
|
|
68
|
-
} catch {
|
|
69
|
-
this.onStatus('error')
|
|
68
|
+
} catch (error) {
|
|
69
|
+
this.onStatus('error', error)
|
|
70
70
|
}
|
|
71
71
|
}
|
|
72
72
|
}
|