pg-promise (master) doxdox documentation

PostgreSQL interface for Node.js

# attachModuleSymbols(doclets, modules)

private method

Look for classes or functions with the same name as modules (which indicates that the module exports only that class or function), then attach the classes or functions to the module property of the appropriate module doclets. The name of each class or function is also updated for display purposes. This function mutates the original arrays.

Parameters

Name Types Description
doclets Array.<module:jsdoc/doclet.Doclet> - The array of classes and functions to check.
modules Array.<module:jsdoc/doclet.Doclet> - The array of module doclets to search.

# buildNav(members, members.classes, members.externals, members.globals, members.mixins, members.modules, members.namespaces, members.tutorials, members.events, members.interfaces)

Create the navigation sidebar.

Parameters

Name Types Description
members object The members that will be used to create the sidebar.
members.classes array<object>
members.externals array<object>
members.globals array<object>
members.mixins array<object>
members.modules array<object>
members.namespaces array<object>
members.tutorials array<object>
members.events array<object>
members.interfaces array<object>

Returns

string

The HTML for the navigation sidebar.

# exports.publish(taffyData, opts, tutorials)

Parameters

Name Types Description
taffyData TAFFY See http://taffydb.com/.
opts object
tutorials Tutorial

# ConnectionContext(cc, cc.cn, cc.dc, cc.options, cc.db, cc.level, cc.txLevel, cc.parentCtx)

private method

Parameters

Name Types Description
cc object Connection Context.
cc.cn object Connection details
cc.dc * Database Context
cc.options object Library's Initialization Options
cc.db object Database Session we're attached to, if any.
cc.level number Task Level
cc.txLevel number Transaction Level
cc.parentCtx object Connection Context of the parent operation, if any.

# module.exports()

private method

Connection Context

# createContextKey(db)

Parameters

Name Types Description
db Database - Database instance.

# DatabasePool()

private method

# instance()

Global instance of the database pool repository.

# register(db)

Parameters

Name Types Description
db Database - The new Database object being registered.

# shutDown()

# unregister(db)

Parameters

Name Types Description
db

# Database(cn, dc)

Parameters

Name Types Description
cn string object
dc * Database Context. Any object or value to be propagated through the protocol, to allow implementations and event handling that depend on the database context. This is mainly to facilitate the use of multiple databases which may need separate protocol extensions, or different implementations within a single task / transaction callback, depending on the database context. This parameter also adds uniqueness to the connection context that's used in combination with the connection parameters, i.e. use of unique database context will prevent getting the warning about creating a duplicate Database object. The value can be accessed from the database object via property {@link Database#$dc $dc}.

# npm.utils.addReadProp()

# npm.utils.addReadProp()

# npm.utils.addReadProp()

# npm.utils.addReadProp()

# npm.utils.addReadProp()

private method

# obj.any(query, values)

Parameters

Name Types Description
query string function
values array value

# obj.each(query, values, cb, thisArg)

Parameters

