MCPcopy Create free account
hub / github.com/sqlchat/sqlchat / newPostgresClient

Function newPostgresClient

src/lib/connectors/postgres/index.ts:10–51  ·  view source on GitHub ↗
(connection: Connection)

Source from the content-addressed store, hash-verified

8const systemTables = "'_prisma_migrations'";
9
10const newPostgresClient = async (connection: Connection) => {
11 const clientConfig: ClientConfig = {
12 host: connection.host,
13 port: Number(connection.port),
14 user: connection.username,
15 password: connection.password,
16 database: connection.database,
17 application_name: "sqlchat",
18 };
19 if (connection.ssl) {
20 clientConfig.ssl = {
21 ca: connection.ssl?.ca,
22 cert: connection.ssl?.cert,
23 key: connection.ssl?.key,
24 };
25 } else {
26 clientConfig.ssl = {
27 rejectUnauthorized: false,
28 };
29 }
30
31 let client = new Client(clientConfig);
32
33 if (connection.ssl) {
34 await client.connect();
35 } else {
36 try {
37 await client.connect();
38 } catch (error) {
39 // Because node-postgres didn't implement `sslmode: preferred`. So first try to connect via SSL, otherwise connect via non-SSL.
40 // Connecting postgres via non-ssl requires `clientConfig.ssl` is undefined. ref: https://github.com/sqlchat/sqlchat/issues/108
41 if (error instanceof Error && error.message.includes("The server does not support SSL connections")) {
42 clientConfig.ssl = undefined;
43 client = new Client(clientConfig);
44 await client.connect();
45 } else {
46 throw error;
47 }
48 }
49 }
50 return client;
51};
52
53const testConnection = async (connection: Connection): Promise<boolean> => {
54 const client = await newPostgresClient(connection);

Callers 4

testConnectionFunction · 0.85
executeFunction · 0.85
getDatabasesFunction · 0.85
getTableSchemaFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected