How to use the pg-promise/lib/formatting.formatQuery 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 apsavin / pgrights / src / models / DbPolicy.js View on Github external
return policy.db.tx(async t => {
          const { name, schemaName, tableName } = policy;
          const dropNames = { name, schemaName, tableName };

          await t.none(`drop policy if exists $(name:name) on $(schemaName:name).$(tableName:name);`, dropNames);

          const names = { name: dataForUpdate.name, schemaName, tableName };
          const policyQueryPart = formatQuery('$(name:name) on $(schemaName:name).$(tableName:name)', names);

          const typeQueryPart = dataForUpdate.permissive ? '' : 'as restrictive';

          const commandAndRoles = {
            command: dataForUpdate.command,
            roles: dataForUpdate.roles,
          };
          const forQueryPart = formatQuery(`for $(command:raw) to $(roles:name)`, commandAndRoles);

          const qualifierQueryPart = dataForUpdate.qualifier ? `using (${dataForUpdate.qualifier})` : '';
          const checkQueryPart = dataForUpdate.check ? `with check (${dataForUpdate.check})` : '';

          await t.none(
            `create policy ${policyQueryPart} ${typeQueryPart} ${forQueryPart} ${qualifierQueryPart} ${checkQueryPart};`
          );
        });
      },
github apsavin / pgrights / src / models / DbPolicy.js View on Github external
return policy.db.tx(async t => {
          const { name, schemaName, tableName } = policy;
          const dropNames = { name, schemaName, tableName };

          await t.none(`drop policy if exists $(name:name) on $(schemaName:name).$(tableName:name);`, dropNames);

          const names = { name: dataForUpdate.name, schemaName, tableName };
          const policyQueryPart = formatQuery('$(name:name) on $(schemaName:name).$(tableName:name)', names);

          const typeQueryPart = dataForUpdate.permissive ? '' : 'as restrictive';

          const commandAndRoles = {
            command: dataForUpdate.command,
            roles: dataForUpdate.roles,
          };
          const forQueryPart = formatQuery(`for $(command:raw) to $(roles:name)`, commandAndRoles);

          const qualifierQueryPart = dataForUpdate.qualifier ? `using (${dataForUpdate.qualifier})` : '';
          const checkQueryPart = dataForUpdate.check ? `with check (${dataForUpdate.check})` : '';

          await t.none(
            `create policy ${policyQueryPart} ${typeQueryPart} ${forQueryPart} ${qualifierQueryPart} ${checkQueryPart};`
          );
        });