Introduction
pg-sourcerer reads the catalog of a PostgreSQL database and writes TypeScript for the role that runs your application. The catalog is the set of system tables that describe tables, columns, policies and grants.
The generator knows which visitor role will run the code. It writes a query only when that role can run it, so the generated API follows the grants and the row-level security policies of your database.
What you get
The files sit in a folder for each table, <schema>/<table>/. The names below are the files in that folder.
| Output | Files | Content |
|---|---|---|
effect | schemas.ts, queries.ts, errors.ts, rpc.ts, http.ts | Effect Schema.Struct values, query functions, RPC and HTTP API definitions |
kysely | kysely/db.ts, kysely/queries.ts | The DB interface and one query function per query |
types | types.ts | Plain TypeScript row, insert and update types |
suggestion-report | query-suggestions.json | A report of the queries the generator could produce |
Try it
The playground on the home page runs the generator in your browser. It loads a schema into an in-page PostgreSQL (PGlite), reads its catalog and shows the generated files.
Run it
pg-sourcerer needs Bun and a database that has the visitor role.
bun run src/cli.ts --connection postgres://owner@localhost/app generated visitor --plugins effect,kysely
The command writes the output to generated/, one <schema>/<table>/ folder for each table. Configuration explains the config file and the flags. Outputs explains the files. The authoring guide walks through a complete run, and Conjure is the builder you use to write your own output.