@antelopejs/dms-database 0.4.4 → 0.5.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 (173) hide show
  1. package/README.md +12 -10
  2. package/dist/automation/tableWatch.d.ts +2 -2
  3. package/dist/automation/tableWatch.js +3 -3
  4. package/dist/automation/tableWatch.js.map +1 -1
  5. package/dist/config.d.ts +11 -0
  6. package/dist/config.js +12 -0
  7. package/dist/config.js.map +1 -1
  8. package/dist/connectionAccess.d.ts +3 -0
  9. package/dist/connectionAccess.js +20 -0
  10. package/dist/connectionAccess.js.map +1 -0
  11. package/dist/db/models/connection_selections.model.d.ts +27 -0
  12. package/dist/db/models/connection_selections.model.js +17 -0
  13. package/dist/db/models/connection_selections.model.js.map +1 -0
  14. package/dist/db/models/diagram_layouts.model.d.ts +4 -4
  15. package/dist/db/models/diagram_layouts.model.js +14 -12
  16. package/dist/db/models/diagram_layouts.model.js.map +1 -1
  17. package/dist/db/models/diagram_notes.model.d.ts +3 -3
  18. package/dist/db/models/index.d.ts +1 -0
  19. package/dist/db/models/index.js +1 -0
  20. package/dist/db/models/index.js.map +1 -1
  21. package/dist/db/models/query_history.model.d.ts +3 -3
  22. package/dist/db/models/saved_queries.model.d.ts +3 -3
  23. package/dist/db/tables/connection_selections.table.d.ts +6 -0
  24. package/dist/db/tables/connection_selections.table.js +30 -0
  25. package/dist/db/tables/connection_selections.table.js.map +1 -0
  26. package/dist/db/tables/index.d.ts +1 -0
  27. package/dist/db/tables/index.js +1 -0
  28. package/dist/db/tables/index.js.map +1 -1
  29. package/dist/index.d.ts +1 -1
  30. package/dist/index.js +4 -0
  31. package/dist/index.js.map +1 -1
  32. package/dist/pages/externalLock.d.ts +8 -0
  33. package/dist/pages/externalLock.js +32 -0
  34. package/dist/pages/externalLock.js.map +1 -0
  35. package/dist/pages/overview.js +6 -3
  36. package/dist/pages/overview.js.map +1 -1
  37. package/dist/pages/schemas.js +6 -2
  38. package/dist/pages/schemas.js.map +1 -1
  39. package/dist/routes/connections.d.ts +14 -0
  40. package/dist/routes/connections.js +121 -0
  41. package/dist/routes/connections.js.map +1 -0
  42. package/dist/routes/data.d.ts +10 -9
  43. package/dist/routes/data.js +45 -61
  44. package/dist/routes/data.js.map +1 -1
  45. package/dist/routes/diagram.d.ts +6 -6
  46. package/dist/routes/diagram.js +16 -6
  47. package/dist/routes/diagram.js.map +1 -1
  48. package/dist/routes/health.d.ts +1 -1
  49. package/dist/routes/health.js +3 -2
  50. package/dist/routes/health.js.map +1 -1
  51. package/dist/routes/index.d.ts +1 -0
  52. package/dist/routes/index.js +1 -0
  53. package/dist/routes/index.js.map +1 -1
  54. package/dist/routes/overview.d.ts +2 -2
  55. package/dist/routes/overview.js +11 -4
  56. package/dist/routes/overview.js.map +1 -1
  57. package/dist/routes/query.d.ts +1 -1
  58. package/dist/routes/query.js +26 -10
  59. package/dist/routes/query.js.map +1 -1
  60. package/dist/routes/schemas.d.ts +1 -1
  61. package/dist/routes/schemas.js +4 -2
  62. package/dist/routes/schemas.js.map +1 -1
  63. package/dist/routes/tables.d.ts +2 -1
  64. package/dist/routes/tables.js +29 -21
  65. package/dist/routes/tables.js.map +1 -1
  66. package/dist/service/browse.d.ts +14 -6
  67. package/dist/service/browse.js +120 -18
  68. package/dist/service/browse.js.map +1 -1
  69. package/dist/service/builderRefusal.d.ts +2 -0
  70. package/dist/service/builderRefusal.js +28 -0
  71. package/dist/service/builderRefusal.js.map +1 -0
  72. package/dist/service/catalog.js +2 -1
  73. package/dist/service/catalog.js.map +1 -1
  74. package/dist/service/connectionRefusals.d.ts +17 -0
  75. package/dist/service/connectionRefusals.js +26 -0
  76. package/dist/service/connectionRefusals.js.map +1 -0
  77. package/dist/service/connectionState.d.ts +27 -0
  78. package/dist/service/connectionState.js +167 -0
  79. package/dist/service/connectionState.js.map +1 -0
  80. package/dist/service/connections.d.ts +12 -0
  81. package/dist/service/connections.js +73 -0
  82. package/dist/service/connections.js.map +1 -0
  83. package/dist/service/database.d.ts +12 -0
  84. package/dist/service/database.js +30 -0
  85. package/dist/service/database.js.map +1 -0
  86. package/dist/service/declaredFields.d.ts +3 -2
  87. package/dist/service/declaredFields.js +6 -7
  88. package/dist/service/declaredFields.js.map +1 -1
  89. package/dist/service/health.d.ts +2 -1
  90. package/dist/service/health.js +48 -21
  91. package/dist/service/health.js.map +1 -1
  92. package/dist/service/introspect.d.ts +7 -5
  93. package/dist/service/introspect.js +49 -41
  94. package/dist/service/introspect.js.map +1 -1
  95. package/dist/service/overview.d.ts +3 -2
  96. package/dist/service/overview.js +4 -4
  97. package/dist/service/overview.js.map +1 -1
  98. package/dist/service/queryInspection.js +6 -3
  99. package/dist/service/queryInspection.js.map +1 -1
  100. package/dist/service/references.d.ts +3 -2
  101. package/dist/service/references.js +9 -8
  102. package/dist/service/references.js.map +1 -1
  103. package/dist/service/rowEdit.d.ts +3 -0
  104. package/dist/service/rowEdit.js +36 -3
  105. package/dist/service/rowEdit.js.map +1 -1
  106. package/dist/service/types.d.ts +1 -1
  107. package/dist/utils/deadline.d.ts +2 -0
  108. package/dist/utils/deadline.js +18 -0
  109. package/dist/utils/deadline.js.map +1 -0
  110. package/dist/utils/guards.d.ts +2 -0
  111. package/dist/utils/guards.js +8 -0
  112. package/dist/utils/guards.js.map +1 -1
  113. package/dist/utils/query.js +5 -4
  114. package/dist/utils/query.js.map +1 -1
  115. package/frontend-vue/app/build/components/DataBrowserAccessBar.vue +88 -0
  116. package/frontend-vue/app/build/components/DataGridFooter.vue +16 -1
  117. package/frontend-vue/app/build/components/DataGridToolbar.vue +10 -5
  118. package/frontend-vue/app/build/components/FeatureLocked.vue +16 -0
  119. package/frontend-vue/app/build/components/QueryConsoleBoard.vue +601 -0
  120. package/frontend-vue/app/build/components/SchemaDiagramBoard.vue +691 -0
  121. package/frontend-vue/app/build/composables/registerDatabaseSwitcher.ts +16 -0
  122. package/frontend-vue/app/build/composables/reloadForSelection.ts +11 -0
  123. package/frontend-vue/app/build/composables/useDataBrowserGrid.ts +1 -0
  124. package/frontend-vue/app/build/composables/useDataBrowserTabs.ts +100 -15
  125. package/frontend-vue/app/build/composables/useDatabaseConnections.ts +164 -0
  126. package/frontend-vue/app/build/composables/useGridAccess.ts +25 -0
  127. package/frontend-vue/app/build/composables/useLockedPages.ts +53 -0
  128. package/frontend-vue/app/build/composables/useQueryRuntime.ts +4 -4
  129. package/frontend-vue/app/build/composables/useSelectionRecheck.ts +23 -0
  130. package/frontend-vue/app/build/data/assistantOnboarding.ts +12 -0
  131. package/frontend-vue/app/build/data/gridData.ts +22 -1
  132. package/frontend-vue/app/build/data/readOnlyReason.ts +30 -0
  133. package/frontend-vue/app/build/data/recentTables.ts +15 -8
  134. package/frontend-vue/app/build/data/useStagedSave.ts +7 -1
  135. package/frontend-vue/app/components/DataBrowser.vue +17 -14
  136. package/frontend-vue/app/components/DataBrowserSidebar.vue +24 -13
  137. package/frontend-vue/app/components/DataGrid.vue +22 -18
  138. package/frontend-vue/app/components/DataGridCell.vue +5 -2
  139. package/frontend-vue/app/components/DatabaseSwitcher.vue +101 -0
  140. package/frontend-vue/app/components/QueryBuilderPanel.vue +4 -4
  141. package/frontend-vue/app/components/QueryConsole.vue +11 -592
  142. package/frontend-vue/app/components/QueryLibrary.vue +3 -3
  143. package/frontend-vue/app/components/QueryResultPanel.vue +5 -5
  144. package/frontend-vue/app/components/RowDrawer.vue +21 -14
  145. package/frontend-vue/app/components/SchemaDiagram.vue +10 -683
  146. package/frontend-vue/app/components/SchemasFilterBar.vue +3 -0
  147. package/frontend-vue/app/components/TableInspector.vue +19 -10
  148. package/frontend-vue/build/backend-boundary.ts +8 -3
  149. package/frontend-vue/dms.frontend.ts +2 -0
  150. package/frontend-vue/eslint.config.mjs +0 -1
  151. package/frontend-vue/i18n/locales/database-en-GB.json +43 -1
  152. package/frontend-vue/i18n/locales/database-fr-FR.json +43 -1
  153. package/frontend-vue/package.json +3 -3
  154. package/frontend-vue/pnpm-lock.yaml +14 -11
  155. package/frontend-vue/tests/accessBar.test.ts +196 -0
  156. package/frontend-vue/tests/backend-boundary.test.ts +15 -7
  157. package/frontend-vue/tests/dataBrowserDatabase.test.ts +89 -0
  158. package/frontend-vue/tests/databaseSwitcher.test.ts +343 -0
  159. package/frontend-vue/tests/editRequest.test.ts +95 -0
  160. package/frontend-vue/tests/externalBrowser.test.ts +318 -0
  161. package/frontend-vue/tests/fixtures/connections.ts +73 -0
  162. package/frontend-vue/tests/fixtures/serverRender.ts +135 -12
  163. package/frontend-vue/tests/lockedPages.test.ts +68 -0
  164. package/frontend-vue/tests/onDemandReads.test.ts +1 -1
  165. package/frontend-vue/tests/persistence.test.ts +254 -4
  166. package/frontend-vue/tests/recentTables.test.ts +70 -0
  167. package/frontend-vue/tests/registration.test.ts +15 -1
  168. package/frontend-vue/tests/serverRender.test.ts +80 -4
  169. package/frontend-vue/vitest.config.ts +11 -1
  170. package/package.json +19 -15
  171. package/dist/service/schemaRegistry.d.ts +0 -2
  172. package/dist/service/schemaRegistry.js +0 -8
  173. package/dist/service/schemaRegistry.js.map +0 -1
