Mongster

With Hono

Keep connection setup outside the request path and reuse typed models in handlers

Hono works well with Mongster because both prefer small, explicit building blocks. The main rule is the same as any other server: connect once during boot, then reuse the model in route handlers.

import { , ,  } from "mongster";

const  = .({
  : .().(1),
  : .().(false),
}).();

const  = ("todos", );

let  = false;

export async function () {
  if () return;
  await .("mongodb://127.0.0.1:27017/hono-app");
   = true;
}

type <> = {
  : { (): <> };
  (: unknown, ?: number): Response;
};

type  = { : string };

export async function (: <>) {
  await ();
  const  = await ..();

  const  = await .({
    : .,
  });

  return .(, 201);
}

Put ensureDb() in middleware or a shared server module, then call your models from Hono route handlers. That keeps request-time work focused on validation and queries rather than on reconnecting.