
Show HN: Postgres.js – Fast PostgreSQL Client for Node.js - porsager
https://github.com/porsager/postgres
======
porsager
I bit more than a month ago my curiosity got the better of me, and I took a
deep dive in the Postgres protocol docs[1]. I was fascinated by it's
simplicity, and since I've been wrapping current node modules pg or pg-promise
in a tagged template function for easier use, for a long time, I thought it
would be interesting to see how it would be to build a client from scratch.
The result ended up being a 0 dependency full featured client in 1250 loc,
that's somewhere between 2-10x faster than pg (still cleaning up benchmarks to
publish).

I also think I've managed to make a welcoming, simple and safe surface API at
the same time, so the postgres experience in Node becomes even nicer. Feedback
is very much welcomed.

I found a lot of inspiration for tricks to make it faster in other libraries,
but the biggest boost came from following the Postgres protocol docs
recommendations.

Another thing I've found since becoming a Postgres user is how amazing their
documentation is once you wrap your head around it. It took me a while,
probably because I've always been used to quickly scanning documentation for
the thing I need, instead of actually reading it. That's a foolish thing to do
with the Postgres docs. There is so much useful information in there, so now I
just read from top to bottom every time I look at a topic.

[1]
[https://www.postgresql.org/docs/current/protocol.html](https://www.postgresql.org/docs/current/protocol.html)

~~~
rgharris
I really like the _sql()_ helpers and the naming of the _unsafe()_ method. The
file query helper is great - I've moved to using external query files for
everything in a current project.

I've used _pg-promise_ a lot and this seems to cover a good bit of the core
functionality and could be extended pretty well in a project. Do you plan to
continue building out features or are you looking to keep this as lean as
possible long term?

Also, does _sql.file()_ support named parameters or just indexed parameters
(like _$1_ )?

~~~
porsager
Thanks.

I'll definitely be open to features that improves daily usage, and features
that treats SQL as a first class citizen. It'll never turn into an ORM. So if
a feature has great value and it doesn't interfere with the core I'm all ears
:) Your example with named parameters in sql files is definitely something I'd
like to do, since that's not supported currently. You're very welcome to
create an issue for it so we can continue that idea there.

~~~
rgharris
Awesome, I think your plans on feature development make a lot of sense.
Hopefully I’ll have some time to try it out in a real project before long!

------
bgdam
Wow, I love the simplicity of this. I generally go with `pg` for my projects,
but I'm definitely going to give this a try.

As it so happens, I'll be building out a little idea over the holidays, so
I'll give more/better feedback after then.

Also, thanks for teaching me about tagged template literals. TIL.

~~~
porsager
Thanks a lot! I'll be looking forward to it ;)

And yeah, I discarded tagged template literals when I first saw them, but
they've certainly gained my love + trust since then. There's a really
interesting optimization option for them because the first argument (string
array) will always be the same instance for the same callsite so you can cache
and make things really performant using that.

------
a_band
Nice! The documentation looks thorough and I appreciate how you've designed
the module. Great example of what a good NPM module should look like. :-)

~~~
porsager
Thanks! That means a lot.

------
miohtama
Does it support application level conflict resolution and transactions
retries? Or does this even need a special support on the application level?

~~~
porsager
Yes you should be able to model that using `.begin()` and `.savepoint()`
within. [https://github.com/porsager/postgres#savepoint-
sqlsavepointn...](https://github.com/porsager/postgres#savepoint-
sqlsavepointname-fn---promise)

Is that what you're looking for?

------
topicseed
Great library – will use it for the next project!

