Edge & Serverless Features
SlintORM is designed for edge runtimes where traditional TCP-based database drivers are unavailable. Use proxyExec, custom ExecFnimplementations, and pre-generated schemas to run in Cloudflare Workers, Vercel Edge, Deno, and Bun.
proxyExec — HTTP proxy for SQL
proxyExec creates an ExecFn that sends SQL statements over HTTP to a proxy server, which executes them against the database. This bypasses the need for direct TCP connections — ideal for edge runtimes that only support HTTP.
// proxyExec — HTTP proxy for database access in edge runtimes
import ORMManager, { proxyExec } from 'slintorm';
// Create an exec function that tunnels SQL through an HTTP proxy
const exec = proxyExec({
endpoint: 'https://your-proxy.com',
// Optional: authentication
headers: { Authorization: 'Bearer ' + process.env.PROXY_SECRET! },
});
const orm = new ORMManager({
exec,
driver: 'sqlite', // logical driver for query generation
schema, // pre-generated schema (required on edge)
});Custom exec functions
Pass any function matching ExecFn to ORMManager to bypass SlintORM's built-in TCP-based drivers. This works in Cloudflare Workers, Deno, Vercel Edge, and Bun — anywhere you can execute SQL but can't open raw TCP sockets.
ExecFn = (sql: string, params?: any[]) => Promise<SQLExecResult>// Custom exec function — full control over SQL execution
import ORMManager from 'slintorm';
import type { ExecFn, SQLExecResult } from 'slintorm';
// Signature: (sql: string, params?: any[]) => Promise<SQLExecResult>
const myExec: ExecFn = async (sql, params) => {
// Send SQL to your own endpoint or driver
const result = await myDatabaseClient.query(sql, params);
return {
columns: result.fields.map(f => f.name),
rows: result.rows,
rowCount: result.rowCount,
};
};
const orm = new ORMManager({
exec: myExec,
driver: 'postgres',
schema,
});Pre-generated schemas
Edge runtimes typically lack filesystem access. Run npx slintorm generate at build time to produce a JSON schema, then pass it to the constructor. This eliminates all runtime filesystem reads.
// Pre-generated schema — no filesystem reads at runtime
// Run at build time:
// npx slintorm generate
// Import the generated schema
import { schema } from './schema/generated';
// Pass it to ORMManager constructor
const orm = new ORMManager({
driver: 'sqlite',
schema, // Skips filesystem scan entirely
});Read replicas on edge
Use the replicas config option to distribute read queries across replicas while directing writes to the primary.
// Read replicas for edge
const orm = new ORMManager({
driver: 'postgres',
databaseUrl: process.env.PG_WRITER_URL!,
replicas: [
{ databaseUrl: process.env.PG_REPLICA_1_URL! },
{ databaseUrl: process.env.PG_REPLICA_2_URL! },
],
schema,
});End-to-end: Cloudflare Workers
// Complete example — Cloudflare Workers
import ORMManager, { proxyExec } from 'slintorm';
import schema from './schema.json';
// In Cloudflare Workers, direct TCP connections are unavailable.
// Use proxyExec to tunnel through an HTTP proxy service.
const exec = proxyExec({
endpoint: 'https://db-proxy.example.com',
headers: {
Authorization: 'Bearer ' + (process.env as any).PROXY_SECRET!,
},
});
const orm = new ORMManager({
driver: 'sqlite',
exec,
schema, // Required — no filesystem in Workers
logs: false,
});
export default {
async fetch(request: Request): Promise<Response> {
orm.withContext({ requestId: request.headers.get('cf-ray') });
try {
const users = await orm.db.User.findAll();
return Response.json({ users });
} finally {
orm.clearContext();
}
},
};Limitations & considerations
proxyExecadds HTTP latency — keep your proxy server geographically close to your edge runtime.- Pre-generated schemas must be regenerated when models change (add to your CI/CD pipeline).
- Custom
ExecFnimplementations must handle parameter binding and return the correctSQLExecResultshape. - Not all drivers are supported in all edge runtimes — test your specific runtime before deploying.
- Connection pooling is managed externally when using
proxyExec; configure pool size on your proxy server.