@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.
Files changed (41) hide show
  1. package/backend/database/migrations/030_add_files_panel_state_to_user_projects.ts +34 -0
  2. package/backend/database/migrations/031_create_db_client_tables.ts +74 -0
  3. package/backend/database/migrations/index.ts +14 -0
  4. package/backend/database/queries/db-client-connection-queries.ts +237 -0
  5. package/backend/database/queries/db-client-query-history-queries.ts +113 -0
  6. package/backend/database/queries/index.ts +3 -1
  7. package/backend/database/queries/project-queries.ts +23 -0
  8. package/backend/db-client/connection-manager.ts +257 -0
  9. package/backend/db-client/drivers/bun-sql-helpers.ts +41 -0
  10. package/backend/db-client/drivers/mongodb.ts +358 -0
  11. package/backend/db-client/drivers/mysql.ts +385 -0
  12. package/backend/db-client/drivers/postgres.ts +446 -0
  13. package/backend/db-client/drivers/redis.ts +267 -0
  14. package/backend/db-client/drivers/sql-builders.ts +215 -0
  15. package/backend/db-client/drivers/sqlite.ts +346 -0
  16. package/backend/db-client/drivers/types.ts +92 -0
  17. package/backend/db-client/query-executor.ts +126 -0
  18. package/backend/db-client/ssh-tunnel.ts +122 -0
  19. package/backend/git/git-service.ts +12 -0
  20. package/backend/preview/browser/scripts/video-stream.ts +9 -3
  21. package/backend/ws/db-client/connections.ts +180 -0
  22. package/backend/ws/db-client/index.ts +17 -0
  23. package/backend/ws/db-client/io.ts +458 -0
  24. package/backend/ws/db-client/query.ts +162 -0
  25. package/backend/ws/db-client/schema.ts +76 -0
  26. package/backend/ws/db-client/structure.ts +218 -0
  27. package/backend/ws/files/index.ts +5 -1
  28. package/backend/ws/files/read.ts +32 -0
  29. package/backend/ws/files/state.ts +39 -0
  30. package/backend/ws/index.ts +5 -1
  31. package/dist/assets/{index-C86jwBxR.js → index-DqAGCTFT.js} +2 -2
  32. package/dist/assets/{main-DhLx8wD1.js → main-DsED0rLr.js} +216 -197
  33. package/dist/assets/main-eWY9R8g9.css +1 -0
  34. package/dist/index.html +2 -2
  35. package/package.json +4 -1
  36. package/shared/types/database/schema.ts +48 -1
  37. package/shared/types/db-client/connection.ts +60 -0
  38. package/shared/types/db-client/index.ts +6 -0
  39. package/shared/types/db-client/query.ts +77 -0
  40. package/shared/utils/logger.ts +1 -0
  41. package/dist/assets/main-jTnmAoXS.css +0 -1
