dbgate/api/src/engines/postgres/index.js

23 lines
712 B
JavaScript
Raw Normal View History

2020-01-04 20:59:53 +00:00
const { Client } = require('pg');
module.exports = {
2020-01-19 20:01:48 +00:00
async connect({ server, port, user, password, database }) {
const client = new Client({ host: server, port, user, password, database: database || 'postgres' });
2020-01-04 20:59:53 +00:00
await client.connect();
return client;
},
async query(client, sql) {
const res = await client.query(sql);
2020-01-25 16:26:51 +00:00
return { rows: res.rows };
2020-01-04 20:59:53 +00:00
},
async getVersion(client) {
2020-01-25 16:26:51 +00:00
const { rows } = await this.query(client, 'SELECT version()');
2020-01-04 20:59:53 +00:00
const { version } = rows[0];
return { version };
},
async listDatabases(client) {
2020-01-25 16:26:51 +00:00
const { rows } = await this.query(client, 'SELECT datname AS name FROM pg_database WHERE datistemplate = false');
return rows;
2020-01-04 20:59:53 +00:00
},
};