Database wrapper for the pg npm package. Use this to add:
-
Logging
-
Compatible API to larvitdbmigration for database migrations
-
Great glory
import { Db } from 'larvitdb-pg';
// All parameters are optional
const db = new Db({
log: log, // Logging object. Will default to a simple console logger if not provided
host: 'database.server.com', // Hostname of the server to connect to, can also be a socket, like this: '/cloudsql/myproject:zone:mydb'
port: 3211, // Port, ignored if host is a socket
user: 'dbuser', // Database account username
password: 'secretpassword', // Database account password
database: 'mydb', // Database name within the database server
});
// OR
const db = new Db({
log: log,
connectionString: 'postgresql://dbuser:secretpassword@database.server.com:3211/mydb',
});A direct query to any connection in the pool
const dbRes = await db.query('SELECT 1 + 1 AS solution');
console.log('solution is: ' + dbRes.rows[0].solution); // 2
console.log('All fields: ' + dbRes.fields); // ['solution']For example if you want to lock tables and run multiple queries on the samme connection.
const dbCon = await db.getConnection();
const sql = 'SELECT * FROM users WHERE username LIKE ' + dbCon.escape(dataToBeEscaped);
const dbRes = await dbCon.query(sql);
dbCon.release(); // Always release your connection when the query is doneRemove all tables from current database
This function will clean the current database from all tables, in the public schema, by removing and then recreating it.
await db.resetSchema('public');-
Changed behaviour of getConnection and added getPool
-
Added logging for individual connection queries