dsh-data-window 0.0.0-stage → 0.1.2

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 +267 -0
  3. package/LICENSE +21 -0
  4. package/README.md +151 -2
  5. package/client.js +8271 -0
  6. package/cordis.patch.yml +4 -0
  7. package/docs/BRIDGE.md +106 -0
  8. package/docs/DESIGN.md +417 -0
  9. package/docs/DETAIL.md +242 -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 +304 -0
  15. package/locale/zh.json +304 -0
  16. package/package.json +68 -4
  17. package/src/client/00-css.js +1110 -0
  18. package/src/client/05-icons.js +156 -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 +202 -0
  22. package/src/client/10-core.js +1320 -0
  23. package/src/client/20-tree.js +293 -0
  24. package/src/client/25-sql.js +631 -0
  25. package/src/client/30-editor.js +742 -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 +768 -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 +217 -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 +180 -0
  45. package/src/host/yaml.js +335 -0
@@ -0,0 +1,293 @@
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
+ // A row click only selects: expanding belongs to the twisty (and to double-click
159
+ // for the kinds whose whole purpose is to be opened).
160
+ const activate = (row) => {
161
+ const node = row.node
162
+ if (node.kind === 'retry') {
163
+ store.retryNode(node.retryOf)
164
+ return
165
+ }
166
+ store.select(node)
167
+ }
168
+
169
+ const openRow = (row) => {
170
+ const node = row.node
171
+ if (node.kind === 'retry') {
172
+ store.retryNode(node.retryOf)
173
+ return
174
+ }
175
+ store.select(node)
176
+ if (node.kind === 'database') {
177
+ store.openDatabase(node)
178
+ if (row.open !== true) void store.toggleNode(node)
179
+ return
180
+ }
181
+ if (node.kind === 'table' || node.kind === 'view') {
182
+ openTableData(row)
183
+ return
184
+ }
185
+ if (node.kind === 'instance' && (node.databases ?? []).length === 0) {
186
+ onChooseDatabases(node)
187
+ return
188
+ }
189
+ if (row.expandable === true) void store.toggleNode(node)
190
+ }
191
+
192
+ const openTableData = (row) => {
193
+ const node = row.node
194
+ if (node.kind !== 'table' && node.kind !== 'view') return
195
+ const statement = `SELECT *\nFROM \`${node.database}\`.\`${node.table}\`\nLIMIT 100`
196
+ store.seedStatement(node, statement)
197
+ // Run only the statement just appended: the tab may already hold earlier
198
+ // statements, and sending the whole buffer would be a multi-statement error.
199
+ void store.runActiveTab(statement)
200
+ }
201
+
202
+ if (rows.length === 0) {
203
+ const empty = state.phase === 'loading'
204
+ ? tr('treeLoading')
205
+ : String(state.filter ?? '') !== ''
206
+ ? tr('treeNoMatch')
207
+ : tr('treeEmpty')
208
+ return h('div', { className: 'dw-tree' }, h('div', { className: 'dw-empty', style: { padding: 18 } }, [
209
+ h('div', { key: 't', className: 'dw-empty-hint' }, empty),
210
+ String(state.filter ?? '') !== ''
211
+ ? h('button', {
212
+ key: 'c',
213
+ type: 'button',
214
+ className: 'dw-btn',
215
+ onClick: () => store.patch({ filter: '' }),
216
+ }, tr('clearFilter'))
217
+ : null,
218
+ ]))
219
+ }
220
+
221
+ return h('div', { className: 'dw-tree' }, rows.map((row) => {
222
+ const node = row.node
223
+ const selected = state.selected === node.key
224
+ const twisty = row.expandable === true
225
+ ? h('span', {
226
+ key: 'tw',
227
+ className: 'dw-node-twisty',
228
+ style: { transform: row.open === true ? 'rotate(90deg)' : 'none', transition: 'transform 110ms ease', cursor: 'pointer' },
229
+ onClick: (event) => {
230
+ // The twisty is the only control that expands or collapses a node;
231
+ // clicking the row never changes the expansion.
232
+ event.stopPropagation()
233
+ void store.toggleNode(node)
234
+ },
235
+ }, row.loading === true ? h(DwIcon, { name: 'spinner', size: 11 }) : h(DwIcon, { name: 'chevron', size: 11 }))
236
+ : h('span', { key: 'tw', className: 'dw-node-twisty dw-node-twisty-empty' })
237
+
238
+ const detail = node.kind === 'retry' ? '' : node.detail
239
+
240
+ return h('div', {
241
+ key: node.key,
242
+ className: [
243
+ 'dw-node',
244
+ `dw-node-${node.kind}`,
245
+ selected ? 'dw-node-selected' : '',
246
+ row.loadingError === true ? 'dw-node-error' : '',
247
+ ].filter(Boolean).join(' '),
248
+ style: { paddingLeft: 6 + row.depth * 13 },
249
+ title: node.kind === 'column'
250
+ ? [
251
+ node.label,
252
+ node.detail ?? '',
253
+ node.column?.nullable === false ? 'NOT NULL' : 'NULL',
254
+ node.column?.key === 'PRI' ? 'PRIMARY KEY' : '',
255
+ node.column?.comment ?? '',
256
+ ].filter((part) => part !== '').join(' · ')
257
+ : `${node.title ?? `${node.label}${node.detail ? ` ${node.detail}` : ''}`}`,
258
+ onClick: () => activate(row),
259
+ onDoubleClick: () => openRow(row),
260
+ onContextMenu: (event) => {
261
+ event.preventDefault()
262
+ store.select(node)
263
+ onContextMenu(event, node)
264
+ },
265
+ }, [
266
+ twisty,
267
+ h('span', { key: 'ic', className: 'dw-node-icon' }, h(DwIcon, { name: dwNodeIconName(node), size: 13 })),
268
+ h('span', { key: 'lb', className: 'dw-node-label' }, node.kind === 'retry'
269
+ ? tr('retry')
270
+ : node.label),
271
+ node.manual === true ? dwManualBadge(tr) : null,
272
+ node.remembered === true ? h('span', { key: 'saved', className: 'dw-node-tag' }, tr('rememberedBadge')) : null,
273
+ node.detail !== '' && node.detail !== undefined && node.kind !== 'retry'
274
+ ? h('span', { key: 'dt', className: 'dw-node-detail' }, detail)
275
+ : null,
276
+ node.kind === 'instance'
277
+ ? h('button', {
278
+ key: 'pick',
279
+ type: 'button',
280
+ className: 'dw-node-more',
281
+ title: tr('chooseDatabases'),
282
+ onClick: (event) => {
283
+ event.stopPropagation()
284
+ store.select(node)
285
+ onChooseDatabases(node)
286
+ },
287
+ }, h(DwIcon, { name: 'grid', size: 12 }))
288
+ : null,
289
+ ])
290
+ }))
291
+ }
292
+
293
+ export { DwTree, dwVisibleRows, dwNodeIconName, dwBuildTree, dwPruneTree }