Hacker News new | past | comments | ask | show | jobs | submit login
Slonik: A PostgreSQL client with strict types, detailed logging, and assertions (github.com/gajus)
60 points by sarahdellysse on March 10, 2021 | hide | past | favorite | 18 comments



I like the idea and project goals.

There are however gems like this in the source:

https://github.com/gajus/slonik/blob/master/src/routines/exe...

You can spend an hour trying to reason about the control flow of that triple try/catch alone. Bonus points if that function can recursively call itself (not sure what retryTransaction can get up to).

There may be an explanation for this horrifying code, but it should probably be right above in the form of:

     /* 
      * The following code is not for the weak minded.
      *
      * Great care needs to be taken with it, for even the smallest mistake
      * risks summoning the gods of old to end all life on earth
      * in the most terrible way imaginable.
      * 
      * There is however no other way because a, b, and c.
      */
To untangle that mess you'd probably have to rewrite it once just to understand it, rewrite it again to get it right, then spend a month getting the edge-cases right.

But at least afterwards a newcomer can fix a bug without endangering the multiverse.


Its really not that complicated.. each except either handles an error or rethrows it for the outer except to handle it.

Maybe a one liner comment would help, but exaggerating as if the code is some horrifying monstrosity is not helpful IMO. It just smells of elitism and makes the code seem more unapproachable than it really is.


It's not really that complicated it's just error bubbling with a lack of comments. Definitely worth a refactor into an exception map or something instead of this nested mess though. I've seen worse pass lazy code review honestly (working with scientists you really see some crazy code).


Great to see this package here. We have been using slonik for over a year now with few regrets. I am not a fan of query builders. Query builders work for simple inserts or updates. For really complex queries, nothing beats writing SQL inside of the sql template tag.

Another nice thing about slonik is that it is built on top of https://github.com/brianc/node-postgres. One gets all the benefit of node-pg with the nicer dev experience of slonik.


Any pitfalls that you encountered?

I'm trying to decide between this, or writing my own helpers around https://github.com/felixfbecker/node-sql-template-strings.

Also, have you found anything the solves typechecking of embedded expressions inside the SQL template?

For example, Webstorm will help you autocomplete your SQL, but you can still type:

   sql`SELECT * FROM foo WHERE barInt = ${bazStr}`

Would be nice to catch that with TypeScript magic rather than at runtime.


slonik has no knowledge of the pg schema, so it cannot help with this.

You may want to look at something like https://github.com/adelsz/pgtyped. The downside is that dynamic queries are more challenging.

We have been looking at moving over to pgtyped, but it will be a hard transition because we do a fair amount of building up queries at runtime.


Anyone know how this compares to porsager's "postgres" lib?

https://github.com/porsager/postgres


Have to say I'm pretty happy with TypeORM[0], this is going to have to be really good to pry that from my hands.

Slonik does have a wonderfully smaller API surface and I sure do like writing SQL manually and getting to take advantage of various lower level benefits (ex. postgres JSON operations) without too much fuss/work arounds, but TypeORM also lets you drop to raw (but parametrized) SQL fairly quickly, and it's query builder is actually pretty good...

[0]: https://typeorm.io/


Last few years I've been using a similar workflow with something like https://www.npmjs.com/package/pg-template-tag in combination with `pg`. I'm never going back to ORMs and query builders.


Any reason you chose that over node-sql-template-strings [1], which looks very similar?

[1] https://github.com/felixfbecker/node-sql-template-strings


The template tag solution is cute but dangerous if you can accidentally leave off the `SQL` tag and the query still works.

Ideally the system would require you to use the tag and prohibit a plain ol string.


TypeScript compiler will error on this.


It does require you to use the tag.


Great library, gets out of the way while preventing common mistakes. makes my work easier. Thank you gajus!!!


As a memory aid, Slonik means "little elephant" in Russian, and probably other Slavic languages.


Reminds me of Rybka.

http://rybkachess.com/


I wish it had a standard OSS license


Looks like 3-clause BSD to me.




Consider applying for YC's W25 batch! Applications are open till Nov 12.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: