Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'pg-promise' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
///
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;
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();
///
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;
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)) {
// 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;
}
}
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]();
}
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;
}
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;
};
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;
}