Skip to content
This repository was archived by the owner on Mar 25, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions Makefile

This file was deleted.

39 changes: 20 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ Node.js implementation of [PostgreSQL format()](http://www.postgresql.org/docs/9

## Install

npm install pg-format
npm install node-pg-format

## Example
```js
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
import { format } from 'node-pg-format';
const 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
Expand All @@ -26,15 +26,15 @@ Returns a formatted string based on ```fmt``` which has a style similar to the C
#### Argument position
You can define where an argument is positioned using ```n$``` where ```n``` is the argument index starting at 1.
```js
var format = require('pg-format');
var sql = format('SELECT %1$L, %1$L, %L', 34, 'test');
console.log(sql); // SELECT '34', '34', 'test'
import { format } from 'node-pg-format';
const sql = format('SELECT %1$L, %1$L, %L', 34, 'test');
console.log(sql); // SELECT 34, 34, 'test'
```

### format.config(cfg)
Changes the global configuration. You can change which letters are used to denote identifiers, literals, and strings in the formatted string. This is useful when the formatted string contains a PL/pgSQL function which calls [PostgreSQL format()](http://www.postgresql.org/docs/9.3/static/functions-string.html#FUNCTIONS-STRING-FORMAT) itself.
```js
var format = require('pg-format');
import { format } from 'node-pg-format';
format.config({
pattern: {
ident: 'V',
Expand All @@ -45,16 +45,16 @@ format.config({
format.config(); // reset to default
```

### format.ident(input)
### quoteIdent(input)
Returns the input as an escaped SQL identifier string. ```undefined```, ```null```, and objects will throw an error.

### format.literal(input)
### quoteLiteral(input)
Returns the input as an escaped SQL literal string. ```undefined``` and ```null``` will return ```'NULL'```;

### format.string(input)
### quoteString(input)
Returns the input as a simple string. ```undefined``` and ```null``` will return an empty string. If an array element is ```undefined``` or ```null```, it will be removed from the output string.

### format.withArray(fmt, array)
### formatWithArray(fmt, array)
Same as ```format(fmt, ...)``` except parameters are provided in an array rather than as function arguments. This is useful when dynamically creating a SQL query and the number of parameters is unknown or variable.

## <a name="buffer"></a> Node Buffers
Expand All @@ -65,22 +65,23 @@ For arrays, each element is escaped when appropriate and concatenated to a comma
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.

```js
var format = require('pg-format');
import { format } from 'node-pg-format';

var myArray = [ 1, 2, 3 ];
var myObject = { a: 1, b: 2 };
var myNestedArray = [['a', 1], ['b', 2]];
const myArray = [ 1, 2, 3 ];
const myObject = { a: 1, b: 2 };
const 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}'
let 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')
console.log(sql); // INSERT INTO t (name, age) VALUES ('a', 1), ('b', 2)
```

## Testing

```
npm install
npm run build
npm test
```
6 changes: 6 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export declare function quoteIdent(value: any): string;
export declare function quoteLiteral(value: any): string;
export declare function quoteString(value: any): string;
export declare function config(cfg: any): void;
export declare function formatWithArray(fmt: any, parameters: any): any;
export declare function format(fmt: string, ...args: any[]): string;
Loading