Show HN: PugSQL, a Python Port of HugSQL

by mcfunleyon 5/29/19, 3:09 PMwith 53 comments
by avolcanoon 5/29/19, 7:46 PM

I really like the idea of this! Potentially quite helpful for the GraphQL space, I think, since these query files could map 1:1 with resolvers (if I understand GraphQL correctly).

I've been working on a Node+TypeScript web app on and off for the last couple years, and one thing that's always bugged me with it is database access - it uses Knex.js as a query builder. Knex is a solid (if imperfect) DSL for interacting with SQL, but as time goes on and I get more comfortable with SQL, I've started wishing I could write raw SQL instead more easily. I think an architecture like PugSQL might help bridge the gap between "passing a bunch of SQL strings around" and a query builder.

Slightly off topic further thinking - one problem I've always had with Knex and TypeScript, though, is the lack of static typing - I've been writing runtime validations for each individual query result. This has been a bit annoying to maintain at scale since I don't have very good patterns for it. With a system like PugSQL, though, I could imagine just having input and output validators for each parameterized query.

Of course, the long term dream would be to generate type definitions from the SQL files, but I assume that would require a heck of a lot of magic (e.g. "actually run a query, figure out what the schema of the result table is, and create a snapshot of that"). I haven't seen a lot of prior art in terms of "static typing of DB access without a big ol' ORM," but I'm hopeful there's some options.

by cjauvinon 5/29/19, 6:30 PM

The context seems relevant to plug my own take at this "problem" (aka. finding an alternative to a full-blown Python ORM), which involves talking to Postgres via only builtin data structures:

https://github.com/cjauvin/little_pger

by benatkinon 5/29/19, 7:17 PM

Perhaps slightly unrelated: I'm considering moving to asyncpg and using quart, which is a port of flask to async python. What I wonder if it's time to start using async python, and if these libraries are mature enough. If so, I hope libraries like this and little_pger will switch to it or support it!

https://github.com/MagicStack/asyncpg https://gitlab.com/pgjones/quart

by holtalanmon 5/29/19, 8:22 PM

i really really want to work on porting HugSQL to Elixir. debating starting on that for my next side project.

i remember when learning clojure, HugSQL was one my of favorite things ever. it was just...clean, simple, and awesome.

by loop0on 5/29/19, 7:55 PM

I started the same project with the same name a few months ago, I’m happy to see someone went farther than I did. Congrats on this project, I’m testing later. HugSQL achieves the right balance between orm and boilerplate code.

by fulafelon 5/29/19, 5:35 PM

Nice to see Python ports of Clojure libraries. Are there others? Is there something close to spec or Plumatic Schema?

by born2discoveron 5/29/19, 5:33 PM

Interesting. But what a potential use case for this? I mean what makes it stand out when put side by side with SQLAlchemy? Does it do anything differently?

From what I've been able to gather from that website, PugSQL is a wrapper around SQLAlchemy. So my question, why do we need a wrapper around an already well established, popular, robust and very powerful library?

by burembaon 5/29/19, 4:09 PM

The Java/Kotlin alternative would be `the amazing library` JDBI: http://jdbi.org/

by gigatexalon 5/29/19, 4:37 PM

Very cool! Kudos to the dev(s)

by cwpon 5/29/19, 7:35 PM

Awesome. I do the same sort of thing in Javascript and it works great. Nice to see it available in Python.

by coleiferon 5/29/19, 5:12 PM

A simple Python interface ... built atop the most powerful and widely used Python ORM. This is the library equivalent of virtualization!

This is crazy, right? Let's take SQLAlchemy, all the experience and expertise that went into building it, throw that out the window and make the dumbest possible wrapper on top.

by robodaleon 5/29/19, 4:12 PM

Watch your naming - pug is also a template engine for npm.