Registry / database / ts-pg-model

ts-pg-model

JSON →
library2.0.39jsnpmunverified

ts-pg-model generates TypeScript type definitions directly from a live PostgreSQL database by introspecting schemas, tables, columns, enums, and user-defined types. The current stable version is 2.0.39, with releases on GitHub and npm. Key differentiators: supports Postgres connection via URI or environment variable, dotenv integration, customizable naming conventions (change-case), target selection for schemas/tables/columns, and user-defined type mapping for JSON/UDTs. Output directory defaults to ./src/generated.

npm install ts-pg-model
INSTALL
IMPORT
SIG · TS-PG-MODEL
T
ts-pg-model
databasejavascriptv2.0.39
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.

UserConfig
import { UserConfig } from 'ts-pg-model'
const UserConfig = require('ts-pg-model');
ESM-only; use named import. UserConfig is a type for configuration objects.
Config
import { Config } from 'ts-pg-model'
import Config from 'ts-pg-model';
Config is a generic type; must use named import. Yes, it is a type even though the name suggests a class.
ChangeCase
import type { ChangeCase } from 'ts-pg-model'
import { ChangeCase } from 'ts-pg-model'
ChangeCase is a type union, prefer type import for better tree-shaking.

Configuration file for ts-pg-model that connects to Postgres and generates TypeScript types in ./src/generated with camelCase conventions.

// ts-pg-model.config.ts import type { Config } from 'ts-pg-model'; export const config: Config = { connectionURI: process.env.PG_CONNECTION_URI ?? 'postgresql://user:pass@localhost:5432/mydb', output: { root: './src/generated', }, conventions: { schemas: 'camelCase', columns: 'camelCase', types: 'pascalCase', paths: 'kebabCase', }, }; // Then run: npx ts-pg-model
Debug
Known issues
gotchaSchema output directory structure differs based on number of schemas. Single schema avoids directory nesting unless includeSchemaPath is true.
fix
Set output.includeSchemaPath: true if you always want schema directory nesting.
affects: >=0.0.0
gotchaGenerated files may be deleted if no longer needed, potentially losing hand-written modifications.
fix
Add file paths to output.keepFiles to prevent deletion.
affects: >=0.0.0
breakingIn version 2.0, configuration changed from default export to named export. Old configs with export default config break.
fix
Change export default config to export const config: Config = ...
affects: >=2.0.0
gotchaEnvironment variable PG_CONNECTION_URI takes precedence over config.connectionURI.
fix
Ensure you don't have a leftover .env value overriding your config.
affects: >=0.0.0
Errors
Common errors & fixes
Error: Cannot find module 'ts-pg-model'
Package not installed or mismatched version.
fix
Run npm install ts-pg-model --save-dev
TypeError: config is not a function
Old default export style used in version 1.x.
fix
Update config file to use named export: export const config: Config = { ... }
error: type "UserConfig" is not generic
Passing type parameter to non-generic type.
fix
Use Config<JsonTypeMap> instead of UserConfig<JsonTypeMap>
Upgrade
Version history
2.0.39latest on npm
Audit
Dependencies
pgoptionalPostgreSQL client for connecting to the database
dotenvoptionaloptional, loads .env files for connection URI
change-caserequiredprovides naming convention transformations
Agent activity
19 hits · last 30 days
node
14
Meta
2
Amazon
1
OpenAI (training)
1
Resources
ts-pg-model — npm install ts-pg-model · libregistry