Simple project in Go to play around with some CRUD operations using a PostgreSQL database and pgx

Record Store

November 2021

I started learning Go a few weeks ago and this is my first proper project using Go. I wanted to use it to get to grips with some basic CRUD operations using a PostgreSQL database and pgx (a Go toolkit for PostgreSQL). It runs in the terminal without any web interface or API's.

Main Learning Points:

  • how to structure a Go project
  • how pgx works / what it offers
  • tried out a validation library called ozzo
  • how to use os.Args to access raw command-line arguments
  • how to access env variables in the project
  • getting used to working with a strongly typed langauge
  • recapped on SQL (I didn't want to use an ORM)

I'm sure there are lots of improvements that could be made but it's a start!

Built with:

  • Go

Getting Started

  • Firstly you will need to create and connect to a PostgreSQL DB and add your env variables to the .env file

  • Make sure you are in the root of the project

  • Create the table - psql nameOfYourDb -f ./db/scripts/create_table.sql

  • Insert data to the table - psql nameOfYourDb -f ./db/scripts/insert_data.sql

  • Run the program - go run cmd/main.go (you should get a message saying Welcome to Katie's Record Store!)

  • Now you know everything is working you can run the program followed by the operation you want to perform and the input data (if neccessary)...

    • get all the albums - go run cmd/main.go getAlbums
    • get a specific album - go run cmd/main.go getAlbum 3 (gets the album with an id of 3)
    • add an album - go run cmd/main.go addAlbum 21 Adele 4 (creates an album with the title '21', artist 'Adele', rating '4')
    • update an album - go run cmd/main.go updateAlbum 11 21 Adele 5 (updates the album with an id of 11 to title '21', artist 'Adele', rating '5')
    • delete an album - go run cmd/main.go deleteAlbum 1 (deletes the album with the id of 1)
  • The validation I implemented means that:

    • the rating has to be between 1 and 5
    • the title and artist have to be at least 1 character
Owner
Katie Hawcutt
Junior Software Engineer at Wise
Katie Hawcutt
Similar Resources

CRUDist Model Driven Web Development. Automagically generate CRUD APIs from your model.

CRUDist - Model Driven API Development Automagicaly create CRUD APIs for your gorm models. Example Model definition type BaseModel struct { ID

Nov 15, 2021

Basic-crud-with-go - API Rest utilizando go

basic-crud-with-go API Rest utilizando go API Docs Postman file at docs/Books.po

Jan 16, 2022

100% type-safe ORM for Go (Golang) with code generation and MySQL, PostgreSQL, Sqlite3, SQL Server support. GORM under the hood.

go-queryset 100% type-safe ORM for Go (Golang) with code generation and MySQL, PostgreSQL, Sqlite3, SQL Server support. GORM under the hood. Contents

Dec 30, 2022

Golang ORM with focus on PostgreSQL features and performance

go-pg is in a maintenance mode and only critical issues are addressed. New development happens in Bun repo which offers similar functionality but works with PostgreSQL, MySQL, and SQLite.

Jan 8, 2023

Examples of using various popular database libraries and ORM in Go.

Introduction Examples of using various popular database libraries and ORM in Go. sqlx sqlc Gorm sqlboiler ent The aim is to demonstrate and compare us

Dec 12, 2021

Examples of using various popular database libraries and ORM in Go.

Introduction Examples of using various popular database libraries and ORM in Go. sqlx sqlc Gorm sqlboiler ent The aim is to demonstrate and compare us

Dec 28, 2022

Converts a database into gorm structs and RESTful api

gen The gen tool produces a CRUD (Create, read, update and delete) REST api project template from a given database. The gen tool will connect to the d

Dec 28, 2022

beedb is a go ORM,support database/sql interface,pq/mysql/sqlite

Beedb ❗ IMPORTANT: Beedb is being deprecated in favor of Beego.orm ❗ Beedb is an ORM for Go. It lets you map Go structs to tables in a database. It's

Nov 25, 2022

Database agnostic ORM for Go

If you are looking for something more lightweight and flexible, have a look at jet For questions, suggestions and general topics visit the group. Inde

Nov 28, 2022
Related tags
Crud - A mysql crud code generate tool from table DDL sql file

crud is a mysql crud code generate tool 中文文档 Getting Started Overview Crud is a

Oct 13, 2022
Api-project - Api project with Golang, Gorm, Gorilla-Mux, Postgresql

TECHNOLOGIES GOLANG 1.14 GORM GORILLA-MUX POSTGRESQL API's PATHS For Product Ser

Nov 23, 2022
Simple-crm-system - Simple CRM system CRUD backend using Go, Fiber, SQLite, Gorm

Simple CRM system CRUD backend using GO, Fiber, Gorm, SQLite Developent go mod t

Nov 13, 2022
Golang Event Scheduling Sample Using Postgresql Database as persisting layer.

Database Based Event Scheduling Example that demonstrates super basic database based event scheduling. To run this example; Copy .env.example to .env

Nov 28, 2022
A simple CRUD API made with Go, Postgres, FIber, Gorm and Docker.

golang-test-api A simple CRUD API made with Go, Postgres, FIber, Gorm and Docker. Cloning the repository To clone the repository run the following com

Dec 14, 2022
A simple wrapper around sql.DB to help with structs. Not quite an ORM.

go-modeldb A simple wrapper around sql.DB to help with structs. Not quite an ORM. Philosophy: Don't make an ORM Example: // Setup require "modeldb" db

Nov 16, 2019
Very simple example of golang buffalo CRUD

Buffalo CRUD exemple Introduction Site du projet : https://gobuffalo.io/fr Documentation générale : https://gobuffalo.io/fr/docs/overview/ Documentati

Nov 7, 2021
A simple CRUD app built with Go for Shopify Backend Developer Intern Challenge - Summer 2022

Shopify Backend Developer Intern Challenge - Summer 2022 A simple CRUD app built with Go and Gin. This let you update track your inventory, add new it

Jan 18, 2022
Using-orm-with-db - Trying to use ORM to work with PostgreSQL
Using-orm-with-db - Trying to use ORM to work with PostgreSQL

Using ORM with db This repo contains the training (rough) code, and possibly not

Jul 31, 2022
A pure golang SQL database for learning database theory

Go SQL DB 中文 "Go SQL DB" is a relational database that supports SQL queries for research purposes. The main goal is to show the basic principles and k

Dec 29, 2022