How to use pg-promise - 10 common examples

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 vitaly-t / pg-promise / test / typescript / errors.ts View on Github external
/// 

import * as pgPromise from 'pg-promise';

var result = new pgPromise.errors.QueryResultError();
var resultCheck = result instanceof pgPromise.errors.QueryResultError;

var query = new pgPromise.errors.QueryFileError();
var queryCheck = result instanceof pgPromise.errors.QueryFileError;
var line = query.error.position.line;

var ps = new pgPromise.errors.PreparedStatementError();
var queryCheck = result instanceof pgPromise.errors.PreparedStatementError;
var file = ps.error.file;

var qrec = pgPromise.errors.queryResultErrorCode;
var t = qrec.multiple;
github vitaly-t / pg-promise / test / typescript / prepared.ts View on Github external
var qf = new pgPromise.QueryFile('file');
var ps4 = new pgp.PreparedStatement({name: '', text: qf});

db.one(ps1);

db.one({
    name: '',
    text: ''
});

db.one({
    name: '',
    text: qf
});

var w = pgPromise.errors.PreparedStatementError;

// TODO: need to figure out how to export the type namespace;
//var test1 = ps1.parse();
github vitaly-t / pg-promise / test / typescript / errors.ts View on Github external
/// 

import * as pgPromise from 'pg-promise';

var result = new pgPromise.errors.QueryResultError();
var resultCheck = result instanceof pgPromise.errors.QueryResultError;

var query = new pgPromise.errors.QueryFileError();
var queryCheck = result instanceof pgPromise.errors.QueryFileError;
var line = query.error.position.line;

var ps = new pgPromise.errors.PreparedStatementError();
var queryCheck = result instanceof pgPromise.errors.PreparedStatementError;
var file = ps.error.file;

var qrec = pgPromise.errors.queryResultErrorCode;
var t = qrec.multiple;
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 / api / services / passwordReset.ts View on Github external
// verify and deactivate the token in one operation
        const usedToken = await db.one(
            `UPDATE
                user_password_reset_tokens
            SET used = true
            WHERE
            token = $1::uuid
            AND NOT used
            AND now() at time zone 'utc' < expires_on
            RETURNING *`, [token]
            );
        console.log('verified');

        return usedToken.user_id;
    } catch (err) {
        if (err instanceof errors.QueryResultError)  {
            console.log(err.code);
            if (err.code === errors.queryResultErrorCode.noData) return undefined;
        }

        throw err;
    }
}
github dstack-group / Butterfly / user-manager / user-manager-rest-api / src / utils / getSQLFile.ts View on Github external
schema, // replaces ${schema~} with the value of the `schema` argument
    },
  };

  const queryFile: QueryFile = new QueryFile(fullPath, options);

  if (queryFile.error) {
    // Something is wrong with our query file :(
    // Testing all files through queries can be cumbersome,
    // so we also report it here, while loading the module:
    // tslint:disable-next-line:no-console
    console.error(queryFile.error);
  }

  // @ts-ignore
  return queryFile[as.ctf.toPostgres]();
}
github parse-community / parse-server / src / Adapters / Storage / Postgres / sql / index.js View on Github external
function sql(file) {
  var fullPath = path.join(__dirname, file); // generating full path;

  var qf = new QueryFile(fullPath, { minify: true });

  if (qf.error) {
    throw qf.error;
  }

  return qf;
}
github nlf / muckraker / lib / index.js View on Github external
try {
    scripts = Fs.readdirSync(dir).filter((file) => Path.extname(file) === '.sql');
  }
  catch (e) {
    // let's not bother trying to get coverage for fs errors
    // $lab:coverage:off$
    if (e.code === 'ENOENT') {
      return queries;
    }

    throw e;
    // $lab:coverage:on$
  }

  for (const script of scripts) {
    queries[Path.basename(script, '.sql')] = new PG.QueryFile(Path.join(dir, script), { minify: true, debug: process.env.NODE_ENV !== 'production', noWarnings: true });
  }

  return queries;
};
github LiskHQ / lisk-sdk / framework / src / components / storage / adapters / pgp_adapter.js View on Github external
loadSQLFile(filePath, sqlDirectory = this.sqlDirectory) {
		const fullPath = path.join(sqlDirectory, filePath); // Generating full path;

		if (_private.queryFiles[fullPath]) {
			return _private.queryFiles[fullPath];
		}

		const options = {
			minify: true, // Minifies the SQL
		};

		const qf = new QueryFile(fullPath, options);

		if (qf.error) {
			this.logger.error({ err: qf.error }, 'SQL query file error'); // Something is wrong with our query file
			throw qf.error; // throw pg-promisse QueryFileError error
		}

		_private.queryFiles[fullPath] = qf;

		return qf;
	}