A slightly nicer interface to Postgres over node-libpq providing both sync and async operations
88
Quality
Pending
Does it follow best practices?
Impact
88%
1.20xAverage score across 10 eval scenarios
Core connection management functionality for establishing and maintaining PostgreSQL database connections using native libpq bindings.
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
});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:
postgresql://user:password@host:5432/databasehost=localhost port=5432 dbname=mydb user=myuserUsage 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!');
});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);
}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();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
});PG Native respects standard PostgreSQL environment variables when no connection parameters are provided:
PGHOST - Database server hostPGPORT - Database server portPGDATABASE - Database namePGUSER - Database userPGPASSWORD - Database passwordPGCONNECT_TIMEOUT - Connection timeoutInstall with Tessl CLI
npx tessl i tessl/npm-pg-nativeevals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10