{"id":22319929,"url":"https://github.com/aldis-ameriks/pg-typegen","last_synced_at":"2025-08-09T19:06:55.554Z","repository":{"id":40509550,"uuid":"278857566","full_name":"aldis-ameriks/pg-typegen","owner":"aldis-ameriks","description":"Generate TypeScript type definitions from Postgres database","archived":false,"fork":false,"pushed_at":"2025-06-02T07:53:45.000Z","size":264,"stargazers_count":7,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-09T05:06:00.901Z","etag":null,"topics":["codegen","database","pg-typegen","postgres","schema","schema-typegen","typegen","types","typescript"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aldis-ameriks.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-07-11T12:30:32.000Z","updated_at":"2025-06-02T07:53:49.000Z","dependencies_parsed_at":"2023-11-12T17:41:41.513Z","dependency_job_id":"fa77e893-78a2-4c87-96ff-d31e986155f2","html_url":"https://github.com/aldis-ameriks/pg-typegen","commit_stats":{"total_commits":204,"total_committers":3,"mean_commits":68.0,"dds":"0.014705882352941124","last_synced_commit":"828a69b14ecdc882835f9a6af80ff6e04245e38e"},"previous_names":["aldis-ameriks/schema-typegen"],"tags_count":61,"template":false,"template_full_name":null,"purl":"pkg:github/aldis-ameriks/pg-typegen","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aldis-ameriks%2Fpg-typegen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aldis-ameriks%2Fpg-typegen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aldis-ameriks%2Fpg-typegen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aldis-ameriks%2Fpg-typegen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aldis-ameriks","download_url":"https://codeload.github.com/aldis-ameriks/pg-typegen/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aldis-ameriks%2Fpg-typegen/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267694877,"owners_count":24129149,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-29T02:00:12.549Z","response_time":2574,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["codegen","database","pg-typegen","postgres","schema","schema-typegen","typegen","types","typescript"],"created_at":"2024-12-04T00:11:48.295Z","updated_at":"2025-07-29T13:31:52.870Z","avatar_url":"https://github.com/aldis-ameriks.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003epg-typegen\u003c/h1\u003e\n\u003cp\u003e\n    \u003ca href=\"https://www.npmjs.com/package/pg-typegen\" target=\"_blank\"\u003e\n        \u003cimg alt=\"Version\" src=\"https://img.shields.io/npm/v/pg-typegen.svg\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://github.com/aldis-ameriks/pg-typegen/graphs/commit-activity\" target=\"_blank\"\u003e\n        \u003cimg alt=\"Maintenance\" src=\"https://img.shields.io/badge/Maintained%3F-yes-green.svg\" /\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://github.com/aldis-ameriks/pg-typegen/blob/master/LICENSE\" target=\"_blank\"\u003e\n        \u003cimg alt=\"License: MIT\" src=\"https://img.shields.io/github/license/aldis-ameriks/pg-typegen\" /\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://github.com/aldis-ameriks/pg-typegen/workflows/CI/badge.svg\" target=\"_blank\"\u003e\n        \u003cimg alt=\"CI\" src=\"https://github.com/aldis-ameriks/pg-typegen/workflows/CI/badge.svg\" /\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n\u003e Generate TypeScript type definitions from Postgres database.\n\n## Install\n\n```sh\n$ npm install -D pg-typegen\n# or\n$ yarn add -D pg-typegen\n# or\n$ pnpm add -D pg-typegen\n```\n\n## Usage\n\n```\nUsage: pg-typegen [options] \u003cconnection\u003e\n\nOptions:\n  -V, --version              output the version number\n  -f, --suffix  \u003csuffix\u003e     suffix to append to generated table type, e.g. item -\u003e ItemEntity (default: \"Entity\")\n  -s, --schema  \u003cschema\u003e     schema (default: \"public\")\n  -h, --header  \u003cheader\u003e     header content (default: \"\")\n  -o, --output  \u003coutput\u003e     file output path (default: \"stdout\")\n  -e, --exclude \u003cexclude\u003e    excluded tables and enums as comma separated string e.g. knex_migrations,knex_migrations_lock (default: [])\n  --type                     use type definitions instead of interfaces in generated output (default: false)\n  --semicolons               use semicolons in generated types (default: false)\n  --ssl                      use ssl (default: false)\n  --optionals                use optionals \"?\" instead of null (default: false)\n  --comments                 generate table and column comments (default: false)\n  --pascal-enums             transform enum keys to pascal case (default: false)\n  --bigint                   use bigint for int8 types instead of strings (default: false)\n  --date-as-string           use string for date types instead of javascript Date object (default: false)\n  --insert-types             generate separate insert types with optional fields for columns allowing NULL value or having default values (default: false)\n  --table-names              generate string literal type with all table names (default: false)\n  --view-names               generate string literal types for views and materialized views (default: false)\n  --help                     display help for command\n\nExample:\n  $ pg-typegen -o ./entities.ts postgres://username:password@localhost:5432/database\n```\n\nGiven database table\n```sql\nCREATE TYPE user_state AS ENUM (\n  'asleep',\n  'awake'\n);\n\nCREATE TABLE users (\n    id int4 NOT NULL,\n    name varchar(255),\n    state user_state,\n    is_enabled bool NOT NULL DEFAULT FALSE\n);\n```\n\nRunning `pg-typegen -o ./entities.ts postgres://username:password@localhost:5432/database`\nWill generate the following type definitions\n```ts\nenum UserState {\n  asleep = 'asleep',\n  awake = 'awake'\n}\n\ninterface UserEntity {\n  id: number;\n  name: string | null;\n  state: UserState | null;\n  is_enabled: boolean;\n}\n```\n\n\u003e By default, the types will be generated based on how [pg](https://github.com/brianc/node-postgres) returns the values.\n\n#### Insert types\nTo simplify database inserts, separate types can be generated with optional values where NULL is allowed or default values for column exist in postgres.\n\nGiven database table\n```sql\nCREATE TYPE user_state AS ENUM (\n  'asleep',\n  'awake'\n);\n\nCREATE TABLE users (\n    id serial4 PRIMARY KEY,\n    name varchar(255) NOT NULL,\n    state user_state,\n    is_enabled bool NOT NULL DEFAULT FALSE\n);\n```\n\nRunning `pg-typegen -o ./entities.ts --insert-types postgres://username:password@localhost:5432/database`\nWill generate the following type definitions\n```ts\nenum UserState {\n  asleep = 'asleep',\n  awake = 'awake'\n}\n\ninterface UserEntity {\n  id: number;\n  name: string;\n  state: UserState | null;\n  is_enabled: boolean;\n}\n\ninterface UserInsertEntity {\n  id?: number;\n  name: string;\n  state?: UserState | null;\n  is_enabled?: boolean;\n}\n```\n\nWhich should allow working with insert objects without having to define all optional and nullable fields.\n```ts\nconst user: UserInsertEntity = { name: 'foo' }\nknex\u003cUserInsertEntity\u003e('users').insert(user)\n```\n\n### Running from code\n\n```ts\nimport { join } from 'path'\nimport generate from 'pg-typegen'\n;(async () =\u003e {\n  const output = join(__dirname, 'entities.ts')\n  await generate({ connection: 'postgres://username:password@localhost:5432/database', output })\n})()\n```\n\n### Loading database config\n\n#### from .env file\n```\nexport $(grep -v '^#' .env | xargs) \u0026\u0026 pg-typegen -o ./entities.ts postgres://$DB_USERNAME:$DB_PASSWORD@$DB_HOST:$DB_PORT/$DB_NAME\n```\n\n#### from json file\n```\npg-typegen -o ./entities.ts postgres://$(jq -r '.DB.USERNAME' config.json):$(jq -r '.DB.PASSWORD' config.json)@$(jq -r '.DB.HOST' config.json):$(jq -r '.DB.PORT' config.json)/$(jq -r '.DB.NAME' config.json)\n```\n\n\n## Run tests\n\n```sh\ndocker-compose up -d\nnpm test\n```\n\n\u003e Use `RECREATE_DATABASE=true npm test` when running tests for the first time\n\n## Contributing\n\nContributions, issues and feature requests are welcome!\n\n## License\n\nCopyright © 2020 [Aldis Ameriks](https://github.com/aldis-ameriks).\u003cbr /\u003e\nThis project is [MIT](https://github.com/aldis-ameriks/pg-typegen/blob/master/LICENSE) licensed.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faldis-ameriks%2Fpg-typegen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faldis-ameriks%2Fpg-typegen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faldis-ameriks%2Fpg-typegen/lists"}