Express
Use MeshQL with Express via meshExpressRouter.
Prerequisites
Section titled “Prerequisites”- Node.js 22+
- Express 4 or 5
Install
Section titled “Install”npm:
npm install meshql-core meshql-http expressJSR:
npx jsr add @meshql/core @meshql/http expressBun: bun add meshql-core meshql-http express or bunx jsr add @meshql/core @meshql/http && bun add express
Project setup
Section titled “Project setup”{ "type": "module", "scripts": { "start": "node src/server.js" }}Server
Section titled “Server”import { createMesh, buildSelectSql, type MeshSchema } from "@meshql/core";import { meshExpressRouter } from "@meshql/http/express";import express from "express";
const schema: MeshSchema = { entities: { user: { fields: ["id", "name"], table: "users" }, token: { fields: ["accessToken"], table: "tokens", columns: { accessToken: "access_token" }, }, }, joins: { "user.tokens": { entity: "token", on: "tokens.user_id = users.id", type: "many", }, },};
const mesh = createMesh(schema);
mesh.resolve("user", async (plan) => { const { sql, params } = buildSelectSql(plan, schema); // Replace with pool.query(sql, params) when using Postgres return [{ user_id: 1, user_name: "Ada", tokens_accessToken: "tok_abc" }];});
const app = express();app.use(express.json());app.use(meshExpressRouter(mesh, "/mesh"));
app.listen(3001, () => { console.log("MeshQL on http://localhost:3001/mesh");});Client test
Section titled “Client test”See Run example for curl commands.
import { createClient } from "@meshql/client";
const client = createClient({ url: "http://localhost:3001/mesh" });
const user = await client.query( { user: { $select: { id: true, name: true, tokens: { $select: { accessToken: true } } } } }, { entityId: "1" },);Middleware alternative
Section titled “Middleware alternative”import { meshExpressMiddleware } from "@meshql/http/express";
app.use(meshExpressMiddleware(mesh, "/mesh"));Full example
Section titled “Full example”The express-postgres example includes in-memory and Postgres modes.
Reference
Section titled “Reference”Transport headers, routes, and errors: HTTP adapters.