Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

pg-format

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pg-format

Node.js implementation of PostgreSQL's format() to safely create dynamic SQL queries.

  • 0.1.3
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created

What is pg-format?

The pg-format npm package is used to safely create SQL query strings by formatting them with user-provided data. It helps prevent SQL injection attacks by properly escaping and quoting values.

What are pg-format's main functionalities?

String Formatting

This feature allows you to format strings safely by escaping and quoting values. In this example, the %L placeholder is used to safely insert a literal value into the SQL query.

const format = require('pg-format');
const sql = format('SELECT * FROM users WHERE id = %L', 123);
console.log(sql); // SELECT * FROM users WHERE id = '123'

Identifier Formatting

This feature allows you to safely format SQL identifiers such as table or column names. The %I placeholder is used to safely insert an identifier into the SQL query.

const format = require('pg-format');
const sql = format('SELECT %I FROM users', 'user_id');
console.log(sql); // SELECT "user_id" FROM users

Array Formatting

This feature allows you to format arrays of values safely. The %L placeholder is used to safely insert an array of literal values into the SQL query.

const format = require('pg-format');
const sql = format('SELECT * FROM users WHERE id IN (%L)', [1, 2, 3]);
console.log(sql); // SELECT * FROM users WHERE id IN ('1','2','3')

Composite Formatting

This feature allows you to format composite values such as arrays of arrays. The %L placeholder is used to safely insert composite values into the SQL query.

const format = require('pg-format');
const sql = format('INSERT INTO users (name, age) VALUES %L', [['John', 30], ['Jane', 25]]);
console.log(sql); // INSERT INTO users (name, age) VALUES ('John', 30), ('Jane', 25)

Other packages similar to pg-format

FAQs

Package last updated on 11 Dec 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc