{"id":13434626,"url":"https://github.com/sqlc-dev/sqlc-gen-typescript","last_synced_at":"2025-10-05T16:15:34.662Z","repository":{"id":210778390,"uuid":"716732593","full_name":"sqlc-dev/sqlc-gen-typescript","owner":"sqlc-dev","description":null,"archived":false,"fork":false,"pushed_at":"2024-11-26T06:22:58.000Z","size":83,"stargazers_count":472,"open_issues_count":32,"forks_count":33,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-09-29T10:44:02.993Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sqlc-dev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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}},"created_at":"2023-11-09T18:54:25.000Z","updated_at":"2025-09-29T10:02:08.000Z","dependencies_parsed_at":"2024-01-18T14:44:39.124Z","dependency_job_id":"378f1488-b1f3-4c4b-bc69-df13aab41b38","html_url":"https://github.com/sqlc-dev/sqlc-gen-typescript","commit_stats":null,"previous_names":["sqlc-dev/sqlc-gen-typescript","sqlc-dev/sqlc-gen-ts"],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/sqlc-dev/sqlc-gen-typescript","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sqlc-dev%2Fsqlc-gen-typescript","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sqlc-dev%2Fsqlc-gen-typescript/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sqlc-dev%2Fsqlc-gen-typescript/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sqlc-dev%2Fsqlc-gen-typescript/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sqlc-dev","download_url":"https://codeload.github.com/sqlc-dev/sqlc-gen-typescript/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sqlc-dev%2Fsqlc-gen-typescript/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278479666,"owners_count":25993899,"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-10-05T02:00:06.059Z","response_time":54,"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":[],"created_at":"2024-07-31T03:00:19.140Z","updated_at":"2025-10-05T16:15:34.641Z","avatar_url":"https://github.com/sqlc-dev.png","language":"TypeScript","funding_links":[],"categories":["Supported languages","TypeScript"],"sub_categories":[],"readme":"# sqlc-gen-typescript\n\n\u003e [!CAUTION]\n\u003e Here be dragons! This plugin is still in early access. Expect breaking changes, missing functionality, and sub-optimal output. Please report all issues and errors. Good luck!\n\n## Usage\n\n```yaml\nversion: '2'\nplugins:\n- name: ts\n  wasm:\n    url: https://downloads.sqlc.dev/plugin/sqlc-gen-typescript_0.1.3.wasm\n    sha256: 287df8f6cc06377d67ad5ba02c9e0f00c585509881434d15ea8bd9fc751a9368\nsql:\n- schema: \"schema.sql\"\n  queries: \"query.sql\"\n  engine: postgresql\n  codegen:\n  - out: src/authors\n    plugin: ts\n    options:\n      runtime: node\n      driver: postgres\n```\n\n## Supported engines and drivers\n\n- PostgreSQL via [pg](https://www.npmjs.com/package/pg) or [postgres](https://www.npmjs.com/package/postgres).\n- MySQL via [mysql2](https://www.npmjs.com/package/mysql2).\n- SQLite via [sqlite3](https://www.npmjs.com/package/better-sqlite3).\n\n## Getting started\n\nThis tutorial assumes that the latest version of sqlc is\n[installed](https://docs.sqlc.dev/en/latest/overview/install.html) and ready to use.\n\nWe'll generate TypeScript here, but other [language\nplugins](https://docs.sqlc.dev/en/latest/reference/language-support.html) are\navailable. You'll need Bun (or Node.js) installed if you want to build and run a\nprogram with the code sqlc generates, but sqlc itself has no dependencies.\n\nWe'll also rely on sqlc's [managed databases](https://docs.sqlc.dev/en/latest/howto/managed-databases.html),\nwhich require a sqlc Cloud project and auth token. You can get those from\nthe [sqlc Cloud dashboard](https://dashboard.sqlc.dev/). Managed databases are\nan optional feature that improves sqlc's query analysis in many cases, but you\ncan turn it off simply by removing the `cloud` and `database` sections of your\nconfiguration.\n\n### Setting up\n\nCreate a new directory called `sqlc-tutorial` and open it up.\n\nInitialize a new package.\n\n```shell\n$ bun init\n```\n\nsqlc looks for either a `sqlc.(yaml|yml)` or `sqlc.json` file in the current\ndirectory. In our new directory, create a file named `sqlc.yaml` with the\nfollowing contents:\n\n```yaml\nversion: \"2\"\ncloud:\n  # Replace \u003cPROJECT_ID\u003e with your project ID from the sqlc Cloud dashboard\n  project: \"\u003cPROJECT_ID\u003e\"\nplugins:\n- name: ts\n  wasm:\n    url: https://downloads.sqlc.dev/plugin/sqlc-gen-typescript_0.1.3.wasm\n    sha256: 287df8f6cc06377d67ad5ba02c9e0f00c585509881434d15ea8bd9fc751a9368\nsql:\n  - engine: \"postgresql\"\n    queries: \"query.sql\"\n    schema: \"schema.sql\"\n    database:\n      managed: true\n    codegen:\n    - out: db\n      plugin: ts\n      options:\n        runtime: node\n        driver: pg\n```\n\nReplace `\u003cPROJECT_ID\u003e` with your project ID from the sqlc Cloud dashboard. It\nwill look something like `01HA8SZH31HKYE9RR3N3N3TSJM`.\n\nAnd finally, set the `SQLC_AUTH_TOKEN` environment variable:\n\n```shell\nexport SQLC_AUTH_TOKEN=\"\u003cyour sqlc auth token\u003e\"\n```\n\n### Schema and queries\n\nsqlc needs to know your database schema and queries in order to generate code.\nIn the same directory, create a file named `schema.sql` with the following\ncontent:\n\n```sql\nCREATE TABLE authors (\n  id   BIGSERIAL PRIMARY KEY,\n  name text      NOT NULL,\n  bio  text\n);\n```\n\nNext, create a `query.sql` file with the following five queries:\n\n```sql\n-- name: GetAuthor :one\nSELECT * FROM authors\nWHERE id = $1 LIMIT 1;\n\n-- name: ListAuthors :many\nSELECT * FROM authors\nORDER BY name;\n\n-- name: CreateAuthor :one\nINSERT INTO authors (\n  name, bio\n) VALUES (\n  $1, $2\n)\nRETURNING *;\n\n-- name: UpdateAuthor :exec\nUPDATE authors\n  set name = $2,\n  bio = $3\nWHERE id = $1;\n\n-- name: DeleteAuthor :exec\nDELETE FROM authors\nWHERE id = $1;\n```\n\nIf you prefer, you can alter the `UpdateAuthor` query to return the updated\nrecord:\n\n```sql\n-- name: UpdateAuthor :one\nUPDATE authors\n  set name = $2,\n  bio = $3\nWHERE id = $1\nRETURNING *;\n```\n\n### Generating code\n\nYou are now ready to generate code. You shouldn't see any output when you run\nthe `generate` subcommand, unless something goes wrong:\n\n```shell\n$ sqlc generate\n```\n\nYou should now have a `tutorial` subdirectory with three files containing Go\nsource code. These files comprise a Go package named `tutorial`:\n\n```\n├── package.json\n├── query.sql\n├── schema.sql\n├── sqlc.yaml\n└── db\n    ├── query_sql.ts\n```\n\n### Using generated code\n\nYou can use your newly-generated code package from any TypeScript program.\nCreate a file named `index.ts` and add the following contents:\n\n```ts\nimport { Pool } from \"pg\";\n\nimport {\n  createAuthor,\n  deleteAuthor,\n  getAuthor,\n  listAuthors,\n} from \"./db/query_sql\";\n\nasync function main() {\n  const client = new Pool({ connectionString: process.env[\"DATABASE_URL\"] });\n  await client.connect();\n\n  // list all authors\n  const authors = await listAuthors(client);\n  console.log(authors);\n\n  // create an author\n  const author = await createAuthor(client, {\n    name: \"Anders Hejlsberg\",\n\tbio: \"Original author of Turbo Pascal and co-creator of TypeScript\",\n  });\n  if (author === null) {\n    throw new Error(\"author not created\");\n  }\n  console.log(author);\n\n  // get the author we just created\n  const anders = await getAuthor(client, { id: author.id });\n  if (anders === null) {\n    throw new Error(\"anders not found\");\n  }\n  console.log(anders);\n\n  // delete the author\n  await deleteAuthor(client, { id: anders.id });\n}\n\n(async () =\u003e {\n  await main();\n  process.exit()\n})();\n```\n\nBefore this code will run you'll need to install the `pg` package:\n\n```shell\n$ bun install pg\n```\n\nThe program should compile without errors. To make that possible, sqlc generates\nreadable, **idiomatic** TypeScript code that you otherwise would've had to write\nyourself. Take a look in `db/query_sql.ts`.\n\nOf course for this program to run successfully you'll need to run after setting\nthe `DATABASE_URL` environment variable. And your database must have the\n`authors` table as defined in `schema.sql`.\n\n```shell\n$ DATABASE_URL=\"$(sqlc createdb)\" bun run index.ts\n```\n\n```shell\n$ bun run index.ts\n```\n\nYou should now have a working program using sqlc's generated TypeScript source\ncode, and hopefully can see how you'd use sqlc in your own real-world\napplications.\n\n## Configuration\n\n### PostgreSQL and node-postgres\n\n```yaml\nversion: '2'\nplugins:\n- name: ts\n  wasm:\n    url: https://downloads.sqlc.dev/plugin/sqlc-gen-typescript_0.1.3.wasm\n    sha256: 287df8f6cc06377d67ad5ba02c9e0f00c585509881434d15ea8bd9fc751a9368\nsql:\n- schema: \"schema.sql\"\n  queries: \"query.sql\"\n  engine: postgresql\n  codegen:\n  - out: db\n    plugin: ts\n    options:\n      runtime: node\n      driver: pg # npm package name\n```\n\n### PostgreSQL and postgres.js\n\n```yaml\nversion: '2'\nplugins:\n- name: ts\n  wasm:\n    url: https://downloads.sqlc.dev/plugin/sqlc-gen-typescript_0.1.3.wasm\n    sha256: 287df8f6cc06377d67ad5ba02c9e0f00c585509881434d15ea8bd9fc751a9368\nsql:\n- schema: \"schema.sql\"\n  queries: \"query.sql\"\n  engine: postgresql\n  codegen:\n  - out: db\n    plugin: ts\n    options:\n      runtime: node\n      driver: postgres # npm package name\n```\n\n\n### MySQL and mysql2\n\n```yaml\nversion: '2'\nplugins:\n- name: ts\n  wasm:\n    url: https://downloads.sqlc.dev/plugin/sqlc-gen-typescript_0.1.3.wasm\n    sha256: 287df8f6cc06377d67ad5ba02c9e0f00c585509881434d15ea8bd9fc751a9368\nsql:\n- schema: \"schema.sql\"\n  queries: \"query.sql\"\n  engine: \"mysql\"\n  codegen:\n  - out: db\n    plugin: ts\n    options:\n      runtime: node\n      driver: mysql2 # npm package name\n```\n\n### SQLite and better-sqlite3 (Beta)\n\n```yaml\nversion: '2'\nplugins:\n- name: ts\n  wasm:\n    url: https://downloads.sqlc.dev/plugin/sqlc-gen-typescript_0.1.3.wasm\n    sha256: 287df8f6cc06377d67ad5ba02c9e0f00c585509881434d15ea8bd9fc751a9368\nsql:\n- schema: \"schema.sql\"\n  queries: \"query.sql\"\n  engine: sqlite\n  codegen:\n  - out: db\n    plugin: ts\n    options:\n      runtime: node\n      driver: better-sqlite3 # npm package name\n```\n\n## Development\n\nIf you want to build and test sqlc-gen-typescript locally, follow these steps:\n\n1. Clone the repository and install dependencies:\n   ```\n   git clone https://github.com/sqlc-dev/sqlc-gen-typescript.git\n   cd sqlc-gen-typescript\n   npm install\n   ```\n\n2. Make your desired changes to the codebase. The main source files are located in the `src` directory.\n\n3. If you've made changes that require updating dependencies, run:\n   ```\n   npm install\n   ```\n\n4. Build the WASM plugin:  \nCheck the `Makefile` for details.\n   ```\n   make out.js\n\n   # Ensure you have Javy installed and available in your PATH\n   make examples/plugin.wasm\n   ```\n\n5. To test your local build, create a test project with a `sqlc.yaml` file containing:\n\n   ```yaml\n   version: '2'\n   plugins:\n   - name: ts\n     wasm:\n       url: file://{path_to_your_local_wasm_file}\n       sha256: {sha256_of_your_wasm_file}\n   sql:\n   - schema: \"schema.sql\"\n     queries: \"query.sql\"\n     engine: {your_database_engine}\n     codegen:\n     - out: db\n       plugin: ts\n       options:\n         runtime: node\n         driver: {your_database_driver}\n   ```\n\n   Replace the placeholders with appropriate values for your setup.\n\n6. Run sqlc in your test project to generate TypeScript code using your local plugin build:\n   ```\n   sqlc generate\n   ```\n\nFor more details on sqlc development, refer to the sqlc core development guide. This guide provides additional information on setting up and working with sqlc in general, which may be useful for contributors to this project.  \nhttps://docs.sqlc.dev/en/latest/guides/development.html","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsqlc-dev%2Fsqlc-gen-typescript","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsqlc-dev%2Fsqlc-gen-typescript","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsqlc-dev%2Fsqlc-gen-typescript/lists"}