DEV Community

Cover image for How to use SQLC with Golang
Timileyin D
Timileyin D

Posted on

How to use SQLC with Golang

What is SQLC

SQLC is a type-safe SQL code generator, it is used to generates fully type-safe idiomatic code from SQL and catch failure before they happen, it support different languages like Golang, Kotlin and Python. The main advantage of using SQLC is that it generate type-safe code from the SQL queries.

How SQLC work

  • You write SQL queries

  • You run sqlc to generate Go code that presents type-safe interfaces to those queries

  • You write application code that calls the methods sqlc generated

SQLC golang sample

To get started, we need to navigate to the desired directory and run the command below in our terminal:

mkdir sqlc_go && cd sqlc_go
Enter fullscreen mode Exit fullscreen mode

also create db/migration and db/queries folder

Implementing SQLC in golang involves installing *golang-migrate * package,this helps to fully utilize SQLC full potential.

We proceed to install the required golang-migrate dependencies with:

$ curl -L https://github.com/golang-migrate/migrate/releases/download/$version/migrate.$os-$arch.tar.gz | tar xvz
Enter fullscreen mode Exit fullscreen mode

The code above install golang-migrate. We can then proceed to creating the migration file for the user by running the below code.

migrate create -ext sql -dir db/migration -seq add_users
Enter fullscreen mode Exit fullscreen mode

This will generate two serialized file that looks like the image attached below.

Image description
where the .up.sql file is for the SQL query model definition while the .down.sql file is for changes reversal.

  • Insert the SQL query into the .up.sql file. see the code below

Image description
while the below code inside the .down.sql file

Image description
The code in the .down.sql file will be use to reverse the changes made to the database(DB)

The user.sql file in the db/queries folder will contain the code below which instruct SQLC on the method to be generated.
It will create CreateUser, GetUser among other method

Image description

  • Pull a Postgres image from the docker hub and create a database on the running Postgres instance.

  • Create sqlc.yaml by running

sqlc init
Enter fullscreen mode Exit fullscreen mode

(image below) to set the configuration for the SQLC

Image description
The code above instruct golang-migrate to look out for the db/migration folder when the migration code below is been executed

migrate -path db/migration -database "$(DB_URL)" -verbose up 
Enter fullscreen mode Exit fullscreen mode
sqlc generate
Enter fullscreen mode Exit fullscreen mode

the code above will create sqlc folder in the db folder also create a db.go file in the db/sqlc folder
which explicitly define the database architecture and database method that can be used in our controller, remember this method was defined in our db/queries folder. Image attached below

Image description

Conclusion

This article demonstrate how to use SQLC in a golang application, its role in building type-safe idiomatic code and how to get started by building a user management service with Golang and Postgres DB.

Top comments (0)