# ts-pg-model

> Generates Typescript type definitions from live Postgres server.

Latest version **2.0.39** (published 2025-11-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install ts-pg-model
pnpm add ts-pg-model
yarn add ts-pg-model
bun add ts-pg-model
```

Provides the command `ts-pg-model`.

## Health

**Score 65/100 (B)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.0.39 |
| Published | 2025-11-28 |
| First published | 2022-01-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | ^12.20.0 \|\| ^14.13.1 \|\| >=16.0.0 |
| Dependencies | 12 |
| Unpacked size | 95.6 KB |
| Known vulnerabilities | 0 (+3 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | hoonoh |
| Maintainers | hoonoh |
| Keywords | pg, postgres, postgresql, typescript |

## Links

- npm: https://www.npmjs.com/package/ts-pg-model
- Repository: https://github.com/hoonoh/ts-pg-model
- Homepage: https://github.com/hoonoh/ts-pg-model#readme
- Issues: https://github.com/hoonoh/ts-pg-model/issues
- npm.io page: https://npm.io/package/ts-pg-model

## Dependencies (12)

- [ora](https://npm.io/package/ora.md) 9.0.0
- [zod](https://npm.io/package/zod.md) 4.1.13
- [meow](https://npm.io/package/meow.md) 14.0.0
- [chalk](https://npm.io/package/chalk.md) 5.6.2
- [dotenv](https://npm.io/package/dotenv.md) 17.2.3
- [slonik](https://npm.io/package/slonik.md) 48.8.5
- [esbuild](https://npm.io/package/esbuild.md) 0.27.0
- [ts-morph](https://npm.io/package/ts-morph.md) 27.0.2
- [lodash-es](https://npm.io/package/lodash-es.md) 4.17.21
- [change-case](https://npm.io/package/change-case.md) 5.4.4
- [read-package-up](https://npm.io/package/read-package-up.md) 12.0.0
- [strip-json-comments](https://npm.io/package/strip-json-comments.md) 5.0.3

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 2.0.39 (latest) — 2025-11-28
- 1.0.0-preview.2 (preview) — 2022-01-24
- 2.0.38 — 2025-11-22
- 2.0.37 — 2025-11-14
- 2.0.36 — 2025-11-10
- 2.0.35 — 2025-11-10
- 2.0.34 — 2025-11-10
- 2.0.33 — 2025-11-10
- 2.0.32 — 2025-09-20
- 2.0.31 — 2025-09-19
- 2.0.30 — 2025-08-22
- 2.0.29 — 2025-08-14
- 2.0.28 — 2025-08-14
- 2.0.27 — 2025-06-28
- 2.0.26 — 2025-06-27
- … 44 more at https://npm.io/package/ts-pg-model/versions

## README

# ts-pg-model

Generates Typescript type definitions from live Postgres server.

## Configuration

### Postgres connection

You can assign `connectionURI` in configuration or use `PG_CONNECTION_URI` environment variable.
URI value must follow valid Postgres URI scheme starting with `postgresql://` or `postgres://`.

[`dotenv`](https://www.npmjs.com/package/dotenv) is also supported. You can also set `.env` file
location with `dotEnvPath` in configuration.

### Output paths

```ts
{
  output: {
    root?: string;
    includeSchemaPath?: boolean;
    keepFiles?: string[];
  }
}
```

### Root path

`output.root`

Defaults to `./src/generated`.

### tsconfig.json path

`tsConfig`

Defaults to `./tsconfig.json`

### Schema paths

If there are multiple schemas targeted for file generation, each resources within a schema will be
always grouped within a directory named after the schema. If only single schema is targeted (e.g.
`public`) directory structure will not be used unless `output.includeSchemaPath` is set to `true`.

### Keeping files from being removed

Files will only be overwritten if there are structural changes (`@generated` date will not be
updated) and will be deleted if no longer needed. If any files should be kept from being removed
inside the root folder, add file paths (relative to `output.root`) to `output.keepFiles`.

### Naming convensions

```ts
export type ChangeCase =
  | 'camelCase'
  | 'capitalCase'
  | 'constantCase'
  | 'dotCase'
  | 'kebabCase'
  | 'noCase'
  | 'pascalCase'
  | 'pathCase'
  | 'sentenceCase'
  | 'snakeCase'
  | 'trainCase'
  | 'keep';

const config: UserConfig = {
  conventions: {
    schemas: 'camelCase',
    columns: 'keep',
    types: 'camelCase',
    paths: 'kebabCase',
  },
};
```

#### Code convensions

You can choose code convensions of schemas, columns and types. Choices are open to all methods
provided by [`change-case`](https://www.npmjs.com/package/change-case) or `keep` (no transform).

##### Schema names

Schema code convensions are applied to namespaces. Defaults to `camelCase`.

##### Column names

Column code conventions are applied to column names. Defaults to `keep`. Can be useful if you prefer
automatic column name transformation to `camelCase`, etc.

##### Enum Types

Enum type code conventions are applied to Enum member names. Defaults to `camelCase`.

#### Paths and filename conventions

Generated file paths and filename conventions. Defaults to `kebabCase`.

### Selecting targets

You can target schemas, tables and columns using target selectors.

#### schema

If undefined, default schemas will be chosen by the result of `show search_path` query, excluding
`"$user"`. In most of user cases, the result would be `["public"]`.

#### tables & columns

Tables and columns can be explicitly included or excluded using `targetSelectors` configuration.
`targetSelectors` is a series of filters which includes or excludes targets in order.

You can define table / columns names with dot notation, including schema / table names.

If schema / tables names are not defined using dot notation, all matching targets will be
included / excluded.

### Type mapping

#### User defined types (UDT)

Most of known Postgres types should be mapped automatically. If there is an unknown data type that
needs casting into a primitive type, use `typeMap.udt` settings. Array type (e.g. `_text`, with
underscore prefix in `udt_name`) should be automatically handled.

#### JSON(B) definitions

User defined JSON(B) types can be achived via `typeMap.json` settings & type definitions in the
settings file. When `typeMap.json` is defined, this will require JSON type definitions as
`UserConfig` arguments (`UserConfig<JsonTypeMap>`).

#### Example

```ts
export interface JsonTypeMap {
  MediaMetadata: {
    width: number;
    height: number;
  };
}

// `UserConfig` variable has to be either exported via named export (with name `userConfig`) or default export
export const userConfig: Config<JsonTypeMap> = {
  // includes `users` schema only.
  schemas: ['users'],
  targetSelectors: [
    {
      include: {
        // includes `media.images` table, despite `media` schema was not included.
        tables: ['media.images'],
      },
    },
    {
      exclude: {
        // excludes all tables with name `sessions`, in this case `users.sessions` table.
        tables: ['sessions'],
        // excludes `created_at`, `updated_at` columns in all tables
        columns: ['created_at', 'updated_at'],
      },
    },
    {
      include: {
        // include `users.sessions` again.
        // be noted that all columns including `created_at` column in this table will be included
        // despite all columns with name `created_at` was removed in previous exclude rule.
        tables: ['sessions'],
        // includes `media.images.updated_at` column, despite all `updated_at` columns were excluded.
        columns: ['media.images.updated_at'],
      },
    },
  ],
  typeMap: {
    json: [
      {
        schema: 'media',
        tableName: 'images',
        columnName: 'metadata',
        name: 'MediaMetadata',
      },
    ],
  },
  conventions: {
    columns: 'camelCase',
  },
};
```

## Type definition output

### Single Schema Mode

### Multi Schema Mode

---
_Source: https://npm.io/package/ts-pg-model · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
