SQL in Go With SQLBoiler

TheDeveloperCafe
TheDeveloperCafe
Published in
2 min readAug 7, 2022

πŸ‘‰ Read the full article on TheDeveloperCafe blog πŸ‘ˆ

Getting Started

SQLBoiler is a package that introspects your database schema and generates a static ORM client. In this article I am going to give you a taste of SQLBoiler, this is not an in-depth article on SQLBoiler (that will come later).

Below is what the workflow looks like with SQLBoiler.

I recommend you follow the article and replicate everything on your local machine. The code for this article is available in this GitHub repository, please go ahead and clone it.

This is a fairly information packaged article πŸ“¦ so give some good amount of time to read it.

Installing SQLBoiler CLI tool

To generate the static ORM client you need the SQLBoiler cli, install it using go install.

go install github.com/volatiletech/sqlboiler/v4@latest

SQLBoiler requires you to install a driver based on the database you are using (list of supported database by SQLBoiler).

We will be using PostgreSQL for this article, install the postgres driver.

go install github.com/volatiletech/sqlboiler/v4/drivers/sqlboiler-psql@latest

CLI part is all done βœ… let’s move on to the project.

Project Setup

SQLBoiler reads the schema directly from your database. We need a database with a schema. I am not going to spend time on schema creation.

I have already provided a sample schema in the GitHub repository of this article, all you need is Docker installed on your machine. Make sure you have cloned this repository.

Setting up database

Once the project is cloned, run docker compose up it in the project folder and you will have the database ready to go.

Here are the create table queries that run behind the scenes.

CREATE TABLE author(
id serial primary key,
email varchar not null,
name varchar not null
);
CREATE TABLE article(
id serial primary key,
title varchar not null,
body text,
created_at timestamp default now(),
author_id int not null,
constraint fk_author_id foreign key(author_id) references author(id)
);

You have got two tables, author and article, the relationship between them is pretty self-explanatory.

πŸ‘‰ Read the full article on TheDeveloperCafe blog πŸ‘ˆ

--

--