UNPKG

2.84 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const pg_1 = require("pg");
4const DBClient_1 = require("../db/DBClient");
5const DBTransaction_1 = require("../db/DBTransaction");
6const LogManager_1 = require("../log/LogManager");
7const LOGGER = LogManager_1.LogManager.getLogger(__filename);
8class PostgresTransaction extends DBTransaction_1.DBTransaction {
9 runQueryInConnection(sql, bindsArr) {
10 return new Promise((resolve, reject) => this.connection.query(sql, bindsArr, (err, rows) => {
11 if (err) {
12 LOGGER.error(err, `SQL error for ${sql}`);
13 return reject(err);
14 }
15 return resolve(rows.rows);
16 }));
17 }
18}
19exports.PostgresTransaction = PostgresTransaction;
20class PostgresConnectionFactory {
21 constructor(name, connectionConfig) {
22 this.connConfig = connectionConfig;
23 this.name = name;
24 }
25 async create() {
26 LOGGER.trace(`Creating new connection for pool ${this.name}`);
27 const connection = new pg_1.Client(this.connConfig);
28 await new Promise((resolve, reject) => connection.connect((err) => {
29 if (err) {
30 reject(err);
31 }
32 else {
33 resolve();
34 }
35 }));
36 return connection;
37 }
38 async destroy(connection) {
39 LOGGER.trace(`Destroying connection for pool ${this.name}`);
40 await new Promise((resolve, reject) => connection.end((err) => {
41 if (err) {
42 reject(err);
43 }
44 else {
45 resolve();
46 }
47 }));
48 return undefined;
49 }
50 validate(connection) {
51 LOGGER.trace(`Validating connection for pool ${this.name}`);
52 return new Promise((resolve, reject) => {
53 connection.query('SELECT 1', (err, results) => {
54 if (err) {
55 resolve(false);
56 }
57 resolve(true);
58 });
59 });
60 }
61}
62/**
63 * A MySQL client you can use to execute queries against MySQL
64 */
65class PostgresClient extends DBClient_1.DBClient {
66 async initialise() {
67 await super.initialise();
68 }
69 getConnectionFactory(name, connectionConfig) {
70 return new PostgresConnectionFactory(name, connectionConfig);
71 }
72 getNewDBTransaction(readonly, connectionPool) {
73 return new PostgresTransaction(this.clientConfiguration.name, readonly, connectionPool);
74 }
75 getPingQuery() {
76 return 'SELECT 1';
77 }
78 getDefaultConnectionConfig() {
79 return {
80 host: 'localhost',
81 port: 5432,
82 user: 'postgres',
83 password: '',
84 database: '',
85 };
86 }
87}
88exports.PostgresClient = PostgresClient;
89//# sourceMappingURL=PostgresClient.js.map
\No newline at end of file