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,225 @@
1
+ /**
2
+ * Databases the user opened from an instance's full list.
3
+ *
4
+ * An instance usually carries more schemas than the project's configuration names
5
+ * — `bw_order` is configured, `bw_inventory` is not. The tree can list every schema
6
+ * on demand, but that list used to live only in the panel's memory: the next open
7
+ * of the workspace forgot it, and the user had to find the database again.
8
+ *
9
+ * This store remembers the databases a user actually opened, per workspace, so the
10
+ * next discovery puts them next to the configured ones. It stores only the instance
11
+ * id and the schema name: the credentials behind the instance are already in the
12
+ * discovery cache, so a remembered database needs no secret of its own.
13
+ */
14
+ import { createHash } from 'node:crypto'
15
+ import { existsSync, mkdirSync, readFileSync, readdirSync, renameSync, writeFileSync } from 'node:fs'
16
+ import { basename, isAbsolute, join } from 'node:path'
17
+
18
+ /** Bump when the document shape changes so old files are ignored. */
19
+ const REMEMBERED_VERSION = 1
20
+
21
+ /** A user who opened more schemas than this has a different problem. */
22
+ const MAX_REMEMBERED = 200
23
+
24
+ /** The shared document: the databases picked anywhere on this machine. */
25
+ const SHARED_FILE = 'databases.json'
26
+
27
+ /** One workspace's file, named so a human can tell them apart. */
28
+ function rememberedFile(directory, root) {
29
+ const hash = createHash('sha256').update(root).digest('hex').slice(0, 16)
30
+ const name = basename(root).replace(/[^\w.-]+/g, '_').slice(0, 40) || 'workspace'
31
+ return join(directory, `${hash}-${name}.databases.json`)
32
+ }
33
+
34
+ /** Coerce one stored entry into `{ instanceId, database }`; undefined when unsuited. */
35
+ function normalizeEntry(value) {
36
+ if (value === null || typeof value !== 'object') return undefined
37
+ const instanceId = String(value.instanceId ?? '').trim()
38
+ const database = String(value.database ?? '').trim()
39
+ if (instanceId === '' || database === '') return undefined
40
+ if (!/^i:[\w-]+$/.test(instanceId)) return undefined
41
+ return { instanceId, database }
42
+ }
43
+
44
+ /** Remembered extra databases, one small JSON file per workspace. */
45
+ export class RememberedStore {
46
+ /**
47
+ * @param directory the `remembered/` storage directory
48
+ * @param options `{ shared: true }` keeps one list for the whole machine; the
49
+ * tree is shared, so a schema picked in one workspace applies everywhere
50
+ */
51
+ constructor(directory, options = {}) {
52
+ this.directory = directory
53
+ this.shared = options.shared === true
54
+ if (this.directory !== '') {
55
+ mkdirSync(this.directory, { recursive: true, mode: 0o700 })
56
+ if (this.shared) this.migrateLegacy()
57
+ }
58
+ }
59
+
60
+ /** The document these entries live in. */
61
+ fileFor(root) {
62
+ if (this.shared) return join(this.directory, SHARED_FILE)
63
+ return rememberedFile(this.directory, root)
64
+ }
65
+
66
+ /** Fold the per-workspace files into the shared one, once. */
67
+ migrateLegacy() {
68
+ const shared = this.fileFor('')
69
+ if (existsSync(shared)) return
70
+ let names = []
71
+ try {
72
+ names = readdirSync(this.directory).filter((name) => name.endsWith('.databases.json'))
73
+ } catch (error) {
74
+ void error
75
+ return
76
+ }
77
+ if (names.length === 0) return
78
+ const databases = []
79
+ for (const name of names) {
80
+ try {
81
+ const document = JSON.parse(readFileSync(join(this.directory, name), 'utf8'))
82
+ for (const raw of Array.isArray(document?.databases) ? document.databases : []) {
83
+ const entry = normalizeEntry(raw)
84
+ if (entry === undefined) continue
85
+ if (databases.some((existing) => existing.instanceId === entry.instanceId && existing.database === entry.database)) continue
86
+ databases.push(entry)
87
+ }
88
+ } catch (error) {
89
+ void error
90
+ }
91
+ }
92
+ this.save('', databases)
93
+ for (const name of names) {
94
+ try {
95
+ renameSync(join(this.directory, name), join(this.directory, `${name}.migrated`))
96
+ } catch (error) {
97
+ void error
98
+ }
99
+ }
100
+ }
101
+
102
+ /** Read the entries; a missing or unreadable file means "none remembered yet". */
103
+ load(root) {
104
+ if (this.directory === '') return { databases: [] }
105
+ if (!this.shared && (typeof root !== 'string' || root === '' || !isAbsolute(root))) return { databases: [] }
106
+ try {
107
+ const parsed = JSON.parse(readFileSync(this.fileFor(root), 'utf8'))
108
+ if (parsed?.version !== REMEMBERED_VERSION) return { databases: [] }
109
+ const databases = []
110
+ for (const raw of Array.isArray(parsed.databases) ? parsed.databases : []) {
111
+ const entry = normalizeEntry(raw)
112
+ if (entry === undefined) continue
113
+ if (databases.some((existing) => existing.instanceId === entry.instanceId && existing.database === entry.database)) continue
114
+ databases.push(entry)
115
+ }
116
+ return { databases: databases.slice(0, MAX_REMEMBERED) }
117
+ } catch (error) {
118
+ void error
119
+ return { databases: [] }
120
+ }
121
+ }
122
+
123
+ /** Replace one workspace's entries atomically, 0600. */
124
+ save(root, databases) {
125
+ if (this.directory === '') {
126
+ const error = new Error('remembered databases are unavailable in this build')
127
+ error.code = 'remembered-unavailable'
128
+ throw error
129
+ }
130
+ if (!this.shared && (typeof root !== 'string' || root === '' || !isAbsolute(root))) {
131
+ const error = new Error('workspace root must be an absolute path')
132
+ error.code = 'invalid-root'
133
+ throw error
134
+ }
135
+ const document = JSON.stringify({
136
+ version: REMEMBERED_VERSION,
137
+ ...(this.shared ? {} : { root }),
138
+ savedAt: Date.now(),
139
+ databases: databases.slice(0, MAX_REMEMBERED),
140
+ })
141
+ const file = this.fileFor(root)
142
+ const temporary = `${file}.${process.pid}.tmp`
143
+ writeFileSync(temporary, document, { mode: 0o600 })
144
+ renameSync(temporary, file)
145
+ return { saved: true, databases: databases.length }
146
+ }
147
+
148
+ /** Remember one database; a duplicate is a no-op. */
149
+ add(root, entry) {
150
+ const normalized = normalizeEntry(entry)
151
+ if (normalized === undefined) return false
152
+ const { databases } = this.load(root)
153
+ if (databases.some((existing) => existing.instanceId === normalized.instanceId && existing.database === normalized.database)) return false
154
+ this.save(root, [...databases, normalized])
155
+ return true
156
+ }
157
+
158
+ /**
159
+ * Replace everything remembered for one instance with this list.
160
+ *
161
+ * The picker edits a set, not individual rows: an unchecked schema must disappear
162
+ * from the tree, which only a replace can express.
163
+ */
164
+ replaceInstance(root, instanceId, databases) {
165
+ const { databases: existing } = this.load(root)
166
+ const kept = existing.filter((entry) => entry.instanceId !== instanceId)
167
+ const seen = new Set()
168
+ const next = [...kept]
169
+ for (const name of Array.isArray(databases) ? databases : []) {
170
+ const database = String(name ?? '').trim()
171
+ if (database === '' || database.length > 64 || database.includes('\u0000')) continue
172
+ if (seen.has(database)) continue
173
+ seen.add(database)
174
+ next.push({ instanceId, database })
175
+ }
176
+ this.save(root, next.slice(0, MAX_REMEMBERED))
177
+ return { databases: [...seen] }
178
+ }
179
+
180
+ /**
181
+ * Move remembered schemas to their instance's new id.
182
+ *
183
+ * Renaming a manual environment changes the derived instance id; without this the
184
+ * user's picked databases would silently disappear from the tree.
185
+ */
186
+ remap(root, mapping) {
187
+ if (!(mapping instanceof Map) || mapping.size === 0) return 0
188
+ const { databases } = this.load(root)
189
+ const seen = new Set()
190
+ const next = []
191
+ let moved = 0
192
+ for (const entry of databases) {
193
+ const target = mapping.get(entry.instanceId) ?? entry.instanceId
194
+ if (target !== entry.instanceId) moved += 1
195
+ const key = `${target}\u0000${entry.database}`
196
+ if (seen.has(key)) continue
197
+ seen.add(key)
198
+ next.push({ instanceId: target, database: entry.database })
199
+ }
200
+ if (moved > 0) this.save(root, next)
201
+ return moved
202
+ }
203
+
204
+ /** Drop every remembered schema of the given instances (a deleted environment). */
205
+ forgetInstances(root, instanceIds) {
206
+ const ids = new Set((Array.isArray(instanceIds) ? instanceIds : []).filter((id) => typeof id === 'string' && id !== ''))
207
+ if (ids.size === 0) return 0
208
+ const { databases } = this.load(root)
209
+ const next = databases.filter((entry) => !ids.has(entry.instanceId))
210
+ const removed = databases.length - next.length
211
+ if (removed > 0) this.save(root, next)
212
+ return removed
213
+ }
214
+
215
+ /** Forget one database; returns whether it was remembered. */
216
+ remove(root, entry) {
217
+ const normalized = normalizeEntry(entry)
218
+ if (normalized === undefined) return false
219
+ const { databases } = this.load(root)
220
+ const next = databases.filter((existing) => !(existing.instanceId === normalized.instanceId && existing.database === normalized.database))
221
+ if (next.length === databases.length) return false
222
+ this.save(root, next)
223
+ return true
224
+ }
225
+ }