wilton.js  v202103141
 All Namespaces Functions
Functions
PGConnection Namespace Reference

wilton/PGConnection
Connect to PostgreSQL database. More...

Functions

Object PGConnection (String url, Function|Undefined callback)
 Open connection to database.

Detailed Description

This module allows to work with PostgreSQL database.

It implements a lightweight ORM - object-relational mapping, allows to map JavaScript objects to query parameters and to map query results to JavaScript objects.

Postgres connection can be closed manually to release system resource, otherwise it will be closed during the shutdown.

Usage example:

// open connection
var conn = new PGConnection("postgresql://host=127.0.0.1 port=5432 dbname=test user=test password=test");
// execute DDL
conn.execute("create table t1 (foo varchar, bar int)");
// execute DQL
var res = conn.queryList("select foo, bar from t1 where foo = :foo or bar = :bar order by bar", {
foo: "ccc",
bar: 42
});
// execute DML in transaction
conn.doInTransaction(function() {
conn.execute("insert into t1 values(:foo, :bar)", {
foo: "bbb",
bar: 42
});
});
// close connection
conn.close();

Function Documentation

Object PGConnection::PGConnection ( String  url,
Function|Undefined  callback 
)

Opens connection to database.

Parameters
urlString backend-specific connection URL, example: postgresql://host=127.0.0.1 port=5432 dbname=test user=test password=test,
callbackFunction|Undefined callback to receive result or error
Returns
Object pgsql instance