dsh-data-window 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/AGENTS.md +111 -0
  2. package/CHANGELOG.md +239 -0
  3. package/LICENSE +21 -0
  4. package/README.md +146 -2
  5. package/client.js +7779 -0
  6. package/cordis.patch.yml +4 -0
  7. package/docs/BRIDGE.md +100 -0
  8. package/docs/DESIGN.md +417 -0
  9. package/docs/DETAIL.md +220 -0
  10. package/docs/screenshots/ddl.png +0 -0
  11. package/docs/screenshots/workbench.png +0 -0
  12. package/icon.svg +30 -0
  13. package/index.js +180 -0
  14. package/locale/en.json +286 -0
  15. package/locale/zh.json +286 -0
  16. package/package.json +68 -4
  17. package/src/client/00-css.js +993 -0
  18. package/src/client/05-icons.js +137 -0
  19. package/src/client/05-theme.js +107 -0
  20. package/src/client/06-shortcuts.js +89 -0
  21. package/src/client/07-bridge.js +193 -0
  22. package/src/client/10-core.js +1352 -0
  23. package/src/client/20-tree.js +281 -0
  24. package/src/client/25-sql.js +631 -0
  25. package/src/client/30-editor.js +537 -0
  26. package/src/client/40-results.js +556 -0
  27. package/src/client/45-export.js +219 -0
  28. package/src/client/50-panel.js +761 -0
  29. package/src/client/60-settings.js +180 -0
  30. package/src/client/63-schema.js +186 -0
  31. package/src/client/64-manual.js +350 -0
  32. package/src/client/65-ai.js +320 -0
  33. package/src/client/66-optimize.js +98 -0
  34. package/src/client/90-apply.js +183 -0
  35. package/src/host/ai.js +721 -0
  36. package/src/host/cache.js +157 -0
  37. package/src/host/catalog.js +161 -0
  38. package/src/host/consoles.js +116 -0
  39. package/src/host/discovery.js +603 -0
  40. package/src/host/manual.js +276 -0
  41. package/src/host/mysql.js +650 -0
  42. package/src/host/remembered.js +225 -0
  43. package/src/host/rpc.js +487 -0
  44. package/src/host/shared.js +172 -0
  45. package/src/host/yaml.js +335 -0
