postgres~ PGClient

PGClient is a client for Postgres database. Internally client uses go-pg/pg driver.

Constructor

new PGClient()

Methods

Connect(host, port, username, password) → {bool}

Connect connects to Postgres database using given credentials. The connection is closed after the function returns.
Parameters:
NameTypeDescription
hoststringThe host of the Postgres database.
portintThe port of the Postgres database.
usernamestringThe username to connect to the Postgres database.
passwordstringThe password to connect to the Postgres database.
Throws:
- If connection is unsuccessful, it returns the error.
Type
error
Returns:
- If connection is successful, it returns true.
Type: 
bool
Example
let m = require('nuclei/postgres');
let c = m.PGClient();
let isConnected = c.Connect('localhost', 5432, 'username', 'password');

ConnectWithDB(host, port, username, password, dbName) → {bool}

ConnectWithDB connects to Postgres database using given credentials and database name. The connection is closed after the function returns.
Parameters:
NameTypeDescription
hoststringThe host of the Postgres database.
portintThe port of the Postgres database.
usernamestringThe username to connect to the Postgres database.
passwordstringThe password to connect to the Postgres database.
dbNamestringThe name of the database to connect to.
Throws:
- If connection is unsuccessful, it returns the error.
Type
error
Returns:
- If connection is successful, it returns true.
Type: 
bool
Example
let m = require('nuclei/postgres');
let c = m.PGClient();
let isConnected = c.ConnectWithDB('localhost', 5432, 'username', 'password', 'mydb');

ExecuteQuery(host, port, username, password, dbName, query) → {string}

ExecuteQuery connects to Postgres database using given credentials and database name and executes a query on the db.
Parameters:
NameTypeDescription
hoststringThe host of the Postgres database.
portintThe port of the Postgres database.
usernamestringThe username to connect to the Postgres database.
passwordstringThe password to connect to the Postgres database.
dbNamestringThe name of the database to connect to.
querystringThe query to execute on the database.
Throws:
- If query execution is unsuccessful, it returns the error.
Type
error
Returns:
- The result of the query execution.
Type: 
string
Example
let m = require('nuclei/postgres');
let c = m.PGClient();
let result = c.ExecuteQuery('localhost', 5432, 'username', 'password', 'mydb', 'SELECT * FROM users');

IsPostgres(host, port) → {bool}

IsPostgres checks if the given host and port are running Postgres database.
Parameters:
NameTypeDescription
hoststringThe host to check.
portintThe port to check.
Throws:
- If the check is unsuccessful, it returns the error.
Type
error
Returns:
- If the host and port are running Postgres database, it returns true.
Type: 
bool
Example
let m = require('nuclei/postgres');
let c = m.PGClient();
let isPostgres = c.IsPostgres('localhost', 5432);