Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,55 @@ jobs:
- uses: actions/checkout@v4
- uses: sqlc-dev/setup-sqlc@v4
with:
sqlc-version: '1.24.0'
sqlc-version: '1.31.1'
- uses: actions/setup-node@v4
with:
node-version: 20
- run: wget https://github.com/bytecodealliance/javy/releases/download/v1.2.0/javy-x86_64-linux-v1.2.0.gz
- run: gzip -d javy-x86_64-linux-v1.2.0.gz
- run: chmod +x javy-x86_64-linux-v1.2.0
- run: npm install
- run: npm ci
- run: npx tsc --noEmit
- run: npm test
- run: npx esbuild --bundle src/app.ts --tree-shaking=true --format=esm --target=es2020 --outfile=out.js
- run: ./javy-x86_64-linux-v1.2.0 compile out.js -o examples/plugin.wasm
- run: sqlc -f sqlc.dev.yaml diff
working-directory: examples
working-directory: examples
- uses: actions/upload-artifact@v4
with:
name: plugin.wasm
path: examples/plugin.wasm

# Generates test/postgres with the database analyzer (the way PostgreSQL
# itself names types) and runs the generated functions with postgres.js.
postgres:
needs: build
runs-on: ubuntu-latest
services:
postgres:
image: postgres:17
env:
POSTGRES_PASSWORD: postgres
ports:
- 5432:5432
options: >-
--health-cmd pg_isready --health-interval 5s --health-timeout 5s --health-retries 10
env:
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/postgres
steps:
- uses: actions/checkout@v4
- uses: sqlc-dev/setup-sqlc@v4
with:
sqlc-version: '1.31.1'
- uses: actions/setup-node@v4
with:
node-version: 20
- uses: actions/download-artifact@v4
with:
name: plugin.wasm
path: examples
- run: npm ci
- run: psql "$DATABASE_URL" -v ON_ERROR_STOP=1 -f test/postgres/schema.sql
- run: sqlc diff
working-directory: test/postgres
- run: npm run test:postgres
42 changes: 42 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: release
# A tag vX.Y.Z publishes the plugin as a release asset; pin it in sqlc.yaml by
# its URL and the sha256 printed in the release notes.
on:
push:
tags:
- 'v*'
permissions:
contents: write
jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: wget https://github.com/bytecodealliance/javy/releases/download/v1.2.0/javy-x86_64-linux-v1.2.0.gz
- run: gzip -d javy-x86_64-linux-v1.2.0.gz
- run: chmod +x javy-x86_64-linux-v1.2.0
- run: npm ci
- run: npx tsc --noEmit
- run: npm test
- run: npx esbuild --bundle src/app.ts --tree-shaking=true --format=esm --target=es2020 --outfile=out.js
- run: ./javy-x86_64-linux-v1.2.0 compile out.js -o sqlc-gen-typescript.wasm
- name: Publish
env:
GH_TOKEN: ${{ github.token }}
TAG: ${{ github.ref_name }}
run: |
sha=$(sha256sum sqlc-gen-typescript.wasm | cut -d' ' -f1)
url="https://github.com/${GITHUB_REPOSITORY}/releases/download/${TAG}/sqlc-gen-typescript.wasm"
{
echo '```yaml'
echo 'plugins:'
echo '- name: ts'
echo ' wasm:'
echo " url: $url"
echo " sha256: $sha"
echo '```'
} > notes.md
gh release create "$TAG" sqlc-gen-typescript.wasm --title "$TAG" --notes-file notes.md
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ out.js
*.wasm
javy
tests
test/out
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,53 @@
# sqlc-gen-typescript

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

> [!CAUTION]
> 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!

Expand Down
182 changes: 181 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"build": "tsc --noEmit && esbuild --bundle src/app.ts --tree-shaking=true --format=esm --target=es2020 --outfile=out.js",
"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",
"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"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@types/node": "^20.19.43",
"esbuild": "^0.19.5",
"postgres": "^3.4.9",
"typescript": "^5.2.2"
},
"dependencies": {
Expand Down
Loading