Skip to content

Commit f27ab73

Browse files
committed
don't bring af app down if was not able to connect to pg
1 parent ef1d545 commit f27ab73

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

adminforth/dataConnectors/postgres.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
import dayjs from 'dayjs';
2-
import pkg from 'pg';
32
import { AdminForthResource, IAdminForthDataSourceConnector } from '../types/Back.js';
43
import { AdminForthDataTypes, AdminForthFilterOperators, AdminForthSortDirections, } from '../types/Common.js';
54
import AdminForthBaseConnector from './baseConnector.js';
5+
import pkg from 'pg';
66
const { Client } = pkg;
77

88

99
class PostgresConnector extends AdminForthBaseConnector implements IAdminForthDataSourceConnector {
1010

1111
db: any;
12+
connected: boolean = false;
1213

1314
constructor({ url }) {
1415
super();
1516
this.db = new Client({
1617
connectionString: url
1718
});
1819
(async () => {
19-
await this.db.connect();
20-
this.db.on('error', (err) => {
21-
console.log('Postgres error: ', err.message, err.stack)
22-
this.db.end();
23-
this.db = new PostgresConnector({ url }).db;
24-
});
20+
try {
21+
await this.db.connect();
22+
this.connected = true;
23+
this.db.on('error', (err) => {
24+
console.log('Postgres error: ', err.message, err.stack)
25+
this.db.end();
26+
this.db = new PostgresConnector({ url }).db;
27+
});
28+
} catch (e) {
29+
console.error('ERROR: Failed to connect to Postgres', e);
30+
}
2531
})();
2632
}
2733

@@ -44,6 +50,9 @@ class PostgresConnector extends AdminForthBaseConnector implements IAdminForthDa
4450
};
4551

4652
async discoverFields(resource) {
53+
if (!this.connected) {
54+
return null;
55+
}
4756
const tableName = resource.table;
4857
const stmt = await this.db.query(`
4958
SELECT

0 commit comments

Comments
 (0)