How to use the pg-sql2.query function in pg-sql2

To help you get started, we’ve selected a few pg-sql2 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 graphile / postgraphile / src / postgraphile / withPostGraphileContext.ts View on Github external
const seenKeys: Array = [];
    // TODO:perf: looping backwards is slow
    for (let i = localSettings.length - 1; i >= 0; i--) {
      const [key, value] = localSettings[i];
      if (!seenKeys.includes(key)) {
        seenKeys.push(key);
        // Make sure that the third config is always `true` so that we are only
        // ever setting variables on the transaction.
        // Also, we're using `unshift` to undo the reverse-looping we're doing
        sqlSettings.unshift(sql.fragment`set_config(${sql.value(key)}, ${sql.value(value)}, true)`);
      }
    }
  }

  const sqlSettingsQuery =
    sqlSettings.length > 0 ? sql.compile(sql.query`select ${sql.join(sqlSettings, ', ')}`) : null;

  // If we can avoid transactions, we get greater performance.
  const needTransaction =
    pgForceTransaction ||
    !!sqlSettingsQuery ||
    (operationType !== 'query' && operationType !== 'subscription');

  // Now we've caught as many errors as we can at this stage, let's create a DB connection.
  const withAuthenticatedPgClient: WithAuthenticatedPgClientFunction = !needTransaction
    ? simpleWithPgClient(pgPool)
    : async cb => {
        // Connect a new Postgres client
        const pgClient = await pgPool.connect();

        // Begin our transaction
        await pgClient.query('begin');
github graphile / graphile-engine / packages / graphile-build-pg / src / plugins / viaTemporaryTable.ts View on Github external
const { rows } = result;
    const firstNonNullRow = rows.find(row => row !== null);
    // TODO: we should be able to have `pg` not interpret the results as
    // objects and instead just return them as arrays - then we can just do
    // `row[0]`. PR welcome!
    const firstKey = firstNonNullRow && Object.keys(firstNonNullRow)[0];
    const rawValues = rows.map(row => row && row[firstKey]);
    const values = rawValues.filter(rawValue => rawValue !== null);
    const sqlValuesAlias = sql.identifier(Symbol());
    const convertFieldBack = isPgClassLike
      ? sql.query`\
select (str::${sqlTypeIdentifier}).*
from unnest((${sql.value(values)})::text[]) str`
      : outputArgNames != null && outputArgTypes != null // It's a record
      ? sql.query`\
select ${sql.join(
          outputArgNames.map(
            (outputArgName, idx) =>
              sql.query`(${sqlValuesAlias}.output_value_list)[${sql.literal(
                idx + 1
              )}]::${sql.identifier(
                outputArgTypes[idx].namespaceName,
                outputArgTypes[idx].name
              )} as ${sql.identifier(
                // According to https://www.postgresql.org/docs/10/static/sql-createfunction.html,
                // "If you omit the name for an output argument, the system will choose a default column name."
                // In PG 9.x and 10, the column names appear to be assigned with a `column` prefix.
                outputArgName !== "" ? outputArgName : `column${idx + 1}`
              )}`
          ),

pg-sql2

Generate safe Postgres-compliant SQL with tagged template literals

MIT
Latest version published 2 years ago

Package Health Score

64 / 100
Full package analysis