Registry / database / sqltyper

sqltyper

JSON →
library1.3.2jsnpmunverified

sqltyper 1.3.2 generates typed TypeScript functions from raw PostgreSQL queries by analyzing schema from a running database. It connects to a PostgreSQL database, parses SQL files containing queries with parameter placeholders (${param} or :param), and outputs .ts files with correct TypeScript types for parameters and result rows. Unlike ORMs or query builders, it works directly with SQL while providing compile-time type safety. Compatible with node-postgres, postgres.js, and pg-promise runtime libraries. Released via npm, with active development on GitHub.

npm install sqltyper
INSTALL
IMPORT
SIG · SQLTYPER
S
sqltyper
databasejavascriptv1.3.2
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.

generated functions
import * as sql from './sqls'
import { myQuery } from './sqls'
Generated index.ts re-exports all functions as named exports; prefer namespace import for convenience.
ClientBase (type)
import { ClientBase } from 'pg'
ClientBase is the base type for pg Client and Pool; used in generated function signatures.
Client / Pool
import { Client } from 'pg'
const { Client } = require('pg')
ESM imports recommended; CommonJS require also works but TypeScript may need esModuleInterop.

Complete setup and usage example: install, write SQL file, run sqltyper, and use generated typed function with pg client.

// 1. Install sqltyper as dev dependency and pg as runtime dependency // npm install --save-dev sqltyper pg // 2. Create src/sqls/find-persons.sql: /* SELECT initcap(name) as name_capitalized, age, shoe_size FROM person WHERE name LIKE ${namePattern} AND age > ${minimumAge} */ // 3. Run sqltyper (ensure your DB is running and schema exists) // npx sqltyper --database postgres://user:pass@host/dbname src/sqls // 4. Use the generated function: import { Client } from 'pg' import * as sql from './sqls' async function run(client: Client) { const rows = await sql.findPersons(client, { namePattern: '%John%', minimumAge: 18 }) rows[0].name_capitalized // string rows[0].age // number rows[0].shoe_size // number | null }
Debug
Known issues
gotchaAll output columns in a query must have unique names; otherwise columns with duplicate names become inaccessible in the result.
fix
Use aliases (AS) in SQL to ensure unique column names.
affects: >=0.0.0
gotchasqltyper connects to a real database to analyze schema; ensure the database is running and accessible during code generation.
fix
Start your PostgreSQL database and provide correct connection string via --database.
affects: >=0.0.0
gotchaGenerated functions are specific to the database schema at generation time; schema changes require re-running sqltyper to regenerate types.
fix
Run sqltyper again after schema changes to update TypeScript types.
affects: >=0.0.0
gotchaThe postgres.js runtime requires the @beta version for TypeScript support; stable version may lack types.
fix
Install postgres@beta instead of postgres for TypeScript projects.
affects: >=0.0.0
Errors
Common errors & fixes
Error: Cannot find module 'pg' or 'postgres'
Missing required runtime dependency (one of pg, postgres, or pg-promise)
fix
npm install --save pg (or postgres@beta, or pg-promise)
Error: connect ECONNREFUSED 127.0.0.1:5432
PostgreSQL database is not running or connection string is wrong
fix
Ensure PostgreSQL is running and the --database argument points to a valid database.
Upgrade
Version history
1.3.2latest on npm
Audit
Dependencies
pgoptionalRequired runtime dependency for executing queries (one of pg, postgres, or pg-promise)
postgresoptionalAlternative runtime dependency for postgres.js (beta version recommended for TypeScript)
pg-promiseoptionalAlternative runtime dependency for pg-promise
Agent activity
10 hits · last 30 days
node
8
Meta
1
OpenAI (training)
1
Resources
sqltyper — npm install sqltyper · libregistry