(query: string, conn: pg.PoolClient)
| 25 | |
| 26 | // Pretty query execution display such as spinner and actual returned content for `SELECT` query. |
| 27 | export async function interactiveExecuteQuery(query: string, conn: pg.PoolClient) { |
| 28 | const spinner = ora("Executing query...").start(); |
| 29 | try { |
| 30 | const results = await conn.query(query); |
| 31 | spinner.succeed(clc.green("Query executed successfully")); |
| 32 | |
| 33 | if (Array.isArray(results.rows) && results.rows.length > 0) { |
| 34 | const table: any[] = new Table({ |
| 35 | head: Object.keys(results.rows[0]).map((key) => clc.cyan(key)), |
| 36 | style: { head: [], border: [] }, |
| 37 | }); |
| 38 | |
| 39 | for (const row of results.rows) { |
| 40 | table.push(Object.values(row) as any); |
| 41 | } |
| 42 | |
| 43 | logger.info(table.toString()); |
| 44 | } else { |
| 45 | // If nothing is returned and the query was select, let the user know there was no results. |
| 46 | if (query.toUpperCase().includes("SELECT")) { |
| 47 | logger.info(clc.yellow("No results returned")); |
| 48 | } |
| 49 | } |
| 50 | } catch (err) { |
| 51 | spinner.fail(clc.red(`Failed executing query: ${err}`)); |
| 52 | } |
| 53 | } |
no test coverage detected
searching dependent graphs…