Skip to content

Commit 4c926a4

Browse files
docs: sync from coderbuzz/codex@9a7a8a5
1 parent 50b09b9 commit 4c926a4

2 files changed

Lines changed: 44 additions & 3 deletions

File tree

‎AI_KNOWLEDGE.md‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- docs: sync from coderbuzz/codex@30320de -->
1+
<!-- docs: sync from coderbuzz/codex@9a7a8a5 -->
22

33
# @coderbuzz/sql: AI Expert Knowledge Reference
44

@@ -1370,7 +1370,8 @@ On a runtime without `Bun.SQL` the constructor throws with a message pointing at
13701370
| Option | Default | Meaning |
13711371
| --- | --- | --- |
13721372
| `connectionString` | none | `postgres://user:pass@host:port/db`; an explicit `user`/`password` overrides the credentials inside it (this is what lets `unsafeCrossTenant` swap in the BYPASSRLS role) |
1373-
| `host` / `port` / `database` / `user` / `password` | `localhost` / `5432` / none / none / none | connection parts |
1373+
| `host` / `port` | `localhost` / `5432` | pinned like the `pg` engine, so `DATABASE_URL`/`PGHOST`/`PGPORT` cannot redirect the engine |
1374+
| `database` / `user` / `password` | `PGDATABASE` / `PGUSER` / `PGPASSWORD`, then `USER` | same libpq fallback as the `pg` engine; see below |
13741375
| `max` | `10` | pool size |
13751376
| `idleTimeout` | driver default | seconds a pooled connection may idle |
13761377
| `connectionTimeout` | driver default | seconds to wait for a connection |
@@ -1383,6 +1384,41 @@ On a runtime without `Bun.SQL` the constructor throws with a message pointing at
13831384
| `crossTenant` | none | `{ user, password, max? }` for a `BYPASSRLS` role |
13841385
| `onCrossTenantAccess` | none | called with the reason on every `unsafeCrossTenant()` |
13851386

1387+
**Environment fallback (measured on Bun 1.4.2 and `pg` 8.21.0).** Left to
1388+
itself, `Bun.SQL` with `adapter: 'postgres'` fills every connection field left
1389+
out from the environment, and an empty string (or `port: 0`) counts as left out:
1390+
1391+
| Source | Variables | Read by raw `Bun.SQL` | Read by this engine | Read by the `pg` engine |
1392+
| --- | --- | --- | --- | --- |
1393+
| URL | `DATABASE_URL` > `POSTGRES_URL` > `PGURL` > `PG_URL`; `TLS_DATABASE_URL`, `TLS_POSTGRES_DATABASE_URL` also turn TLS on | yes, any scheme (`mysql://` included) | **no** | no |
1394+
| host / port | `PGHOST`, `PGPORT` | yes | only `PGPORT`, and only when `connectionString` has no port | same as this engine |
1395+
| credentials | `PGUSER`, `PGPASSWORD`, `PGDATABASE`, then `USER` for user and database | yes | yes | yes |
1396+
| TLS | `PGSSLMODE` | yes | yes | yes |
1397+
| ignored | `POSTGRES_HOST`/`_USER`/`_PASSWORD`/`_DATABASE`/`_DB`, `POSTGRES_DATABASE_URL` | no | no | no |
1398+
1399+
Up to 0.8.0 the engine passed raw `Bun.SQL` the fields you gave and
1400+
nothing else. With `DATABASE_URL=postgres://du:dupw@du-host:5111/dudb` in the
1401+
environment, `pg.connect({ user, password, database })` connected to
1402+
`du-host:5111` and sent *your* password there. `bunOptions()` now always
1403+
passes a `url` (the `connectionString`, or `postgres://localhost:5432` with
1404+
`hostname`/`port` set explicitly), which is what stops `Bun.SQL` from reading
1405+
the URL variables at all. The libpq `PG*` fallback is kept because the `pg`
1406+
engine has it too.
1407+
1408+
Details:
1409+
1410+
- Raw `Bun.SQL` lets `PGDATABASE` override the database named in the url.
1411+
`pg` does not, so the engine passes the url's database as `database`
1412+
explicitly. `config.database` still wins over both.
1413+
- An empty `password: ''` does not mean "no password" under either engine:
1414+
`PGPASSWORD` fills it. Unset `PGPASSWORD` if the server uses trust auth.
1415+
- `unsafeCrossTenant()` builds its client through the same `bunOptions()`,
1416+
so it connects to the same host/port/database as the main client with the
1417+
`crossTenant` credentials swapped in.
1418+
1419+
Tests: `tests/sql.entrypoints.test.ts`, "pg namespace on Bun.SQL", set the
1420+
variables in `process.env`, construct the engine and read `db.client.options`.
1421+
13861422
### Type mapping (verified against PostgreSQL 16 via Bun)
13871423

13881424
| PostgreSQL type | JS value from the driver | Column factory | Declared TS type |

‎README.md‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- docs: sync from coderbuzz/codex@30320de -->
1+
<!-- docs: sync from coderbuzz/codex@9a7a8a5 -->
22

33
# @coderbuzz/sql
44

@@ -988,6 +988,11 @@ Bun-specific options: `bigint: true` (return `int8` as a JS `bigint`),
988988
reachable as `db.client` for the parts Bun offers and this engine does not wrap,
989989
such as `LISTEN`/`NOTIFY`.
990990

991+
Environment variables work as they do with `pg`: `PGUSER`, `PGPASSWORD`,
992+
`PGDATABASE` and `PGSSLMODE` fill fields you leave out, while host and port
993+
default to `localhost:5432`. `DATABASE_URL` and the other URL variables
994+
`Bun.SQL` would read on its own are ignored; pass them as `connectionString`.
995+
991996
### MySQL
992997

993998
```ts

0 commit comments

Comments
 (0)