@@ -0,0 +1,34 @@
1
+ import type { DatabaseConnection } from '$shared/types/database/connection';
2
+ import { debug } from '$shared/utils/logger';
3
+
4
+ export const description = 'Add files_panel_state to user_projects for files panel restore';
5
+
6
+ export const up = (db: DatabaseConnection): void => {
7
+ debug.log('migration', 'Adding files_panel_state to user_projects...');
8
+ db.exec(`ALTER TABLE user_projects ADD COLUMN files_panel_state TEXT`);
9
+ debug.log('migration', 'files_panel_state column added');
10
+ };
11
+
12
+ export const down = (db: DatabaseConnection): void => {
13
+ debug.log('migration', 'Removing files_panel_state from user_projects...');
14
+ db.exec(`
15
+ CREATE TABLE user_projects_backup AS
16
+ SELECT user_id, project_id, joined_at, current_session_id FROM user_projects
17
+ `);
18
+ db.exec(`DROP TABLE user_projects`);
19
+ db.exec(`
20
+ CREATE TABLE user_projects (
21
+ user_id TEXT NOT NULL,
22
+ project_id TEXT NOT NULL,
23
+ joined_at TEXT NOT NULL,
24
+ current_session_id TEXT,
25
+ PRIMARY KEY (user_id, project_id),
26
+ FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE
27
+ )
28
+ `);
29
+ db.exec(`INSERT INTO user_projects SELECT * FROM user_projects_backup`);
30
+ db.exec(`DROP TABLE user_projects_backup`);
31
+ db.exec(`CREATE INDEX IF NOT EXISTS idx_user_projects_user_id ON user_projects(user_id)`);
32
+ db.exec(`CREATE INDEX IF NOT EXISTS idx_user_projects_project_id ON user_projects(project_id)`);
33
+ debug.log('migration', 'files_panel_state column removed');
34
+ };
@@ -0,0 +1,74 @@
1
+ import type { DatabaseConnection } from '$shared/types/database/connection';
2
+ import { debug } from '$shared/utils/logger';
3
+
4
+ export const description = 'Create db_client_connections and db_client_query_history tables';
5
+
6
+ export const up = (db: DatabaseConnection): void => {
7
+ debug.log('migration', 'Creating db_client_connections table...');
8
+
9
+ db.exec(`
10
+ CREATE TABLE IF NOT EXISTS db_client_connections (
11
+ id TEXT PRIMARY KEY,
12
+ name TEXT NOT NULL,
13
+ driver TEXT NOT NULL CHECK (driver IN ('mysql','postgres','sqlite','mongodb','redis')),
14
+ host TEXT,
15
+ port INTEGER,
16
+ username TEXT,
17
+ password TEXT,
18
+ database TEXT,
19
+ ssl_mode TEXT DEFAULT 'disable' CHECK (ssl_mode IN ('disable','require','verify-ca','verify-full')),
20
+ ssl_ca TEXT,
21
+ ssh_enabled INTEGER NOT NULL DEFAULT 0,
22
+ ssh_host TEXT,
23
+ ssh_port INTEGER DEFAULT 22,
24
+ ssh_username TEXT,
25
+ ssh_auth_method TEXT DEFAULT 'password' CHECK (ssh_auth_method IN ('password','key')),
26
+ ssh_password TEXT,
27
+ ssh_private_key TEXT,
28
+ ssh_passphrase TEXT,
29
+ options_json TEXT,
30
+ color TEXT,
31
+ created_at TEXT NOT NULL,
32
+ updated_at TEXT NOT NULL,
33
+ last_used_at TEXT
34
+ )
35
+ `);
36
+
37
+ db.exec(`
38
+ CREATE INDEX IF NOT EXISTS idx_db_client_conn_last_used
39
+ ON db_client_connections(last_used_at DESC)
40
+ `);
41
+
42
+ debug.log('migration', 'Creating db_client_query_history table...');
43
+
44
+ db.exec(`
45
+ CREATE TABLE IF NOT EXISTS db_client_query_history (
46
+ id TEXT PRIMARY KEY,
47
+ connection_id TEXT NOT NULL,
48
+ user_id TEXT,
49
+ query TEXT NOT NULL,
50
+ duration_ms INTEGER,
51
+ row_count INTEGER,
52
+ status TEXT NOT NULL CHECK (status IN ('success','error')),
53
+ error TEXT,
54
+ executed_at TEXT NOT NULL,
55
+ FOREIGN KEY (connection_id) REFERENCES db_client_connections(id) ON DELETE CASCADE
56
+ )
57
+ `);
58
+
59
+ db.exec(`
60
+ CREATE INDEX IF NOT EXISTS idx_db_client_hist_conn_time
61
+ ON db_client_query_history(connection_id, executed_at DESC)
62
+ `);
63
+
64
+ debug.log('migration', 'db-client tables created');
65
+ };
66
+
67
+ export const down = (db: DatabaseConnection): void => {
68
+ debug.log('migration', 'Dropping db-client tables...');
69
+ db.exec('DROP INDEX IF EXISTS idx_db_client_hist_conn_time');
70
+ db.exec('DROP INDEX IF EXISTS idx_db_client_conn_last_used');
71
+ db.exec('DROP TABLE IF EXISTS db_client_query_history');
72
+ db.exec('DROP TABLE IF EXISTS db_client_connections');
73
+ debug.log('migration', 'db-client tables dropped');
74
+ };
@@ -28,6 +28,8 @@ import * as migration026 from './026_create_invite_tokens_table';
28
28
  import * as migration027 from './027_create_opencode_providers_table';
