Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
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)) {
/**
* 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;
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)
}
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 };
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) {
constructor(config: DatabaseConfig) {
this.config = config;
this.pgPromise = pgPromiseFactory();
this.pgErrors = this.pgPromise.errors;
this.database = this.pgPromise(this.config);
}
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;
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;