Skip to content

Express

Use MeshQL with Express via meshExpressRouter.

  • Node.js 22+
  • Express 4 or 5

npm:

Terminal window
npm install meshql-core meshql-http express

JSR:

Terminal window
npx jsr add @meshql/core @meshql/http express

Bun: bun add meshql-core meshql-http express or bunx jsr add @meshql/core @meshql/http && bun add express

{
"type": "module",
"scripts": {
"start": "node src/server.js"
}
}
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");
});

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" },
);
import { meshExpressMiddleware } from "@meshql/http/express";
app.use(meshExpressMiddleware(mesh, "/mesh"));

The express-postgres example includes in-memory and Postgres modes.

Transport headers, routes, and errors: HTTP adapters.