N
Neonβ€’15mo ago
quickest-silver

Drizzle ORM + Hyperdrive

Hello all 🫑 May I please ask you which postgresql driver you consider the most optimal one, when being used by drizzle orm and hyperdrive for cloudflare workers? I understand that drizzle orm supports the neondb serverless as well: https://orm.drizzle.team/docs/get-started-postgresql However hyperdrive recommends postgres.js: https://developers.cloudflare.com/hyperdrive/configuration/connect-to-postgres/ Which one should I use? Thanks in advance for your valuable timeπŸ™
Drizzle ORM - PostgreSQL
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Cloudflare Docs
Connect to PostgreSQL | Hyperdrive
Hyperdrive supports PostgreSQL and PostgreSQL-compatible databases, popular drivers and Object Relational Mapper (ORM) libraries that use those drivers.
26 Replies
harsh-harlequin
harsh-harlequinβ€’15mo ago
I would default to using the Neon Serverless driver Postgres.js should also work haha you can try the Neon serverless driver and letting us know if you run into issues
quickest-silver
quickest-silverOPβ€’15mo ago
Hi @Mahmoud , it indeed works with postgres.js πŸ˜… With hyperdrive I was getting around 200ms only for a single query (total response time was 400-600ms), is that normal?πŸ˜… I'll check with the neon serverless driver as well
extended-salmon
extended-salmonβ€’15mo ago
200ms seems high. The goal of Hyperdrive is to reduce those latencies to tens of ms worldwide. I'm not entirely sure it's a driver problem.
quickest-silver
quickest-silverOPβ€’15mo ago
@raoufchebri thank you for your kind reply, I'm investigating it further. I will update you here about it. Thanks.
quickest-silver
quickest-silverOPβ€’15mo ago
@Mahmoud @raoufchebri can you please investigate the connection of NeonDB with Hyperdrive? In case it works, can you please provide a basic documentation with it? like an extension on this beautiful repo: https://github.com/neondatabase/cloudflare-drizzle-neon
GitHub
GitHub - neondatabase/cloudflare-drizzle-neon: Example API using Cl...
Example API using Cloudflare Workers, Drizzle ORM and Neon - neondatabase/cloudflare-drizzle-neon
extended-salmon
extended-salmonβ€’15mo ago
Do you see high response time still?
quickest-silver
quickest-silverOPβ€’15mo ago
it's not the high response, I think I have a connection issue with it (using the serverless driver with hyperdrive). Postgres.js works though (with the high latency).
extended-salmon
extended-salmonβ€’15mo ago
Can you please share the code of how you're connecting to Postgres? Also which region you're running your instance? If I get it right, you're saying that: 1. The serverless driver does not work with Hyperdrive 2. Postgres.js works, but it's slow Correct?
quickest-silver
quickest-silverOPβ€’15mo ago
Correct. To be 100% clear, I'm a python dev and the typescript part is handled by some friends who use typescript and they're helping me out. Both of them told me they faced issues with it. Personally I didn't get time to dig into their code yet, but that's what they reported me both. Either way, I think a clear documentation of using hyperdrive with neondb would be super helpful. Apart from that, the only neondb+hyperdrive example I've found online: https://dev.to/hackmamba/faster-postgres-queries-with-cloudflare-hyperdrive-and-neon-4200 points to this github: https://github.com/DesmondSanctity/neon-hyperdrive which points to this video: https://app.opentape.io/share/3b6d4fae-65d2-4f00-9341-f0cb574476be which has ~300ms end to end latency for a single query with neondb and hyperdrive the code should be this one:
import { neon } from '@neondatabase/serverless';
import { drizzle, NeonHttpDatabase } from 'drizzle-orm/neon-http';
import { Env } from "@/index";
import * as schema from "@/db/schema"

let dbInstance: NeonHttpDatabase<typeof schema>;

export const initDb = (env: Env) => {
const sql = neon(env.HYPERDRIVE.connectionString);
dbInstance = drizzle(sql, { schema, logger: true });
}

export { dbInstance as db };
import { neon } from '@neondatabase/serverless';
import { drizzle, NeonHttpDatabase } from 'drizzle-orm/neon-http';
import { Env } from "@/index";
import * as schema from "@/db/schema"

let dbInstance: NeonHttpDatabase<typeof schema>;

export const initDb = (env: Env) => {
const sql = neon(env.HYPERDRIVE.connectionString);
dbInstance = drizzle(sql, { schema, logger: true });
}

