|
1 | 1 | # sqlc-gen-typescript |
2 | 2 |
|
| 3 | +> [!NOTE] |
| 4 | +> **Wind-Growth fork** of [sqlc-dev/sqlc-gen-typescript](https://github.com/sqlc-dev/sqlc-gen-typescript) |
| 5 | +> (upstream `main`, which is 0.1.3 plus a `DO NOT EDIT` header and a null-row check). |
| 6 | +> It fixes the PostgreSQL gaps that made generated code silently wrong when |
| 7 | +> sqlc's [database analyzer](https://docs.sqlc.dev/en/latest/howto/generate.html#enhanced-analysis-with-managed-databases) |
| 8 | +> is on. The output shape is unchanged: `xArgs` / `xRow` interfaces, camelCase |
| 9 | +> fields, `null`, rows returned directly. |
| 10 | +> |
| 11 | +> - **Type names.** The analyzer names types the way PostgreSQL's `format_type` |
| 12 | +> prints them (`integer`, `boolean`, `timestamp(3) with time zone`, |
| 13 | +> `numeric(12,2)`); upstream only knew the `pg_type` names (`int4`, `bool`, |
| 14 | +> `timestamptz`), so every such parameter and computed column fell back to |
| 15 | +> `string`. postgres.js then sends any string given for a boolean parameter as |
| 16 | +> `false`. Both spellings now map to the same TypeScript type, for the |
| 17 | +> `postgres` and `pg` drivers. |
| 18 | +> - **Enums** become string literal unions (`"open" | "closed"`), arrays of them |
| 19 | +> included. |
| 20 | +> - **Array parameters** (`any(sqlc.arg(ids)::uuid[])`) are typed by their |
| 21 | +> element type through the same table, so `integer[]` is `number[]` rather |
| 22 | +> than `string[]`; postgres.js serialises a JavaScript array for them. |
| 23 | +> - **Date and timestamp parameters** (`postgres` driver) take `Date | string`, |
| 24 | +> which is what postgres.js accepts; both go through `new Date(x)`, so a |
| 25 | +> timestamp keeps milliseconds only. Cast through text to keep microseconds. |
| 26 | +> - **Query text is escaped** in the emitted template literal: a backslash, a |
| 27 | +> backtick or `${` in a query reaches the database as written. Upstream turned |
| 28 | +> `'\%'` into `'%'` and produced invalid TypeScript for a backtick. |
| 29 | +> - Each query file is emitted once rather than once per query. |
| 30 | +> |
| 31 | +> Not fixed here, because the plugin does not decide it: sqlc's analysis marks |
| 32 | +> a `::text`-cast column non-nullable, and a string given to a `jsonb` |
| 33 | +> parameter is a JSON string value. |
| 34 | +> |
| 35 | +> Pin a release (the release notes carry the snippet): |
| 36 | +> |
| 37 | +> ```yaml |
| 38 | +> plugins: |
| 39 | +> - name: ts |
| 40 | +> wasm: |
| 41 | +> url: https://github.com/Wind-Growth/sqlc-gen-typescript/releases/download/<tag>/sqlc-gen-typescript.wasm |
| 42 | +> sha256: <sha256 from the release notes> |
| 43 | +> ``` |
| 44 | +> |
| 45 | +> Tests: `npm test` (code generator, no database) and `npm run test:postgres` |
| 46 | +> (needs `DATABASE_URL` with `test/postgres/schema.sql` applied and |
| 47 | +> `examples/plugin.wasm` built; regenerate with `sqlc generate` in |
| 48 | +> `test/postgres`). CI runs both against PostgreSQL 17. A `vX.Y.Z` tag publishes |
| 49 | +> the WASM as a release asset. |
| 50 | +
|
3 | 51 | > [!CAUTION] |
4 | 52 | > 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! |
5 | 53 |
|
|
0 commit comments