News for hackers. Stuff that matters.
Re:Show HN: Postgres.js – Fastest Full-Featured PostgreSQL C... by porsager on Thursday March 24, 2022 @03:30PM (#30794341)
Hi everyone. A bit more than two years ago I released the first version of Postgres.js. A fully featured PostgreSQL driver for Node.js written as a learning experience out of curiosity and annoyance with the current options. It greatly outperformed the alternatives[1] using pipelining and prepared statements, while providing a much better development experience safe from SQL injections. Since then I've been busy building things using it, now running in production, and although quite delayed I'm so happy to release a new major today with some really exciting new features:

1. Realtime subscribe to changes through Logical Replication [2]

It's now possible to use logical replication to subscribe in realtime to any changes in your database with a simple api like `sql.subscribe('insert:events', row => ...)`. Inspired from Supabase Realtime you can now have it yourself in Node.

2. A Safe Dynamic Query Builder

Nesting the sql`` tagged template literal function allows building highly dynamic queries while staying safe and using parameterized queries at the same time.

3. Multi-host connection URLs for High Availability support

It's really nice to be able to quickly spin up a High Availability Postgres setup using pg_auto_failover[3] and connect using Postgres.js with automatic failover and almost 0 downtime.

4. Deno support

It also works with Deno now, completing all tests except a few SSL specific ones which requires fixes in Deno.

5. And much more

Large object support, efficient connection handling for large scale use, cancellation of requests, Typescript suport, async cursors.

[1] https://github.com/porsager/postgres-benchmarks#results

[2] https://github.com/porsager/postgres#realtime-subscribe

[3] https://github.com/citusdata/pg_auto_failover

Re:Show HN: Postgres.js – Fastest Full-Featured PostgreSQL C... by kiwicopple on Thursday March 24, 2022 @04:21PM (#30794882)
Congrats on the release - as I mentioned on twitter, your templating design is the best DX i've seen for any PG client.

> Inspired from Supabase Realtime you can now have it yourself in Node.

Very cool! We have some Node servers internally, I'll see if the team want to switch.

Re:Show HN: Postgres.js – Fastest Full-Featured PostgreSQL C... by porsager on Thursday March 24, 2022 @04:31PM (#30795000)
Thanks - that's really nice to hear!

That would be really interesting. Are you using `pg` currently, and are you using WebSocket connections in Node to Elixir for realtime, or not using realtime in Node?

Re:Show HN: Postgres.js – Fastest Full-Featured PostgreSQL C... by kiwicopple on Thursday March 24, 2022 @05:49PM (#30795741)
yes, we use `pg` in all our Node servers (and no realtime)
Re:Show HN: Postgres.js – Fastest Full-Featured PostgreSQL C... by porsager on Friday March 25, 2022 @04:59AM (#30799926)
Alright. I've got a pg wrapper lying around that can help with migration I can send you ;) I've DM'ed you on twitter, so let me know if that could be useful.
Re:Show HN: Postgres.js – Fastest Full-Featured PostgreSQL C... by jadbox on Thursday March 24, 2022 @07:52PM (#30797051)
I hit a small type issue migrating from v2 to v3 that's not listed in the migration: https://github.com/porsager/postgres/issues/283
Re:Show HN: Postgres.js – Fastest Full-Featured PostgreSQL C... by aPoCoMiLogin on Friday March 25, 2022 @05:21AM (#30800049)
One question - is there a way to explicitly start the connection, instead of auto connection at the initialization?
Re:Show HN: Postgres.js – Fastest Full-Featured PostgreSQL C... by porsager on Friday March 25, 2022 @05:25AM (#30800066)
Yeah, just make a simple query :)

  await sql`select 1`
Re:Show HN: Postgres.js – Fastest Full-Featured PostgreSQL C... by aPoCoMiLogin on Friday March 25, 2022 @05:30AM (#30800096)
well.. my bad, somehow I didn't think about that..

anyway this lib looks really cool! definitely will use that instead of hand crafted tagged template functions passed to `pg`.

Re:Show HN: Postgres.js – Fastest Full-Featured PostgreSQL C... by twomoonsbysurf on Thursday March 24, 2022 @09:03PM (#30797600)
Super cool, looks great. Interesting to see you built it with Typescript.

I was wondering-- and I know this is a long story, but I am curious:

How does one go about building a PostgreSQL driver for NodeJS in Typescript?

How do you design and begin that sort of thing? What sort of knowledge resources did you rely on (i.e. documentation, other references)?

I googled 'database driver' to get a definition: "A database driver is a computer program that implements a protocol (ODBC or JDBC) for a database connection. The driver works like an adaptor which connects a generic interface to a specific database vendor implementation."

Re:Show HN: Postgres.js – Fastest Full-Featured PostgreSQL C... by porsager on Friday March 25, 2022 @04:03AM (#30799657)
Thanks a lot.

I did not built it with Typescript, just plain js - I don't use Typescript at all(not my cup of tee), but a kind soul (thanks @minigugus) contributed the typings ;)

I actually began it out of curiosity and because I was missing features in the current options. I had been using a wrapper around pg-promise to give me the API i wanted for quite a while, and one day when going through the PostgreSQL documentation I landed on the protocol (https://www.postgresql.org/docs/current/protocol.html) pages. Now the PostgreSQL documentation might seem daunting at first, but once you figure out the structure and semantics it's such an insanely good resource. I had the first POC version working over a weekend, and from there it was about 80% of the way to support all the things I needed. Now the Postgres protocol is actually fairly simple, and I suppose that's why other databases have chosen to use it as well (eg. Postgres.js also works with cockroachdb). I would love to write a longer post about the progress at some point, but I haven't found the time for it yet.

Re:Show HN: Postgres.js – Fastest Full-Featured PostgreSQL C... by domlebo70 on Friday March 25, 2022 @04:39AM (#30799806)
Hey.

Love this lib. I am curious why you decided against using TS here? You mentioned it's not your cup of tea.

Re:Show HN: Postgres.js – Fastest Full-Featured PostgreSQL C... by lukaszkups on Friday March 25, 2022 @07:39AM (#30800878)
I can only assume that I've finally found a soulmate that just don't like writing TS code.

Like, in my full-time job I'm using 100% TypeScript, but I don't really ENJOY writing in it - that's why in my all side-projects I use good old JavaScript.

I think it's just a personal syntax preference.

But I don't want to answer this for the OP, just added my 2cents, maybe he has similar feeling about it :)

Re:Show HN: Postgres.js – Fastest Full-Featured PostgreSQL C... by porsager on Friday March 25, 2022 @09:12AM (#30801712)
Haha.. There's plenty of us out there, it's probably just that we'd rather do actual stuff than talk about doing it. A bit like Typescript - it doesn't really do anything, it just talks about it.

Joking aside, I generally don't want to get into the debate, because I believe people work differently, and there should be room for doing both things. It's just a bit sad that Typescript is being pushed so hard as if it's the only right way.

Re:Show HN: Postgres.js – Fastest Full-Featured PostgreSQL C... by jamil7 on Friday March 25, 2022 @09:42AM (#30802054)
> It's just a bit sad that Typescript is being pushed so hard as if it's the only right way.

It's being evangelised as if it has zero downsides, it introduces overhead to what can already be a brittle dependency/build chain - providing questionable levels of actual type safety. This is coming from someone who generally prefers typed languages too, Typescript to me is the exception to that rule.

Re:Show HN: Postgres.js – Fastest Full-Featured PostgreSQL C... by keb_ on Friday March 25, 2022 @12:42PM (#30804328)
Yep! I'm the same way -- it comes from a love of simplicity and an affair I had with Lua, which I find marvelously simple and inspirational.
Re:Show HN: Postgres.js – Fastest Full-Featured PostgreSQL C... by Aeolun on Friday March 25, 2022 @07:44AM (#30800911)
Some people really love the simplicity that comes from zero compilation.

I like that too, so I always start with just JS, but then two files in realize I really want my typings.

Now I use esbuild instead of tsc, and I have the best of both worlds.

Re:Show HN: Postgres.js – Fastest Full-Featured PostgreSQL C... by keb_ on Friday March 25, 2022 @12:40PM (#30804300)
> Now I use esbuild instead of tsc, and I have the best of both worlds.

I'm interested in this. I know esbuild can compile TypeScript to JS, but that it doesn't serve as an actual typechecker. Without tsc as a dev dependency, do you just rely on your IDE's intellisense to tell you when there's a type error?

Re:Show HN: Postgres.js – Fastest Full-Featured PostgreSQL C... by Aeolun on Saturday March 26, 2022 @09:53PM (#30817052)
I still install tsc, but I don’t actually do the type checking except at release time (and whatever typechecking the IDE provides through the language server).