Name Types Description
query string function
values array value
cb function Function to execute for each row, taking three arguments: - row - the current row object being processed in the array - index - the index of the current row being processed in the array - data - the array of rows resolved by method {@link Database#any any}
thisArg * Value to use as this when executing the callback.

# obj.func(funcName, values, qrm)

Parameters

Name Types Description
funcName string Name of the function to be executed. When it is not same-case, or contains extended symbols, it is double-quoted, as per the :alias filter, which also supports ., to auto-split into a composite name.
values array value
qrm queryResult - {@link queryResult Query Result Mask}.

# obj.many(query, values)

Parameters

Name Types Description
query string function
values array value

# obj.manyOrNone(query, values)

Parameters

Name Types Description
query string function
values array value

# obj.map(query, values, cb, thisArg)

Parameters

Name Types Description
query string function
values array value
cb function Function that produces an element of the new array, taking three arguments: - row - the current row object being processed in the array - index - the index of the current row being processed in the array - data - the original array of rows resolved by method {@link Database#any any}
thisArg * Value to use as this when executing the callback.

# obj.multi(query, values)

Parameters

Name Types Description
query string function
values array value

# obj.multiResult(query, values)

Parameters

Name Types Description
query string function
values array value

# obj.none(query, values)

Parameters

Name Types Description
query string function
values array value

# obj.one(query, values, cb, thisArg)

Parameters

Name Types Description
query string function
values array value
cb function Value-transformation callback, to allow in-line value change. When specified, the returned value replaces the original one. The function takes only one parameter - value resolved from the query.
thisArg * Value to use as this when executing the transformation callback.

# obj.oneOrNone(query, values, cb, thisArg)

Parameters

Name Types Description
query string function
values array value
cb function Value-transformation callback, to allow in-line value change. When specified, the returned value replaces the original one. The function takes only one parameter - value resolved from the query.
thisArg * Value to use as this when executing the transformation callback.

# obj.proc(procName, values, cb, thisArg)

Parameters

Name Types Description
procName string Name of the stored procedure to be executed. When it is not same-case, or contains extended symbols, it is double-quoted, as per the :alias filter, which also supports ., to auto-split into a composite SQL name.
values array value
cb function Value-transformation callback, to allow in-line value change. When specified, the returned value replaces the original one. The function takes only one parameter - value resolved from the query.
thisArg * Value to use as this when executing the transformation callback.

# obj.result(query, values, cb, thisArg)

Parameters

Name Types Description
query string function
values array value
cb function Value-transformation callback, to allow in-line value change. When specified, the returned value replaces the original one. The function takes only one parameter - value resolved from the query.
thisArg * Value to use as this when executing the transformation callback.

# obj.stream(qs, initCB)

Parameters

Name Types Description
qs QueryStream Stream object of type $[QueryStream].
initCB Database.streamInitCB Stream initialization callback. It is invoked with the same this context as the calling method.

# obj.task(options, options.tag, cb)

Parameters

Name Types Description
options string number
options.tag Traceable context for the task (see $[tags]).
cb function Task callback function, to return the result that will determine either success or failure for the operation. The function can be either the first of the second parameter passed into the method. It also can be an ES7 async function.

# obj.taskIf(options, options.tag, options.cnd, cb)

Parameters

Name Types Description
options string number
options.tag Traceable context for the task/transaction (see $[tags]).
options.cnd boolean function
cb function Task callback function, to return the result that will determine either success or failure for the operation. The function can be either the first or the second parameter passed into the method. It also can be an ES7 async function.

# obj.tx(options, options.tag, options.mode, cb)

Parameters

Name Types Description
options string number
options.tag Traceable context for the transaction (see $[tags]).
options.mode txMode.TransactionMode Transaction Configuration Mode - extends the transaction-opening command with additional configuration.
cb function Transaction callback function, to return the result that will determine either success or failure for the operation. The function can be either the first of the second parameter passed into the method. It also can be an ES7 async function.

# obj.txIf(options, options.tag, options.mode, options.cnd, options.reusable, cb)

Parameters

Name Types Description
options string number
options.tag Traceable context for the task/transaction (see $[tags]).
options.mode txMode.TransactionMode Transaction Configuration Mode - extends the transaction-opening command with additional configuration.
options.cnd boolean function
options.reusable boolean function
cb function Transaction/task callback function, to return the result that will determine either success or failure for the operation. The function can be either the first or the second parameter passed into the method. It also can be an ES7 async function.

# this.connect(options, options.direct, options.onLost)

Parameters

Name Types Description
options object Connection Options.
options.direct boolean Creates a new connection directly, as a stand-alone {@link external:Client Client} object, bypassing the connection pool. By default, all connections are acquired from the connection pool. But if you set this option, the library will instead create a new {@link external:Client Client} object directly (separately from the pool), and then call its connect method. Note that specifically for direct connections, method done returns a {@link external:Promise Promise}, because those connections are closed physically, which may take time. WARNING: Do not use this option for regular query execution, because it exclusively occupies one physical channel, and it cannot scale. This option is only suitable for global connection usage, such as event listeners.
options.onLost function Notification callback of the lost/broken connection, called with the following parameters: - err - the original connectivity error - e - error context object, which contains: - cn - safe connection string/config (with the password hashed); - dc - Database Context, as was used during {@link Database} construction; - start - Date/Time (Date type) when the connection was established; - client - {@link external:Client Client} object that has lost the connection. The notification is mostly valuable with direct: true, to be able to re-connect direct/permanent connections by calling method {@link Database#connect connect} again. You do not need to call done on lost connections, as it happens automatically. However, if you had event listeners set up on the connection's client object, you should remove them to avoid leaks: js function onLostConnection(err, e) { e.client.removeListener('my-event', myHandler); } For a complete example see $[Robust Listeners].

# this.query(query, values, qrm)

Parameters

Name Types Description
query string function
values array value
qrm queryResult {@link queryResult Query Result Mask}

# module.exports()

# ParameterizedQueryError()

# ParameterizedQueryError.toString(level)

Parameters

Name Types Description
level number Nested output level, to provide visual offset.

# PreparedStatementError()

# PreparedStatementError.toString(level)

Parameters

Name Types Description
level number Nested output level, to provide visual offset.

# QueryFileError()

# QueryFileError.toString(level)

Parameters

Name Types Description
level number Nested output level, to provide visual offset.

# multiple()

Multiple rows were not expected.

# noData()

No data returned from the query.

# notEmpty()

No return data was expected.

# QueryResultError()

# QueryResultError.toString(level)

Parameters

Name Types Description
level number Nested output level, to provide visual offset.

# queryResultErrorCode()

# connect(e, e.client, e.dc, e.useCount)

Parameters

Name Types Description
e {} Event Properties
e.client external:Client $[pg.Client] object that represents the connection.
e.dc * Database Context that was used when creating the database object (see {@link Database}).
e.useCount number Number of times the connection has been previously used, starting with 0, for a freshly allocated physical connection. This parameter is always 0 for direct connections (created by calling {@link Database#connect Database.connect} with parameter {direct: true}).

# disconnect(e, e.client, e.dc)

Parameters

Name Types Description
e {} Event Properties
e.client external:Client - $[pg.Client] object that represents connection with the database.
e.dc * - Database Context that was used when creating the database object (see {@link Database}).

# error(err, e)

Parameters

Name Types Description
err * The error encountered, of the same value and type as it was reported.
e EventContext Event Context Object.

# extend(obj, dc)

Parameters

Name Types Description
obj object - Protocol object to be extended.
dc * - Database Context that was used when creating the {@link Database} object.

# k(e)

Parameters

Name Types Description
e EventContext Event Context Object.

# query(e)

Parameters

Name Types Description
e EventContext Event Context Object.

# receive(e, e.data, e.result, e.ctx)

Parameters

Name Types Description
e {} Event Properties
e.data Array<Object> Array of received objects/rows. If any of those objects are modified during notification, the client will receive the modified data.
e.result external:Result - Original $[Result] object, if the data is from a non-stream query, in which case data = result.rows. For single-query requests, $[Result] object is extended with property duration - number of milliseconds it took to send the query, execute it and get the result back. - It is undefined when the data comes from a stream (method {@link Database#stream Database.stream}).
e.ctx EventContext Event Context Object.

# transact(e)

Parameters

Name Types Description
e EventContext Event Context Object.

# unexpected(event, e)

private method

Parameters

Name Types Description
event string - unhandled event name.
e string Error

# alias(name)

Parameters

Name Types Description
name string function

# array(arr, options, options.capSQL)

Parameters

Name Types Description
arr Array function
options {} Array-Formatting Options.
options.capSQL boolean When true, outputs ARRAY instead of array.

# bool(value)

Parameters

Name Types Description
value boolean function

# buffer(obj, raw)

Parameters

Name Types Description
obj Buffer function
raw boolean Indicates when not to wrap the resulting string in quotes. The generated hex string doesn't need to be escaped.

# csv(values)

Parameters

Name Types Description
values Array Object

# ctf()

# date(d, raw)

Parameters

Name Types Description
d Date function
raw boolean Indicates when not to escape the value.

# format(query, values, options, options.capSQL, options.partial, options.def)

Parameters

Name Types Description
query string QueryFile
values array object
options {} Formatting Options.
options.capSQL boolean Formats reserved SQL words capitalized. Presently, this only concerns arrays, to output ARRAY when required.
options.partial boolean Indicates that we intend to do only a partial replacement, i.e. throw no error when encountering a variable or property name that's missing within the formatting parameters. NOTE: This option has no meaning when option def is used.
options.def * Sets default value for every variable that's missing, consequently preventing errors when encountering a variable or property name that's missing within the formatting parameters. It can also be set to a function, to be called with two parameters that depend on the type of formatting being used, and to return the actual default value: - For $[Named Parameters] formatting: - name - name of the property missing in the formatting object - obj - the formatting object, and is the same as this context - For $[Index Variables] formatting: - index - element's index (starts with 1) that's outside the input array - arr - the formatting/input array, and is the same as this context You can tell which type of call it is by checking the type of the first parameter.

# func(func, raw, cc)

Parameters

Name Types Description
func function Function to be called, with support for nesting.
raw boolean Indicates when not to escape the result.
cc * Calling Context: this + the only value to be passed into the function on all nested levels.

# json(data, raw)

Parameters

Name Types Description
data * Object/value to be converted, or a function that returns it.
raw boolean Indicates when not to escape the result.

# name(name)

Parameters

Name Types Description
name string function

# number(num)

Parameters

Name Types Description
num number bigint

# text(value, raw)

Parameters

Name Types Description
value value function
raw boolean Indicates when not to escape the resulting text.

# value(value)

Parameters

Name Types Description
value value function

# Array.isArray()

# ColumnSet(columns, options, options.table, options.inherit)

Parameters

Name Types Description
columns object helpers.Column
options object
options.table helpers.TableName string
options.inherit boolean Use inherited properties in addition to the object's own properties. By default, only the object's own properties are enumerated for column names.

# ColumnSet.assign(options, options.source, options.prefix)

Parameters

Name Types Description
options object Assignment/formatting options.
options.source object Source - a single object that contains values for columns. The object is only necessary to correctly apply the logic of skipping columns dynamically, based on the source data and the rules defined in the {@link helpers.ColumnSet ColumnSet}. If, however, you do not care about that, then you do not need to specify any object. Note that even if you do not specify the object, the columns marked as conditional (cnd: true) will always be skipped.
options.prefix string In cases where needed, an alias prefix to be added before each column.

# ColumnSet.assignColumns(options, options.from, options.to, options.skip)

Parameters

Name Types Description
options {} Optional Parameters.
options.from string Alias for the source columns.
options.to string Alias for the destination columns.
options.skip string Array<string>

# ColumnSet.extend(columns)

Parameters

Name Types Description
columns helpers.Column helpers.ColumnSet

# ColumnSet.merge(columns)

Parameters

Name Types Description
columns helpers.Column helpers.ColumnSet

# ColumnSet.prepare(source)

Parameters

Name Types Description
source object The source object to be prepared, if required. It must be a non-null object, which the method does not validate, as it is intended primarily for internal use by the library.

# ColumnSet.toString(level)

Parameters

Name Types Description
level number Nested output level, to provide visual offset.

# names()

# variables()

# Column(col)

Parameters

Name Types Description
col string helpers.ColumnConfig

# Column.toString(level)

Parameters

Name Types Description
level number Nested output level, to provide visual offset.

# escapedName()

# module.exports(col, col.source, col.name, col.value, col.exists)

Parameters

Name Types Description
col * Column-to-property descriptor.
col.source object The source object, equals to this that's passed into the function.
col.name string Resolved name of the property within the source object, i.e. the value of name when prop is not used for the column, or the value of prop when it is specified.
col.value * Property value, set to one of the following: - Value of the property within the source object (value = source[name]), if the property exists - If the property doesn't exist and def is set in the column, then value is set to the value of def - If the property doesn't exist and def is not set in the column, then value is set to undefined
col.exists boolean Indicates whether the property exists in the source object (exists = name in source).

# tText()

# variable()

# module.exports()

# concat(queries)

Parameters

Name Types Description
queries array<string helpers.QueryFormat

# insert(data, columns, table)

Parameters

Name Types Description
data object object[]
columns array helpers.Column
table helpers.TableName string

# sets(data, columns)

Parameters

Name Types Description
data object A simple, non-null and non-array source object. If it is anything else, the method will throw {@link external:TypeError TypeError} = Invalid parameter 'data' specified.
columns array helpers.Column

# update(data, columns, table, options, options.tableAlias, options.valueAlias, options.emptyUpdate)

Parameters

Name Types Description
data object object[]
columns array helpers.Column
table helpers.TableName string
options {} An object with formatting options for multi-row UPDATE queries.
options.tableAlias string Name of the SQL variable that represents the destination table.
options.valueAlias string Name of the SQL variable that represents the values.
options.emptyUpdate * This is a convenience option, to avoid throwing an error when generating a conditional update results in no columns. When present, regardless of the value, this option overrides the method's behavior when applying skip logic results in no columns, i.e. when every column is being skipped. By default, in that situation the method throws {@link external:Error Error} = Cannot generate an UPDATE without any columns. But when this option is present, the method will instead return whatever value the option was passed.

# values(data, columns)

Parameters

Name Types Description
data object object[]
columns array helpers.Column

# TableName(table)

Parameters

Name Types Description
table string object

# TableName(self)

Parameters

Name Types Description
self helpers.TableName Optional self-reference, for ES6 arrow functions.

# TableName.toString()

# extendState()

Extends or overrides inner state with the specified properties. Only own properties are used, i.e. inherited ones are skipped.

# InnerState()

private method

# module.exports()

# inst.pg()

# main()

# npm.errors()

# npm.formatting.as()

# npm.minify()

# npm.mode()

# npm.pubUtils()

# npm.utils.addReadProp()

# npm.utils.addReadProp()

# npm.utils.addReadProp()

# PromiseAdapter(api, api.create, api.resolve, api.reject, api.all)

Parameters

Name Types Description
api object Promise API configuration object. Passing in anything other than an object will throw {@link external:TypeError TypeError} = Adapter requires an api configuration object.
api.create function A function that takes a callback parameter and returns a new promise object. The callback parameter is expected to be function(resolve, reject). Passing in anything other than a function will throw {@link external:TypeError TypeError} = Function 'create' must be specified.
api.resolve function A function that takes an optional data parameter and resolves a promise with it. Passing in anything other than a function will throw {@link external:TypeError TypeError} = Function 'resolve' must be specified.
api.reject function A function that takes an optional error parameter and rejects a promise with it. Passing in anything other than a function will throw {@link external:TypeError TypeError} = Function 'reject' must be specified.
api.all function A function that resolves an array of promises. Passing in anything other than a function will throw {@link external:TypeError TypeError} = Function 'all' must be specified.

# error()

# file()

# instance()

Global instance of the file-path repository.

Returns

{usedPath: {}}

# options()

# prepare(throwErrors)

Parameters

Name Types Description
throwErrors boolean Throw any error encountered.

# QueryFile(file, options)

Parameters

Name Types Description
file string Path to the SQL file with the query, either absolute or relative to the application's entry point file. If there is any problem reading the file, it will be reported when executing the query.
options QueryFile.Options Set of configuration options, as documented by {@link QueryFile.Options}.

# QueryFile(self)

Parameters

Name Types Description
self QueryFile Optional self-reference, for ES6 arrow functions.

# QueryFile.toString(level)

Parameters

Name Types Description
level number Nested output level, to provide visual offset.

# any()

many|none - any result is expected, to be resolved with an array of rows-objects.

# many()

One or more rows expected, to be resolved as an array, with at least 1 row-object.

# none()

Expecting no rows, to be resolved with null.

# one()

Single row is expected, to be resolved as a single row-object.

# queryResult()

# callback(ctx, obj, cb, config)

private method

Parameters

Name Types Description
ctx
obj
cb
config

# execute(ctx, obj, isTX, config)

private method

Parameters

Name Types Description
ctx
obj
isTX
config

# Task()

# this.batch(values, options, options.cb)

Parameters

Name Types Description
values array
options Object Optional Parameters.
options.cb function

# this.ctx()

# this.page(source, options, options.dest, options.limit)

Parameters

Name Types Description
source function
options Object Optional Parameters.
options.dest function
options.limit number

# this.sequence(source, options, options.dest, options.limit, options.track)

Parameters

Name Types Description
source function
options Object Optional Parameters.
options.dest function
options.limit number
options.track boolean

# begin(cap)

Parameters

Name Types Description
cap boolean Indicates whether the returned SQL must be capitalized.

# isolationLevel()

# module.exports()

# none()

Isolation level not specified.

# readCommitted()

ISOLATION LEVEL READ COMMITTED

# repeatableRead()

ISOLATION LEVEL REPEATABLE READ

# serializable()

ISOLATION LEVEL SERIALIZABLE

# TransactionMode(options, options.tiLevel, options.readOnly, options.deferrable)

Parameters

Name Types Description
options Transaction Mode options.
options.tiLevel txMode.isolationLevel Transaction Isolation Level.
options.readOnly boolean Sets transaction access mode based on the read-only flag: - undefined - access mode not specified (default) - true - access mode is set to READ ONLY - false - access mode is set to READ WRITE
options.deferrable boolean Sets transaction deferrable mode based on the boolean value: - undefined - deferrable mode not specified (default) - true - mode is set to DEFERRABLE - false - mode is set to NOT DEFERRABLE It is used only when tiLevel=isolationLevel.serializable and readOnly=true, or else it is ignored.

# ParameterizedQuery(options, options.text, options.values, options.binary, options.rowMode)

Parameters

Name Types Description
options string QueryFile
options.text string QueryFile
options.values array - See property {@link ParameterizedQuery#values values}.
options.binary boolean - See property {@link ParameterizedQuery#binary binary}.
options.rowMode string - See property {@link ParameterizedQuery#rowMode rowMode}.

# ParameterizedQuery.parse()

# ParameterizedQuery.toString(level)

Parameters

Name Types Description
level number Nested output level, to provide visual offset.

# name()

# PreparedStatement(options, options.name, options.text, options.values, options.binary, options.rowMode, options.rows)

Parameters

Name Types Description
options Object Object configuration options / properties.
options.name string - See property {@link PreparedStatement#name name}.
options.text string QueryFile
options.values array - See property {@link PreparedStatement#values values}.
options.binary boolean - See property {@link PreparedStatement#binary binary}.
options.rowMode string - See property {@link PreparedStatement#rowMode rowMode}.
options.rows number - See property {@link PreparedStatement#rows rows}.

# PreparedStatement.parse()

# PreparedStatement.toString(level)

Parameters

Name Types Description
level number Nested output level, to provide visual offset.

# rows()

# ServerFormatting()

private method

# setValues()

# camelize(text)

Parameters

Name Types Description
text string Input text string.

# camelizeVar(text)

Parameters

Name Types Description
text string Input text string. If it doesn't contain any symbols to make up a valid variable name, the result will be an empty string.

# enumSql(dir, options, options.recursive, options.ignoreErrors, cb)

Parameters

Name Types Description
dir string Directory path where SQL files are located, either absolute or relative to the current directory. SQL files are identified by using .sql extension (case-insensitive).
options {} Search options.
options.recursive boolean Include sub-directories into the search. Sub-directories without SQL files will be skipped from the result.
options.ignoreErrors boolean Ignore the following types of errors: - access errors, when there is no read access to a file or folder - empty or duplicate camelized property names This flag does not affect errors related to invalid input parameters, or if you pass in a non-existing directory.
cb function A callback function that takes three arguments: - file - SQL file path, relative or absolute, according to how you specified the search directory - name - name of the property that represents the SQL file - path - property resolution path (full property name) If the function returns anything other than undefined, it overrides the corresponding property value in the tree.

# module.exports()

# taskArgs(args)

Parameters

Name Types Description
args {Object} Array-like object of arguments that was passed into the method. It is expected that the arguments are always made of two parameters - (options, cb), same as all the default task/transaction methods. And if your custom method needs additional parameters, they should be passed in as extra properties within options.