@myrialabs/clopen 0.3.7 → 0.3.8
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.
- package/backend/database/migrations/030_add_files_panel_state_to_user_projects.ts +34 -0
- package/backend/database/migrations/031_create_db_client_tables.ts +74 -0
- package/backend/database/migrations/index.ts +14 -0
- package/backend/database/queries/db-client-connection-queries.ts +237 -0
- package/backend/database/queries/db-client-query-history-queries.ts +113 -0
- package/backend/database/queries/index.ts +3 -1
- package/backend/database/queries/project-queries.ts +23 -0
- package/backend/db-client/connection-manager.ts +257 -0
- package/backend/db-client/drivers/bun-sql-helpers.ts +41 -0
- package/backend/db-client/drivers/mongodb.ts +358 -0
- package/backend/db-client/drivers/mysql.ts +385 -0
- package/backend/db-client/drivers/postgres.ts +446 -0
- package/backend/db-client/drivers/redis.ts +267 -0
- package/backend/db-client/drivers/sql-builders.ts +215 -0
- package/backend/db-client/drivers/sqlite.ts +346 -0
- package/backend/db-client/drivers/types.ts +92 -0
- package/backend/db-client/query-executor.ts +126 -0
- package/backend/db-client/ssh-tunnel.ts +122 -0
- package/backend/git/git-service.ts +12 -0
- package/backend/preview/browser/scripts/video-stream.ts +9 -3
- package/backend/ws/db-client/connections.ts +180 -0
- package/backend/ws/db-client/index.ts +17 -0
- package/backend/ws/db-client/io.ts +458 -0
- package/backend/ws/db-client/query.ts +162 -0
- package/backend/ws/db-client/schema.ts +76 -0
- package/backend/ws/db-client/structure.ts +218 -0
- package/backend/ws/files/index.ts +5 -1
- package/backend/ws/files/read.ts +32 -0
- package/backend/ws/files/state.ts +39 -0
- package/backend/ws/index.ts +5 -1
- package/dist/assets/{index-C86jwBxR.js → index-DqAGCTFT.js} +2 -2
- package/dist/assets/{main-DhLx8wD1.js → main-DsED0rLr.js} +216 -197
- package/dist/assets/main-eWY9R8g9.css +1 -0
- package/dist/index.html +2 -2
- package/package.json +4 -1
- package/shared/types/database/schema.ts +48 -1
- package/shared/types/db-client/connection.ts +60 -0
- package/shared/types/db-client/index.ts +6 -0
- package/shared/types/db-client/query.ts +77 -0
- package/shared/utils/logger.ts +1 -0
- package/dist/assets/main-jTnmAoXS.css +0 -1
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MySQL adapter — Bun.sql (mysql://) per PHASE 0 findings.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { SQL } from 'bun';
|
|
6
|
+
import type {
|
|
7
|
+
DbClientConnection,
|
|
8
|
+
DbClientHealth,
|
|
9
|
+
DbClientObjectColumn,
|
|
10
|
+
DbClientObjectDetails,
|
|
11
|
+
DbClientObjectForeignKey,
|
|
12
|
+
DbClientObjectIndex,
|
|
13
|
+
DbClientQueryResult,
|
|
14
|
+
DbClientSchemaNode,
|
|
15
|
+
DbClientSchemaNodeType
|
|
16
|
+
} from '$shared/types/db-client';
|
|
17
|
+
import type {
|
|
18
|
+
AlterOperation,
|
|
19
|
+
DbClientDriverAdapter,
|
|
20
|
+
IndexDefinition,
|
|
21
|
+
SchemaOpts,
|
|
22
|
+
TableDefinition
|
|
23
|
+
} from './types';
|
|
24
|
+
import { normalizeBunSqlResult } from './bun-sql-helpers';
|
|
25
|
+
import {
|
|
26
|
+
assertSafeIdentifier,
|
|
27
|
+
buildDelete,
|
|
28
|
+
buildInsert,
|
|
29
|
+
buildUpdate,
|
|
30
|
+
qualified,
|
|
31
|
+
quoteMysql,
|
|
32
|
+
renderColumn,
|
|
33
|
+
renderCreateIndex,
|
|
34
|
+
renderCreateTable
|
|
35
|
+
} from './sql-builders';
|
|
36
|
+
import { debug } from '$shared/utils/logger';
|
|
37
|
+
|
|
38
|
+
const Q = quoteMysql;
|
|
39
|
+
|
|
40
|
+
export class MysqlAdapter implements DbClientDriverAdapter {
|
|
41
|
+
readonly kind = 'mysql' as const;
|
|
42
|
+
|
|
43
|
+
private sql: SQL | null = null;
|
|
44
|
+
private alive = false;
|
|
45
|
+
private defaultDb: string | null = null;
|
|
46
|
+
|
|
47
|
+
async connect(conn: DbClientConnection, tunnelPort?: number): Promise<void> {
|
|
48
|
+
const host = tunnelPort ? '127.0.0.1' : (conn.host ?? '127.0.0.1');
|
|
49
|
+
const port = tunnelPort ?? conn.port ?? 3306;
|
|
50
|
+
const user = encodeURIComponent(conn.username ?? '');
|
|
51
|
+
const pass = conn.password ? `:${encodeURIComponent(conn.password)}` : '';
|
|
52
|
+
const auth = user ? `${user}${pass}@` : '';
|
|
53
|
+
const db = conn.database ? `/${encodeURIComponent(conn.database)}` : '';
|
|
54
|
+
|
|
55
|
+
const url = `mysql://${auth}${host}:${port}${db}`;
|
|
56
|
+
this.sql = new SQL(url);
|
|
57
|
+
await this.sql.connect();
|
|
58
|
+
this.defaultDb = conn.database || null;
|
|
59
|
+
this.alive = true;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async close(): Promise<void> {
|
|
63
|
+
this.alive = false;
|
|
64
|
+
if (!this.sql) return;
|
|
65
|
+
try {
|
|
66
|
+
await this.sql.close();
|
|
67
|
+
} catch (err) {
|
|
68
|
+
debug.warn('db-client', 'MySQL close error:', err);
|
|
69
|
+
}
|
|
70
|
+
this.sql = null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
isAlive(): boolean {
|
|
74
|
+
return this.alive && this.sql !== null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async health(): Promise<DbClientHealth> {
|
|
78
|
+
if (!this.sql) return notConnected();
|
|
79
|
+
const start = performance.now();
|
|
80
|
+
try {
|
|
81
|
+
const rows = (await this.sql.unsafe('SELECT VERSION() AS version')) as Array<{ version: string }>;
|
|
82
|
+
return {
|
|
83
|
+
ok: true,
|
|
84
|
+
latencyMs: Math.round(performance.now() - start),
|
|
85
|
+
serverVersion: rows[0]?.version ?? null,
|
|
86
|
+
sshOk: null,
|
|
87
|
+
error: null
|
|
88
|
+
};
|
|
89
|
+
} catch (err) {
|
|
90
|
+
return {
|
|
91
|
+
ok: false,
|
|
92
|
+
latencyMs: null,
|
|
93
|
+
serverVersion: null,
|
|
94
|
+
sshOk: null,
|
|
95
|
+
error: err instanceof Error ? err.message : String(err)
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
private requireSql(): SQL {
|
|
101
|
+
if (!this.sql) throw new Error('MySQL not connected');
|
|
102
|
+
return this.sql;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
private targetDb(opts?: SchemaOpts): string | undefined {
|
|
106
|
+
return opts?.database || this.defaultDb || undefined;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
private async ensureDatabase(database?: string): Promise<void> {
|
|
110
|
+
if (!database) return;
|
|
111
|
+
if (database === this.defaultDb) return;
|
|
112
|
+
const sql = this.requireSql();
|
|
113
|
+
assertSafeIdentifier(database);
|
|
114
|
+
await sql.unsafe(`USE ${quoteMysql(database)}`);
|
|
115
|
+
this.defaultDb = database;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async executeRead(q: string, params: unknown[] = [], opts?: { database?: string }): Promise<DbClientQueryResult> {
|
|
119
|
+
await this.ensureDatabase(opts?.database);
|
|
120
|
+
const sql = this.requireSql();
|
|
121
|
+
const start = performance.now();
|
|
122
|
+
const raw = await sql.unsafe(q, params as never);
|
|
123
|
+
return normalizeBunSqlResult(raw, Math.round(performance.now() - start));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async executeWrite(q: string, params: unknown[] = [], opts?: { database?: string }): Promise<DbClientQueryResult> {
|
|
127
|
+
return this.executeRead(q, params, opts);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async explain(q: string, opts?: { database?: string }): Promise<DbClientQueryResult> {
|
|
131
|
+
return this.executeRead(`EXPLAIN ${q}`, [], opts);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// ── Schema ────────────────────────────────────────────────────────────
|
|
135
|
+
|
|
136
|
+
async listDatabases(): Promise<DbClientSchemaNode[]> {
|
|
137
|
+
const rows = (await this.requireSql().unsafe('SHOW DATABASES')) as Array<Record<string, string>>;
|
|
138
|
+
return rows.map((r) => {
|
|
139
|
+
const name = Object.values(r)[0];
|
|
140
|
+
return { name, type: 'database' as const };
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async listObjects(database?: string): Promise<DbClientSchemaNode[]> {
|
|
145
|
+
const target = this.targetDb({ database });
|
|
146
|
+
if (!target) throw new Error('MySQL: database is required');
|
|
147
|
+
const rows = (await this.requireSql().unsafe(
|
|
148
|
+
'SELECT TABLE_NAME, TABLE_TYPE FROM information_schema.tables WHERE TABLE_SCHEMA = ? ORDER BY TABLE_NAME',
|
|
149
|
+
[target] as never
|
|
150
|
+
)) as Array<{ TABLE_NAME: string; TABLE_TYPE: string }>;
|
|
151
|
+
return rows.map((r) => ({
|
|
152
|
+
name: r.TABLE_NAME,
|
|
153
|
+
type: r.TABLE_TYPE === 'VIEW' ? 'view' as const : 'table' as const
|
|
154
|
+
}));
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async getObjectDetails(
|
|
158
|
+
name: string,
|
|
159
|
+
_type: DbClientSchemaNodeType,
|
|
160
|
+
database?: string
|
|
161
|
+
): Promise<DbClientObjectDetails> {
|
|
162
|
+
const target = this.targetDb({ database });
|
|
163
|
+
if (!target) throw new Error('MySQL: database is required');
|
|
164
|
+
const sql = this.requireSql();
|
|
165
|
+
|
|
166
|
+
const colRows = (await sql.unsafe(
|
|
167
|
+
`SELECT COLUMN_NAME, DATA_TYPE, COLUMN_TYPE, IS_NULLABLE, COLUMN_DEFAULT, COLUMN_KEY, EXTRA
|
|
168
|
+
FROM information_schema.columns
|
|
169
|
+
WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?
|
|
170
|
+
ORDER BY ORDINAL_POSITION`,
|
|
171
|
+
[target, name] as never
|
|
172
|
+
)) as Array<Record<string, unknown>>;
|
|
173
|
+
|
|
174
|
+
const columns: DbClientObjectColumn[] = colRows.map((r) => ({
|
|
175
|
+
name: String(r.COLUMN_NAME),
|
|
176
|
+
type: String(r.COLUMN_TYPE ?? r.DATA_TYPE ?? ''),
|
|
177
|
+
nullable: r.IS_NULLABLE === 'YES',
|
|
178
|
+
default: r.COLUMN_DEFAULT === null ? null : String(r.COLUMN_DEFAULT),
|
|
179
|
+
isPrimary: r.COLUMN_KEY === 'PRI',
|
|
180
|
+
isUnique: r.COLUMN_KEY === 'UNI'
|
|
181
|
+
}));
|
|
182
|
+
|
|
183
|
+
const idxRows = (await sql.unsafe(
|
|
184
|
+
`SHOW INDEXES FROM ${Q(name)} FROM ${Q(target)}`
|
|
185
|
+
)) as Array<Record<string, unknown>>;
|
|
186
|
+
const indexMap = new Map<string, DbClientObjectIndex>();
|
|
187
|
+
for (const r of idxRows) {
|
|
188
|
+
const key = String(r.Key_name);
|
|
189
|
+
const existing = indexMap.get(key);
|
|
190
|
+
if (existing) {
|
|
191
|
+
existing.columns.push(String(r.Column_name));
|
|
192
|
+
} else {
|
|
193
|
+
indexMap.set(key, {
|
|
194
|
+
name: key,
|
|
195
|
+
columns: [String(r.Column_name)],
|
|
196
|
+
unique: r.Non_unique === 0 || r.Non_unique === '0',
|
|
197
|
+
type: typeof r.Index_type === 'string' ? r.Index_type : undefined
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const fkRows = (await sql.unsafe(
|
|
203
|
+
`SELECT COLUMN_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME
|
|
204
|
+
FROM information_schema.key_column_usage
|
|
205
|
+
WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? AND REFERENCED_TABLE_NAME IS NOT NULL`,
|
|
206
|
+
[target, name] as never
|
|
207
|
+
)) as Array<Record<string, unknown>>;
|
|
208
|
+
|
|
209
|
+
const foreignKeys: DbClientObjectForeignKey[] = fkRows.map((r) => ({
|
|
210
|
+
column: String(r.COLUMN_NAME),
|
|
211
|
+
refTable: String(r.REFERENCED_TABLE_NAME),
|
|
212
|
+
refColumn: String(r.REFERENCED_COLUMN_NAME)
|
|
213
|
+
}));
|
|
214
|
+
|
|
215
|
+
return {
|
|
216
|
+
name,
|
|
217
|
+
type: 'table',
|
|
218
|
+
columns,
|
|
219
|
+
indexes: [...indexMap.values()],
|
|
220
|
+
foreignKeys
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// ── Structure ─────────────────────────────────────────────────────────
|
|
225
|
+
|
|
226
|
+
async createDatabase(name: string): Promise<string> {
|
|
227
|
+
assertSafeIdentifier(name);
|
|
228
|
+
const ddl = `CREATE DATABASE ${Q(name)}`;
|
|
229
|
+
await this.requireSql().unsafe(ddl);
|
|
230
|
+
return ddl;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
async createTable(definition: TableDefinition, opts?: SchemaOpts): Promise<string> {
|
|
234
|
+
assertSafeIdentifier(definition.name);
|
|
235
|
+
const ddl = renderCreateTable({
|
|
236
|
+
quote: Q,
|
|
237
|
+
definition,
|
|
238
|
+
database: this.targetDb(opts),
|
|
239
|
+
driver: 'mysql'
|
|
240
|
+
});
|
|
241
|
+
await this.requireSql().unsafe(ddl);
|
|
242
|
+
return ddl;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
async alterTable(name: string, operations: AlterOperation[], opts?: SchemaOpts): Promise<string> {
|
|
246
|
+
assertSafeIdentifier(name);
|
|
247
|
+
const fqt = qualified(Q, [this.targetDb(opts), name]);
|
|
248
|
+
const parts: string[] = [];
|
|
249
|
+
for (const op of operations) {
|
|
250
|
+
switch (op.kind) {
|
|
251
|
+
case 'add-column':
|
|
252
|
+
assertSafeIdentifier(op.column.name);
|
|
253
|
+
parts.push(`ADD COLUMN ${renderColumn({ quote: Q, column: op.column, driver: 'mysql' })}`);
|
|
254
|
+
break;
|
|
255
|
+
case 'drop-column':
|
|
256
|
+
assertSafeIdentifier(op.name);
|
|
257
|
+
parts.push(`DROP COLUMN ${Q(op.name)}`);
|
|
258
|
+
break;
|
|
259
|
+
case 'rename-column':
|
|
260
|
+
assertSafeIdentifier(op.name);
|
|
261
|
+
assertSafeIdentifier(op.newName);
|
|
262
|
+
parts.push(`RENAME COLUMN ${Q(op.name)} TO ${Q(op.newName)}`);
|
|
263
|
+
break;
|
|
264
|
+
case 'modify-column':
|
|
265
|
+
assertSafeIdentifier(op.column.name);
|
|
266
|
+
parts.push(`MODIFY COLUMN ${renderColumn({ quote: Q, column: op.column, driver: 'mysql' })}`);
|
|
267
|
+
break;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
const ddl = `ALTER TABLE ${fqt} ${parts.join(', ')}`;
|
|
271
|
+
await this.requireSql().unsafe(ddl);
|
|
272
|
+
return ddl;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
async dropTable(name: string, opts?: SchemaOpts): Promise<string> {
|
|
276
|
+
assertSafeIdentifier(name);
|
|
277
|
+
const ddl = `DROP TABLE ${qualified(Q, [this.targetDb(opts), name])}`;
|
|
278
|
+
await this.requireSql().unsafe(ddl);
|
|
279
|
+
return ddl;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
async truncateTable(name: string, opts?: SchemaOpts): Promise<string> {
|
|
283
|
+
assertSafeIdentifier(name);
|
|
284
|
+
const ddl = `TRUNCATE TABLE ${qualified(Q, [this.targetDb(opts), name])}`;
|
|
285
|
+
await this.requireSql().unsafe(ddl);
|
|
286
|
+
return ddl;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
async renameTable(name: string, newName: string, opts?: SchemaOpts): Promise<string> {
|
|
290
|
+
assertSafeIdentifier(name);
|
|
291
|
+
assertSafeIdentifier(newName);
|
|
292
|
+
const db = this.targetDb(opts);
|
|
293
|
+
const ddl = `RENAME TABLE ${qualified(Q, [db, name])} TO ${qualified(Q, [db, newName])}`;
|
|
294
|
+
await this.requireSql().unsafe(ddl);
|
|
295
|
+
return ddl;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
async createIndex(tableName: string, def: IndexDefinition, opts?: SchemaOpts): Promise<string> {
|
|
299
|
+
assertSafeIdentifier(tableName);
|
|
300
|
+
assertSafeIdentifier(def.name);
|
|
301
|
+
def.columns.forEach(assertSafeIdentifier);
|
|
302
|
+
const ddl = renderCreateIndex({ quote: Q, tableName, def, database: this.targetDb(opts) });
|
|
303
|
+
await this.requireSql().unsafe(ddl);
|
|
304
|
+
return ddl;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
async dropIndex(tableName: string, indexName: string, opts?: SchemaOpts): Promise<string> {
|
|
308
|
+
assertSafeIdentifier(tableName);
|
|
309
|
+
assertSafeIdentifier(indexName);
|
|
310
|
+
const ddl = `DROP INDEX ${Q(indexName)} ON ${qualified(Q, [this.targetDb(opts), tableName])}`;
|
|
311
|
+
await this.requireSql().unsafe(ddl);
|
|
312
|
+
return ddl;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
async createView(name: string, query: string, opts?: SchemaOpts): Promise<string> {
|
|
316
|
+
assertSafeIdentifier(name);
|
|
317
|
+
const ddl = `CREATE VIEW ${qualified(Q, [this.targetDb(opts), name])} AS ${query}`;
|
|
318
|
+
await this.requireSql().unsafe(ddl);
|
|
319
|
+
return ddl;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
async dropView(name: string, opts?: SchemaOpts): Promise<string> {
|
|
323
|
+
assertSafeIdentifier(name);
|
|
324
|
+
const ddl = `DROP VIEW ${qualified(Q, [this.targetDb(opts), name])}`;
|
|
325
|
+
await this.requireSql().unsafe(ddl);
|
|
326
|
+
return ddl;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// ── Data CRUD ─────────────────────────────────────────────────────────
|
|
330
|
+
|
|
331
|
+
async insertRow(table: string, row: Record<string, unknown>, opts?: SchemaOpts): Promise<DbClientQueryResult> {
|
|
332
|
+
assertSafeIdentifier(table);
|
|
333
|
+
Object.keys(row).forEach(assertSafeIdentifier);
|
|
334
|
+
const { sql, params } = buildInsert({
|
|
335
|
+
quote: Q,
|
|
336
|
+
table,
|
|
337
|
+
row,
|
|
338
|
+
database: this.targetDb(opts),
|
|
339
|
+
placeholder: () => '?'
|
|
340
|
+
});
|
|
341
|
+
return this.executeWrite(sql, params);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
async updateRow(
|
|
345
|
+
table: string,
|
|
346
|
+
pk: Record<string, unknown>,
|
|
347
|
+
changes: Record<string, unknown>,
|
|
348
|
+
opts?: SchemaOpts
|
|
349
|
+
): Promise<DbClientQueryResult> {
|
|
350
|
+
assertSafeIdentifier(table);
|
|
351
|
+
[...Object.keys(pk), ...Object.keys(changes)].forEach(assertSafeIdentifier);
|
|
352
|
+
const { sql, params } = buildUpdate({
|
|
353
|
+
quote: Q,
|
|
354
|
+
table,
|
|
355
|
+
pk,
|
|
356
|
+
changes,
|
|
357
|
+
database: this.targetDb(opts),
|
|
358
|
+
placeholder: () => '?'
|
|
359
|
+
});
|
|
360
|
+
return this.executeWrite(sql, params);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
async deleteRows(table: string, pks: Record<string, unknown>[], opts?: SchemaOpts): Promise<DbClientQueryResult> {
|
|
364
|
+
assertSafeIdentifier(table);
|
|
365
|
+
if (pks.length > 0) Object.keys(pks[0]).forEach(assertSafeIdentifier);
|
|
366
|
+
const { sql, params } = buildDelete({
|
|
367
|
+
quote: Q,
|
|
368
|
+
table,
|
|
369
|
+
pks,
|
|
370
|
+
database: this.targetDb(opts),
|
|
371
|
+
placeholder: () => '?'
|
|
372
|
+
});
|
|
373
|
+
return this.executeWrite(sql, params);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
function notConnected(): DbClientHealth {
|
|
378
|
+
return {
|
|
379
|
+
ok: false,
|
|
380
|
+
latencyMs: null,
|
|
381
|
+
serverVersion: null,
|
|
382
|
+
sshOk: null,
|
|
383
|
+
error: 'Not connected'
|
|
384
|
+
};
|
|
385
|
+
}
|