gajus / slonik

A Node.js PostgreSQL client with runtime and build time type safety, and composable SQL.
Other
4.6k stars 140 forks source link

slonik support @neondatabase/serverless (postgres over HTTP / websockets) - pg compatible #642

Open firxworx opened 2 months ago

firxworx commented 2 months ago

Desired Behavior

Slonik project offers official guidance in docs and/or a bridge/adapter to @neondatabase/serverless which, like slonik, is based on pg and intended to serve as a drop-in replacement.

This would enable use of slonik in serverless/edge environments to connect to postgres over HTTP or websockets and take advantage over various performance and scalability benefits in this deployment context.

Motivation

Honestly I think it seems like a good fit and future forward step for Slonik -- it opens the door to more applications, larger community, broader and more varied ecosystem with wider range of expertise.

I wonder if you reach out to the right people at Neon they may even sponsor this :)

Serverless/edge type environments aren't going anywhere. New breed devs are brought up on CloudFlare, Vercel, Netlify and others, plus AWS is going to be a rock for the foreseeable future not to mention Azure + Google. Neon is frequently suggested as the way to go for serverless/edge + postgres.

Neon is an innovator (e.g. branching) and a lot of their developments are open source so they will only influence more in the foreseeable future.

Other popular database libraries for Node / TypeScript already have official adapters for Neon serverless including Drizzle.

If you haven't tried Neon I'd suggest trying their generous free tier.

Implementation

Slonik already has a bridge/adapter for postgres.js which has way more differences and nuances than something based on pg and intended to be drop-in.

gajus commented 1 month ago

This would belong in community packages.

I will leave this issue open in case anyone wants to contribute a bridge. I will add it to Slonik's README.

capaj commented 1 month ago

@gajus what about support for https://github.com/electric-sql/pglite ? Does that also belong to community packages? If so, can you give any pointers where to start when developing a "bridge"?

firxworx commented 1 month ago

@gajus is there a repo for community packages or a contribution doc / process? Like if I or another dev were to implement something where would we PR / where would you add it (e.g. like a ./packages/community in this repo or another repo)?

Or did you mean that if I build something to publish it independently vs. contribute to something under the slonik banner?

I searched down the README for "community" and "contribut..." and didn't find anything. A CONTRIBUTING.md might be helpful.

I looked at the code for https://github.com/gajus/slonik/blob/main/packages/pg-driver/src/factories/createPgDriverFactory.ts and at least on the surface it looks do-able. To use the custom driver I assume I would use the driverFactory client configuration option to specify it.

gajus commented 1 week ago

Or did you mean that if I build something to publish it independently vs. contribute to something under the slonik banner?

Just that.

firxworx commented 2 days ago

OK I wrote a package to support @neondatabase/serverless based on the @slonik/pg-driver package as a reference. It is working without issue in a demo CRUD API.

When I find some more time I will review it, make it public, and publish it to npm. As far as I can tell from searching GitHub it might be the first public independent driver factory for slonik created using @slonik/driver.

I'm not sure the ideal way to test it. Both @slonik/driver and @slonik/pg-driver only have stub tests however I know slonik itself has thorough integration tests that cover the bases. I suppose I can always use my Neon account with the demo API to enable a scripted integration/e2e-like test. Input/ideas welcome!