@@ -31,6 +31,7 @@ export interface GridData {
31
31
  rows: Record<string, unknown>[]
32
32
  total: number
33
33
  primaryKey: string | null
34
+ capped: boolean
34
35
  }
35
36
 
36
37
  export const GRID_PAGE_SIZES = [25, 50, 100, 200]
@@ -1,7 +1,11 @@
1
1
  import { CROSS_INSTANCE_SENTINEL } from '../utils/stagedSerialization'
2
2
  import { useDataBrowserGrid } from './useDataBrowserGrid'
3
3
  import type { InstanceChoice } from '../data/instanceOptions'
4
- import { rememberTable } from '../data/recentTables'
4
+ import {
5
+ type RecentTable,
6
+ readRecentTables,
7
+ rememberTable,
8
+ } from '../data/recentTables'
5
9
  import { useStagedEdits } from '../data/stagedEdits'
6
10
  import {
7
11
  decodeMatch,
@@ -58,23 +62,42 @@ export interface BrowserTab {
58
62
  }
59
63
 
60
64
  const STORAGE_KEY = 'dms-database:data-browser:tabs'
65
+ const DATABASE_STATE = 'dms-database-browser-database'
61
66
  const ACTIVATIONS_STATE = 'dms-database-browser-activations'
67
+ const RECENT_STATE = 'dms-database-browser-recent'
62
68
 
63
69
  function browserTabId(schema: string, instance: string, table: string): string {
64
70
  return `${schema}::${instance}::${table}`
65
71
  }
66
72
 
73
+ /**
74
+ * Drops the tabs this browser tab kept: they name tables of the database
75
+ * that was browsed, which the next one may not hold.
76
+ */
77
+ export function forgetBrowserTabs(): void {
78
+ if (typeof window === 'undefined') return
79
+ window.sessionStorage.removeItem(STORAGE_KEY)
80
+ }
81
+
67
82
  interface PersistedTabs {
83
+ /**
84
+ * The connection the tabs were opened on: another one may not hold their
85
+ * tables. Null when they were opened while the selection was unknown.
86
+ */
87
+ database: string | null
68
88
  tabs: BrowserTab[]
69
89
  activeId: string | null
70
90
  }
71
91
 
92
+ /** The tabs this browser tab kept, with the database they were opened on. */
72
93
  function readPersisted(): PersistedTabs | null {
73
94
  if (typeof window === 'undefined') return null
74
95
  try {
75
96
  const raw = window.sessionStorage.getItem(STORAGE_KEY)
76
97
  if (!raw) return null
77
98
  const parsed = JSON.parse(raw) as PersistedTabs
99
+ const { database } = parsed
100
+ if (typeof database !== 'string' && database !== null) return null
78
101
  if (!Array.isArray(parsed.tabs)) return null
79
102
  const tabs = parsed.tabs
80
103
  .filter(
@@ -103,7 +126,7 @@ function readPersisted(): PersistedTabs | null {
103
126
  const activeId = tabs.some((tab) => tab.id === parsed.activeId)
104
127
  ? parsed.activeId
105
128
  : (tabs[0]?.id ?? null)
106
- return { tabs, activeId }
129
+ return { database, tabs, activeId }
107
130
  } catch {
108
131
  return null
109
132
  }
@@ -118,6 +141,10 @@ export function useDataBrowserTabs() {
118
141
  // Counts the activations, a tab already active included: the sidebar
119
142
  // follows the tab the user goes to, even back to the same one.
120
143
  const activations = useDmsState<number>(ACTIVATIONS_STATE, () => 0)
144
+ const database = useDmsState<string | null>(DATABASE_STATE, () => null)
145
+ // Read from the browser's storage once restored: the server renders
146
+ // without it, and a list only the client has would not match its markup.
147
+ const recent = useDmsState<RecentTable[]>(RECENT_STATE, () => [])
121
148
  const route = useDmsRoute()
122
149
  const router = useDmsRouter()
123
150
  // Captured at setup so closeTab can drop the closed tab's
@@ -138,7 +165,11 @@ export function useDataBrowserTabs() {
138
165
  try {
139
166
  window.sessionStorage.setItem(
140
167
  STORAGE_KEY,
141
- JSON.stringify({ tabs: tabs.value, activeId: activeId.value }),
168
+ JSON.stringify({
169
+ database: database.value,
170
+ tabs: tabs.value,
171
+ activeId: activeId.value,
172
+ }),
142
173
  )
143
174
  } catch {
144
175
  // Quota/private-mode failures only cost tab restoration.
@@ -171,11 +202,14 @@ export function useDataBrowserTabs() {
171
202
  function activateTab(id: string) {
172
203
  const tab = tabs.value.find((candidate) => candidate.id === id)
173
204
  if (!tab) return
174
- rememberTable({
175
- schema: tab.schema,
176
- instance: tab.instance,
177
- table: tab.table,
178
- })
205
+ if (database.value !== null) {
206
+ rememberTable(database.value, {
207
+ schema: tab.schema,
208
+ instance: tab.instance,
209
+ table: tab.table,
210
+ })
211
+ recent.value = readRecentTables(database.value)
212
+ }
179
213
  activeId.value = id
180
214
  activations.value += 1
181
215
  syncUrl()
@@ -336,24 +370,74 @@ export function useDataBrowserTabs() {
336
370
  return true
337
371
  }
338
372
 
339
- // Restore persisted tabs, preview included, then let the URL win for the
340
- // active selection. Call once, from the page's onMounted.
341
- function restore() {
342
- const persisted = readPersisted()
343
- if (persisted) {
344
- tabs.value = persisted.tabs
345
- activeId.value = persisted.activeId
373
+ /** Drops the grid state and staged edits of tabs that are gone; returns their ids. */
374
+ function release(gone: BrowserTab[]): string[] {
375
+ for (const tab of gone) {
376
+ grid.drop(tab.id)
377
+ staged.discard(tab.id)
346
378
  }
379
+ return gone.map((tab) => tab.id)
380
+ }
381
+
382
+ /**
383
+ * Makes `owner` the database of the tabs, open and saved: the tabs of
384
+ * another known database are released, tabs of no known database are
385
+ * claimed. Returns the ids released, so a URL that mirrors one of them is
386
+ * cleared instead of opened.
387
+ */
388
+ function settle(owner: string | null, saved: PersistedTabs | null) {
389
+ const dropped: string[] = []
390
+ if (database.value !== null && database.value !== owner) {
391
+ dropped.push(...release(tabs.value))
392
+ tabs.value = []
393
+ activeId.value = null
394
+ }
395
+ if (saved && (saved.database === owner || saved.database === null)) {
396
+ tabs.value = saved.tabs
397
+ activeId.value = saved.activeId
398
+ } else if (saved) {
399
+ dropped.push(...release(saved.tabs))
400
+ }
401
+ database.value = owner
402
+ recent.value = owner === null ? [] : readRecentTables(owner)
403
+ if (owner !== null && saved?.database !== owner) persist()
404
+ return new Set(dropped)
405
+ }
406
+
407
+ /**
408
+ * Restores the tabs this browser tab kept on the selected database,
409
+ * preview included, then lets the URL win for the active selection. Tabs
410
+ * of another database, kept or still open, are dropped, and a URL that
411
+ * mirrors one of them is cleared. Call once, from the page's onMounted,
412
+ * with the selected connection's id, undefined when the selection is
413
+ * unknown.
414
+ */
415
+ function restore(selected: string | undefined) {
416
+ const saved = readPersisted()
417
+ const dropped = settle(selected ?? saved?.database ?? database.value, saved)
418
+ const target = routeTable()
419
+ if (target && dropped.has(target.id)) return syncUrl()
347
420
  // A schema-only link (?schema=X) is the sidebar's to honour.
348
421
  if (!openFromRoute() && !route.query.schema && activeTab.value) syncUrl()
349
422
  }
350
423
 
424
+ /**
425
+ * Follows a selection the page learns once restored (its connections
426
+ * read again in the background). Register it after restore().
427
+ */
428
+ function follow(selected: string | undefined) {
429
+ if (selected === undefined || selected === database.value) return
430
+ if (settle(selected, null).size > 0) syncUrl()
431
+ }
432
+
351
433
  return {
352
434
  tabs,
353
435
  activeId,
354
436
  activeTab,
355
437
  activations,
356
438
  hasMultipleSchemas,
439
+ /** The tables opened lately on the tabs' database, newest first. */
440
+ recent: computed(() => recent.value),
357
441
  openTab,
358
442
  replaceTab,
359
443
  closeTab,
@@ -361,6 +445,7 @@ export function useDataBrowserTabs() {
361
445
  pinTab,
362
446
  moveTab,
363
447
  restore,
448
+ follow,
364
449
  openFromRoute,
365
450
  }
366
451
  }
@@ -0,0 +1,164 @@
1
+ import { reloadForSelection } from './reloadForSelection'
2
+
3
+ /** A part of the console an external database cannot serve. */
4
+ export type ConnectionFeature =
5
+ | 'diagram'
6
+ | 'queryConsole'
7
+ | 'instances'
8
+ | 'rowReferences'
9
+ | 'schemaEdits'
10
+ | 'migrations'
11
+ | 'indexes'
12
+
13
+ export type ConnectionAccess = 'read' | 'read-write'
14
+
15
+ export interface DatabaseConnection {
16
+ id: string
17
+ kind: 'own' | 'external'
18
+ label: string
19
+ engine: string | null
20
+ access: ConnectionAccess
21
+ /** Null when the catalog could not tell whether the account may write. */
22
+ canWrite: boolean | null
23
+ unsupported: ConnectionFeature[]
24
+ }
25
+
26
+ export interface ConnectionsState {
27
+ /** The connection this user browses: every page of the module follows it. */
28
+ selected: string
29
+ accessEditable: boolean
30
+ connections: DatabaseConnection[]
31
+ }
32
+
33
+ export const OWN_CONNECTION = 'own'
34
+
35
+ const CONNECTIONS_ENDPOINT = '/api/database/connections'
36
+ const EMPTY_STATE: ConnectionsState = {
37
+ selected: OWN_CONNECTION,
38
+ accessEditable: false,
39
+ connections: [],
40
+ }
41
+
42
+ /** Whether the connection is one the console reaches through the catalog. */
43
+ export function isExternal(
44
+ connection: DatabaseConnection | undefined,
45
+ ): connection is DatabaseConnection {
46
+ return connection?.kind === 'external'
47
+ }
48
+
49
+ /** Whether the connection lists the feature as one it cannot serve. */
50
+ export function lacksFeature(
51
+ connection: DatabaseConnection | undefined,
52
+ feature: ConnectionFeature,
53
+ ): boolean {
54
+ return connection?.unsupported.includes(feature) ?? false
55
+ }
56
+
57
+ /** Whether the connection refuses edits: an external one left on read. */
58
+ export function refusesEdits(
59
+ connection: DatabaseConnection | undefined,
60
+ ): boolean {
61
+ return isExternal(connection) && connection.access === 'read'
62
+ }
63
+
64
+ export function useDatabaseConnections() {
65
+ const read = useApiData<ConnectionsState>(CONNECTIONS_ENDPOINT, {
66
+ default: () => EMPTY_STATE,
67
+ })
68
+ const { $authFetch } = useAuthFetch()
69
+ const { presentError } = useApiErrorPresenter()
70
+
71
+ const state = computed(() => read.data.value ?? EMPTY_STATE)
72
+ const connections = computed(() => state.value.connections)
73
+ const selected = computed(() =>
74
+ connections.value.find(
75
+ (connection) => connection.id === state.value.selected,
76
+ ),
77
+ )
78
+
79
+ const failed = computed(() => read.status.value === 'error')
80
+
81
+ async function write(
82
+ path: string,
83
+ body: object,
84
+ fallbackKey: string,
85
+ keepsSelection: boolean,
86
+ ): Promise<boolean> {
87
+ try {
88
+ const next = await $authFetch<ConnectionsState>(
89
+ `${CONNECTIONS_ENDPOINT}/${path}`,
90
+ { method: 'PUT', body },
91
+ )
92
+ if (keepsSelection && next.selected !== state.value.selected) {
93
+ reloadForSelection()
94
+ return false
95
+ }
96
+ read.data.value = next
97
+ return true
98
+ } catch (error) {
99
+ presentError(error, { fallbackKey })
100
+ return false
101
+ }
102
+ }
103
+
104
+ let rechecking: Promise<void> | null = null
105
+
106
+ return {
107
+ connections,
108
+ selected,
109
+ accessEditable: computed(() => state.value.accessEditable),
110
+ external: computed(() => isExternal(selected.value)),
111
+ refusesEdits: computed(() => failed.value || refusesEdits(selected.value)),
112
+ lacks: (feature: ConnectionFeature) =>
113
+ lacksFeature(selected.value, feature),
114
+ /** Whether the connections could not be read: no database is known, so no edit is safe. */
115
+ failed,
116
+ /**
117
+ * Stores the connection this user browses; true once the server took
118
+ * it, and every API of the module answers for it from then on.
119
+ */
120
+ select: (id: string) =>
121
+ write(
122
+ 'selected',
123
+ { connection: id },
124
+ 'dms_database.connections.switcher.failed',
125
+ false,
126
+ ),
127
+ /**
128
+ * Allows or refuses the edits of an external connection. Reloads the
129
+ * page instead when the server's selection is no longer the one this
130
+ * page loaded with.
131
+ */
132
+ setAccess: (id: string, access: ConnectionAccess) =>
133
+ write(
134
+ `${encodeURIComponent(id)}/access`,
135
+ { access },
136
+ 'dms_database.connections.access.failed',
137
+ true,
138
+ ),
139
+ /**
140
+ * Reads the connections again, apart from the page's state, and
141
+ * reloads the page when the server's selection is no longer the one
142
+ * it loaded with (another tab switched), or when the first read failed
143
+ * and this one answered. A failed read changes nothing.
144
+ */
145
+ recheck: (): Promise<void> => {
146
+ rechecking ??= $authFetch<ConnectionsState>(CONNECTIONS_ENDPOINT)
147
+ .then((next) => {
148
+ if (failed.value || next.selected !== state.value.selected)
149
+ reloadForSelection()
150
+ })
151
+ .catch(() => undefined)
152
+ .finally(() => {
153
+ rechecking = null
154
+ })
155
+ return rechecking
156
+ },
157
+ /**
158
+ * Settles once the first read is in, at once when the connections were
159
+ * read before: a page awaiting it does not fetch for a selection it
160
+ * does not know yet.
161
+ */
162
+ ready: read.then(() => undefined),
163
+ }
164
+ }
@@ -0,0 +1,25 @@
1
+ import { useDatabaseConnections } from './useDatabaseConnections'
2
+ import { CROSS_INSTANCE_VALUE, type BrowserTab } from './useDataBrowserTabs'
3
+
4
+ /**
5
+ * What a grid may do, by its tab's instance and the database browsed: the
6
+ * cross-instance union and an external database left on read take no edit,
7
+ * and an external database offers no instance, row reference or query console.
8
+ */
9
+ export function useGridAccess(tab: () => BrowserTab) {
10
+ const { refusesEdits, lacks } = useDatabaseConnections()
11
+ const crossInstance = computed(() => tab().instance === CROSS_INSTANCE_VALUE)
12
+ const readOnly = computed(() => crossInstance.value || refusesEdits.value)
13
+
14
+ return {
15
+ crossInstance,
16
+ readOnly,
17
+ showReferences: computed(
18
+ () => !crossInstance.value && !lacks('rowReferences'),
19
+ ),
20
+ canOpenQuery: computed(() => !lacks('queryConsole')),
21
+ canSearchAllInstances: computed(
22
+ () => !readOnly.value && !lacks('instances'),
23
+ ),
24
+ }
25
+ }
@@ -0,0 +1,53 @@
1
+ import type { Ref } from 'vue'
2
+ import {
3
+ lacksFeature,
4
+ type ConnectionFeature,
5
+ type DatabaseConnection,
6
+ } from './useDatabaseConnections'
7
+
8
+ /** The module's pages an external database cannot serve, by the feature each needs. */
9
+ const LOCKABLE_PAGES: ReadonlyArray<{
10
+ path: string
11
+ feature: ConnectionFeature
12
+ }> = [
13
+ { path: '/modules/database/diagram', feature: 'diagram' },
14
+ { path: '/modules/database/query', feature: 'queryConsole' },
15
+ ]
16
+
17
+ const LOCK_REASON = '$dms_database.connections.locked.reason'
18
+
19
+ /**
20
+ * Keeps the sidebar entries of the pages the selected database cannot serve
21
+ * locked, with the reason as their tooltip, and unlocks them once it can.
22
+ * Applied once mounted: the server renders the sidebar before it knows the
23
+ * selection, so a lock set there would reach the client's state and not the
24
+ * server's markup, and the menu would not hydrate.
25
+ * The lock closes the entry only: each page shows its own locked state for a
26
+ * direct URL.
27
+ *
28
+ * @param selected The connection this user browses
29
+ */
30
+ export function useLockedPages(selected: Ref<DatabaseConnection | undefined>) {
31
+ const siteLayout = useSiteLayout()
32
+ const { lock, unlock } = useMenuLocks()
33
+ const pages = computed(() =>
34
+ LOCKABLE_PAGES.flatMap((page) => {
35
+ const fullId = siteLayout.findMatchingRoute(page.path)?.metadata.fullId
36
+ return fullId
37
+ ? [{ fullId, locked: lacksFeature(selected.value, page.feature) }]
38
+ : []
39
+ }),
40
+ )
41
+ onMounted(() => {
42
+ watch(
43
+ pages,
44
+ (entries) => {
45
+ for (const { fullId, locked } of entries) {
46
+ if (locked) lock({ fullId, reason: LOCK_REASON })
47
+ else unlock(fullId)
48
+ }
49
+ },
50
+ { immediate: true },
51
+ )
52
+ })
53
+ }
@@ -62,11 +62,11 @@ function toSchemaDefinition(summary: SchemaSummary): SchemaDefLike {
62
62
 
63
63
  function buildSchemasMap(
64
64
  summaries: SchemaSummary[],
65
- ): Record<string, Schema<unknown, SchemaDefLike>> {
66
- const bindings: Record<string, Schema<unknown, SchemaDefLike>> = {}
65
+ ): Record<string, Schema<unknown, unknown>> {
66
+ const bindings: Record<string, Schema<unknown, unknown>> = {}
67
67
  for (const summary of summaries) {
68
68
  bindings[summary.id] =
69
- Schema.get<unknown, SchemaDefLike>(summary.id) ??
69
+ Schema.get(summary.id) ??
70
70
  new Schema(summary.id, toSchemaDefinition(summary))
71
71
  }
72
72
  return bindings
@@ -155,7 +155,7 @@ function compile(code: string, text: string): QueryFunction {
155
155
  function evaluateUserCode(
156
156
  code: string,
157
157
  text: string,
158
- schemas: Record<string, Schema<unknown, SchemaDefLike>>,
158
+ schemas: Record<string, Schema<unknown, unknown>>,
159
159
  ): unknown {
160
160
  return compile(code, text)(
161
161
  guardSchemas(schemas),
@@ -0,0 +1,23 @@
1
+ import { onBeforeUnmount, onMounted } from 'vue'
2
+
3
+ /**
4
+ * Runs `recheck` each time the window regains focus or the page becomes
5
+ * visible again, while the component is mounted: what another tab did to the
6
+ * server's selection in the meantime shows then.
7
+ */
8
+ export function useSelectionRecheck(recheck: () => Promise<void>): void {
9
+ function onVisibility() {
10
+ if (document.visibilityState === 'visible') void recheck()
11
+ }
12
+ function onFocus() {
13
+ void recheck()
14
+ }
15
+ onMounted(() => {
16
+ window.addEventListener('focus', onFocus)
17
+ document.addEventListener('visibilitychange', onVisibility)
18
+ })
19
+ onBeforeUnmount(() => {
20
+ window.removeEventListener('focus', onFocus)
21
+ document.removeEventListener('visibilitychange', onVisibility)
22
+ })
23
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * What dms-assistant offers the rest of the app, mirrored here so the module
3
+ * depends on its shape only: `isAvailable` is false while the assistant
4
+ * cannot take the user through a step.
5
+ */
6
+ export interface PublicOnboarding {
7
+ readonly isAvailable: boolean
8
+ open(step?: 'connect-database'): void
9
+ }
10
+
11
+ /** The key dms-assistant provides its onboarding under, app-wide. */
12
+ export const ONBOARDING_KEY = Symbol.for('dms-assistant:onboarding')
@@ -1,4 +1,4 @@
1
- import type { GridColumn } from '../composables/useDataBrowserGrid'
1
+ import type { GridColumn, GridData } from '../composables/useDataBrowserGrid'
2
2
  import type { TableSummary } from '../composables/useDatabaseSchemas'
3
3
 
4
4
  /** Primary key assumed when the backend reports none (MongoDB's implicit id). */
@@ -10,10 +10,15 @@ export interface ColumnsResponse {
10
10
  primaryKey: string | null
11
11
  }
12
12
 
13
+ /** The rows a search or a column filter reads on an external database. */
14
+ export const EXTERNAL_SCAN_CAP = 10_000
15
+
13
16
  export interface ListResponse {
14
17
  results: Record<string, unknown>[]
15
18
  total: number
16
19
  primaryKey: string | null
20
+ /** The rows or the total cover only part of an external table. */
21
+ capped: boolean
17
22
  }
18
23
 
19
24
  /**
@@ -34,6 +39,22 @@ export function columnsOf(
34
39
  }))
35
40
  }
36
41
 
42
+ /** The grid of a page of rows and the table's sampled columns. */
43
+ export function gridDataOf(
44
+ list: ListResponse,
45
+ meta: ColumnsResponse | null,
46
+ table: TableSummary | undefined,
47
+ ): GridData {
48
+ const primaryKey = list.primaryKey ?? meta?.primaryKey ?? null
49
+ return {
50
+ columns: columnsOf(meta, primaryKey, table),
51
+ rows: list.results ?? [],
52
+ total: list.total ?? 0,
53
+ primaryKey,
54
+ capped: list.capped ?? false,
55
+ }
56
+ }
57
+
37
58
  export interface GridCell {
38
59
  r: number
39
60
  c: number
@@ -0,0 +1,30 @@
1
+ import type { InjectionKey, Ref } from 'vue'
2
+ import {
3
+ refusesEdits,
4
+ useDatabaseConnections,
5
+ } from '../composables/useDatabaseConnections'
6
+
7
+ /**
8
+ * Why the grid's cells take no edit, which each cell shows where the user
9
+ * tries one: set while the selected external database is read only, absent
10
+ * otherwise.
11
+ */
12
+ export const READ_ONLY_REASON: InjectionKey<Ref<string | null>> = Symbol(
13
+ 'dms-database:read-only-reason',
14
+ )
15
+
16
+ /** Gives the cells below the reason the selected database refuses edits. */
17
+ export function provideReadOnlyReason(): void {
18
+ const { t } = useI18n()
19
+ const { selected } = useDatabaseConnections()
20
+ provide(
21
+ READ_ONLY_REASON,
22
+ computed(() =>
23
+ selected.value && refusesEdits(selected.value)
24
+ ? t('dms_database.data.save.read_only_access', {
25
+ connection: selected.value.label,
26
+ })
27
+ : null,
28
+ ),
29
+ )
30
+ }
@@ -1,5 +1,5 @@
1
- // The tables opened lately, offered by the data browser's first-visit state
2
- // (D-15). Kept in this browser only.
1
+ // The tables opened lately on each database, offered by the data browser's
2
+ // first-visit state (D-15). Kept in this browser only.
3
3
 
4
4
  export interface RecentTable {
5
5
  schema: string
@@ -8,13 +8,16 @@ export interface RecentTable {
8
8
  openedAt: string
9
9
  }
10
10
 
11
- const STORAGE_KEY = 'dms-database:data-browser:recent'
11
+ const STORAGE_PREFIX = 'dms-database:data-browser:recent:'
12
12
  const MAX_RECENT = 5
13
13
 
14
- export function readRecentTables(): RecentTable[] {
14
+ /** The tables opened lately on `database`, newest first. */
15
+ export function readRecentTables(database: string): RecentTable[] {
15
16
  if (typeof window === 'undefined') return []
16
17
  try {
17
- const parsed = JSON.parse(window.localStorage.getItem(STORAGE_KEY) ?? '[]')
18
+ const parsed = JSON.parse(
19
+ window.localStorage.getItem(STORAGE_PREFIX + database) ?? '[]',
20
+ )
18
21
  return Array.isArray(parsed)
19
22
  ? parsed.filter(
20
23
  (entry): entry is RecentTable =>
@@ -28,9 +31,13 @@ export function readRecentTables(): RecentTable[] {
28
31
  }
29
32
  }
30
33
 
31
- export function rememberTable(entry: Omit<RecentTable, 'openedAt'>) {
34
+ /** Records a table opened on `database` as its newest. */
35
+ export function rememberTable(
36
+ database: string,
37
+ entry: Omit<RecentTable, 'openedAt'>,
38
+ ): void {
32
39
  if (typeof window === 'undefined') return
33
- const others = readRecentTables().filter(
40
+ const others = readRecentTables(database).filter(
34
41
  (recent) =>
35
42
  recent.schema !== entry.schema ||
36
43
  recent.table !== entry.table ||
@@ -41,7 +48,7 @@ export function rememberTable(entry: Omit<RecentTable, 'openedAt'>) {
41
48
  ...others,
42
49
  ].slice(0, MAX_RECENT)
43
50
  try {
44
- window.localStorage.setItem(STORAGE_KEY, JSON.stringify(next))
51
+ window.localStorage.setItem(STORAGE_PREFIX + database, JSON.stringify(next))
45
52
  } catch {
46
53
  // A full or private storage only loses the suggestions.
47
54
  }
@@ -5,6 +5,7 @@ import {
5
5
  import { browseSelectionQuery } from '../composables/useDataBrowserGrid'
6
6
  import { h, onScopeDispose } from 'vue'
7
7
  import type { BrowserTab } from '../composables/useDataBrowserTabs'
8
+ import { useDatabaseConnections } from '../composables/useDatabaseConnections'
8
9
  import { cellLabel } from './cellValues'
9
10
  import { type StagedChange, undoBodies, useStagedEdits } from './stagedEdits'
10
11
  import { changesOf, type RowWriteOutcome, writeRows } from './stagedWrites'
@@ -53,6 +54,7 @@ export function useStagedSave(options: StagedSaveOptions) {
53
54
  const { confirm } = useConfirm()
54
55
  const toast = useToast()
55
56
  const { presentError, resolveError } = useApiErrorPresenter()
57
+ const { selected } = useDatabaseConnections()
56
58
  const staged = useStagedEdits()
57
59
  const saving = ref(false)
58
60
  const notice = ref<SaveNotice | null>(null)
@@ -100,7 +102,11 @@ export function useStagedSave(options: StagedSaveOptions) {
100
102
  ): Promise<Record<string, unknown>> {
101
103
  return $authFetch<Record<string, unknown>>(EDIT_ENDPOINT, {
102
104
  method: 'PUT',
103
- query: { ...browseSelectionQuery(tab), id: rowId },
105
+ query: {
106
+ ...browseSelectionQuery(tab),
107
+ id: rowId,
108
+ ...(selected.value ? { connection: selected.value.id } : {}),
109
+ },
104
110
  body,
105
111
  })
106
112
  }