29
29
  import * as migration028 from './028_create_opencode_accounts_table';
30
30
  import * as migration029 from './029_migrate_messages_to_unified';
31
+ import * as migration030 from './030_add_files_panel_state_to_user_projects';
32
+ import * as migration031 from './031_create_db_client_tables';
31
33
 
32
34
  // Export all migrations in order
33
35
  export const migrations = [
@@ -204,6 +206,18 @@ export const migrations = [
204
206
  description: migration029.description,
205
207
  up: migration029.up,
206
208
  down: migration029.down
209
+ },
210
+ {
211
+ id: '030',
212
+ description: migration030.description,
213
+ up: migration030.up,
214
+ down: migration030.down
215
+ },
216
+ {
217
+ id: '031',
218
+ description: migration031.description,
219
+ up: migration031.up,
220
+ down: migration031.down
207
221
  }
208
222
  ];
209
223
 
@@ -0,0 +1,237 @@
1
+ import { getDatabase } from '../index';
2
+ import type { DBDbClientConnectionRow } from '$shared/types/database/schema';
3
+ import type {
4
+ DbClientConnection,
5
+ DbClientConnectionInput,
6
+ DbClientSshConfig,
7
+ DbDriver,
8
+ DbSshAuthMethod,
9
+ DbSslMode
10
+ } from '$shared/types/db-client';
11
+
12
+ const DEFAULT_SSH: DbClientSshConfig = {
13
+ enabled: false,
14
+ host: '',
15
+ port: 22,
16
+ username: '',
17
+ authMethod: 'password',
18
+ password: '',
19
+ privateKey: '',
20
+ passphrase: ''
21
+ };
22
+
23
+ function rowToConnection(row: DBDbClientConnectionRow): DbClientConnection {
24
+ let options: Record<string, unknown> = {};
25
+ if (row.options_json) {
26
+ try {
27
+ const parsed = JSON.parse(row.options_json);
28
+ if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
29
+ options = parsed as Record<string, unknown>;
30
+ }
31
+ } catch {
32
+ options = {};
33
+ }
34
+ }
35
+
36
+ const ssh: DbClientSshConfig = {
37
+ enabled: row.ssh_enabled === 1,
38
+ host: row.ssh_host ?? '',
39
+ port: row.ssh_port ?? 22,
40
+ username: row.ssh_username ?? '',
41
+ authMethod: (row.ssh_auth_method ?? 'password') as DbSshAuthMethod,
42
+ password: row.ssh_password ?? '',
43
+ privateKey: row.ssh_private_key ?? '',
44
+ passphrase: row.ssh_passphrase ?? ''
45
+ };
46
+
47
+ return {
48
+ id: row.id,
49
+ name: row.name,
50
+ driver: row.driver as DbDriver,
51
+ host: row.host,
52
+ port: row.port,
53
+ username: row.username,
54
+ password: row.password,
55
+ database: row.database,
56
+ sslMode: (row.ssl_mode ?? 'disable') as DbSslMode,
57
+ sslCa: row.ssl_ca,
58
+ ssh,
59
+ options,
60
+ color: row.color,
61
+ createdAt: row.created_at,
62
+ updatedAt: row.updated_at,
63
+ lastUsedAt: row.last_used_at
64
+ };
65
+ }
66
+
67
+ interface InsertParams {
68
+ name: string;
69
+ driver: DbDriver;
70
+ host: string | null;
71
+ port: number | null;
72
+ username: string | null;
73
+ password: string | null;
74
+ database: string | null;
75
+ sslMode: DbSslMode;
76
+ sslCa: string | null;
77
+ ssh: DbClientSshConfig;
78
+ options: Record<string, unknown>;
79
+ color: string | null;
80
+ }
81
+
82
+ function normalizeInput(input: DbClientConnectionInput): InsertParams {
83
+ const ssh: DbClientSshConfig = {
84
+ ...DEFAULT_SSH,
85
+ ...(input.ssh ?? {})
86
+ };
87
+ return {
88
+ name: input.name,
89
+ driver: input.driver,
90
+ host: input.host ?? null,
91
+ port: input.port ?? null,
92
+ username: input.username ?? null,
93
+ password: input.password ?? null,
94
+ database: input.database ?? null,
95
+ sslMode: input.sslMode ?? 'disable',
96
+ sslCa: input.sslCa ?? null,
97
+ ssh,
98
+ options: input.options ?? {},
99
+ color: input.color ?? null
100
+ };
101
+ }
102
+
103
+ export const dbClientConnectionQueries = {
104
+ list(): DbClientConnection[] {
105
+ const db = getDatabase();
106
+ const rows = db.prepare(`
107
+ SELECT * FROM db_client_connections
108
+ ORDER BY (last_used_at IS NULL), last_used_at DESC, created_at DESC
109
+ `).all() as DBDbClientConnectionRow[];
110
+ return rows.map(rowToConnection);
111
+ },
112
+
113
+ get(id: string): DbClientConnection | null {
114
+ const db = getDatabase();
115
+ const row = db.prepare(`
116
+ SELECT * FROM db_client_connections WHERE id = ?
117
+ `).get(id) as DBDbClientConnectionRow | null;
118
+ return row ? rowToConnection(row) : null;
119
+ },
120
+
121
+ create(input: DbClientConnectionInput): DbClientConnection {
122
+ const db = getDatabase();
123
+ const id = crypto.randomUUID();
124
+ const now = new Date().toISOString();
125
+ const params = normalizeInput(input);
126
+
127
+ db.prepare(`
128
+ INSERT INTO db_client_connections (
129
+ id, name, driver,
130
+ host, port, username, password, database,
131
+ ssl_mode, ssl_ca,
132
+ ssh_enabled, ssh_host, ssh_port, ssh_username, ssh_auth_method,
133
+ ssh_password, ssh_private_key, ssh_passphrase,
134
+ options_json, color,
135
+ created_at, updated_at, last_used_at
136
+ ) VALUES (
137
+ ?, ?, ?,
138
+ ?, ?, ?, ?, ?,
139
+ ?, ?,
140
+ ?, ?, ?, ?, ?,
141
+ ?, ?, ?,
142
+ ?, ?,
143
+ ?, ?, ?
144
+ )
145
+ `).run(
146
+ id, params.name, params.driver,
147
+ params.host, params.port, params.username, params.password, params.database,
148
+ params.sslMode, params.sslCa,
149
+ params.ssh.enabled ? 1 : 0,
150
+ params.ssh.host || null,
151
+ params.ssh.port,
152
+ params.ssh.username || null,
153
+ params.ssh.authMethod,
154
+ params.ssh.password || null,
155
+ params.ssh.privateKey || null,
156
+ params.ssh.passphrase || null,
157
+ JSON.stringify(params.options),
158
+ params.color,
159
+ now, now, null
160
+ );
161
+
162
+ const row = db.prepare('SELECT * FROM db_client_connections WHERE id = ?').get(id) as DBDbClientConnectionRow;
163
+ return rowToConnection(row);
164
+ },
165
+
166
+ update(id: string, patch: Partial<DbClientConnectionInput>): DbClientConnection {
167
+ const db = getDatabase();
168
+ const existing = db.prepare('SELECT * FROM db_client_connections WHERE id = ?').get(id) as DBDbClientConnectionRow | null;
169
+ if (!existing) {
170
+ throw new Error('db-client connection not found');
171
+ }
172
+
173
+ const sets: string[] = [];
174
+ const values: unknown[] = [];
175
+ const push = (col: string, val: unknown): void => {
176
+ sets.push(`${col} = ?`);
177
+ values.push(val);
178
+ };
179
+
180
+ if (patch.name !== undefined) push('name', patch.name);
181
+ if (patch.driver !== undefined) push('driver', patch.driver);
182
+ if (patch.host !== undefined) push('host', patch.host || null);
183
+ if (patch.port !== undefined) push('port', patch.port ?? null);
184
+ if (patch.username !== undefined) push('username', patch.username || null);
185
+ if (patch.password !== undefined) push('password', patch.password || null);
186
+ if (patch.database !== undefined) push('database', patch.database || null);
187
+ if (patch.sslMode !== undefined) push('ssl_mode', patch.sslMode);
188
+ if (patch.sslCa !== undefined) push('ssl_ca', patch.sslCa || null);
189
+ if (patch.color !== undefined) push('color', patch.color || null);
190
+ if (patch.options !== undefined) push('options_json', JSON.stringify(patch.options ?? {}));
191
+
192
+ if (patch.ssh !== undefined) {
193
+ const merged: DbClientSshConfig = {
194
+ enabled: existing.ssh_enabled === 1,
195
+ host: existing.ssh_host ?? '',
196
+ port: existing.ssh_port ?? 22,
197
+ username: existing.ssh_username ?? '',
198
+ authMethod: (existing.ssh_auth_method ?? 'password') as DbSshAuthMethod,
199
+ password: existing.ssh_password ?? '',
200
+ privateKey: existing.ssh_private_key ?? '',
201
+ passphrase: existing.ssh_passphrase ?? '',
202
+ ...patch.ssh
203
+ };
204
+ push('ssh_enabled', merged.enabled ? 1 : 0);
205
+ push('ssh_host', merged.host || null);
206
+ push('ssh_port', merged.port);
207
+ push('ssh_username', merged.username || null);
208
+ push('ssh_auth_method', merged.authMethod);
209
+ push('ssh_password', merged.password || null);
210
+ push('ssh_private_key', merged.privateKey || null);
211
+ push('ssh_passphrase', merged.passphrase || null);
212
+ }
213
+
214
+ push('updated_at', new Date().toISOString());
215
+
216
+ if (sets.length === 0) {
217
+ return rowToConnection(existing);
218
+ }
219
+
220
+ values.push(id);
221
+ db.prepare(`UPDATE db_client_connections SET ${sets.join(', ')} WHERE id = ?`).run(...values);
222
+
223
+ const row = db.prepare('SELECT * FROM db_client_connections WHERE id = ?').get(id) as DBDbClientConnectionRow;
224
+ return rowToConnection(row);
225
+ },
226
+
227
+ delete(id: string): void {
228
+ const db = getDatabase();
229
+ db.prepare('DELETE FROM db_client_connections WHERE id = ?').run(id);
230
+ },
231
+
232
+ markUsed(id: string): void {
233
+ const db = getDatabase();
234
+ const now = new Date().toISOString();
235
+ db.prepare('UPDATE db_client_connections SET last_used_at = ? WHERE id = ?').run(now, id);
236
+ }
237
+ };
@@ -0,0 +1,113 @@
1
+ import { getDatabase } from '../index';
2
+ import type { DBDbClientQueryHistoryRow } from '$shared/types/database/schema';
3
+ import type { DbClientQueryHistoryEntry } from '$shared/types/db-client';
4
+
5
+ function rowToEntry(row: DBDbClientQueryHistoryRow): DbClientQueryHistoryEntry {
6
+ return {
7
+ id: row.id,
8
+ connectionId: row.connection_id,
9
+ userId: row.user_id,
10
+ query: row.query,
11
+ durationMs: row.duration_ms,
12
+ rowCount: row.row_count,
13
+ status: row.status,
14
+ error: row.error,
15
+ executedAt: row.executed_at
16
+ };
17
+ }
18
+
19
+ interface InsertHistoryInput {
20
+ connectionId: string;
21
+ userId: string | null;
22
+ query: string;
23
+ durationMs: number | null;
24
+ rowCount: number | null;
25
+ status: 'success' | 'error';
26
+ error: string | null;
27
+ }
28
+
29
+ export const dbClientQueryHistoryQueries = {
30
+ insert(input: InsertHistoryInput): DbClientQueryHistoryEntry {
31
+ const db = getDatabase();
32
+ const id = crypto.randomUUID();
33
+ const now = new Date().toISOString();
34
+
35
+ db.prepare(`
36
+ INSERT INTO db_client_query_history (
37
+ id, connection_id, user_id, query,
38
+ duration_ms, row_count, status, error, executed_at
39
+ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
40
+ `).run(
41
+ id, input.connectionId, input.userId, input.query,
42
+ input.durationMs, input.rowCount, input.status, input.error, now
43
+ );
44
+
45
+ const row = db.prepare(
46
+ 'SELECT * FROM db_client_query_history WHERE id = ?'
47
+ ).get(id) as DBDbClientQueryHistoryRow;
48
+ return rowToEntry(row);
49
+ },
50
+
51
+ list(opts: {
52
+ connectionId: string;
53
+ limit?: number;
54
+ offset?: number;
55
+ search?: string;
56
+ }): { items: DbClientQueryHistoryEntry[]; total: number } {
57
+ const db = getDatabase();
58
+ const limit = opts.limit ?? 50;
59
+ const offset = opts.offset ?? 0;
60
+ const search = opts.search?.trim();
61
+
62
+ let where = 'connection_id = ?';
63
+ const params: unknown[] = [opts.connectionId];
64
+ if (search) {
65
+ where += ' AND query LIKE ?';
66
+ params.push(`%${search}%`);
67
+ }
68
+
69
+ const totalRow = db.prepare(
70
+ `SELECT COUNT(*) as count FROM db_client_query_history WHERE ${where}`
71
+ ).get(...params) as { count: number };
72
+
73
+ const rows = db.prepare(
74
+ `SELECT * FROM db_client_query_history
75
+ WHERE ${where}
76
+ ORDER BY executed_at DESC
77
+ LIMIT ? OFFSET ?`
78
+ ).all(...params, limit, offset) as DBDbClientQueryHistoryRow[];
79
+
80
+ return {
81
+ items: rows.map(rowToEntry),
82
+ total: totalRow.count
83
+ };
84
+ },
85
+
86
+ deleteByConnection(connectionId: string): number {
87
+ const db = getDatabase();
88
+ const result = db.prepare(
89
+ 'DELETE FROM db_client_query_history WHERE connection_id = ?'
90
+ ).run(connectionId) as { changes: number };
91
+ return result.changes;
92
+ },
93
+
94
+ deleteOne(id: string): void {
95
+ const db = getDatabase();
96
+ db.prepare('DELETE FROM db_client_query_history WHERE id = ?').run(id);
97
+ },
98
+
99
+ prune(connectionId: string, keep: number): number {
100
+ const db = getDatabase();
101
+ const result = db.prepare(`
102
+ DELETE FROM db_client_query_history
103
+ WHERE connection_id = ?
104
+ AND id NOT IN (
105
+ SELECT id FROM db_client_query_history
106
+ WHERE connection_id = ?
107
+ ORDER BY executed_at DESC
108
+ LIMIT ?
109
+ )
110
+ `).run(connectionId, connectionId, keep) as { changes: number };
111
+ return result.changes;
112
+ }
113
+ };
@@ -7,4 +7,6 @@ export { dbUtils } from './utils-queries';
7
7
  export { snapshotQueries } from './snapshot-queries';
