{"id":13412419,"url":"https://github.com/adelsz/pgtyped","last_synced_at":"2025-05-16T01:03:50.156Z","repository":{"id":37502806,"uuid":"210450402","full_name":"adelsz/pgtyped","owner":"adelsz","description":"pgTyped - Typesafe SQL in TypeScript","archived":false,"fork":false,"pushed_at":"2025-05-15T22:44:48.000Z","size":11467,"stargazers_count":3093,"open_issues_count":74,"forks_count":100,"subscribers_count":14,"default_branch":"master","last_synced_at":"2025-05-16T01:02:14.688Z","etag":null,"topics":["generator","postgres","postgresql","query","sql","type-safety","types","typescript"],"latest_commit_sha":null,"homepage":"https://pgtyped.dev","language":"TypeScript","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/adelsz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","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},"funding":{"open_collective":"pgtyped","github":"adelsz"}},"created_at":"2019-09-23T20:52:29.000Z","updated_at":"2025-05-15T16:54:37.000Z","dependencies_parsed_at":"2023-10-03T05:12:02.727Z","dependency_job_id":"d44f4d5d-01fe-40af-bbcb-087d299a340a","html_url":"https://github.com/adelsz/pgtyped","commit_stats":{"total_commits":722,"total_committers":48,"mean_commits":"15.041666666666666","dds":0.5581717451523546,"last_synced_commit":"fd612f7a6e9131f6ebe0f0720d3ae17208e8af91"},"previous_names":[],"tags_count":69,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adelsz%2Fpgtyped","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adelsz%2Fpgtyped/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adelsz%2Fpgtyped/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adelsz%2Fpgtyped/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/adelsz","download_url":"https://codeload.github.com/adelsz/pgtyped/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254448579,"owners_count":22072764,"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","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":["generator","postgres","postgresql","query","sql","type-safety","types","typescript"],"created_at":"2024-07-30T20:01:24.450Z","updated_at":"2025-05-16T01:03:45.114Z","avatar_url":"https://github.com/adelsz.png","language":"TypeScript","readme":"\u003cimg width=\"340\" height=\"150\" align=\"right\" src=\"https://raw.githubusercontent.com/adelsz/pgtyped/master/header.png\"\u003e\n\n# [PgTyped](https://pgtyped.dev/)\n\n![Version](https://img.shields.io/github/v/release/adelsz/pgtyped)\n[![Actions Status](https://github.com/adelsz/pgtyped/workflows/CI/badge.svg)](https://github.com/adelsz/pgtyped/actions) [![Join the chat at https://gitter.im/pgtyped/community](https://badges.gitter.im/pgtyped/community.svg)](https://gitter.im/pgtyped/community?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n\nPgTyped makes it possible to use raw SQL in TypeScript with guaranteed type-safety.  \nNo need to map or translate your DB schema to TypeScript, PgTyped automatically generates types and interfaces for your SQL queries by using your running Postgres database as the source of type information.\n\n---\n\n## Features:\n\n1. Automatically generates TS types for parameters/results of SQL queries of any complexity.\n2. Supports extracting and typing queries from both SQL and TS files.\n3. Generate query types as you write them, using watch mode.\n4. Useful parameter interpolation helpers for arrays and objects.\n5. No need to define your DB schema in TypeScript, your running DB is the live source of type data.\n6. Prevents SQL injections by not doing explicit parameter substitution. Instead, queries and parameters are sent separately to the DB driver, allowing parameter substitution to be safely done by the PostgreSQL server.\n7. Native ESM support. Runtime dependencies are also provided as CommonJS.\n\n### Documentation\n\nVisit our documentation page at [https://pgtyped.dev/](https://pgtyped.dev/)\n\n### Getting started\n\n1. `npm install -D @pgtyped/cli typescript` (typescript is a required peer dependency for pgtyped)\n2. `npm install @pgtyped/runtime` (`@pgtyped/runtime` is the only required runtime dependency of pgtyped)\n3. Create a PgTyped `config.json` file.\n4. Run `npx pgtyped -w -c config.json` to start PgTyped in watch mode.\n\nMore info on getting started can be found in the [Getting Started](https://pgtyped.dev/docs/getting-started) page.\nYou can also refer to the [example app](./packages/example/README.md) for a preconfigured example.\n\n### Example\n\nLets save some queries in `books.sql`:\n\n```sql\n/* @name FindBookById */\nSELECT * FROM books WHERE id = :bookId;\n```\n\nPgTyped parses the SQL file, extracting all queries and generating strictly typed TS queries in `books.queries.ts`:\n\n```ts\n/** Types generated for queries found in \"books.sql\" */\n\n//...\n\n/** 'FindBookById' parameters type */\nexport interface IFindBookByIdParams {\n  bookId: number | null;\n}\n\n/** 'FindBookById' return type */\nexport interface IFindBookByIdResult {\n  id: number;\n  rank: number | null;\n  name: string | null;\n  author_id: number | null;\n}\n\n/**\n * Query generated from SQL:\n * SELECT * FROM books WHERE id = :bookId\n */\nexport const findBookById = new PreparedQuery\u003c\n  IFindBookByIdParams,\n  IFindBookByIdResult\n\u003e(...);\n```\n\nQuery `findBookById` is now statically typed, with types inferred from the PostgreSQL schema.  \nThis generated query can be imported and executed as follows:\n\n```ts\nimport { Client } from 'pg';\nimport { findBookById } from './books.queries';\n\nexport const client = new Client({\n  host: 'localhost',\n  user: 'test',\n  password: 'example',\n  database: 'test',\n});\n\nasync function main() {\n  await client.connect();\n  const books = await findBookById.run(\n    {\n      bookId: 5,\n    },\n    client,\n  );\n  console.log(`Book name: ${books[0].name}`);\n  await client.end();\n}\n\nmain();\n```\n\n### Resources\n\n1. [Configuring pgTyped](https://pgtyped.dev/docs/cli)\n2. [Writing queries in SQL files](https://pgtyped.dev/docs/sql-file-intro)\n3. [Advanced queries and parameter expansions in SQL files](https://pgtyped.dev/docs/sql-file)\n4. [Writing queries in TS files](https://pgtyped.dev/docs/ts-file-intro)\n5. [Advanced queries and parameter expansions in TS files](https://pgtyped.dev/docs/ts-file)\n\n### Project state:\n\nThis project is being actively developed and its APIs might change.\nAll issue reports, feature requests and PRs appreciated.\n\n### License\n\n[MIT](https://github.com/adelsz/pgtyped/tree/master/LICENSE)\n\nCopyright (c) 2019-present, Adel Salakh\n","funding_links":["https://opencollective.com/pgtyped","https://github.com/sponsors/adelsz"],"categories":["TypeScript","typescript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadelsz%2Fpgtyped","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fadelsz%2Fpgtyped","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadelsz%2Fpgtyped/lists"}