@tspg/rest
v0.2.0
Published
PostgREST as a library using web-standard `Request` and `Response`
Readme
@tspg/rest
PostgREST as a library. The same behaviour, as a web-standard handle(Request) => Promise<Response> you mount in your own server.
One difference is accepted today → see PARITY.md.
No JWT: you decide who the caller is — cookie, API key, session, mTLS — and hand over a role and
claims, applied with set_config so Row Level Security sees what it would behind PostgREST. No runtime dependencies, and your driver stays yours.
npm i @tspg/rest pg
yarn add @tspg/rest pg
pnpm i @tspg/rest pg
bun add @tspg/rest # Bun.sql is built inWe also support other postgres adapters, like PGlite and postgres-js.
import pg from "pg";
import { createRest } from "@tspg/rest";
import { nodePostgresAdapter } from "@tspg/rest/adapters/node-postgres";
const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL });
const rest = await createRest({
db: nodePostgresAdapter(pool), // you create and close the driver
schemas: ["api"], // the first one is the default schema
anonymousRole: "anon",
resolveContext: async (req) => {
const user = await mySessionLookup(req); // your auth, by any means
return user ? { role: "authenticated", claims: { sub: user.id } } : undefined;
},
});
Bun.serve({ fetch: (req) => rest.handle(req) });
// or @hono/node-server, or Hono, or any Request/Response hostcurl 'localhost:3000/todos?select=id,title,tags(label)&done=is.false&order=priority.desc&limit=10'Any PostgREST client works — the wire format is the whole contract, including @supabase/postgrest-js and
@neondatabase/postgrest-js.
Not an ORM: there is no model of your database here to drift from it. The types are read out of the catalog, one way, and the query language stays PostgREST's.
