Object/Relational mapping with GORM
Using a framework to make database access easier
Project setup
bash
mkdir 0014-orm-with-gorm
cd 0014-orm-with-gorm
go mod init 0014-orm-with-gorm
go get -u gorm.io/gorm
go get -u gorm.io/driver/sqlite
mkdir -p app/{config,model,service}
touch README.md main.go app/sample.go app/sample_test.go \
app/config/database.go app/model/contact.go app/model/address.go \
app/service/agenda-service.goHow to run
bash
go build
./0014-orm-with-gormHow to test
bash
go test -v -coverprofile=coverage.out ./appIf you want a better coverage report (line above just prints total coverage %) add the following command:
bash
go tool cover -html=coverage.out -o coverage.htmlNoteworthy
- gorm has a very opinionated way to deal with databases. tables must be pluralized, timestamps must exists and primary key is
id. - associations, when desired, must be explicitly preloaded on queries, but will be saved in a transparent way.
- golang has a rudimentary setup/teardown system for testcases. But it does the job and we can gracefully prepare and close the database.