Drizzle connections

Hi, I'm using Hyperdrive + Neon + Drizzle, and I've an issue: I've to connect a new connection (const db = drizzle()...) for each query, in the same "request/worker" execution. When I not use Hyperdrive, I can have a singleton of drizzle and do multiples queries. I'm use thats imports: import { drizzle, NodePgDatabase } from "drizzle-orm/node-postgres"; Any ideas?
23 Replies
AJR
AJR•4d ago
I don't think you should have to do that. What's happening that suggests to you that it's necessary?
letincho5
letincho5OP•4d ago
Without hyperdrive, I've singleton variable db = drizzle(...) across all my repositories clases: I've good performance but no control over max connections. (and reach too many connections errors on my stress test). With Hyperdrive, the too many connections disappear, but I've bad performance, because I've to do a .connect each time (I've ten queries per request)
letincho5
letincho5OP•4d ago
Stack Overflow
Drizzle + Neon Postgres + Hyperdrive: The script will never generat...
I'm using Hyperdrive + Neon + Drizzle, and I've an issue: I've to connect a new connection (const db = drizzle()...) for each query, in the same "request/worker" execution. When I not use
letincho5
letincho5OP•4d ago
Here I try to explain my issue I think that this._client.connect() is too expensive?
AJR
AJR•4d ago
You do not have to make a new connection each time you send a query. I'm not understanding why you think you do.
letincho5
letincho5OP•4d ago
Because, in the second query, I've receive this error: the-script-will-never-generate-a-response The underling connection is closed Without Hyperdrive, it was no necesary
AJR
AJR•4d ago
Interesting. You definitely don't need a fresh connection each time. That, at least, I can say for certain Not sure what that error's about, though. I don't use Drizzle so I'm afraid I'm not sure what the issue is with the code structure, though.
letincho5
letincho5OP•4d ago
Which driver may I use? ahh... I'm using Drizzle
AJR
AJR•4d ago
We recommend postgres.js as a good default, but I know others here have mentioned using Drizzle Looks like you're using node-postgres as the driver, which we also support This call should be incredibly cheap, too. Not sure why it's giving you perf problems. Where's your origin DB?
letincho5
letincho5OP•4d ago
Neon with business plan, 16CPU
AJR
AJR•4d ago
I meant which region?
letincho5
letincho5OP•4d ago
Virginia AWS
AJR
AJR•4d ago
Hmmmm. No networking issues there, it's always pretty reliable
letincho5
letincho5OP•4d ago
I'm going to try drizzle wih postgres js
AJR
AJR•4d ago
Looks like the Nile folks put together a good example with Drizzle. Might be helpful for you? https://www.thenile.dev/blog/nile-cloudflare
Bringing Nile and Cloudflare together with Hyperdrive
Use Nile databases with Cloudflare Workers and Hyperdrive for hyper-fast experience - globally.
letincho5
letincho5OP•4d ago
Thanks, I'll read it import { drizzle } from "drizzle-orm/postgres-js"; I'm going to try Same result 😦 : (log) ######## usando db existente X [ERROR] Error: The script will never generate a response.
AJR
AJR•4d ago
This seems like a code structure issue. I'd recommend taking a look at that example, see if that helps you.
letincho5
letincho5OP•4d ago
What about Hono... I'm using Hono for routing Do you think that Hono may be a problem?
AJR
AJR•4d ago
Hono is pretty popular. I'd be surprised if nobody's using it with Drizzle + Hyperdrive. We use it ourselves for some internal tooling, though without Drizzle. You might have better luck with general javascript help in the more general Workers channels, I think. Sorry man, not really my wheelhouse here.
letincho5
letincho5OP•3d ago
@AJR hi, reading documentation, I've realice that I'm using a TCP connection (with .connect() sentence ). That's may be the problem I guess
AJR
AJR•3d ago
Hyperdrive intercepts those TCP connects iif you're using a Hyperdrive binding for the connection string. If you're using something else it will behave like a normal TCP connect statement
letincho5
letincho5OP•3d ago
Just initialize drizzle in this way: return drizzle(this.bindings.HYPERDRIVE.connectionString); generate a HTTP connection and can be reuse without problems
AJR
AJR•3d ago
Good to know!!

Did you find this page helpful?