Files
OpenStock/database/request-connection.ts
Ravi 7e193d0f8b feat: run on Cloudflare Workers via vinext (experimental; Vercel stays production) (#150)
Adds vinext/Workers build and deploy scripts, a per-request MongoDB connection on Workers (Node, Docker and Vercel keep the shared one), a lazy nodemailer transporter, and runtime-specific client-IP headers for rate limiting. Docs: an experimental Cloudflare Workers self-hosting guide.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 00:02:01 +05:30

20 lines
977 B
TypeScript

import type { Connection, Model, Schema } from 'mongoose';
import { isWorkersRuntime } from '@/database/mongoose';
// What connectToDatabase() returns: the mongoose module on Node, or `{ connection }` on Workers.
export type DbHandle = { connection: Connection };
// Compiled outside the generic so tsc does not expand Connection#model.
function bind(conn: Connection, name: string, schema: Schema): object {
const models = conn.models as Record<string, object | undefined>;
const compile = conn.model as (name: string, schema: Schema) => object;
return models[name] ?? compile(name, schema);
}
// On Workers, queries must use the connection opened for this request.
// On Node/Vercel, `fallback` is the model already compiled on the shared connection.
export function modelOn<T>(db: DbHandle, name: string, schema: Schema, fallback: Model<T>): Model<T> {
if (!isWorkersRuntime()) return fallback;
return bind(db.connection, name, schema) as Model<T>;
}