-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboot.js
More file actions
45 lines (38 loc) · 1.03 KB
/
Copy pathboot.js
File metadata and controls
45 lines (38 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { Pool } from 'pg';
async function createPostgrePool(config, app, clientName = 'default') {
app.coreLogger.info(
'[egg-postgresql] clientName[%s] connecting %s@%s:%s/%s',
clientName,
config.user,
config.host,
config.port,
config.database
);
const client = new Pool(config);
const { rows } = await client.query('SELECT NOW() AS currentTime;');
app.coreLogger.info(
'[egg-postgresql] clientName[%s] status OK, PostgreSQL Server currentTime: %j',
clientName,
rows[0].currentTime
);
return client;
}
export class PostgreSQLBootHook {
constructor(app) {
this.app = app;
}
configDidLoad() {
if (this.app.type === 'application' && !this.app.config.postgresql.app) {
return;
} else if (this.app.type === 'agent' && !this.app.config.postgresql.agent) {
return;
}
this.app.addSingleton('postgresql', createPostgrePool);
// alias to `app.pg`
Reflect.defineProperty(this.app, 'pg', {
get() {
return this.postgresql;
},
});
}
}