With Next.jsx
Use Mongster in route handlers or server actions without adding a second data layer
In a Next.js app, the cleanest pattern is to keep the connection and model definitions in a server-only module, then call them from route handlers or server actions.
import { , } from "./db";
type = { : string };
export async function (: Request) {
await ();
const = (await .()) as ;
const = await .({
: .,
});
return new (.(), {
: 201,
: { "content-type": "application/json" },
});
}The same shared module also works for server actions. The important part is that connection setup stays on the server and does not leak into client components.