Express + Postgres
examples/express-app is a minimal but complete REST backend. It shows the
production-shaped way to wire MountSQLI into Express.
Layout
Section titled “Layout”Directoryexpress-app/
- mountsqli.config.ts
Directoryschema/
- users.ts
Directorysrc/
- server.ts
- package.json
The config
Section titled “The config”import { defineConfig } from "@mountsqli/core";import { users } from "./schema";
export default defineConfig({ driver: "postgres", url: process.env.DATABASE_URL!, tables: [users],});Routes
Section titled “Routes”REST endpoints use the API package’s createRestHandler with crudRoutes:
import { createRouter, createRestHandler, crudRoutes } from "@mountsqli/api";
const router = createRouter();router.use(crudRoutes(users.def));const handle = createRestHandler(router);
app.all("/users/*", async (req, res) => { const out = await handle(toRestRequest(req)); // awaited! if ("status" in out) return res.status(out.status).json(out.body); // run out.plan against db...});Run it
Section titled “Run it”DATABASE_URL=postgres://localhost:5432/app pnpm --filter express-app buildpnpm --filter express-app migrate applypnpm --filter express-app devcurl http://localhost:3741/usersWhat it demonstrates
Section titled “What it demonstrates”- Typed
Dbfrom config (DbFromConfig). - Migrations applied before serving.
- Awaited REST handler with auth.
Best practices
Section titled “Best practices”- Apply migrations in the deploy step, before
listen. - Keep the config as the schema of record.
Common mistakes
Section titled “Common mistakes”- Not
awaitingcreateRestHandler(auth bypass). - Serving before
migrate apply(tables missing).
Related
Section titled “Related”- REST Handler — the handler in depth.
- Migrations → Apply — the deploy step.
