Skip to content

Commit 20c71db

Browse files
timohuovinenclaude
andcommitted
fix(postgres): type names from the database analyzer, enums, escaped query text
sqlc's database analyzer names types the way format_type prints them (integer, boolean, timestamp(3) with time zone, numeric(12,2)); the type tables only knew the pg_type names, so those parameters and computed columns fell back to string, and postgres.js sends a string given for a boolean parameter as false. Both spellings now map to the same type in the postgres and pg drivers, modifiers dropped. - Enum types become string literal unions, from the catalog. - postgres driver: date and timestamp parameters take Date | string. - The query is emitted as a template literal with backslash, backtick and ${ escaped, so `'\%'` reaches the database as written. - Each query file is emitted once instead of once per query. - Code generation moves to src/codegen.ts so tests run it under Node: npm test (no database) and npm run test:postgres (analyzer-generated code run with postgres.js); CI runs both against PostgreSQL 17. - A vX.Y.Z tag publishes the WASM as a release asset with its sha256. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 395a0ba commit 20c71db

19 files changed

Lines changed: 1296 additions & 321 deletions

‎.github/workflows/ci.yml‎

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,55 @@ jobs:
1111
- uses: actions/checkout@v4
1212
- uses: sqlc-dev/setup-sqlc@v4
1313
with:
14-
sqlc-version: '1.24.0'
14+
sqlc-version: '1.31.1'
1515
- uses: actions/setup-node@v4
16+
with:
17+
node-version: 20
1618
- run: wget https://github.com/bytecodealliance/javy/releases/download/v1.2.0/javy-x86_64-linux-v1.2.0.gz
1719
- run: gzip -d javy-x86_64-linux-v1.2.0.gz
1820
- run: chmod +x javy-x86_64-linux-v1.2.0
19-
- run: npm install
21+
- run: npm ci
2022
- run: npx tsc --noEmit
23+
- run: npm test
2124
- run: npx esbuild --bundle src/app.ts --tree-shaking=true --format=esm --target=es2020 --outfile=out.js
2225
- run: ./javy-x86_64-linux-v1.2.0 compile out.js -o examples/plugin.wasm
2326
- run: sqlc -f sqlc.dev.yaml diff
24-
working-directory: examples
27+
working-directory: examples
28+
- uses: actions/upload-artifact@v4
29+
with:
30+
name: plugin.wasm
31+
path: examples/plugin.wasm
32+
33+
# Generates test/postgres with the database analyzer (the way PostgreSQL
34+
# itself names types) and runs the generated functions with postgres.js.
35+
postgres:
36+
needs: build
37+
runs-on: ubuntu-latest
38+
services:
39+
postgres:
40+
image: postgres:17
41+
env:
42+
POSTGRES_PASSWORD: postgres
43+
ports:
44+
- 5432:5432
45+
options: >-
46+
--health-cmd pg_isready --health-interval 5s --health-timeout 5s --health-retries 10
47+
env:
48+
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/postgres
49+
steps:
50+
- uses: actions/checkout@v4
51+
- uses: sqlc-dev/setup-sqlc@v4
52+
with:
53+
sqlc-version: '1.31.1'
54+
- uses: actions/setup-node@v4
55+
with:
56+
node-version: 20
57+
- uses: actions/download-artifact@v4
58+
with:
59+
name: plugin.wasm
60+
path: examples
61+
- run: npm ci
62+
- run: psql "$DATABASE_URL" -v ON_ERROR_STOP=1 -f test/postgres/schema.sql
63+
- run: sqlc diff
64+
working-directory: test/postgres
65+
- run: npm run test:postgres

‎.github/workflows/release.yml‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: release
2+
# A tag vX.Y.Z publishes the plugin as a release asset; pin it in sqlc.yaml by
3+
# its URL and the sha256 printed in the release notes.
4+
on:
5+
push:
6+
tags:
7+
- 'v*'
8+
permissions:
9+
contents: write
10+
jobs:
11+
release:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v4
15+
- uses: actions/setup-node@v4
16+
with:
17+
node-version: 20
18+
- run: wget https://github.com/bytecodealliance/javy/releases/download/v1.2.0/javy-x86_64-linux-v1.2.0.gz
19+
- run: gzip -d javy-x86_64-linux-v1.2.0.gz
20+
- run: chmod +x javy-x86_64-linux-v1.2.0
21+
- run: npm ci
22+
- run: npx tsc --noEmit
23+
- run: npm test
24+
- run: npx esbuild --bundle src/app.ts --tree-shaking=true --format=esm --target=es2020 --outfile=out.js
25+
- run: ./javy-x86_64-linux-v1.2.0 compile out.js -o sqlc-gen-typescript.wasm
26+
- name: Publish
27+
env:
28+
GH_TOKEN: ${{ github.token }}
29+
TAG: ${{ github.ref_name }}
30+
run: |
31+
sha=$(sha256sum sqlc-gen-typescript.wasm | cut -d' ' -f1)
32+
url="https://github.com/${GITHUB_REPOSITORY}/releases/download/${TAG}/sqlc-gen-typescript.wasm"
33+
{
34+
echo '```yaml'
35+
echo 'plugins:'
36+
echo '- name: ts'
37+
echo ' wasm:'
38+
echo " url: $url"
39+
echo " sha256: $sha"
40+
echo '```'
41+
} > notes.md
42+
gh release create "$TAG" sqlc-gen-typescript.wasm --title "$TAG" --notes-file notes.md

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ out.js
33
*.wasm
44
javy
55
tests
6+
test/out

‎README.md‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,53 @@
11
# sqlc-gen-typescript
22

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+
351
> [!CAUTION]
452
> 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!
553

‎package-lock.json‎

Lines changed: 181 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44
"description": "",
55
"main": "app.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"build": "tsc --noEmit && esbuild --bundle src/app.ts --tree-shaking=true --format=esm --target=es2020 --outfile=out.js",
8+
"test": "esbuild --bundle test/codegen.test.ts --platform=node --format=cjs --target=node20 --alias:javy/fs=./test/javy-fs.ts --log-level=warning --outfile=test/out/codegen.test.cjs && node --test test/out/codegen.test.cjs",
9+
"test:postgres": "esbuild --bundle test/postgres/postgres.test.ts --platform=node --format=esm --target=node20 --alias:javy/fs=./test/javy-fs.ts --external:postgres --log-level=warning --outfile=test/out/postgres.test.mjs && node --test test/out/postgres.test.mjs"
810
},
911
"author": "",
1012
"license": "ISC",
1113
"devDependencies": {
14+
"@types/node": "^20.19.43",
1215
"esbuild": "^0.19.5",
16+
"postgres": "^3.4.9",
1317
"typescript": "^5.2.2"
1418
},
1519
"dependencies": {

0 commit comments

Comments
 (0)