Reputation: 339
I'm developing a Next.js application using the new App Router and considering using Drizzle ORM for type-safe database queries and migrations. My target deployment platform is Cloudflare Pages, but I also want to understand the behavior on Vercel.
From what I understand, Cloudflare Pages runs API routes and Server Actions on the Edge Runtime, which has a limited set of Node.js APIs. Drizzle ORM, on the other hand, depends on Node.js modules like fs (for reading migration files) and net (for establishing direct database connections).
So my questions are:
Can Drizzle ORM be used for runtime queries on Cloudflare Pages? What are the implications compared to deploying on Vercel, where a Node.js Runtime is available?
Upvotes: 0
Views: 43
Reputation: 339
Drizzle ORM does not work at runtime on Cloudflare Pages but works on Vercel Cloudflare Pages (Edge Runtime): Limitations of Edge Runtime: Cloudflare Pages deploys your API routes and Server Actions as Edge Functions (Cloudflare Workers). These workers run in an environment that does not support many Node.js APIs such as:
fs (file system access) net (TCP connections) Drizzle ORM relies on these modules to load migration files and establish direct database connections. Therefore, if you try to use Drizzle ORM for runtime queries on Cloudflare Pages, you will encounter errors because the necessary Node.js modules are missing in the Edge Runtime.
What You Can Do Instead:
Schema Management: You can still use Drizzle ORM locally for schema management and migration generation. Use drizzle-kit to generate and apply migrations to your Supabase database. Database Queries at Runtime: Since direct database connections via Drizzle are not feasible on Cloudflare Pages, you should use the Supabase REST API (or another compatible edge-friendly adapter like Prisma Data Proxy) to perform runtime queries. This allows you to interact with your database without relying on Node.js-specific modules. Vercel (Node.js Runtime): Full Node.js Support: Vercel’s default runtime for API routes and Server Components is the Node.js Runtime, which supports all Node.js modules (including fs, net, etc.). This means Drizzle ORM can be used for both schema management and runtime queries on Vercel without encountering the limitations present in Edge Runtime environments. Developer Experience: With Vercel, you benefit from full support for Node.js APIs, making it straightforward to use ORMs like Drizzle ORM for type-safe, direct database access in your Next.js application. Summary: Cloudflare Pages:
Runtime: Edge Runtime (Cloudflare Workers) Issue: Lacks Node.js modules like fs and net required by Drizzle ORM Workaround: Use Drizzle ORM for migration/schema management only; for runtime queries, rely on the Supabase REST API or an edge-compatible solution like Prisma Data Proxy. Vercel:
Runtime: Node.js Runtime Advantage: Full Node.js API support allows you to use Drizzle ORM both for migrations and runtime database queries. In short, if you plan to deploy on Cloudflare Pages, you won’t be able to use Drizzle ORM for runtime operations due to the limitations of the Edge Runtime. You can still use it for managing your schema, but for data queries you'll need an alternative approach. Conversely, deploying on Vercel provides a Node.js environment where Drizzle ORM works as expected.
Upvotes: 0