ParameterizedQuery

new ParameterizedQuery(optionsopt) → {ParameterizedQuery}

Constructs a new ParameterizedQuery object. All properties can also be set after the object's construction.

This type extends the basic {text, values} object, i.e. when the basic object is used with a query method, a new ParameterizedQuery object is created in its place.

The type can be used in place of the query parameter, with any query method directly.

The type is available from the library's root: pgp.ParameterizedQuery.

Parameters:
Name Type Attributes Description
options string | QueryFile | Object <optional>

Object configuration options / properties.

Properties
Name Type Attributes Description
text string | QueryFile <optional>

See property text.

values array <optional>

See property values.

binary boolean <optional>

See property binary.

rowMode string <optional>

See property rowMode.

Source:
See:
Returns:
Type
ParameterizedQuery
Examples
const {ParameterizedQuery: PQ} = require('pg-promise');

// Creating a complete Parameterized Query with parameters:
const findUser = new PQ({text: 'SELECT * FROM Users WHERE id = $1', values: [123]});

db.one(findUser)
    .then(user => {
        // user found;
    })
    .catch(error => {
        // error;
    });
const {ParameterizedQuery: PQ} = require('pg-promise');

// Creating a reusable Parameterized Query without values:
const addUser = new PQ('INSERT INTO Users(name, age) VALUES($1, $2)');

// setting values explicitly:
addUser.values = ['John', 30];

db.none(addUser)
    .then(() => {
        // user added;
    })
    .catch(error=> {
        // error;
    });

// setting values implicitly, by passing them into the query method:
db.none(addUser, ['Mike', 25])
    .then(() => {
        // user added;
    })
    .catch(error=> {
        // error;
    });

Members

binary :boolean

Activates binary result mode. The default is the text mode.

Type:
  • boolean
Default Value:
  • undefined
Source:
See:

(readonly) error :errors.ParameterizedQueryError

When in an error state, it is set to a ParameterizedQueryError object. Otherwise, it is undefined.

This property is primarily for internal use by the library.

Type:
Default Value:
  • undefined
Source:

rowMode :string

Changes the way data arrives to the client, with only one value supported by pg:

  • array will make all data rows arrive as arrays of values. By default, rows arrive as objects.
Type:
  • string
Default Value:
  • undefined
Source:

text :string|QueryFile

A non-empty query string or a QueryFile object.

Only the basic variables ($1, $2, etc) can be used in the query, because Parameterized Queries are formatted on the server side.

Type:
Source:

values :array

Query formatting parameters, depending on the type:

  • null / undefined means the query has no formatting parameters
  • Array - it is an array of formatting parameters
  • None of the above, means it is a single formatting value, which is then automatically wrapped into an array
Type:
  • array
Source:

Methods

parse() → {Object|errors.ParameterizedQueryError}

Parses the current object and returns a simple {text, values}, if successful, or else it returns a ParameterizedQueryError object.

This method is primarily for internal use by the library.

Source:
Returns:
Type
Object | errors.ParameterizedQueryError

toString(levelopt) → {string}

Creates a well-formatted multi-line string that represents the object's current state.

It is called automatically when writing the object into the console.

Parameters:
Name Type Attributes Default Description
level number <optional>
0

Nested output level, to provide visual offset.

Source:
Returns:
Type
string