257 points · by porsager · 4y ago · 83 comments
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
> 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.
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?
await sql`select 1`anyway this lib looks really cool! definitely will use that instead of hand crafted tagged template functions passed to `pg`.
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."
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.
Love this lib. I am curious why you decided against using TS here? You mentioned it's not your cup of tea.
Even transpilation from ES modules into commonjs is done by the author himself:
https://github.com/porsager/postgres/blob/master/transpile.c...
I’m not sure if I’d made the same choice, but it’s fun the see it can work out great :)
https://github.com/porsager/postgres/blob/master/transpile.c...
I'd also be very curious to hear how replacing pg goes :)
And also good job on Imba! I'm a really big fan of stripping syntax down to the bare essentials, and what you've done with Imba is really impressive!
I'll open an issue on GitHub tomorrow.
33.6 kB Minified
12.5 kB Minified + Gzipped---
EDIT: according to https://github.com/pastelsky/bundlephobia/discussions/546 they say it looks for `main` entry.
Do template literals like this in JS work differently than just straight up string interpolation? As a rails dev this set off alarm bells.
[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... [2] https://github.com/porsager/postgres#await-sql---result
this is great! Can't wait to give a try.
And I've spent quite some time optimizing the js code and parsing.
the c++ to javascript membrane in v8 is expensive to pass. there are many projects that are faster implementing protocols in javascript vs c/c++ because of this.
redis is another.
You can get a perf boost with WASM or NAPI, but only for workloads which are CPU bound and where you expect to do that work in compiled code.
As I noted in my other comment, in -any- of those implementations, you're still going to be required to traverse from libc somewhere to nodejs (even if it's just to read the network data out of the socket and send it to v8). The performance gains would have to either come from a faster (JIT'd) protocol parser, or from eliminating additional FFI calls (that it isn't obvious why they'd exist).
So, would you mind linking to one or some of those projects? I'd love to see what happened to the implementations.
As the author noted elsewhere in thread, the performance gains are mostly coming from prepared statements and pipelining which seems a bit of an apples to oranges benchmark as most of these native drivers will support similar features, just not enable them transparently like this library does.
Out of curiosity, do you have links to these other projects where they have similar benchmarking attempts/results?
yes, you are, but the differences are the object creation that occurs. a single buffer coming from c/c++ (a socket, let's say) can be parsed and turned into a large number of objects in javascript much more quickly. yes, you're passing through that barrier once, but creating all of those objects from c++ and passing through it 20-30 times is a lot more expensive.
> Out of curiosity, do you have links to these other projects where they have similar benchmarking attempts/results?
how about pg vs pg-native? https://github.com/porsager/postgres-benchmarks#results
and unfortunately, I cannot find the original discussions from when node-redis went from native to pure javascript, but it was about a 30-40% speed increase originally if memory serves (I was the one who did that original conversion after a lot of deep dives into v8 and performance crossing the barrier).
as an aside, I'm also the maintainer of plv8, and am happy to discuss the same types of performance issues of dealing with jsonb vs json (which in Postgres is text): creating objects vs a simple JSON.parse() in c++ is a significant difference.
Slonik is a wrapper around another node driver (pg), so it's performance is the same as that, where Postgres.js is significantly faster (2-5x)[1].
Postgres.js is also a zero dependency module, whereas Slonik has quite the dependency graph meaning - compare https://npmgraph.js.org/?q=slonik with https://npmgraph.js.org/?q=postgres. That makes it more difficult to audit the code and preventing chain attacks etc.
Slonik also doesn't have the same lean straight forward developer experience as Postgres.js - again I'm biased saying that ;)
Postgres.js also does things that will make your queries perform better out of the box by implicitly creating prepared statements and using pipelining.
I suppose you could build Slonik on top of Postgres.js instead of pg as well, but that would probably only make sense if migrating to Postgres.js.
This one just made my day. Thanks. I remember trying to build a tool at work with as little as possible dependencies (in python) and how satisfying it was to see quite a few dependencies just being wrappers replaces with 5 lines of my own code that i could easily audit and ensure no supply chain attack was possible for that functionality.
Also what do the error messages look like?
Thanks for your work @porsager!
The template strings and querying work somewhat similarly to my own library, Zapatos[1] (but that sits on top of the pg package and is TypeScript-specific, being more focused on having everything typed).
Would you consider this production ready ? I rarely if ever use relational databases for my personal projects, but this could be a great fit at work.
Also , anyway to add an orm layer. I use Firebase right now, so I'm use to that vs writing queries.
About the ORM layer I wouldn't know, since I much prefer to avoid that at any cost.
That question doesn't really make sense. I'm not familiar with this library, but the developer interface is very similar to slonik. The whole point of this is that SQL tagged template literals replace the need for a query builder like Knex.
Here is a good blog post from the author of slonik explaining: https://gajus.medium.com/stop-using-knex-js-and-earn-30-bf41...
I also wouldn't put it in the core library anyway, so it should be straight forward for someone else to make a knex-postgres package that does this?