Skip to main content

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.

OutputFilesContent
effectschemas.ts, queries.ts, errors.ts, rpc.ts, http.tsEffect Schema.Struct values, query functions, RPC and HTTP API definitions
kyselykysely/db.ts, kysely/queries.tsThe DB interface and one query function per query
typestypes.tsPlain TypeScript row, insert and update types
suggestion-reportquery-suggestions.jsonA 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.