2.11.0 • Published 9 days ago

postgresql-client v2.11.0

Weekly downloads
211
License
MIT
Repository
github
Last release
9 days ago

postgresql-client

NPM Version NPM Downloads CircleCI Test Coverage

Enterprise level PostgreSQL client for NodeJs

Library

  • Pure JavaScript library completely written in TypeScript
  • Works with both CommonJS and ESM module systems
  • Well tested
  • Strictly typed
  • Asynchronous Promise based api

Features

  • Both single connection and advanced pooling support
  • Full binary wire protocol support for all data types
  • Named Prepared Statements
  • Cursors with fast double-link cache
  • High level implementation for notifications (LISTEN/NOTIFY)
  • Extensible data-types and type mapping
  • Bind parameters with OID mappings
  • Multidimensional arrays with fast binary encoding/decoding
  • Low memory utilization and boosted performance with Shared Buffers
  • Supports Clear text, MD5 and SASL password algorithms
  • Can return both array and object rows
  • Auto disposal with "using" syntax (TC30 Explicit Resource Management)

Installation

$ npm install postgresql-client --save

Documentation

Please read :small_orange_diamond: DOCUMENTATION :small_orange_diamond: for detailed usage.

Example usage

Establish a single connection, execute a simple query

import {Connection} from 'postgresql-client';
// Create connection
const connection = new Connection('postgres://localhost');
// Connect to database server
await connection.connect();

// Execute query and fetch rows
const result = await connection.query(
    'select * from cities where name like $1',
    {params: ['%york%']});
const rows: any[] = result.rows;
// Do what ever you want with rows

// Disconnect from server
await connection.close(); 

Establish a pooled connection, create a cursor

import {Pool} from 'postgresql-client';

// Create connection pool
const db = new Pool({
    host: 'postgres://localhost',
    pool: {
       min: 1,
       max: 10,
       idleTimeoutMillis: 5000
    }
});

// Execute query and fetch cursor
const result = await db.query(
    'select * from cities where name like $1',
    {params: ['%york%'], cursor: true});

// Walk through the cursor, and do whatever you want with fetched rows
const cursor = result.cursor;
let row;
while ((row = await cursor.next())) {
  console.log(row);
}
// Close cursor, (Send connection back to the pool)
await cursor.close();

// Disconnect all connections and shutdown pool
await db.close(); 

Using prepared statements

import {DataTypeOIDs} from 'postgresql-client'; 

// .....
const statement = await connection.prepare( 
    'insert into my_table(id, name) values ($1, $2)', {
        paramTypes: [DataTypeOIDs.Int4, DataTypeOIDs.Varchar]
    });

for (let i = 0; i < 100; i++) {
    await statement.execute({params: [i, ('name' + i)]});
}
await statement.close(); // When you done, close the statement to relase resources

Check DOCUMENTATION for other examples.

Type mappings

The table below lists builtin data type mappings.

Posgtres typeJS typeReceiveSend
boolbooleantext,binarybinary
int2numbertext,binarybinary
int4numbertext,binarybinary
int8BigInttext,binarybinary
float4numbertext,binarybinary
float8numbertext,binarybinary
charstringtext,binarybinary
bpcharstringtext,binarybinary
varcharstringtext,binarybinary
dateDatetext,binarybinary
timeDatetext,binarybinary
timestampDatetext,binarybinary
timestamptzDatetext,binarybinary
oidnumbertext,binarybinary
byteaBuffertext,binarybinary
uuidstringtext,binarybinary
jsonobjecttext,binarybinary
jsonbobjecttext,binarybinary
xmlstringtext,binarybinary
pointPointtext,binarybinary
circleCircletext,binarybinary
lsegRectangletext,binarybinary
boxRectangletext,binarybinary
int2Vectornumber[]text,binarybinary
_boolboolean[]text,binarybinary
_int2number[]text,binarybinary
_int4number[]text,binarybinary
_int8BigInt[]text,binarybinary
_float4number[]text,binarybinary
_float8number[]text,binarybinary
_charstring[]text,binarybinary
_bpcharstring[]text,binarybinary
_varcharstring[]text,binarybinary
_dateDate[]text,binarybinary
_timeDate[]text,binarybinary
_timestampDate[]text,binarybinary
_timestamptzDate[]text,binarybinary
_uuidstring[]text,binarybinary
_oidnumber[]text,binarybinary
_byteaBuffer[]text,binarybinary
_jsonobject[]text,binarybinary
_jsonbobject[]text,binarybinary
_xmlstring[]text,binarybinary
_pointPoint[]text,binarybinary
_circleCircle[]text,binarybinary
_lsegRectangle[]text,binarybinary
_boxRectangle[]text,binarybinary
_int2Vectornumbertext,binarybinary

Support

You can report bugs and discuss features on the GitHub issues page When you open an issue please provide version of NodeJS and PostgreSQL server.

Node Compatibility

  • node >= 14.x

License

postgresql-client is available under MIT license.

2.11.0

9 days ago

2.10.7

10 days ago

2.10.6

2 months ago

2.10.5

4 months ago

2.10.3

4 months ago

2.10.4

4 months ago

2.10.2

4 months ago

2.8.1

7 months ago

2.9.1

7 months ago

2.10.1

6 months ago

2.10.0

6 months ago

2.9.0

7 months ago

2.8.0

7 months ago

2.7.2

8 months ago

2.6.1

9 months ago

2.6.0

9 months ago

2.5.10

9 months ago

2.7.0

9 months ago

2.7.1

9 months ago

2.5.8

12 months ago

2.5.7

12 months ago

2.5.9

12 months ago

2.5.5

1 year ago

2.5.3

1 year ago

2.5.2

1 year ago

2.4.1

2 years ago

2.3.0

2 years ago

2.5.0

2 years ago

2.5.1

2 years ago

2.1.5

2 years ago

2.0.3

2 years ago

2.0.4

2 years ago

2.1.2

2 years ago

2.1.1

2 years ago

2.1.4

2 years ago

2.1.3

2 years ago

2.1.0

2 years ago

2.0.2

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.21.4

2 years ago

1.21.5

2 years ago

1.21.3

2 years ago

1.21.6

2 years ago

1.21.2

3 years ago

1.21.1

3 years ago

1.21.0

3 years ago

1.19.0

3 years ago

1.20.0

3 years ago

1.18.4

3 years ago

1.18.3

3 years ago

1.18.2

3 years ago

1.18.1

3 years ago

1.18.0

3 years ago

1.17.0

3 years ago

1.16.7

3 years ago

1.16.6

3 years ago

1.16.5

3 years ago

1.16.4

3 years ago

1.16.3

3 years ago

1.16.2

3 years ago

1.16.1

3 years ago

1.16.0

3 years ago

1.15.1

3 years ago

1.15.0

3 years ago

1.14.2

3 years ago

1.14.1

3 years ago

1.14.0

3 years ago

1.13.2

3 years ago

1.13.1

3 years ago

1.13.0

3 years ago

1.11.4

3 years ago

1.11.3

3 years ago

1.11.2

3 years ago

1.11.1

3 years ago

1.11.0

3 years ago

1.10.1

3 years ago

1.10.0

3 years ago

1.9.1

3 years ago

1.9.2

3 years ago

1.9.0

3 years ago

1.8.1

3 years ago

1.8.0

3 years ago

1.7.1

3 years ago

1.7.0

3 years ago

1.6.0

3 years ago

1.5.1

3 years ago

1.5.0

3 years ago

1.4.0

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.0

3 years ago

1.2.1

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.2

3 years ago

1.0.3

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago