File size: 1,070 Bytes
645ad9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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');
  }
}