@@ -0,0 +1,281 @@
1
+ /**
2
+ * The database tree.
3
+ *
4
+ * Rows are derived from the store on every render: expansion state, lazily loaded
5
+ * children, retry markers and the filter all live in one place, so a node can be
6
+ * revealed by a shortcut without the tree caching its own copy of the list.
7
+ *
8
+ * The filter keeps a node when the node itself or any loaded descendant matches,
9
+ * which is what makes searching for a table name actually find it.
10
+ */
11
+
12
+ /** One nested tree of what the store currently knows, before filtering. */
13
+ function dwBuildTree(state, tr) {
14
+ const loadStateOf = (key) => state.loadState.get(key)
15
+ const childrenOf = (node, depth) => {
16
+ const rows = []
17
+ for (const child of state.children.get(node.key) ?? []) {
18
+ rows.push({ node: child, children: childrenOf(child, depth + 1) })
19
+ }
20
+ if (rows.length === 0 && loadStateOf(node.key) === 'error') {
21
+ rows.push({
22
+ node: { key: `${node.key}:retry`, kind: 'retry', label: 'retry', retryOf: node },
23
+ children: [],
24
+ })
25
+ }
26
+ return rows
27
+ }
28
+
29
+ const environments = []
30
+ for (const environment of state.environments ?? []) {
31
+ const envKey = dwNodeKey('env', environment.env)
32
+ const instanceRows = []
33
+ for (const instance of environment.instances) {
34
+ const instanceKey = dwNodeKey('inst', instance.id)
35
+ const databases = [...instance.databases]
36
+ const databaseRows = databases.map((database) => {
37
+ const databaseNode = {
38
+ key: dwNodeKey('db', database.id),
39
+ kind: 'database',
40
+ label: database.name,
41
+ detail: '',
42
+ dbId: database.id,
43
+ database: database.name,
44
+ env: environment.env,
45
+ instanceLabel: instance.label,
46
+ configured: database.discovered !== true,
47
+ remembered: database.remembered === true,
48
+ }
49
+ return { node: databaseNode, children: childrenOf(databaseNode, 3) }
50
+ })
51
+ instanceRows.push({
52
+ node: {
53
+ key: instanceKey,
54
+ kind: 'instance',
55
+ // The alias is the name the project's own configuration uses
56
+ // (`mysql-main`), so it is the one a user recognises; the hostname is
57
+ // kept as the detail because it is what actually identifies the machine.
58
+ label: instance.aliases?.[0] ?? instance.host,
59
+ detail: `${instance.databases.length} ${tr('envDatabases')}`,
60
+ title: [
61
+ instance.host + ':' + instance.port,
62
+ instance.user,
63
+ ...(instance.aliases ?? []),
64
+ ].join(' · '),
65
+ id: instance.id,
66
+ env: environment.env,
67
+ user: instance.user,
68
+ databases: instance.databases,
69
+ aliases: instance.aliases ?? [],
70
+ manual: instance.manual === true,
71
+ entryId: instance.entryId,
72
+ },
73
+ children: databaseRows,
74
+ })
75
+ }
76
+ environments.push({
77
+ node: {
78
+ key: envKey,
79
+ kind: 'env',
80
+ label: environment.label,
81
+ detail: `${environment.instances.length} ${tr('envInstances')} · ${environment.databaseCount} ${tr('envDatabases')}`,
82
+ env: environment.env,
83
+ instanceCount: environment.instances.length,
84
+ databaseCount: environment.databaseCount,
85
+ manual: environment.manual === true,
86
+ },
87
+ children: instanceRows,
88
+ })
89
+ }
90
+ return environments
91
+ }
92
+
93
+ /** Prune a tree to the nodes that match, keeping their ancestors. */
94
+ function dwPruneTree(tree, filter) {
95
+ if (filter === '') return tree
96
+ const keep = (row) => {
97
+ const label = String(row.node.label ?? '').toLowerCase()
98
+ const matched = label.includes(filter)
99
+ const children = dwPruneTree(row.children, filter)
100
+ if (!matched && children.length === 0) return undefined
101
+ return { node: row.node, children, matched }
102
+ }
103
+ const result = []
104
+ for (const row of tree) {
105
+ const kept = keep(row)
106
+ if (kept !== undefined) result.push(kept)
107
+ }
108
+ return result
109
+ }
110
+
111
+ /** Flatten a pruned tree into the visible rows, with depth and open state. */
112
+ function dwFlattenTree(tree, state, filter, depth = 0, into = []) {
113
+ for (const row of tree) {
114
+ const node = row.node
115
+ const expandable = node.kind !== 'column' && node.kind !== 'retry'
116
+ const open = state.expanded.has(node.key) || (filter !== '' && row.children.length > 0)
117
+ into.push({
118
+ node,
119
+ depth,
120
+ expandable,
121
+ open,
122
+ loading: state.loadState.get(node.key) === 'loading',
123
+ loadingError: state.loadState.get(node.key) === 'error',
124
+ })
125
+ if (row.children.length > 0 && open) dwFlattenTree(row.children, state, filter, depth + 1, into)
126
+ }
127
+ return into
128
+ }
129
+
130
+ /** One row per visible node, in display order. */
131
+ function dwVisibleRows(state, tr) {
132
+ const filter = String(state.filter ?? '').trim().toLowerCase()
133
+ const tree = dwPruneTree(dwBuildTree(state, tr), filter)
134
+ return dwFlattenTree(tree, state, filter)
135
+ }
136
+
137
+ /** The icon of one node. */
138
+ function dwNodeIconName(node) {
139
+ switch (node.kind) {
140
+ case 'env': return 'env'
141
+ case 'instance': return 'instance'
142
+ case 'database': return 'database'
143
+ case 'view': return 'view'
144
+ case 'table': return 'table'
145
+ case 'column': return node.column?.key === 'PRI' ? 'key' : 'column'
146
+ case 'retry': return 'refresh'
147
+ case 'error': return 'close'
148
+ default: return 'panel'
149
+ }
150
+ }
151
+
152
+ function DwTree({ store, state, tr, onContextMenu, onChooseDatabases }) {
153
+ const rows = useMemo(
154
+ () => dwVisibleRows(state, tr),
155
+ [state.environments, state.expanded, state.children, state.loadState, state.filter],
156
+ )
157
+
158
+ const activate = (row) => {
159
+ const node = row.node
160
+ if (node.kind === 'retry') {
161
+ store.retryNode(node.retryOf)
162
+ return
163
+ }
164
+ store.select(node)
165
+ if (node.kind === 'database') {
166
+ store.openDatabase(node)
167
+ // Opening the workbench also reveals its tables: that is the list the user
168
+ // came for, and one click is better than two.
169
+ if (row.open !== true) void store.toggleNode(node)
170
+ return
171
+ }
172
+ if (node.kind === 'instance' && (node.databases ?? []).length === 0) {
173
+ onChooseDatabases(node)
174
+ return
175
+ }
176
+ if (row.expandable === true) void store.toggleNode(node)
177
+ }
178
+
179
+ const openTableData = (row) => {
180
+ const node = row.node
181
+ if (node.kind !== 'table' && node.kind !== 'view') return
182
+ const statement = `SELECT *\nFROM \`${node.database}\`.\`${node.table}\`\nLIMIT 100`
183
+ store.seedStatement(node, statement)
184
+ // Run only the statement just appended: the tab may already hold earlier
185
+ // statements, and sending the whole buffer would be a multi-statement error.
186
+ void store.runActiveTab(statement)
187
+ }
188
+
189
+ if (rows.length === 0) {
190
+ const empty = state.phase === 'loading'
191
+ ? tr('treeLoading')
192
+ : String(state.filter ?? '') !== ''
193
+ ? tr('treeNoMatch')
194
+ : tr('treeEmpty')
195
+ return h('div', { className: 'dw-tree' }, h('div', { className: 'dw-empty', style: { padding: 18 } }, [
196
+ h('div', { key: 't', className: 'dw-empty-hint' }, empty),
197
+ String(state.filter ?? '') !== ''
198
+ ? h('button', {
199
+ key: 'c',
200
+ type: 'button',
201
+ className: 'dw-btn',
202
+ onClick: () => store.patch({ filter: '' }),
203
+ }, tr('clearFilter'))
204
+ : null,
205
+ ]))
206
+ }
207
+
208
+ return h('div', { className: 'dw-tree' }, rows.map((row) => {
209
+ const node = row.node
210
+ const selected = state.selected === node.key
211
+ const twisty = row.expandable === true
212
+ ? h('span', {
213
+ key: 'tw',
214
+ className: 'dw-node-twisty',
215
+ style: { transform: row.open === true ? 'rotate(90deg)' : 'none', transition: 'transform 110ms ease', cursor: 'pointer' },
216
+ onClick: (event) => {
217
+ // The twisty is the only control that collapses a node: clicking the row
218
+ // of a database opens its workbench and never closes the list.
219
+ event.stopPropagation()
220
+ store.select(node)
221
+ void store.toggleNode(node)
222
+ },
223
+ }, row.loading === true ? h(DwIcon, { name: 'spinner', size: 11 }) : h(DwIcon, { name: 'chevron', size: 11 }))
224
+ : h('span', { key: 'tw', className: 'dw-node-twisty dw-node-twisty-empty' })
225
+
226
+ const detail = node.kind === 'retry' ? '' : node.detail
227
+
228
+ return h('div', {
229
+ key: node.key,
230
+ className: [
231
+ 'dw-node',
232
+ `dw-node-${node.kind}`,
233
+ selected ? 'dw-node-selected' : '',
234
+ row.loadingError === true ? 'dw-node-error' : '',
235
+ ].filter(Boolean).join(' '),
236
+ style: { paddingLeft: 6 + row.depth * 13 },
237
+ title: node.kind === 'column'
238
+ ? [
239
+ node.label,
240
+ node.detail ?? '',
241
+ node.column?.nullable === false ? 'NOT NULL' : 'NULL',
242
+ node.column?.key === 'PRI' ? 'PRIMARY KEY' : '',
243
+ node.column?.comment ?? '',
244
+ ].filter((part) => part !== '').join(' · ')
245
+ : `${node.title ?? `${node.label}${node.detail ? ` ${node.detail}` : ''}`}`,
246
+ onClick: () => activate(row),
247
+ onDoubleClick: () => (node.kind === 'table' || node.kind === 'view' ? openTableData(row) : undefined),
248
+ onContextMenu: (event) => {
249
+ event.preventDefault()
250
+ store.select(node)
251
+ onContextMenu(event, node)
252
+ },
253
+ }, [
254
+ twisty,
255
+ h('span', { key: 'ic', className: 'dw-node-icon' }, h(DwIcon, { name: dwNodeIconName(node), size: 13 })),
256
+ h('span', { key: 'lb', className: 'dw-node-label' }, node.kind === 'retry'
257
+ ? tr('retry')
258
+ : node.label),
259
+ node.manual === true ? dwManualBadge(tr) : null,
260
+ node.remembered === true ? h('span', { key: 'saved', className: 'dw-node-tag' }, tr('rememberedBadge')) : null,
261
+ node.detail !== '' && node.detail !== undefined && node.kind !== 'retry'
262
+ ? h('span', { key: 'dt', className: 'dw-node-detail' }, detail)
263
+ : null,
264
+ node.kind === 'instance'
265
+ ? h('button', {
266
+ key: 'pick',
267
+ type: 'button',
268
+ className: 'dw-node-more',
269
+ title: tr('chooseDatabases'),
270
+ onClick: (event) => {
271
+ event.stopPropagation()
272
+ store.select(node)
273
+ onChooseDatabases(node)
274
+ },
275
+ }, h(DwIcon, { name: 'grid', size: 12 }))
276
+ : null,
277
+ ])
278
+ }))
279
+ }
280
+
281
+ export { DwTree, dwVisibleRows, dwNodeIconName, dwBuildTree, dwPruneTree }