export { dbInstance as db };
quickest-silver
quickest-silverOPβ€’15mo ago
@raoufchebri @Mahmoud I think if there was a clear documentation that extends this beautiful stack/video (https://www.youtube.com/watch?v=YNtwaUVtvh0) with hyperdrive, especially if it could show the performance claims (or at least lower than 0.5s delay), it would be really cool, I think
Neon
YouTube
Build a Serverless API (Cloudflare Workers, Drizzle ORM, Hono & Neon)
Final code: https://github.com/neondatabase/cloudflare-drizzle-neon In this video, you'll learn how to use Cloudflare Workers, Hono, Drizzle ORM, and Neon to build a serverless API. Instructor: https://twitter.com/thisismahmoud_ | https://www.linkedin.com/in/mahmoud-codes/ β†’ Try Neon at https://neon.tech/youtube β†’ Neon Discord: https://neon....
extended-salmon
extended-salmonβ€’15mo ago
That's a great idea. Thanks for clarifying. We'll definitely look into it and keep you posted. Also, which region is your database located?
quickest-silver
quickest-silverOPβ€’15mo ago
I used the one in Frankfurt, I'm based in Malta. About Hyperdrive, there is nothing about it on the internet, especially with this amazing stack described on the video, so that would be really helpful, especially clearing the confusion between the different drivers (maybe a mini benchmark would look cool on the video as well?). Any update by any chance? πŸ™‚ @Mahmoud @raoufchebri I have a reply from cloudflare guys and they recommend postgres.js instead of the serverless neon driver: https://discord.com/channels/595317990191398933/1150557986239021106/1278695196216922173 https://discord.com/channels/595317990191398933/1150557986239021106/1278695288244277311 They also told me this one:
The Neon serverless driver uses WebSockets β€” because most serverless providers don't have something like Hyperdrive β€” so they aren't compatible and can't be.

WebSocket-per-request is still slower than using Hyperdrive's connection pooling (by a fair bit, as the blog lays out)
The Neon serverless driver uses WebSockets β€” because most serverless providers don't have something like Hyperdrive β€” so they aren't compatible and can't be.

WebSocket-per-request is still slower than using Hyperdrive's connection pooling (by a fair bit, as the blog lays out)
https://discord.com/channels/595317990191398933/1150557986239021106/1278756317279948830 πŸ™‚
harsh-harlequin
harsh-harlequinβ€’15mo ago
Hmm, but for the HTTP version it should be fine πŸ€” I'll need to investigate at some point but if postgres.js works, then that's cool
quickest-silver
quickest-silverOPβ€’15mo ago
yes pleaseπŸ™ If you could collaborate with them closely (which I am sure you do), a combination of neondb + hyperdrive at its peak would be a solution sent from heaven:D Thanks once again!
genetic-orange
genetic-orangeβ€’15mo ago
DEV Community
Faster Postgres Queries with Cloudflare Hyperdrive and Neon
If you have a database that gets queried from multiple regions (e.g. via edge functions) and you want...
quickest-silver
quickest-silverOPβ€’15mo ago
@Rishi Raj Jain I've seen this one, still not using the serverless neondb driver^^
genetic-orange
genetic-orangeβ€’15mo ago
Ah you'd like Serverless + Hyperdriver + Drizzle?
quickest-silver
quickest-silverOPβ€’15mo ago
Yup @Rishi Raj Jain . Seems serverless driver is not possible with hyperdrive and the cloudflare team recommends postgres.js for neon db instead. So somehow, the neon team has to actively have a look to allow the postgres.js to work with their service, otherwise hyperdrive won't work with neondb.
genetic-orange
genetic-orangeβ€’15mo ago
Umm okay, let me read through the same and get back to you.
quickest-silver
quickest-silverOPβ€’15mo ago
@Rishi Raj Jain any update maybe? πŸ™‚ Will neon at least continue to support postgres.js if its own serverless driver isn't working properly with hyperdrive?
harsh-harlequin
harsh-harlequinβ€’14mo ago
Postgres.JS should always work
quickest-silver
quickest-silverOPβ€’14mo ago
Thanks @Mahmoud πŸ™ I'm surprised I get over 1 second response from cloudflare workers end to end latency, I'll do some testing these days and let you know accorsingly if delay comes from neondb (free plan).
harsh-harlequin
harsh-harlequinβ€’14mo ago
Is this for all requests?
quickest-silver
quickest-silverOPβ€’14mo ago
Pretty much yes, cloudflare workers + honojs shouldn't pose much latency themselves but I'll measure latency and let you know with details Update @Mahmoud, the issue was from internet latency insteadπŸ€¦β€β™‚οΈ
harsh-harlequin
harsh-harlequinβ€’14mo ago
That makes sense!
quickest-silver
quickest-silverOPβ€’14mo ago
@Mahmoud check PM pls πŸ™‚

Did you find this page helpful?