Bun
Use MeshQL on Bun with npm packages. Express and Hono both work well.
Prerequisites
Section titled “Prerequisites”- Bun 1+
Install
Section titled “Install”bun add meshql-core meshql-http meshql-client expressOr via JSR: bunx jsr add @meshql/core @meshql/http @meshql/client && bun add express
Server (Express)
Section titled “Server (Express)”import { createMesh, buildSelectSql, type MeshSchema } from "@meshql/core";import { meshExpressRouter } from "@meshql/http/express";import express from "express";
const schema: MeshSchema = { entities: { user: { type: {} as { id: number; name: string }, fields: ["id", "name"], table: "users" }, token: { type: {} as { accessToken: string }, 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); 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");});bun run server.tsHono alternative
Section titled “Hono alternative”Bun’s native fetch works with Hono out of the box:
import { Hono } from "hono";import { meshHonoRoutes } from "@meshql/http/hono";
const app = new Hono();app.route("/", meshHonoRoutes(mesh, { basePath: "/mesh" }));
export default { port: 3001, fetch: app.fetch };Run with bun run server.ts.
Client test
Section titled “Client test”Bun’s built-in fetch works with @meshql/client:
import { createClient } from "@meshql/client";
const client = createClient({ url: "http://localhost:3001/mesh" });const user = await client.query( { user: { id: true, name: true, tokens: { accessToken: true } } }, { entityId: "1" },);console.log(user);