Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Full-Stack Web Development with Go

You're reading from   Full-Stack Web Development with Go Build your web applications quickly using the Go programming language and Vue.js

Arrow left icon
Product type Paperback
Published in Feb 2023
Publisher Packt
ISBN-13 9781803234199
Length 302 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Nick Glynn Nick Glynn
Author Profile Icon Nick Glynn
Nick Glynn
Nanik Tolaram Nanik Tolaram
Author Profile Icon Nanik Tolaram
Nanik Tolaram
Arrow right icon
View More author details
Toc

Table of Contents (21) Chapters Close

Preface 1. Part 1: Building a Golang Backend
2. Chapter 1: Building the Database and Model FREE CHAPTER 3. Chapter 2: Application Logging 4. Chapter 3: Application Metrics and Tracing 5. Part 2:Serving Web Content
6. Chapter 4: Serving and Embedding HTML Content 7. Chapter 5: Securing the Backend and Middleware 8. Chapter 6: Moving to API-First 9. Part 3:Single-Page Apps with Vue and Go
10. Chapter 7: Frontend Frameworks 11. Chapter 8: Frontend Libraries 12. Chapter 9: Tailwind, Middleware, and CORS 13. Chapter 10: Session Management 14. Part 4:Release and Deployment
15. Chapter 11: Feature Flags 16. Chapter 12: Building Continuous Integration 17. Chapter 13: Dockerizing an Application 18. Chapter 14: Cloud Deployment 19. Index 20. Other Books You May Enjoy

Using sqlc

First, let’s take a look at the different commands provided by sqlc and how they work.

Commands

Explanation

compile

This command helps check SQL syntax and reports any typing errors.

completion

This command is to generate an auto-completion script for your environment. The following are the supported environments: Bash, Fish, PowerShell, and zsh.

generate

A command to generate the .go files based on the provided SQL statements. This will be the command that we will be using a lot for the application.

init

This command is the first command that is used to initialize your application to start using this tool.

The following will show how to get started with using sqlc to set up a project. Create a directory inside chapter1 – for example, dbtest – and change the directory to the new directory (dbtest). Next, we will run sqlc with the init command:

sqlc init

This will automatically generate a file called sqlc.yaml, which contains a blank configuration as shown here:

version: "1"
project:
    id: ""
packages: []

The sqlc.yaml contains configuration information that sqlc will use to generate all the relevant .go code for our SQL statements.

Let’s take a look at the structure of the .yaml file to understand the different properties. The following shows an example of a completed structure:

version: "1"
packages:
 - name: "db"
   path: "db"
   queries: "./sqlquery"
   schema: "./sqlquery/schema/"
   engine: "postgresql"
   sql_engine: "database/sql"
   emit_db_tags: "true"
   emit_prepared_queries: true
   emit_interface: false
   emit_exact_table_names: false
   emit_empty_slices: false
   emit_exported_queries: false
   emit_json_tags: true
   json_tags_case_style: "snake"
   output_db_file_name: "db.go"
   output_models_file_name: "dbmodels.go"
   output_querier_file_name: "dbquerier.go"
   output_files_suffix: "_gen"

The following table explains the different fields:

Tag Name

Description

Name

Any string to be used as the package name.

Path

Specifies the name of the directory that will host the generated .go code.

Queries

Specifies the directory name containing the SQL queries that sqlc will use to generate the .go code.

Schema

A directory containing SQL files that will be used to generate all the relevant .go files.

Engine

Specifies the database engine that will be used: sqlc supports either MySQL or Postgres.

emit_db_tags

Setting this to true will generate the struct with db tags – for example:

type ExerciseTable struct {

ExerciseID int64 `db:"exercise_id"

ExerciseName string `db:"exercise_name"

}

emit_prepared_queries

Setting this to true instructs sqlc to support prepared queries in the generated code.

emit_interface

Setting this to true will instruct sqlc to generate the querier interface.

emit_exact_table_names

Setting this to true will instruct sqlc to mirror the struct name to the table name.

emit_empty_slices

Setting this to true will instruct sqlc to return an empty slice for returning data on many sides of the table.

emit_exported_queries

Setting this to true will instruct sqlc to allow the SQL statement used in the auto-generated code to be accessed by an outside package.

emit_json_tags

Setting this to true will generate the struct with JSON tags.

json_tags_case_style

This setting can accept the following – camel, pascal, snake, and none. The case style is used for the JSON tags used in the struct. Normally, this is used with emit_json_tags.

output_db_file_name

Name used as the filename for the auto-generated database file.

output_models_file_name

Name used as the filename for the auto-generated model file.

output_querier_file_name

Name used as the filename for the auto-generated querier file.

output_files_suffix

Suffix to be used as part of the auto-generated query file.

We have looked at the different parameters available in the tool, along with how to use the .yaml file to specify the different properties used to generate the relevant Go files. In the next section, we will set up our sample app database.

You have been reading a chapter from
Full-Stack Web Development with Go
Published in: Feb 2023
Publisher: Packt
ISBN-13: 9781803234199
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime