-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistTables.ts
33 lines (28 loc) · 908 Bytes
/
listTables.ts
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
import knex from "knex";
import knexConfig from "./src/config/knexfile";
const environment = "development";
const db = knex(knexConfig[environment]);
async function listTablesAndColumns() {
try {
// Retrieve the current database name
const tables = await db("information_schema.tables")
.select("table_name")
.where("table_schema", db.client.database());
for (const table of tables) {
console.log(`Table: ${table.TABLE_NAME}`);
const columns = await db("information_schema.columns")
.select("column_name")
.where({
table_name: table.TABLE_NAME,
});
columns.forEach((column) => {
console.log(` Column: ${column.column_name} - ${column.data_type}`);
});
}
} catch (error) {
console.error("Error fetching tables and columns:", error);
} finally {
await db.destroy();
}
}
listTablesAndColumns();