sqlc

I won’t even bother to recollect anything about 2020 on this last day of year. On to something more interesting: sqlc!

Usually my data persistence needs in Go end up being one of:

Of these, the I’m always most drawn to SQL. Once I get past the most basic prototype, I find I almost always want the power of SQL. The problem is that even with sqlx, there is a hell of a lot of boilerplate involved. I then become tired and reach for GORM for a while. I then become irritated at the amount of time I spend learning GORM-isms.

I’ve tried the likes of xo and sqlboiler but found their model of building a schema from the database to be both presumptious and limiting, especially when working between multiple database types.

Enter sqlc, an interesting tool that I recently found and have been using in earnest on a side project. The approach here suits my way of thinking very well. I can express my intentions in bona fide SQL–which is how I’m usually thinking about them anyway–and the tooling can generate a nice set of typed function to actually run the queries.

I often find myself developing with or using sqlite and then deploying with postgres. Given that sqlc doesn’t yet support sqlite, I figured this would be a problem. Not so. If I stay within the set of standard/common SQL features, the “postgres” generated code works just fine with sqlite. I may need to tweak the schemas a bit at the start to get things to line up, but that is a one-time step. Thereafter I can rapidly write my queries and generate the corresponding functions.

Historically I’ve not been satisfied with the database solutions in Go and end up floating between them. Time will tell if sqlc retains its allure.