CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-pg-native

A slightly nicer interface to Postgres over node-libpq providing both sync and async operations

88

1.20x

Quality

Pending

Does it follow best practices?

Impact

88%

1.20x

Average score across 10 eval scenarios

Overview
Eval results
Files

connection.mddocs/

Connection Management

Core connection management functionality for establishing and maintaining PostgreSQL database connections using native libpq bindings.

Capabilities

Client Constructor

Creates a new PostgreSQL client instance with optional configuration.

/**
 * Creates a new PostgreSQL client instance
 * @param config - Optional configuration object
 * @returns New Client instance
 */
const Client = require('pg-native');

// Constructor pattern (recommended)
const client = new Client(config);

// Factory pattern (also supported - no 'new' required)
const client = Client(config);

interface ClientConfig {
  /** Custom type conversion parser (defaults to pg-types) */
  types?: TypeParser;
  /** Return results as arrays instead of objects */
  arrayMode?: boolean;
}

Usage Examples:

const Client = require('pg-native');

// Basic client
const client = new Client();

// Client with array mode enabled  
const arrayClient = new Client({ arrayMode: true });

// Client with custom type parsing
const customClient = new Client({ 
  types: customTypeParser,
  arrayMode: false 
});

Asynchronous Connection

Establishes a connection to the PostgreSQL server asynchronously.

/**
 * Connect to PostgreSQL server asynchronously
 * @param params - Optional connection string in libpq format
 * @param callback - Required callback function
 */
client.connect(params?: string, callback: (err: Error | null) => void): void;

Connection String Formats:

The params string supports any format accepted by libpq:

  • URI format: postgresql://user:password@host:5432/database
  • Key-value format: host=localhost port=5432 dbname=mydb user=myuser
  • If omitted, uses PostgreSQL environment variables

Usage Examples:

const client = new Client();

// Connect using environment variables
client.connect(function(err) {
  if (err) throw err;
  console.log('Connected!');
});

// Connect with connection string
client.connect('postgresql://user:pass@localhost:5432/mydb', function(err) {
  if (err) throw err;
  console.log('Connected with URI!');
});

// Connect with key-value parameters
client.connect('host=localhost port=5432 dbname=test', function(err) {
  if (err) throw err;
  console.log('Connected with key-value params!');
});

Synchronous Connection

Establishes a connection to the PostgreSQL server synchronously.

/**
 * Connect to PostgreSQL server synchronously
 * @param params - Optional connection string in libpq format
 * @throws Error if connection fails
 */
client.connectSync(params?: string): void;

Usage Examples:

const client = new Client();

try {
  // Connect using environment variables
  client.connectSync();
  console.log('Connected synchronously!');
} catch (err) {
  console.error('Connection failed:', err.message);
}

try {
  // Connect with connection string
  client.connectSync('postgresql://user:pass@localhost:5432/mydb');
  console.log('Connected with URI!');
} catch (err) {
  console.error('Connection failed:', err.message);
}

Connection Termination

Ends the connection to the PostgreSQL server.

/**
 * End the connection to PostgreSQL server
 * @param callback - Optional callback called when connection ends
 */
client.end(callback?: () => void): void;

Usage Examples:

// End connection with callback
client.end(function() {
  console.log('Connection ended');
});

// End connection without callback
client.end();

Connection Events

The client emits events for connection-related activities.

// Client extends EventEmitter
client.on('error', (error: Error) => void);
client.on('readyForQuery', () => void);

Usage Examples:

const client = new Client();

client.on('error', function(err) {
  console.error('Connection error:', err.message);
});

client.on('readyForQuery', function() {
  console.log('Client ready for queries');
});

client.connect(function(err) {
  if (err) throw err;
  // Connection successful
});

Environment Variables

PG Native respects standard PostgreSQL environment variables when no connection parameters are provided:

  • PGHOST - Database server host
  • PGPORT - Database server port
  • PGDATABASE - Database name
  • PGUSER - Database user
  • PGPASSWORD - Database password
  • PGCONNECT_TIMEOUT - Connection timeout
  • And other standard libpq environment variables

Install with Tessl CLI

npx tessl i tessl/npm-pg-native

docs

connection.md

copy-operations.md

index.md

prepared-statements.md

queries.md

tile.json