mirror of
https://github.com/sqlchat/sqlchat.git
synced 2025-09-24 16:46:05 +08:00
feat: pg support fetch non-public schema
This commit is contained in:
@ -2,6 +2,11 @@ import { Client, ClientConfig } from "pg";
|
|||||||
import { Connection, ExecutionResult } from "@/types";
|
import { Connection, ExecutionResult } from "@/types";
|
||||||
import { Connector } from "..";
|
import { Connector } from "..";
|
||||||
|
|
||||||
|
const systemSchemas =
|
||||||
|
"'information_schema', 'pg_catalog', 'pg_toast', '_timescaledb_cache', '_timescaledb_catalog', '_timescaledb_internal', '_timescaledb_config', 'timescaledb_information', 'timescaledb_experimental'";
|
||||||
|
|
||||||
|
const systemTables = "'_prisma_migrations'";
|
||||||
|
|
||||||
const newPostgresClient = async (connection: Connection) => {
|
const newPostgresClient = async (connection: Connection) => {
|
||||||
const clientConfig: ClientConfig = {
|
const clientConfig: ClientConfig = {
|
||||||
host: connection.host,
|
host: connection.host,
|
||||||
@ -75,13 +80,17 @@ const getTables = async (connection: Connection, databaseName: string): Promise<
|
|||||||
connection.database = databaseName;
|
connection.database = databaseName;
|
||||||
const client = await newPostgresClient(connection);
|
const client = await newPostgresClient(connection);
|
||||||
const { rows } = await client.query(
|
const { rows } = await client.query(
|
||||||
`SELECT table_name FROM information_schema.tables WHERE table_schema='public' AND table_type='BASE TABLE' AND table_catalog=$1;`,
|
`SELECT table_schema, table_name FROM information_schema.tables WHERE table_schema NOT IN (${systemSchemas}) AND table_name NOT IN (${systemTables}) AND table_type='BASE TABLE' AND table_catalog=$1;`,
|
||||||
[databaseName]
|
[databaseName]
|
||||||
);
|
);
|
||||||
await client.end();
|
await client.end();
|
||||||
const tableList = [];
|
const tableList = [];
|
||||||
for (const row of rows) {
|
for (const row of rows) {
|
||||||
if (row["table_name"]) {
|
if (row["table_name"]) {
|
||||||
|
if (row["table_schema"] !== "public") {
|
||||||
|
tableList.push(`${row["table_schema"]}.${row["table_name"]}`);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
tableList.push(row["table_name"]);
|
tableList.push(row["table_name"]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -97,7 +106,7 @@ const getTableStructure = async (
|
|||||||
connection.database = databaseName;
|
connection.database = databaseName;
|
||||||
const client = await newPostgresClient(connection);
|
const client = await newPostgresClient(connection);
|
||||||
const { rows } = await client.query(
|
const { rows } = await client.query(
|
||||||
`SELECT column_name, data_type, is_nullable FROM information_schema.columns WHERE table_schema='public' AND table_name=$1;`,
|
`SELECT column_name, data_type, is_nullable FROM information_schema.columns WHERE table_schema NOT IN (${systemSchemas}) AND table_name=$1;`,
|
||||||
[tableName]
|
[tableName]
|
||||||
);
|
);
|
||||||
await client.end();
|
await client.end();
|
||||||
@ -127,7 +136,7 @@ const getTableStructureBatch = async (
|
|||||||
await Promise.all(
|
await Promise.all(
|
||||||
tableNameList.map(async (tableName) => {
|
tableNameList.map(async (tableName) => {
|
||||||
const { rows } = await client.query(
|
const { rows } = await client.query(
|
||||||
`SELECT column_name, data_type, is_nullable FROM information_schema.columns WHERE table_schema='public' AND table_name=$1;`,
|
`SELECT column_name, data_type, is_nullable FROM information_schema.columns WHERE table_schema NOT IN (${systemSchemas}) AND table_name=$1;`,
|
||||||
[tableName]
|
[tableName]
|
||||||
);
|
);
|
||||||
const columnList = [];
|
const columnList = [];
|
||||||
|
Reference in New Issue
Block a user