Over these series of posts I have been exploring an approach that could be taken when working with SQL relationships in Go. The precursor to all of this was the initial ORMs and Query Building in Go. This explored one aspect of ORMs, the query building, but it didn't address how relationships could also be handled in an idiomatic way. So before I wrap up this series in this final post, let us address the code we have currently.
~ $ Programming
implemented some new interfaces, and utility functions to aid in the loading and
binding of the model relations. This was a much better improvement than what we
had before, however we still don't have a way of defining the relations
themselves in code. So far, we have heavily utilised callback functions to do
most of the lifting for us. For example, the
model.Bind function we
implemented returns a callback that will gracefully handle binding the models we
load. In this post we will look at how we can utilise callbacks to set the
relations between models via code.
Last time, we refactored our code, and implemented the loading of the relationships for the Post entity. The implementation of this however was rather messy, as we did the equivalent of sweeping everything under the rug. In this post however we will look at what we've done, and see how this could be improved upon.
In the previous post
we setup the entity models for the blogging application, and built some custom
query options to handle the data we would be receiving via an HTTP request. Here
we will go about implementing pagination for our models, so we can support the
page query parameter, look into how we can refactor what we have in our
HTTP handlers, and start looking into loading entity relationships for models.
In my last post I touched on the idea of using query builders for building up somewhat complex SQL queries in Go. Well, in this post I'd like to go further, and explore how this can be used to idiomatically build out a simple system for modelling relationships in Go. This post going forward will assume that you have read the aforementioned post.
Recently, I have been looking into various solutions for interacting with
databases with ease in Go. My go to library for database work in Go is
sqlx, this makes unmarshalling the data from
the database into structs a cinch. You write out your SQL query, tag your
structs using the
db tag, and let sqlx
handle the rest. However, the main problem I have encountered, was with
idiomatic query building. This led me to investigate this problem, and jot down
some of my thoughts in this post.
I am an impatient man (if my sporadic typos, and last minute edits late at night on my blog didn't make it obvious already). Recently this has started to stick out to me much more, especially in regards to programming. More often than not I would find myself just writing code haphazardly, then let the compiler/interpreter point out the errors for me, some not so obvious, others more obvious than I would have liked. This is a rather reckless approach to take when it comes to development. I find that it prevents me from actually thinking about what I am doing, and results in me producing tools and code that are in unsatisfactory state.
jrnl is a simple static site generator written in Go. It takes posts written in Markdown, and converts them to HTML. The generated HTML files are then copied to a remote destination, this can either be a location on disk, or to a remote server. Unlike most other static site generators jrnl does not serve the content that is generated. This post shall serve as a brief introduction to jrnl, for more in depth usage refer to the readme on the project's GitHub.