Spaces:
Runtime error
Runtime error
Download src/utils/database.ts from fullstuckdev/sql-agent: direct link, hf CLI and curl.
- Browser
- Download file 1.07 kB
-
https://huggingface.co/spaces/fullstuckdev/sql-agent/resolve/main/src/utils/database.ts
- Command line
-
hf download hf://spaces/fullstuckdev/sql-agent/src/utils/database.ts
-
curl -L -o database.ts https://huggingface.co/spaces/fullstuckdev/sql-agent/resolve/main/src/utils/database.ts
1.07 kB
| import mysql from 'mysql2/promise'; | |
| export async function createConnection(uri: string) { | |
| try { | |
| const connection = await mysql.createConnection(uri); | |
| return connection; | |
| } catch (error) { | |
| throw new Error('Failed to connect to database'); | |
| } | |
| } | |
| export async function executeQuery(connection: mysql.Connection, query: string) { | |
| try { | |
| const [rows] = await connection.execute(query); | |
| return rows; | |
| } catch (error) { | |
| throw new Error(`Failed to execute query: ${error}`); | |
| } | |
| } | |
| export async function getTables(connection: mysql.Connection) { | |
| try { | |
| const [rows] = await connection.execute('SHOW TABLES'); | |
| return Object.values(rows).map((row: any) => Object.values(row)[0]); | |
| } catch (error) { | |
| throw new Error('Failed to fetch tables'); | |
| } | |
| } | |
| export async function getTablesList(connection: mysql.Connection) { | |
| try { | |
| const [rows] = await connection.execute('SHOW TABLES'); | |
| return Object.values(rows).map((row: any) => Object.values(row)[0]); | |
| } catch (error) { | |
| throw new Error('Failed to fetch tables list'); | |
| } | |
| } |