2.0.61 • Published 14 days ago

@airthium/pg-format v2.0.61

Weekly downloads
-
License
MIT
Repository
github
Last release
14 days ago

node-pg-format

Fork from https://github.com/datalanche/node-pg-format

Node.js implementation of PostgreSQL format() to safely create dynamic SQL queries. SQL identifiers and literals are escaped to help prevent SQL injection. The behavior is equivalent to PostgreSQL format(). This module also supports Node buffers, arrays, and objects which is explained below.

Install

npm install pg-format

Example

var format = require('pg-format')
var sql = format(
  'SELECT * FROM %I WHERE my_col = %L %s',
  'my_table',
  34,
  'LIMIT 10'
)
console.log(sql) // SELECT * FROM my_table WHERE my_col = '34' LIMIT 10

API

format(fmt, ...)

Returns a formatted string based on fmt which has a style similar to the C function sprintf().

  • %% outputs a literal % character.
  • %I outputs an escaped SQL identifier.
  • %L outputs an escaped SQL literal.
  • %s outputs a simple string.

Argument position

You can define where an argument is positioned using n$ where n is the argument index starting at 1.

var format = require('pg-format')
var sql = format('SELECT %1$L, %1$L, %L', 34, 'test')
console.log(sql) // SELECT '34', '34', 'test'

Node Buffers

Node buffers can be used for literals (%L) and strings (%s), and will be converted to PostgreSQL bytea hex format.

Arrays and Objects

For arrays, each element is escaped when appropriate and concatenated to a comma-delimited string. Nested arrays are turned into grouped lists (for bulk inserts), e.g. ['a', 'b', 'c', 'd'] turns into ('a', 'b'), ('c', 'd'). Nested array expansion can be used for literals (%L) and strings (%s), but not identifiers (%I).
For objects, JSON.stringify() is called and the resulting string is escaped if appropriate. Objects can be used for literals (%L) and strings (%s), but not identifiers (%I). See the example below.

var format = require('pg-format')

var myArray = [1, 2, 3]
var myObject = { a: 1, b: 2 }
var myNestedArray = [
  ['a', 1],
  ['b', 2]
]

var sql = format(
  'SELECT * FROM t WHERE c1 IN (%L) AND c2 = %L',
  myArray,
  myObject
)
console.log(sql) // SELECT * FROM t WHERE c1 IN ('1','2','3') AND c2 = '{"a":1,"b":2}'

sql = format('INSERT INTO t (name, age) VALUES %L', myNestedArray)
console.log(sql) // INSERT INTO t (name, age) VALUES ('a', '1'), ('b', '2')

Testing

npm install
npm test
2.0.61

14 days ago

2.0.60

21 days ago

2.0.59

28 days ago

2.0.58

1 month ago

2.0.57

1 month ago

2.0.56

2 months ago

2.0.55

2 months ago

2.0.54

2 months ago

2.0.53

3 months ago

2.0.52

3 months ago

2.0.49

4 months ago

2.0.51

4 months ago

2.0.50

4 months ago

2.0.48

4 months ago

2.0.47

4 months ago

2.0.46

4 months ago

2.0.45

5 months ago

2.0.44

5 months ago

2.0.43

6 months ago

2.0.39

7 months ago

2.0.42

6 months ago

2.0.40

6 months ago

2.0.41

6 months ago

2.0.38

7 months ago

2.0.28

9 months ago

2.0.29

9 months ago

2.0.37

7 months ago

2.0.35

7 months ago

2.0.36

7 months ago

2.0.33

8 months ago

2.0.34

8 months ago

2.0.31

8 months ago

2.0.32

8 months ago

2.0.30

9 months ago

2.0.26

10 months ago

2.0.27

10 months ago

2.0.24

10 months ago

2.0.25

10 months ago

2.0.23

11 months ago

2.0.18

12 months ago

2.0.22

11 months ago

2.0.20

11 months ago

2.0.21

11 months ago

2.0.17

12 months ago

2.0.15

1 year ago

2.0.16

12 months ago

2.0.13

1 year ago

2.0.14

1 year ago

2.0.11

1 year ago

2.0.12

1 year ago

2.0.3

1 year ago

2.0.5

1 year ago

2.0.4

1 year ago

2.0.7

1 year ago

2.0.6

1 year ago

2.0.9

1 year ago

2.0.10

1 year ago

2.0.8

1 year ago

2.0.2

1 year ago

2.0.1

2 years ago

2.0.0

2 years ago

1.0.9

2 years ago