@antelopejs/dms-database 0.4.3 → 0.5.0

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 (165) 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 +116 -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 +43 -62
  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/catalog.js +2 -1
  70. package/dist/service/catalog.js.map +1 -1
  71. package/dist/service/connectionState.d.ts +29 -0
  72. package/dist/service/connectionState.js +157 -0
  73. package/dist/service/connectionState.js.map +1 -0
  74. package/dist/service/connections.d.ts +12 -0
  75. package/dist/service/connections.js +72 -0
  76. package/dist/service/connections.js.map +1 -0
  77. package/dist/service/database.d.ts +12 -0
  78. package/dist/service/database.js +30 -0
  79. package/dist/service/database.js.map +1 -0
  80. package/dist/service/declaredFields.d.ts +3 -2
  81. package/dist/service/declaredFields.js +6 -7
  82. package/dist/service/declaredFields.js.map +1 -1
  83. package/dist/service/health.d.ts +2 -1
  84. package/dist/service/health.js +48 -21
  85. package/dist/service/health.js.map +1 -1
  86. package/dist/service/introspect.d.ts +7 -5
  87. package/dist/service/introspect.js +50 -47
  88. package/dist/service/introspect.js.map +1 -1
  89. package/dist/service/overview.d.ts +3 -2
  90. package/dist/service/overview.js +4 -4
  91. package/dist/service/overview.js.map +1 -1
  92. package/dist/service/queryInspection.js +6 -3
  93. package/dist/service/queryInspection.js.map +1 -1
  94. package/dist/service/references.d.ts +3 -2
  95. package/dist/service/references.js +9 -8
  96. package/dist/service/references.js.map +1 -1
  97. package/dist/service/rowEdit.d.ts +3 -0
  98. package/dist/service/rowEdit.js +36 -3
  99. package/dist/service/rowEdit.js.map +1 -1
  100. package/dist/service/stagedSerialization.js +5 -2
  101. package/dist/service/stagedSerialization.js.map +1 -1
  102. package/dist/service/types.d.ts +1 -1
  103. package/dist/utils/deadline.d.ts +2 -0
  104. package/dist/utils/deadline.js +18 -0
  105. package/dist/utils/deadline.js.map +1 -0
  106. package/dist/utils/guards.d.ts +2 -0
  107. package/dist/utils/guards.js +8 -0
  108. package/dist/utils/guards.js.map +1 -1
  109. package/dist/utils/query.js +5 -4
  110. package/dist/utils/query.js.map +1 -1
  111. package/frontend-vue/app/build/components/DataBrowserAccessBar.vue +88 -0
  112. package/frontend-vue/app/build/components/DataGridToolbar.vue +10 -5
  113. package/frontend-vue/app/build/components/FeatureLocked.vue +16 -0
  114. package/frontend-vue/app/build/components/QueryConsoleBoard.vue +601 -0
  115. package/frontend-vue/app/build/components/SchemaDiagramBoard.vue +691 -0
  116. package/frontend-vue/app/build/composables/registerDatabaseSwitcher.ts +16 -0
  117. package/frontend-vue/app/build/composables/reloadForSelection.ts +13 -0
  118. package/frontend-vue/app/build/composables/useDataBrowserTabs.ts +9 -0
  119. package/frontend-vue/app/build/composables/useDatabaseConnections.ts +164 -0
  120. package/frontend-vue/app/build/composables/useGridAccess.ts +25 -0
  121. package/frontend-vue/app/build/composables/useLockedPages.ts +53 -0
  122. package/frontend-vue/app/build/composables/useQueryRuntime.ts +4 -4
  123. package/frontend-vue/app/build/composables/useSelectionRecheck.ts +23 -0
  124. package/frontend-vue/app/build/data/assistantOnboarding.ts +12 -0
  125. package/frontend-vue/app/build/data/readOnlyReason.ts +30 -0
  126. package/frontend-vue/app/build/data/recentTables.ts +6 -0
  127. package/frontend-vue/app/build/data/staleDatabase.ts +58 -0
  128. package/frontend-vue/app/build/data/useStagedSave.ts +10 -1
  129. package/frontend-vue/app/components/DataBrowser.vue +9 -1
  130. package/frontend-vue/app/components/DataBrowserSidebar.vue +23 -13
  131. package/frontend-vue/app/components/DataGrid.vue +17 -6
  132. package/frontend-vue/app/components/DataGridCell.vue +5 -2
  133. package/frontend-vue/app/components/DatabaseSwitcher.vue +103 -0
  134. package/frontend-vue/app/components/QueryBuilderPanel.vue +4 -4
  135. package/frontend-vue/app/components/QueryConsole.vue +11 -592
  136. package/frontend-vue/app/components/QueryLibrary.vue +3 -3
  137. package/frontend-vue/app/components/QueryResultPanel.vue +14 -12
  138. package/frontend-vue/app/components/RowDrawer.vue +21 -14
  139. package/frontend-vue/app/components/SchemaDiagram.vue +10 -683
  140. package/frontend-vue/app/components/SchemasFilterBar.vue +3 -0
  141. package/frontend-vue/app/components/TableInspector.vue +8 -4
  142. package/frontend-vue/build/backend-boundary.ts +8 -3
  143. package/frontend-vue/dms.frontend.ts +2 -0
  144. package/frontend-vue/eslint.config.mjs +0 -1
  145. package/frontend-vue/i18n/locales/database-en-GB.json +31 -1
  146. package/frontend-vue/i18n/locales/database-fr-FR.json +31 -1
  147. package/frontend-vue/package.json +3 -3
  148. package/frontend-vue/pnpm-lock.yaml +14 -11
  149. package/frontend-vue/tests/accessBar.test.ts +196 -0
  150. package/frontend-vue/tests/backend-boundary.test.ts +15 -7
  151. package/frontend-vue/tests/databaseSwitcher.test.ts +339 -0
  152. package/frontend-vue/tests/editRequest.test.ts +146 -0
  153. package/frontend-vue/tests/externalBrowser.test.ts +193 -0
  154. package/frontend-vue/tests/fixtures/connections.ts +73 -0
  155. package/frontend-vue/tests/fixtures/serverRender.ts +135 -12
  156. package/frontend-vue/tests/lockedPages.test.ts +68 -0
  157. package/frontend-vue/tests/onDemandReads.test.ts +1 -1
  158. package/frontend-vue/tests/registration.test.ts +15 -1
  159. package/frontend-vue/tests/serverRender.test.ts +58 -4
  160. package/frontend-vue/tsconfig.json +1 -2
  161. package/frontend-vue/vitest.config.ts +11 -1
  162. package/package.json +20 -16
  163. package/dist/service/schemaRegistry.d.ts +0 -2
  164. package/dist/service/schemaRegistry.js +0 -8
  165. package/dist/service/schemaRegistry.js.map +0 -1
@@ -0,0 +1,339 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2
+ import { defineComponent, h } from 'vue'
3
+ import {
4
+ hasUnsavedChanges,
5
+ registerUnsavedChanges,
6
+ resetUnsavedChanges,
7
+ } from '#dms-ui/app/build/composables/unsaved-changes/registry'
8
+ import { browserMount, menuLocks, serverRender } from './fixtures/serverRender'
9
+ import type { BackendRead } from './fixtures/serverRender'
10
+ import {
11
+ CONNECTIONS_URL,
12
+ OWN,
13
+ WAREHOUSE,
14
+ connectionWrites,
15
+ connectionsBackend,
16
+ connectionsState,
17
+ } from './fixtures/connections'
18
+ import type { DatabaseConnection } from '../app/build/composables/useDatabaseConnections'
19
+ import DatabaseSwitcher from '../app/components/DatabaseSwitcher.vue'
20
+ import {
21
+ DATABASE_SWITCHER_WIDGET,
22
+ registerDatabaseSwitcher,
23
+ } from '../app/build/composables/registerDatabaseSwitcher'
24
+
25
+ const ONBOARDING = Symbol.for('dms-assistant:onboarding')
26
+ const MENU = (props: Record<string, unknown>) =>
27
+ props['data-stub'] === 'UDropdownMenu'
28
+
29
+ interface MenuItem {
30
+ label?: string
31
+ description?: string
32
+ external?: boolean
33
+ checked?: boolean
34
+ onSelect?: () => void
35
+ }
36
+
37
+ /** The menu with the `item-trailing` slot of each item drawn, which the stub of the library leaves out. */
38
+ const MenuStub = defineComponent({
39
+ props: { items: { type: Array, default: () => [] } },
40
+ setup:
41
+ (props, { slots }) =>
42
+ () =>
43
+ h('div', { 'data-stub': 'UDropdownMenu', items: props.items }, [
44
+ slots.default?.(),
45
+ ...props.items
46
+ .flat()
47
+ .map((item) => h('div', {}, slots['item-trailing']?.({ item }))),
48
+ ]),
49
+ })
50
+
51
+ async function mountSwitcher(
52
+ backend = connectionsBackend(connectionsState(OWN)),
53
+ props: Record<string, unknown> = {},
54
+ provide: [symbol, unknown][] = [],
55
+ ) {
56
+ const page = browserMount(DatabaseSwitcher, backend, props, {
57
+ provide,
58
+ components: { UDropdownMenu: MenuStub },
59
+ })
60
+ await page.shown
61
+ await vi.waitFor(() => expect(page.reads).not.toHaveLength(0))
62
+ await new Promise((resolve) => setTimeout(resolve, 0))
63
+ const groups = page.find(MENU)?.items
64
+ const items: MenuItem[] = Array.isArray(groups) ? groups.flat() : []
65
+ return { page, items }
66
+ }
67
+
68
+ type Listener = () => void
69
+
70
+ /** A window and a document that record what the page listens for, and the reloads and navigations it makes. */
71
+ function stubBrowser(visibility: 'visible' | 'hidden' = 'visible') {
72
+ const assign = vi.fn()
73
+ const reload = vi.fn()
74
+ const removeItem = vi.fn()
75
+ const listeners = new Map<string, Listener[]>()
76
+ const target = {
77
+ addEventListener: (type: string, listener: Listener) =>
78
+ listeners.set(type, [...(listeners.get(type) ?? []), listener]),
79
+ removeEventListener: vi.fn(),
80
+ }
81
+ vi.stubGlobal('window', {
82
+ ...target,
83
+ location: { assign, reload },
84
+ sessionStorage: { removeItem },
85
+ })
86
+ vi.stubGlobal('document', { ...target, visibilityState: visibility })
87
+ return {
88
+ assign,
89
+ reload,
90
+ removeItem,
91
+ fire: (type: string) => {
92
+ for (const listener of listeners.get(type) ?? []) listener()
93
+ },
94
+ }
95
+ }
96
+
97
+ let browser = stubBrowser()
98
+
99
+ beforeEach(() => {
100
+ browser = stubBrowser()
101
+ })
102
+
103
+ afterEach(() => {
104
+ vi.stubGlobal('window', undefined)
105
+ vi.stubGlobal('document', undefined)
106
+ resetUnsavedChanges()
107
+ })
108
+
109
+ describe('database switcher menu', () => {
110
+ it('names the project database from the locale, which an implementation showing its API label gets wrong', async () => {
111
+ const { items, page } = await mountSwitcher()
112
+ expect(items.map((item) => item.label)).toEqual([
113
+ 'dms_database.connections.own',
114
+ 'Warehouse',
115
+ ])
116
+ expect(page.text()).not.toContain('legacy-label')
117
+ expect(page.errors).toEqual([])
118
+ })
119
+
120
+ it('gives an external database its engine and the External badge, and the project database neither', async () => {
121
+ const { items, page } = await mountSwitcher()
122
+ expect(items.map((item) => item.description)).toEqual([
123
+ 'mongodb',
124
+ 'postgres',
125
+ ])
126
+ expect(items.map((item) => item.external)).toEqual([false, true])
127
+ const badges = page.text().match(/dms_database\.connections\.external/g)
128
+ expect(badges).toHaveLength(1)
129
+ })
130
+
131
+ it('checks the selected database, which a menu that always checks the first gets wrong', async () => {
132
+ const { items } = await mountSwitcher(
133
+ connectionsBackend(connectionsState(WAREHOUSE)),
134
+ )
135
+ expect(items.map((item) => item.checked)).toEqual([false, true])
136
+ })
137
+
138
+ it('shows only the icon on the collapsed rail, and the name beside it otherwise', async () => {
139
+ const expanded = await mountSwitcher()
140
+ expect(expanded.page.text()).toContain(
141
+ 'label="dms_database.connections.own"',
142
+ )
143
+ const collapsed = await mountSwitcher(undefined, { collapsed: true })
144
+ expect(collapsed.page.text()).not.toContain(
145
+ 'label="dms_database.connections.own"',
146
+ )
147
+ expect(collapsed.page.text()).toContain('aria-label="')
148
+ })
149
+
150
+ it('shows nothing while no database is listed', async () => {
151
+ const { page } = await mountSwitcher(
152
+ connectionsBackend(connectionsState(OWN, { connections: [] })),
153
+ )
154
+ expect(page.text()).not.toContain('data-database-switcher')
155
+ })
156
+ })
157
+
158
+ describe('database switcher: connect a database', () => {
159
+ const CONNECT = 'dms_database.connections.switcher.connect'
160
+
161
+ it('hides the entry when dms-assistant provides nothing', async () => {
162
+ const { items } = await mountSwitcher()
163
+ expect(items.map((item) => item.label)).not.toContain(CONNECT)
164
+ })
165
+
166
+ it('hides the entry while the assistant is not available', async () => {
167
+ const { items } = await mountSwitcher(undefined, {}, [
168
+ [ONBOARDING, { isAvailable: false, open: vi.fn() }],
169
+ ])
170
+ expect(items.map((item) => item.label)).not.toContain(CONNECT)
171
+ })
172
+
173
+ it('opens the connect step of the assistant when the entry is picked', async () => {
174
+ const open = vi.fn()
175
+ const { items } = await mountSwitcher(undefined, {}, [
176
+ [ONBOARDING, { isAvailable: true, open }],
177
+ ])
178
+ const entry = items.find((item) => item.label === CONNECT)
179
+ expect(entry).toBeDefined()
180
+ entry?.onSelect?.()
181
+ expect(open).toHaveBeenCalledExactlyOnceWith('connect-database')
182
+ })
183
+ })
184
+
185
+ describe('database switcher: picking a database', () => {
186
+ it('stores the choice, drops the tabs of the database left, and reads the page again', async () => {
187
+ const { items, page } = await mountSwitcher(
188
+ connectionsBackend(connectionsState(OWN), {
189
+ answer: () => connectionsState(WAREHOUSE),
190
+ }),
191
+ )
192
+ items.find((item) => item.label === 'Warehouse')?.onSelect?.()
193
+ await vi.waitFor(() => expect(browser.assign).toHaveBeenCalledOnce())
194
+ expect(connectionWrites(page.reads)).toEqual([
195
+ {
196
+ method: 'PUT',
197
+ url: '/api/database/connections/selected',
198
+ body: { connection: 'warehouse' },
199
+ },
200
+ ])
201
+ expect(browser.removeItem).toHaveBeenCalledOnce()
202
+ expect(browser.assign.mock.calls[0]?.[0]).toEqual(expect.any(String))
203
+ expect(String(browser.assign.mock.calls[0]?.[0])).not.toContain('?')
204
+ })
205
+
206
+ it('stays on the page when the server refuses the choice', async () => {
207
+ const { items, page } = await mountSwitcher(
208
+ connectionsBackend(connectionsState(OWN), {
209
+ answer: () => {
210
+ throw new Error('refused')
211
+ },
212
+ }),
213
+ )
214
+ items.find((item) => item.label === 'Warehouse')?.onSelect?.()
215
+ await vi.waitFor(() => expect(connectionWrites(page.reads)).toHaveLength(1))
216
+ await Promise.resolve()
217
+ expect(browser.assign).not.toHaveBeenCalled()
218
+ expect(browser.removeItem).not.toHaveBeenCalled()
219
+ })
220
+
221
+ it('does nothing when the selected database is picked again', async () => {
222
+ const { items, page } = await mountSwitcher()
223
+ items
224
+ .find((item) => item.label === 'dms_database.connections.own')
225
+ ?.onSelect?.()
226
+ await Promise.resolve()
227
+ expect(connectionWrites(page.reads)).toEqual([])
228
+ expect(browser.assign).not.toHaveBeenCalled()
229
+ })
230
+
231
+ it('stores nothing and stays when the user declines to lose unsaved edits, which an implementation that stores first and leaves on the browser prompt gets wrong', async () => {
232
+ const prompt = vi.fn(async () => false)
233
+ registerUnsavedChanges({ isDirty: () => true }, prompt)
234
+ const { items, page } = await mountSwitcher(
235
+ connectionsBackend(connectionsState(OWN), {
236
+ answer: () => connectionsState(WAREHOUSE),
237
+ }),
238
+ )
239
+ items.find((item) => item.label === 'Warehouse')?.onSelect?.()
240
+ await vi.waitFor(() => expect(prompt).toHaveBeenCalledOnce())
241
+ await new Promise((resolve) => setTimeout(resolve, 0))
242
+ expect(connectionWrites(page.reads)).toEqual([])
243
+ expect(browser.assign).not.toHaveBeenCalled()
244
+ expect(browser.removeItem).not.toHaveBeenCalled()
245
+ })
246
+
247
+ it('stores the choice and reloads without asking again once the user accepts to lose them', async () => {
248
+ registerUnsavedChanges({ isDirty: () => true }, async () => true)
249
+ const { items, page } = await mountSwitcher(
250
+ connectionsBackend(connectionsState(OWN), {
251
+ answer: () => connectionsState(WAREHOUSE),
252
+ }),
253
+ )
254
+ items.find((item) => item.label === 'Warehouse')?.onSelect?.()
255
+ await vi.waitFor(() => expect(browser.assign).toHaveBeenCalledOnce())
256
+ expect(connectionWrites(page.reads)).toHaveLength(1)
257
+ expect(hasUnsavedChanges()).toBe(false)
258
+ })
259
+ })
260
+
261
+ describe('database switcher: another tab switched the database', () => {
262
+ function serverSelecting(selected: DatabaseConnection) {
263
+ let state = connectionsState(selected)
264
+ return {
265
+ select: (next: DatabaseConnection) => {
266
+ state = connectionsState(next)
267
+ },
268
+ answers: (read: BackendRead) =>
269
+ read.url === CONNECTIONS_URL ? state : {},
270
+ }
271
+ }
272
+
273
+ const mountWithServer = (server: ReturnType<typeof serverSelecting>) =>
274
+ mountSwitcher(server.answers)
275
+
276
+ it('reloads when the window regains focus on another selection, which an implementation that only reads at load gets wrong', async () => {
277
+ const server = serverSelecting(OWN)
278
+ await mountWithServer(server)
279
+ server.select(WAREHOUSE)
280
+ browser.fire('focus')
281
+ await vi.waitFor(() => expect(browser.reload).toHaveBeenCalledOnce())
282
+ expect(browser.removeItem).toHaveBeenCalled()
283
+ })
284
+
285
+ it('reloads when the page becomes visible on another selection', async () => {
286
+ const server = serverSelecting(OWN)
287
+ await mountWithServer(server)
288
+ server.select(WAREHOUSE)
289
+ browser.fire('visibilitychange')
290
+ await vi.waitFor(() => expect(browser.reload).toHaveBeenCalledOnce())
291
+ })
292
+
293
+ it('does not reload when the selection is the one the page loaded with, which an implementation that reloads on every focus gets wrong', async () => {
294
+ const server = serverSelecting(OWN)
295
+ const { page } = await mountWithServer(server)
296
+ const before = page.reads.length
297
+ browser.fire('focus')
298
+ await vi.waitFor(() => expect(page.reads.length).toBeGreaterThan(before))
299
+ await new Promise((resolve) => setTimeout(resolve, 0))
300
+ expect(browser.reload).not.toHaveBeenCalled()
301
+ })
302
+ })
303
+
304
+ describe('database switcher widget', () => {
305
+ it('registers the switcher under the module title, at the top of its sidebar, by the name the module registers it under', () => {
306
+ const register = vi.fn()
307
+ vi.stubGlobal('useSidebarWidgets', () => ({ register }))
308
+ registerDatabaseSwitcher()
309
+ expect(register).toHaveBeenCalledExactlyOnceWith(DATABASE_SWITCHER_WIDGET)
310
+ expect(DATABASE_SWITCHER_WIDGET).toMatchObject({
311
+ module: 'database',
312
+ position: 'below-search-bar',
313
+ component: 'DmsDatabaseDatabaseSwitcher',
314
+ })
315
+ expect(DATABASE_SWITCHER_WIDGET.order).toBeLessThan(0)
316
+ })
317
+ })
318
+
319
+ describe('database switcher: locked pages', () => {
320
+ const lockedIds = () => menuLocks.value.map((entry) => entry.fullId).sort()
321
+
322
+ it('locks the Diagram and Query console entries while an external database is selected, which a switcher leaving the sidebar open gets wrong', async () => {
323
+ await mountSwitcher(connectionsBackend(connectionsState(WAREHOUSE)))
324
+ expect(lockedIds()).toEqual([
325
+ 'page/modules/database/diagram',
326
+ 'page/modules/database/query',
327
+ ])
328
+ expect(menuLocks.value.map((entry) => entry.reason)).toEqual([
329
+ '$dms_database.connections.locked.reason',
330
+ '$dms_database.connections.locked.reason',
331
+ ])
332
+ })
333
+
334
+ it('unlocks them for the project database, which a lock left from the last selection gets wrong', async () => {
335
+ await mountSwitcher(connectionsBackend(connectionsState(WAREHOUSE)))
336
+ await mountSwitcher(connectionsBackend(connectionsState(OWN)))
337
+ expect(lockedIds()).toEqual([])
338
+ })
339
+ })
@@ -0,0 +1,146 @@
1
+ import { describe, expect, it, vi } from 'vitest'
2
+ import { defineComponent, h } from 'vue'
3
+ import {
4
+ answerConfirmations,
5
+ browserMount,
6
+ confirmations,
7
+ type Backend,
8
+ type BackendRead,
9
+ } from './fixtures/serverRender'
10
+ import {
11
+ CONNECTIONS_URL,
12
+ OWN,
13
+ WAREHOUSE,
14
+ connectionsBackend,
15
+ connectionsState,
16
+ } from './fixtures/connections'
17
+ import type { DatabaseConnection } from '../app/build/composables/useDatabaseConnections'
18
+ import type { BrowserTab } from '../app/build/composables/useDataBrowserTabs'
19
+ import { useStagedEdits } from '../app/build/data/stagedEdits'
20
+ import { useStagedSave } from '../app/build/data/useStagedSave'
21
+
22
+ const EDIT_URL = '/api/database/browse/edit'
23
+ const STALE_MESSAGE =
24
+ 'The selected database changed in another tab: reload the page'
25
+ const TAB: BrowserTab = {
26
+ id: 'public::__DEFAULT__::orders',
27
+ schema: 'public',
28
+ instance: '__DEFAULT__',
29
+ table: 'orders',
30
+ }
31
+
32
+ interface ConfirmOptions {
33
+ onConfirm: () => Promise<unknown>
34
+ }
35
+
36
+ function isConfirm(options: unknown): options is ConfirmOptions {
37
+ return (
38
+ typeof options === 'object' &&
39
+ options !== null &&
40
+ 'onConfirm' in options &&
41
+ typeof options.onConfirm === 'function'
42
+ )
43
+ }
44
+
45
+ /** Stages one edit, opens the review of the save, and writes it as the dialog's confirm button does. */
46
+ async function saveAnEdit(backend: Backend) {
47
+ let review: () => Promise<void> = async () => undefined
48
+ const Harness = defineComponent({
49
+ setup() {
50
+ review = useStagedSave({
51
+ tab: () => TAB,
52
+ onWritten: () => undefined,
53
+ onSaved: () => undefined,
54
+ }).review
55
+ useStagedEdits().stage(TAB.id, {
56
+ rowId: '1',
57
+ field: 'total',
58
+ before: 1,
59
+ after: 2,
60
+ })
61
+ return () => h('div')
62
+ },
63
+ })
64
+ const page = browserMount(Harness, backend)
65
+ await page.shown
66
+ await vi.waitFor(() =>
67
+ expect(page.reads.map((read) => read.url)).toContain(CONNECTIONS_URL),
68
+ )
69
+ await new Promise((resolve) => setTimeout(resolve, 0))
70
+ answerConfirmations(true)
71
+ await review()
72
+ const options = confirmations[0]
73
+ if (!isConfirm(options)) throw new Error('The save asked no confirmation')
74
+ await options.onConfirm()
75
+ return page
76
+ }
77
+
78
+ function backendFor(selected: DatabaseConnection, edit: Backend): Backend {
79
+ const connections = connectionsBackend(connectionsState(selected))
80
+ return (read) => (read.url === EDIT_URL ? edit(read) : connections(read))
81
+ }
82
+
83
+ const edits = (reads: BackendRead[]) =>
84
+ reads.filter((read) => read.url === EDIT_URL)
85
+
86
+ describe('the edit request', () => {
87
+ it.each([OWN, WAREHOUSE])(
88
+ 'names the database the page loaded with: $id, in the query the frontend server relays, which an implementation sending none, or a header the server drops, gets wrong',
89
+ async (selected) => {
90
+ const page = await saveAnEdit(backendFor(selected, () => ({ _id: '1' })))
91
+ expect(edits(page.reads)).toHaveLength(1)
92
+ expect(edits(page.reads)[0]?.query).toMatchObject({
93
+ connection: selected.id,
94
+ })
95
+ },
96
+ )
97
+
98
+ it.each([
99
+ ['as text, the way the API sends it', STALE_MESSAGE],
100
+ ['as JSON', { message: STALE_MESSAGE }],
101
+ ])(
102
+ 'presents the refusal of a write from a stale tab, %s, as a translated error, which an implementation showing the generic failure gets wrong',
103
+ async (_shape, data) => {
104
+ const stale = Object.assign(new Error('Conflict'), {
105
+ statusCode: 409,
106
+ data,
107
+ })
108
+ await expect(
109
+ saveAnEdit(
110
+ backendFor(OWN, () => {
111
+ throw stale
112
+ }),
113
+ ),
114
+ ).rejects.toMatchObject({
115
+ statusCode: 409,
116
+ data: { message: 'dms_database.data.save.database_changed' },
117
+ })
118
+ },
119
+ )
120
+
121
+ it.each([
122
+ ['as text, the way the API sends it', 'Access to warehouse is read only'],
123
+ ['as JSON', { message: 'Access to warehouse is read only' }],
124
+ ])(
125
+ 'presents the refusal of a write to a connection back to read-only access, %s, as a translated error naming the connection, which an implementation showing the generic failure gets wrong',
126
+ async (_shape, data) => {
127
+ const refused = Object.assign(new Error('Forbidden'), {
128
+ statusCode: 403,
129
+ data,
130
+ })
131
+ await expect(
132
+ saveAnEdit(
133
+ backendFor(WAREHOUSE, () => {
134
+ throw refused
135
+ }),
136
+ ),
137
+ ).rejects.toMatchObject({
138
+ statusCode: 403,
139
+ data: {
140
+ message: 'dms_database.data.save.read_only_access',
141
+ params: { connection: 'warehouse' },
142
+ },
143
+ })
144
+ },
145
+ )
146
+ })
@@ -0,0 +1,193 @@
1
+ import { describe, expect, it, vi } from 'vitest'
2
+ import { defineComponent, h } from 'vue'
3
+ import { provideReadOnlyReason } from '../app/build/data/readOnlyReason'
4
+ import {
5
+ browserMount,
6
+ drawers,
7
+ type BackendRead,
8
+ } from './fixtures/serverRender'
9
+ import {
10
+ CONNECTIONS_URL,
11
+ OWN,
12
+ WAREHOUSE,
13
+ connectionsBackend,
14
+ connectionsState,
15
+ } from './fixtures/connections'
16
+ import type { DatabaseConnection } from '../app/build/composables/useDatabaseConnections'
17
+ import DataBrowserSidebar from '../app/components/DataBrowserSidebar.vue'
18
+ import DataGrid from '../app/components/DataGrid.vue'
19
+
20
+ const PICKER = 'data-dms-database-instance-picker'
21
+ const TAB = {
22
+ id: 'public::__DEFAULT__::orders',
23
+ schema: 'public',
24
+ instance: '__DEFAULT__',
25
+ table: 'orders',
26
+ }
27
+
28
+ function others({ url }: BackendRead): unknown {
29
+ if (url === '/api/database/browse/list')
30
+ return { results: [{ _id: '1', total: 5 }], total: 1, primaryKey: '_id' }
31
+ if (url === '/api/database/browse/columns')
32
+ return {
33
+ columns: ['_id', 'total'],
34
+ types: { _id: 'string', total: 'number' },
35
+ primaryKey: '_id',
36
+ }
37
+ return { items: [] }
38
+ }
39
+
40
+ async function openFirstRow(connection: DatabaseConnection | null) {
41
+ const page = browserMount(
42
+ DataGrid,
43
+ connection
44
+ ? connectionsBackend(connectionsState(connection), { others })
45
+ : (read) => {
46
+ if (read.url === CONNECTIONS_URL) throw new Error('unreachable')
47
+ return others(read)
48
+ },
49
+ { tab: TAB },
50
+ )
51
+ await page.shown
52
+ const openRow = () =>
53
+ page.find((props) =>
54
+ Boolean(props['aria-label']?.toString().endsWith('open_row')),
55
+ )?.onClick
56
+ await vi.waitFor(() => expect(openRow()).toBeTypeOf('function'))
57
+ await vi.waitFor(() =>
58
+ expect(page.reads.map((read) => read.url)).toContain(CONNECTIONS_URL),
59
+ )
60
+ await new Promise((resolve) => setTimeout(resolve, 0))
61
+ const open = openRow()
62
+ if (typeof open === 'function') open()
63
+ expect(drawers).toHaveLength(1)
64
+ return drawers[0]?.componentOptions
65
+ }
66
+
67
+ describe('data grid on an external database', () => {
68
+ it('opens a row read-only while the access is read, which an implementation that only reads the tab gets wrong', async () => {
69
+ const options = await openFirstRow(WAREHOUSE)
70
+ expect(options?.readOnly).toBe(true)
71
+ })
72
+
73
+ it('opens a row editable once read and write is allowed', async () => {
74
+ const options = await openFirstRow({ ...WAREHOUSE, access: 'read-write' })
75
+ expect(options?.readOnly).toBe(false)
76
+ })
77
+
78
+ it('opens a row read-only when the connections could not be read, which an implementation that falls back to the project database gets wrong', async () => {
79
+ const options = await openFirstRow(null)
80
+ expect(options?.readOnly).toBe(true)
81
+ })
82
+
83
+ it('opens a row of the project database editable', async () => {
84
+ const options = await openFirstRow(OWN)
85
+ expect(options?.readOnly).toBe(false)
86
+ expect(options?.showReferences).toBe(true)
87
+ })
88
+
89
+ it('offers the rows that point at this one only where the connection supports them', async () => {
90
+ expect((await openFirstRow(WAREHOUSE))?.showReferences).toBe(false)
91
+ drawers.length = 0
92
+ const supported = await openFirstRow({ ...WAREHOUSE, unsupported: [] })
93
+ expect(supported?.showReferences).toBe(true)
94
+ })
95
+ })
96
+
97
+ describe('data grid toolbar', () => {
98
+ async function toolbar(connection: DatabaseConnection) {
99
+ const page = browserMount(
100
+ DataGrid,
101
+ connectionsBackend(connectionsState(connection), { others }),
102
+ { tab: TAB },
103
+ )
104
+ await page.shown
105
+ await vi.waitFor(() =>
106
+ expect(page.reads.map((read) => read.url)).toContain(CONNECTIONS_URL),
107
+ )
108
+ await new Promise((resolve) => setTimeout(resolve, 0))
109
+ return page.text()
110
+ }
111
+
112
+ it('links to the query console where it exists', async () => {
113
+ expect(await toolbar(OWN)).toContain('dms_database.data.grid.open_query')
114
+ })
115
+
116
+ it('drops the link on an external database, whose console is locked', async () => {
117
+ expect(await toolbar(WAREHOUSE)).not.toContain(
118
+ 'dms_database.data.grid.open_query',
119
+ )
120
+ })
121
+ })
122
+
123
+ describe('data browser sidebar', () => {
124
+ async function sidebar(connection: DatabaseConnection) {
125
+ const page = browserMount(
126
+ DataBrowserSidebar,
127
+ connectionsBackend(connectionsState(connection)),
128
+ { schemas: [], loading: false, error: false },
129
+ )
130
+ await page.shown
131
+ await vi.waitFor(() =>
132
+ expect(page.reads.map((read) => read.url)).toContain(CONNECTIONS_URL),
133
+ )
134
+ await new Promise((resolve) => setTimeout(resolve, 0))
135
+ return page
136
+ }
137
+
138
+ it('offers the instance picker on the project database', async () => {
139
+ expect((await sidebar(OWN)).text()).toContain(PICKER)
140
+ })
141
+
142
+ it('hides the instance picker on an external database, which has no instances', async () => {
143
+ const page = await sidebar(WAREHOUSE)
144
+ expect(page.text()).not.toContain(PICKER)
145
+ expect(page.text()).not.toContain('dms_database.data.scope.instance')
146
+ })
147
+ })
148
+
149
+ describe('a read-only external database in the data grid', () => {
150
+ const REASON = 'dms_database.data.save.read_only_access Warehouse'
151
+ const Browser = defineComponent({
152
+ setup() {
153
+ provideReadOnlyReason()
154
+ return () => h(DataGrid, { tab: TAB })
155
+ },
156
+ })
157
+
158
+ async function cellTitles(connection: DatabaseConnection) {
159
+ const page = browserMount(
160
+ Browser,
161
+ connectionsBackend(connectionsState(connection), { others }),
162
+ )
163
+ await page.shown
164
+ await vi.waitFor(() =>
165
+ expect(page.reads.map((read) => read.url)).toContain(
166
+ '/api/database/browse/list',
167
+ ),
168
+ )
169
+ await new Promise((resolve) => setTimeout(resolve, 0))
170
+ const cell = page.find(
171
+ (props) =>
172
+ props.class !== undefined &&
173
+ 'title' in props &&
174
+ String(props.class).includes('align-middle'),
175
+ )
176
+ return { title: cell?.title, cursor: String(cell?.class) }
177
+ }
178
+
179
+ it('names why a cell takes no edit where the user tries one, which a grid saying nothing past the access bar gets wrong', async () => {
180
+ const { title, cursor } = await cellTitles(WAREHOUSE)
181
+ expect(title).toBe(REASON)
182
+ expect(cursor).toContain('cursor-not-allowed')
183
+ })
184
+
185
+ it('gives no such reason once read and write is allowed', async () => {
186
+ const { title, cursor } = await cellTitles({
187
+ ...WAREHOUSE,
188
+ access: 'read-write',
189
+ })
190
+ expect(title).toBeUndefined()
191
+ expect(cursor).not.toContain('cursor-not-allowed')
192
+ })
193
+ })