How to use the pg-promise function in pg-promise

To help you get started, we’ve selected a few pg-promise examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Richie765 / meteor-pg / src / mpg-es6.js View on Github external
function init(connection, channel) {
  if(!connection) {
    connection = process.env.PG_URL ? process.env.PG_URL : 'postgres://localhost/postgres';
  }

  if(!channel) {
    channel = process.env.PG_CHANNEL ? process.env.PG_CHANNEL : 'default_channel';
  }

  // pg-promise connection

  pgp = pgPromise({});

  try {
    db = GLOBAL.Promise.await(pgp(connection));
  }
  catch(err) {
    console.error('meteor-pg: failed to connect to', connection);
    throw err;
  }

  // PgQueryObserver

  query_observer = new PgQueryObserver(db, channel);

  table_observer = query_observer.table_observer;

  // Automatic cleanup
github ArkEcosystem / core / packages / core-database-postgres / src / postgres-connection.ts View on Github external
public async connect(): Promise {
        this.emitter.emit(Database.DatabaseEvents.PRE_CONNECT);

        const options = this.options;

        const pgp: pgPromise.IMain = pgPromise({
            ...options.initialization,
            ...{
                error: async (error, context) => {
                    // https://www.postgresql.org/docs/11/errcodes-appendix.html
                    // Class 53 — Insufficient Resources
                    // Class 57 — Operator Intervention
                    // Class 58 — System Error (errors external to PostgreSQL itself)
                    if (error.code && ["53", "57", "58"].includes(error.code.slice(0, 2))) {
                        app.forceExit("Unexpected database error. Shutting down to prevent further damage.", error);
                    }
                },
                receive(data) {
                    camelizeColumns(pgp, data);
                },
                extend(object) {
                    for (const repository of Object.keys(repositories)) {
github tomalrussell / colouring-london / app / src / db.ts View on Github external
/**
 * Expose query interface to database pool
 *
 * - connection details must be set in environment variables, default to:
 *      PGHOST='localhost'
 *      PGUSER=process.env.USER
 *      PGDATABASE=process.env.USER
 *      PGPASSWORD=null
 *      PGPORT=5432
 */
import pg from 'pg-promise';

// pg-promise, can provide initialisation options
const pgp = pg();
// database connection (default to env vars)
const db = pgp({
    'host': process.env.PGHOST,
    'database': process.env.PGDATABASE,
    'user': process.env.PGUSER,
    'password': process.env.PGPASSWORD,
    'port': parseInt(process.env.PGPORT)
});

export default db;
github the-bitcoin-token / bitcoin-non-standard-server / src / db.js View on Github external
static getConnection(): void {
    // make sure that only one db connection is created
    if (this._db) return

    // otherwise create a new connection
    const pgp = pgPromise({})
    const {
      env: { PGUSER, PGPASSWORD, PGHOST, PGPORT, PGDATABASE }
    } = process
    const cn = `postgres://${PGUSER}:${PGPASSWORD}@${PGHOST}:${PGPORT}/${PGDATABASE}`
    this._db = pgp(cn)
  }
github moovel / teamchatviz / server / db.js View on Github external
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
  USA
*/

import PG from 'pg-promise';
import config from './config';
import promiseLib from 'bluebird';
import monitor from 'pg-monitor';

const options = { promiseLib };
const pgp = PG(options);

// do not convert timestamp to date
pgp.pg.types.setTypeParser(1114, function (str) {
  return str;
});

const db = pgp(config.dbUrl);

if (config.infoQueries) {
  monitor.attach(options);
  monitor.setTheme('matrix');
}

export default db;
export { pgp };
github ktk-2005 / Feedbacker-Forum / server / src / database / database-postgres.js View on Github external
import pgpromise from 'pg-promise'
import path from 'path'
import fs from 'fs'

const pgp = pgpromise({})

/*
Changes query strings from using ?, ?, ? for parameters to using $1, $2, $3
to be compatible with pg-promise's way of handling parameters
 */
function prepStatement(unprepared) {
  let str = unprepared
  let i = 1
  while (str.includes('?')) {
    str = str.replace('?', `$${i}`)
    i += 1
  }
  return str
}

function timeout(timeMs) {
github dstack-group / Butterfly / user-manager / user-manager-rest-api / src / database / PgDatabaseConnection.ts View on Github external
constructor(config: DatabaseConfig) {
    this.config = config;
    this.pgPromise = pgPromiseFactory();
    this.pgErrors = this.pgPromise.errors;
    this.database = this.pgPromise(this.config);
  }
github AraiEzzra / DDKCORE / shared / driver / db / index.ts View on Github external
import pgp, { IMain, IDatabase } from 'pg-promise';
import config from 'shared/config';

const connectionOptions = {
    host: config.DB.HOST,
    port: config.DB.PORT,
    database: config.DB.DATABASE,
    user: config.DB.USER,
    password: config.DB.PASSWORD,
    poolSize: config.DB.POOL_SIZE,
    poolIdleTimeout: config.DB.POOL_IDLE_TIMEOUT,
    reapIntervalMillis: config.DB.REAP_INTERVAL_MILLIS,
    logEvents: config.DB.LOG_EVENTS,
};

export const pgpE: IMain = pgp();

export class DatabaseConnector {
    static instance: DatabaseConnector = undefined;
    connector: IDatabase;

    constructor() {
        if (DatabaseConnector.instance === undefined) {
            this.connector = pgpE(connectionOptions);
            DatabaseConnector.instance = this;
        }
        return DatabaseConnector.instance;
    }
}

export default new DatabaseConnector().connector;
github apsavin / pgrights / src / models / DbConnection.js View on Github external
import { decorate, observable, flow, computed } from 'mobx';
import keytar from 'keytar';
import pgpFactory from 'pg-promise';
import DbSchema from './DbSchema';
import Fetcher from './Fetcher';

const pgp = pgpFactory();

export type TDbConnectionData = {
  name: string,
  database: string,
  host: string,
  port: string,
  user: string,
  password: string
};

class DbConnection {
  constructor({ name, database, host, port, user, password }: TDbConnectionData) {
    this.name = name;

    this.database = database;
    this.host = host;