Build a serverless API using Cloudflare Workers, Drizzle ORM, and Neon
A step-by-step setup for a serverless TypeScript API on Cloudflare Workers that uses Hono for routing, Drizzle ORM for schema definition and migrations, and Neon serverless Postgres for storage. It scaffolds a Worker with create-cloudflare, defines a products table in TypeScript, and uses drizzle-kit to generate SQL migrations. Migrations run through a postgres.js script capped at a single connection so they execute in order. The Worker queries Neon over HTTP via @neondatabase/serverless, is deployed with wrangler, and gets its DATABASE_URL from Cloudflare's Neon integration instead of manually configured secrets.
Related posts
- Build and Deploy a Global Serverless Nuxt SSR App with Cloudflare Hyperdrive and Postgres
Neon mediumAdept
A Nuxt SSR app on Cloudflare Pages can query Neon Postgres through Hyperdrive by routing Drizzle's pg-proxy driver to a separate Hono-based Cloudflare Worker that holds the Hyperdrive binding and runs queries with node-postgres. The detour through a Worker exists because Pages Functions don't fully support node-postgres yet, which breaks local Nuxt builds. The walkthrough covers the nitro cloudflare-pages preset, creating the Hyperdrive config from a pooled Neon connection string, wrangler.toml with node_compat, the Worker's /query endpoint, and the Drizzle proxy driver. Hyperdrive's default cache serves responses for 60 seconds with a 15-second stale-while-revalidate window. The example endpoints are unauthenticated and must be locked down before production use.
- @neondatabase/serverless hits 1.0.0
Neon mediumAdept
Neon's JavaScript/TypeScript serverless driver, which carries Postgres queries over HTTP and WebSockets for environments without raw TCP such as V8 isolates and browsers, reaches 1.0.0 after a refactor that adds auto-generated types via api-extractor and CI tests across Node, Bun, Deno, browsers, Cloudflare Workers, Vercel and the Prisma/Drizzle adapters. Tagged-template queries are now composable, because placeholder numbering is deferred until execution. A breaking change makes calling sql(...) as a plain function an error, since that form used ordinary string interpolation and invited SQL injection. New sql.query and sql.unsafe helpers cover the parameterized and trusted-raw-SQL cases. Binary inserts over HTTP now hex-encode roughly 10x faster, though WebSockets remain the recommended path for large blobs.
- Build a Serverless API using AWS Lambda, CDK, and Neon
Neon mediumAdept
A step-by-step build of a single-endpoint REST API on AWS Lambda, defined with AWS CDK v2 in TypeScript, that returns a product list from a Neon serverless Postgres database. The Lambda uses the Neon serverless driver (HTTP/WebSockets instead of TCP) and fetches the connection string from AWS Secrets Manager once via top-level await, so it isn't retrieved on every request. The CDK stack bundles the function as ESM for Node 20, grants a secretsmanager:GetSecretValue policy and wires it to an API Gateway RestApi GET /products route. Open CORS is used for the demo, with a note to restrict origins in production.
- Introducing neon.ts: infrastructure as code for your Neon projects
Neon mediumAdept
neon.ts is a TypeScript infrastructure-as-code file for Neon projects that declares services (Postgres, Neon Auth, Data API, with functions, buckets and an AI gateway coming under a preview block) and provisions them with `neon config plan` and `neon config apply`. A companion `@neondatabase/env` package provides `parseEnv`, which validates `process.env` against the declared services and returns a typed env object, with optional per-app subsets. A programmable `branch` function sets TTL, autoscaling limits and scale-to-zero for new branches created via `neon checkout`. Type-level encoding of service dependencies turns cryptic `never` errors into messages that explain the fix, such as requiring Neon Auth for the Data API.
- Build a Database App with Drizzle ORM and Deno
Deno mediumAdeptDrizzle ORM can be used from Deno via npm compatibility to get type-safe CRUD access to PostgreSQL. The walkthrough installs drizzle-orm, drizzle-kit and pg, configures drizzle.config.ts with a DATABASE_URL, and shows two schema paths: introspecting an existing database with `drizzle-kit pull` or defining pgTable schemas in TypeScript and generating migrations with `drizzle-kit generate`. It then builds helper functions for inserting, querying, updating and deleting rows across two related tables using a pg Pool-backed Drizzle client, with the Deno flags (--env, --node-modules-dir) needed to make drizzle-kit work.