8
8
  export { checkpointQueries } from './checkpoint-queries';
9
9
  export { engineQueries } from './engine-queries';
10
- export { authQueries } from './auth-queries';
10
+ export { authQueries } from './auth-queries';
11
+ export { dbClientConnectionQueries } from './db-client-connection-queries';
12
+ export { dbClientQueryHistoryQueries } from './db-client-query-history-queries';
@@ -110,5 +110,28 @@ export const projectQueries = {
110
110
  SET current_session_id = ?
111
111
  WHERE user_id = ? AND project_id = ?
112
112
  `).run(sessionId, userId, projectId);
113
+ },
114
+
115
+ getFilesPanelState(userId: string, projectId: string): string | null {
116
+ const db = getDatabase();
117
+ const result = db.prepare(`
118
+ SELECT files_panel_state FROM user_projects
119
+ WHERE user_id = ? AND project_id = ?
120
+ `).get(userId, projectId) as { files_panel_state: string | null } | null;
121
+ return result?.files_panel_state || null;
122
+ },
123
+
124
+ setFilesPanelState(userId: string, projectId: string, state: string | null): void {
125
+ const db = getDatabase();
126
+ const now = new Date().toISOString();
127
+ db.prepare(`
128
+ INSERT OR IGNORE INTO user_projects (user_id, project_id, joined_at)
129
+ VALUES (?, ?, ?)
130
+ `).run(userId, projectId, now);
131
+ db.prepare(`
132
+ UPDATE user_projects
133
+ SET files_panel_state = ?
134
+ WHERE user_id = ? AND project_id = ?
135
+ `).run(state, userId, projectId);
113
136
  }
114
137
  };