Registry / database / pg-ts
library2.5.1jsnpmunverified

TypeScript wrapper around node-postgres offering a typed SQL template tag and transaction helpers. Current version: 2.5.1. Provides both Promise-based and fp-ts Task-based query methods. Integrates tightly with TypeScript to ensure type safety in SQL queries and result sets. Differentiates from raw node-postgres by reducing boilerplate and offering a first-class TypeScript experience.

npm install pg-ts
INSTALL
IMPORT
SIG · PG-TS
P
pg-ts
databasejavascriptv2.5.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

getPool
import getPool from 'pg-ts'
const { getPool } = require('pg-ts')
getPool is a default export; CommonJS require must use .default or default import
SQL
import { SQL } from 'pg-ts'
import SQL from 'pg-ts'
SQL is a named export, not default. Use destructuring.
Pool
import type { Pool } from 'pg-ts'
import { Pool } from 'pg-ts'
Pool is only a type; it is not a value export. Use import type to avoid runtime errors.

Initialize a connection pool, execute a typed query returning a single row, and handle errors.

import getPool, { SQL } from 'pg-ts'; const pool = getPool({ connectionString: process.env.DATABASE_URL ?? 'postgres://localhost:5432/mydb' }); async function main() { const user = await pool.one(SQL`SELECT * FROM users WHERE id = ${1}`); console.log(user); } main().catch(console.error);
Debug
Known issues
gotchaPool is only a type; importing it as a value will cause a runtime error.
fix
Use import type { Pool } from 'pg-ts' or import it only for type annotations.
affects: all
gotchaSQL tag must be used with template literal; forgetting the tag will interpolate strings unsafely.
fix
Always prefix template literal with SQL (e.g., SQL`SELECT * FROM table WHERE id = ${id}`).
affects: all
gotchagetPool() returns a Pool instance but can be called without the 'new' keyword; using 'new getPool()' will fail.
fix
Call getPool(config) as a normal function, not as a constructor.
affects: all
Errors
Common errors & fixes
TypeError: getPool is not a function
Using CommonJS require without .default or incorrect import syntax.
fix
Replace 'const { getPool } = require("pg-ts")' with 'const getPool = require("pg-ts").default' or use ES import.
Error: Cannot find module 'fp-ts'
fp-ts is an optional peer dependency for Task methods but not installed.
fix
Install fp-ts: 'npm install fp-ts' or 'yarn add fp-ts' if using Task methods.
TypeError: pool.one is not a function
getPool() not called; using Pool instance as a class instead of calling the function.
fix
Call getPool() (e.g., 'const pool = getPool(config)') before using pool methods.
Upgrade
Version history
2.5.1latest on npm
Audit
Dependencies
pgrequiredpeer dependency: node-postgres is the underlying PostgreSQL driver
fp-tsoptionalrequired for Task-based query methods (e.g., .oneTask)
Agent activity
53 hits · last 30 days
node
50
Amazon
1
Bingbot
1
Resources
packagepg-ts
pg-ts — npm install pg-ts · libregistry