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
I don't think you should have to do that. What's happening that suggests to you that it's necessary?
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)
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
Here I try to explain my issue
I think that this._client.connect() is too expensive?
You do not have to make a new connection each time you send a query.
I'm not understanding why you think you do.
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
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.
Which driver may I use?
ahh... I'm using Drizzle
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?
Neon
with business plan, 16CPU
I meant which region?
Virginia
AWS
Hmmmm. No networking issues there, it's always pretty reliable
I'm going to try drizzle wih postgres js
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.
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.
This seems like a code structure issue. I'd recommend taking a look at that example, see if that helps you.
What about Hono... I'm using Hono for routing
Do you think that Hono may be a problem?
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.
@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
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
Just initialize drizzle in this way: return drizzle(this.bindings.HYPERDRIVE.connectionString); generate a HTTP connection and can be reuse without problems
Good to know!!