Skip to content

Express + Postgres

examples/express-app is a minimal but complete REST backend. It shows the production-shaped way to wire MountSQLI into Express.

  • Directoryexpress-app/
    • mountsqli.config.ts
    • Directoryschema/
      • users.ts
    • Directorysrc/
      • server.ts
    • package.json
mountsqli.config.ts
import { defineConfig } from "@mountsqli/core";
import { users } from "./schema";
export default defineConfig({
driver: "postgres",
url: process.env.DATABASE_URL!,
tables: [users],
});

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...
});
Terminal window
DATABASE_URL=postgres://localhost:5432/app pnpm --filter express-app build
pnpm --filter express-app migrate apply
pnpm --filter express-app dev
curl http://localhost:3741/users
  • Typed Db from config (DbFromConfig).
  • Migrations applied before serving.
  • Awaited REST handler with auth.
  • Apply migrations in the deploy step, before listen.
  • Keep the config as the schema of record.
  • Not awaiting createRestHandler (auth bypass).
  • Serving before migrate apply (tables missing).