Skip to content

Commit 9072e64

Browse files
author
Drew
committed
Merge branch 'error-handling' into v2.1.0
2 parents 8783b92 + 117f39b commit 9072e64

5 files changed

Lines changed: 55 additions & 63 deletions

File tree

src/database/base-db.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ export class BaseDb {
88
// Database logging function
99
public logger: Function = (message, body?) => console.log(message, body);
1010

11-
// Database error function
12-
public errorHandler: Function = (error) => console.error(error);
13-
1411
// Database entities
1512
public entities: any[] = [ ExampleUser ];
1613

src/database/postgres.ts

Lines changed: 55 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -14,60 +14,68 @@ export class PointyPostgres extends BaseDb {
1414
* a string to load from file, or pass the object directly). Database
1515
* will rely on `process.env.DATABASE_URL` if this is not set.
1616
*/
17-
public async connect(options?: string | Object): Promise<Connection> {
18-
let pgOptions: any;
17+
public connect(options?: string | Object): Promise<Connection> {
18+
return new Promise(async (accept, reject) => {
19+
let pgOptions: any;
1920

20-
if (process.env.DATABASE_URL) {
21-
// Live
22-
pgOptions = PostgressConnectionStringParser.parse(
23-
process.env.DATABASE_URL
24-
);
25-
pgOptions.type = 'postgres';
21+
if (process.env.DATABASE_URL) {
22+
// Live
23+
pgOptions = PostgressConnectionStringParser.parse(
24+
process.env.DATABASE_URL
25+
);
26+
pgOptions.type = 'postgres';
2627

27-
this.logger('Using production database');
28-
this.logger(
29-
'Using database driver',
30-
process.env.TYPEORM_DRIVER_TYPE || pgOptions.type
31-
);
32-
}
33-
else {
34-
// Local
35-
if (typeof options === 'string') {
36-
pgOptions = require(path.join(
37-
options.toString(),
38-
'local.config.json'
39-
));
28+
this.logger('Using production database');
29+
this.logger(
30+
'Using database driver',
31+
process.env.TYPEORM_DRIVER_TYPE || pgOptions.type
32+
);
4033
}
4134
else {
42-
pgOptions = options;
43-
}
35+
// Local
36+
if (typeof options === 'string') {
37+
pgOptions = require(path.join(
38+
options.toString(),
39+
'local.config.json'
40+
));
41+
}
42+
else {
43+
pgOptions = options;
44+
}
4445

45-
this.logger('Using development database');
46-
this.logger(
47-
'Using database driver',
48-
process.env.TYPEORM_DRIVER_TYPE || pgOptions.type
49-
);
46+
this.logger('Using development database');
47+
this.logger(
48+
'Using database driver',
49+
process.env.TYPEORM_DRIVER_TYPE || pgOptions.type
50+
);
5051

51-
this.shouldSync = true;
52-
}
52+
this.shouldSync = true;
53+
}
5354

54-
// Create connection
55-
this.conn = await createConnection(<ConnectionOptions>{
56-
name: this.connectionName,
57-
type: process.env.TYPEORM_DRIVER_TYPE || pgOptions.type,
58-
driver: process.env.TYPEORM_DRIVER_TYPE || pgOptions.type,
59-
host: pgOptions.host,
60-
port: pgOptions.port,
61-
username: pgOptions.user,
62-
password: pgOptions.password,
63-
database: pgOptions.database,
64-
entities: this.entities,
65-
synchronize: this.shouldSync,
66-
uuidExtension: pgOptions.uuidExtension
67-
? pgOptions.uuidExtension
68-
: 'pgcrypto'
69-
}).catch((error) => this.errorHandler(error));
55+
// Create connection
56+
const conn = await createConnection(<ConnectionOptions>{
57+
name: this.connectionName,
58+
type: process.env.TYPEORM_DRIVER_TYPE || pgOptions.type,
59+
driver: process.env.TYPEORM_DRIVER_TYPE || pgOptions.type,
60+
host: pgOptions.host,
61+
port: pgOptions.port,
62+
username: pgOptions.user,
63+
password: pgOptions.password,
64+
database: pgOptions.database,
65+
entities: this.entities,
66+
synchronize: this.shouldSync,
67+
uuidExtension: pgOptions.uuidExtension
68+
? pgOptions.uuidExtension
69+
: 'pgcrypto'
70+
}).catch((error) => {
71+
reject(error);
72+
return false;
73+
});
7074

71-
return this.conn;
75+
if (conn && conn instanceof Connection) {
76+
this.conn = conn;
77+
accept(this.conn);
78+
}
79+
});
7280
}
7381
}

src/pointy-core.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ export class PointyApi {
7171
// Default db
7272
this.db = new PointyPostgres();
7373
this.db.logger = this.log;
74-
this.db.errorHandler = this.error;
7574

7675
if (process.argv.includes('testmode')) {
7776
this.testmode = true;

test/spec/api/database/base-db.spec.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,4 @@ describe('[BaseDb]', () => {
3737
this.baseDb.logger();
3838
expect(result).toBe(true);
3939
});
40-
41-
it('can log an error', () => {
42-
let result = false;
43-
44-
console.error = () => {
45-
result = true;
46-
};
47-
48-
this.baseDb.errorHandler();
49-
expect(result).toBe(true);
50-
});
5140
});

test/spec/api/database/postgres.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ describe('[Database: Postgres]', async () => {
3535
it('can connect with json options', async () => {
3636
const db = new PointyPostgres();
3737
db.connectionName = 'jsonconn';
38-
db.errorHandler = (error) => fail(error);
3938
db.logger = () => {};
4039

4140
// Database

0 commit comments

Comments
 (0)