(connection: Connection)
| 8 | const systemTables = "'_prisma_migrations'"; |
| 9 | |
| 10 | const 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 | |
| 53 | const testConnection = async (connection: Connection): Promise<boolean> => { |
| 54 | const client = await newPostgresClient(connection); |
no outgoing calls
no test coverage detected