()
| 35 | } |
| 36 | |
| 37 | protected async connectToPostgres() { |
| 38 | if (this.db) { |
| 39 | return this.db; |
| 40 | } |
| 41 | |
| 42 | try { |
| 43 | const postgresClient = new Client({ |
| 44 | user: POSTGRESQL_USER, |
| 45 | password: POSTGRESQL_PASSWORD, |
| 46 | database: POSTGRESQL_DBNAME, |
| 47 | hostname: POSTGRESQL_HOST, |
| 48 | port: POSTGRESQL_PORT, |
| 49 | tls, |
| 50 | }); |
| 51 | |
| 52 | await postgresClient.connect(); |
| 53 | |
| 54 | this.db = postgresClient; |
| 55 | } catch (error) { |
| 56 | // Try to connect without TLS, if the connection type is socket |
| 57 | if ((error as Error).toString().includes('No TLS options are allowed when host type is set to "socket"')) { |
| 58 | const postgresClient = new Client({ |
| 59 | user: POSTGRESQL_USER, |
| 60 | password: POSTGRESQL_PASSWORD, |
| 61 | database: POSTGRESQL_DBNAME, |
| 62 | hostname: POSTGRESQL_HOST, |
| 63 | port: POSTGRESQL_PORT, |
| 64 | }); |
| 65 | |
| 66 | await postgresClient.connect(); |
| 67 | |
| 68 | this.db = postgresClient; |
| 69 | } else { |
| 70 | console.log('Failed to connect to Postgres!'); |
| 71 | console.error(error); |
| 72 | |
| 73 | if (this.throwOnConnectionError) { |
| 74 | throw error; |
| 75 | } |
| 76 | |
| 77 | // This allows tests (and the app) to work even if Postgres is not available |
| 78 | const mockPostgresClient = { |
| 79 | queryObject: () => { |
| 80 | return { |
| 81 | rows: [], |
| 82 | }; |
| 83 | }, |
| 84 | } as unknown as Client; |
| 85 | |
| 86 | this.db = mockPostgresClient; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | protected async disconnectFromPostgres() { |
| 92 | if (!this.db) { |
no outgoing calls
no test coverage detected