drift icon indicating copy to clipboard operation
drift copied to clipboard

Connection Pooling for PgDatabase

Open christian98b opened this issue 2 years ago • 1 comments

Hi there,

First of all, thank you for the amazing work on Drift - it works like a charm!

As I’m considering using Drift in a server environment alongside dart frog, I was wondering if it would be possible to add support for connection pooling to the PgDatabase class. This feature could be particularly useful in server-side applications where efficient management of database connections is crucial.

Thanks again for all your hard work!

christian98b avatar Sep 23 '24 16:09 christian98b

Thanks!

I was wondering if it would be possible to add support for connection pooling to the PgDatabase class

You can actually do this already with PgDatabase.opened, which takes an opened Session from package:postgres. As Pool implements Session, it fits into PgDatabase.opened and makes drift use a connection pool. The linked commit adds this to the documentation (I don't think we should add special methods for pooling in drift given that it can already be done, that would just duplicate work as we'd have to keep these in sync with the definitions in package:postgres). In the end, it's just a matter of

final pool = Pool.withEndpoints(yourListOfEndpoints);

final driftDatabase = MyDatabase(PgDatabase.opened(pool));
await driftDatabase.users.select().get(); // this uses a pool

simolus3 avatar Sep 23 '24 19:09 simolus3