Title: Add Typescript support · Issue #296 · sqlc-dev/sqlc · GitHub
Open Graph Title: Add Typescript support · Issue #296 · sqlc-dev/sqlc
X Title: Add Typescript support · Issue #296 · sqlc-dev/sqlc
Description: sqlc, like protoc, aims to generate code for multiple languages. The second language we're looking to add is TypeScript. Getting started WarningThe TypeScript plugin (sqlc-gen-typescript) is under active development. We make no guarantee...
Open Graph Description: sqlc, like protoc, aims to generate code for multiple languages. The second language we're looking to add is TypeScript. Getting started WarningThe TypeScript plugin (sqlc-gen-typescript) is under ...
X Description: sqlc, like protoc, aims to generate code for multiple languages. The second language we're looking to add is TypeScript. Getting started WarningThe TypeScript plugin (sqlc-gen-typescript) is un...
Opengraph URL: https://github.com/sqlc-dev/sqlc/issues/296
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add Typescript support","articleBody":"sqlc, like protoc, aims to generate code for multiple languages. The second language we're looking to add is TypeScript.\r\n\r\n> \r\n\r\n### Getting started\r\n\r\n> [!WARNING] \r\n> The TypeScript plugin (sqlc-gen-typescript) is under active development. We make no guarantees about backwards compatibility. Please try it out and give us feedback, but don't use it in production just yet.\r\n\r\nCreate a new directory. Inside, create a `sqlc.yaml` with these contents. Add your database schema to `schema.sql` and your queries to `query.sql`. If you don't have any to try out, skip to the provided example.\r\n\r\n```yaml\r\nversion: \"2\"\r\nplugins:\r\n- name: ts\r\n wasm:\r\n url: \"https://downloads.sqlc.dev/plugin/alpha/sqlc-gen-typescript_0.0.0_c6bc663.wasm\"\r\n sha256: c6bc663cb5064ef33023f24d96c378512f1d6a0fadd7c9b5bba45a24cbfbb894\r\nsql:\r\n- schema: \"schema.sql\"\r\n queries: \"query.sql\"\r\n engine: \"postgresql\"\r\n codegen:\r\n - plugin: ts\r\n out: src/db\r\n options:\r\n driver: \"pg\"\r\n runtime: \"node\"\r\n```\r\n\r\nRunning `sqlc generate` will output TypeScript into `src/db`. Please let us know what you think by leaving a comment on this issue.\r\n\r\n### Example\r\n\r\nWith the following schema and queries, sqlc will generate code with zero dependencies (beyond the necessary database driver).\r\n\r\n```sql\r\nCREATE TABLE authors (\r\n id BIGSERIAL PRIMARY KEY,\r\n name text NOT NULL,\r\n bio text\r\n);\r\n```\r\n\r\n```sql\r\n-- name: GetAuthor :one\r\nSELECT * FROM authors\r\nWHERE id = $1 LIMIT 1;\r\n\r\n-- name: ListAuthors :many\r\nSELECT * FROM authors\r\nORDER BY name;\r\n\r\n-- name: CreateAuthor :one\r\nINSERT INTO authors (\r\n name, bio\r\n) VALUES (\r\n $1, $2\r\n)\r\nRETURNING *;\r\n\r\n-- name: DeleteAuthor :exec\r\nDELETE FROM authors\r\nWHERE id = $1;\r\n```\r\n\r\n```ts\r\nimport { QueryArrayConfig, QueryArrayResult } from \"pg\";\r\n\r\ninterface Client {\r\n query: (config: QueryArrayConfig) => Promise\u003cQueryArrayResult>;\r\n}\r\n\r\nexport const getAuthorQuery = `-- name: GetAuthor :one\r\nSELECT id, name, bio FROM authors\r\nWHERE id = $1 LIMIT 1`;\r\n\r\nexport interface GetAuthorArgs {\r\n id: string;\r\n}\r\n\r\nexport interface GetAuthorRow {\r\n id: string;\r\n name: string;\r\n bio: string | null;\r\n}\r\n\r\nexport async function getAuthor(client: Client, args: GetAuthorArgs): Promise\u003cGetAuthorRow | null> {\r\n const result = await client.query({\r\n text: getAuthorQuery,\r\n values: [args.id],\r\n rowMode: \"array\"\r\n });\r\n if (result.rows.length !== 1) {\r\n return null;\r\n }\r\n const row = result.rows[0];\r\n return {\r\n id: row[0],\r\n name: row[1],\r\n bio: row[2]\r\n };\r\n}\r\n\r\nexport const listAuthorsQuery = `-- name: ListAuthors :many\r\nSELECT id, name, bio FROM authors\r\nORDER BY name`;\r\n\r\nexport interface ListAuthorsRow {\r\n id: string;\r\n name: string;\r\n bio: string | null;\r\n}\r\n\r\nexport async function listAuthors(client: Client): Promise\u003cListAuthorsRow[]> {\r\n const result = await client.query({\r\n text: listAuthorsQuery,\r\n values: [],\r\n rowMode: \"array\"\r\n });\r\n return result.rows.map(row => {\r\n return {\r\n id: row[0],\r\n name: row[1],\r\n bio: row[2]\r\n };\r\n });\r\n}\r\n\r\nexport const createAuthorQuery = `-- name: CreateAuthor :one\r\nINSERT INTO authors (\r\n name, bio\r\n) VALUES (\r\n $1, $2\r\n)\r\nRETURNING id, name, bio`;\r\n\r\nexport interface CreateAuthorArgs {\r\n name: string;\r\n bio: string | null;\r\n}\r\n\r\nexport interface CreateAuthorRow {\r\n id: string;\r\n name: string;\r\n bio: string | null;\r\n}\r\n\r\nexport async function createAuthor(client: Client, args: CreateAuthorArgs): Promise\u003cCreateAuthorRow | null> {\r\n const result = await client.query({\r\n text: createAuthorQuery,\r\n values: [args.name, args.bio],\r\n rowMode: \"array\"\r\n });\r\n if (result.rows.length !== 1) {\r\n return null;\r\n }\r\n const row = result.rows[0];\r\n return {\r\n id: row[0],\r\n name: row[1],\r\n bio: row[2]\r\n };\r\n}\r\n\r\nexport const deleteAuthorQuery = `-- name: DeleteAuthor :exec\r\nDELETE FROM authors\r\nWHERE id = $1`;\r\n\r\nexport interface DeleteAuthorArgs {\r\n id: string;\r\n}\r\n\r\nexport async function deleteAuthor(client: Client, args: DeleteAuthorArgs): Promise\u003cvoid> {\r\n await client.query({\r\n text: deleteAuthorQuery,\r\n values: [args.id],\r\n rowMode: \"array\"\r\n });\r\n}\r\n```","author":{"url":"https://github.com/kyleconroy","@type":"Person","name":"kyleconroy"},"datePublished":"2020-01-24T00:35:15Z","url":"https://github.com/sqlc-dev/sqlc/issues/296"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:63433c03-0285-d621-b460-edcc0c554d0f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A77C:A70A9:3F4C0A:53DBFD:6B0195D7 |
| html-safe-nonce | d6ba481ddccdda48680e5977c1e1cd3470176424e2fb5d5837a69a77118e0965 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNzdDOkE3MEE5OjNGNEMwQTo1M0RCRkQ6NkIwMTk1RDciLCJ2aXNpdG9yX2lkIjoiOTAyNTExMDI3OTQ3Nzg5MjU2NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 2171da0b2baee786fce3d915a2338d2ab0e0b3a42213876749fd61ee4693e2b7 |
| hovercard-subject-tag | issue:554486291 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/sqlc-dev/sqlc/296/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1d37ba98785908deea9cc41b3f070a9199126b6261d9db6c70c303312393297c/sqlc-dev/sqlc/issues/296 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1d37ba98785908deea9cc41b3f070a9199126b6261d9db6c70c303312393297c/sqlc-dev/sqlc/issues/296 |
| og:image:alt | sqlc, like protoc, aims to generate code for multiple languages. The second language we're looking to add is TypeScript. Getting started WarningThe TypeScript plugin (sqlc-gen-typescript) is under ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | kyleconroy |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/sqlc-dev/sqlc git https://github.com/sqlc-dev/sqlc.git |
| octolytics-dimension-user_id | 136738596 |
| octolytics-dimension-user_login | sqlc-dev |
| octolytics-dimension-repository_id | 193160679 |
| octolytics-dimension-repository_nwo | sqlc-dev/sqlc |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 193160679 |
| octolytics-dimension-repository_network_root_nwo | sqlc-dev/sqlc |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width