GORM: An Object Relational Mapper for Go

This course provides an in-depth examination of GORM and how to use it to provide an interface between a Go application and a relational database.
Course info
Rating
(20)
Level
Intermediate
Updated
Feb 2, 2016
Duration
4h 5m
Table of contents
Course Overview
Introduction
Defining Schemas
Working with Relationships
Creating, Updating, and Deleting Records
Querying the Database
Modifying Schemas
Advanced Topics
Description
Course info
Rating
(20)
Level
Intermediate
Updated
Feb 2, 2016
Duration
4h 5m
Description

Go is a language based on simplicity and speed. However, the complexity of an application can rise substantially when it has to interact with a database. GORM is a library that provides object-relational mapping capabilities to eliminate much of this complexity. This course will introduce you to GORM and help you learn about how to integrate it into your next data-driven application.

About the author
About the author

Michael Van Sickle is an application architect in Akron, Ohio. He is a mechanical engineer by training and a software engineer by choice. He is passionate about learning new programming languages and user experience design.

More from the author
The Go CLI Playbook
Intermediate
2h 8m
Feb 28, 2018
Intern 4: Getting Started
Beginner
2h 41m
Jan 9, 2018
Object-oriented Programming with Go
Intermediate
1h 42m
Aug 11, 2017
More courses by Mike Van Sickle
Section Introduction Transcripts
Section Introduction Transcripts

Course Overview
Hi everyone, my name is Michael Van Sickle and welcome to my course GORM: An Object Relational Mapper for Go. I'm a software engineer at SitePen. Go is a language that focuses on doing things in the most obvious and straightforward way possible. For many applications, the most obvious way to interact with a relational database is by using straight SQL calls. However, there are quite a few others than become unwieldy due to the need to support a complicated data model. In this course we're going to learn about GORM, one of the most popular object relational mappers for Go. Some of the major topics that we'll cover include: how to manage the data in the database via CRUD operations, how to manage the database schema itself, how to work with transactions, indexes, and other support features. And finally, how to work with other features that GORM offers such as callbacks and logging. By the end of this course you'll know how to work with GORM and use it to simplify the interactions with the database in your next application. Before beginning this course, you should be familiar with the basics of the Go language and how to work with relational databases. I hope that you'll join me on this journey to learn GORM with the GORM and Object Relational Mapper for Go course at Pluralsight.

Defining Schemas
Hello and welcome back to this course on GORM, an object relational mapper for Go. In the last module we got everything started up and reviewed why an ORM would be useful. In this module, we're going to dive deep into how to create and customize the schema of the database based on the Go model. In the last module we showed if you don't specify anything, how is GORM going to create your database schema and in general, I hope you'll agree with me, that it follows some pretty good rules. But as you might hope, there are quite a few options available in GORM to customize how the mapping between the objects and your Go application and the schema and the database are associated. So let's see how we're going to go about that. We're going to start by learning how to create tables. Now of course, we've already created tables in the first module, but there's a couple of settings that are available to us and I want to show you how to work with those so that you can get exactly the table name and definitions that you're looking for. After we do that, we'll learn how to customize the models that are represented by those tables by doing things such as changing column names and changing data types and things like that. After that, we'll learn how to work with existing schemas, just because you're creating a new Go application does not mean that the database that you're representing is also new. I'm going to show you some techniques that you can use in order to inform Go of how the underlying database is modeled. Finally, we're going to talk about indexes and how you can add those from your Go application in order to improve the performance of database queries. Now let's get started by learning how we can create tables with GORM.

Working with Relationships
Hello, my name is Michael Van Sickle. Welcome back to this course where we're talking about GORM, an object relational mapper for Go. In the last module we talked about how to work with entities and the underlying database tables that stored those entity's data. In this module we're going to move on to how to link those entities together using relationships, otherwise known as associations in GORM. We're going to start out by talking about one-to-one relationships where one entity on one table is linked to one and only one entity on another. Along the way, we'll take a side trip into discussing foreign key constraints and how to ensure that when we do have a relationship between one table and another, how that relationship will always be enforced. We'll then move on to one-to-many relationships and then on to many-to-many relationships, and finally end up with the concept known as polymorphism that is, as far as I've seen, pretty unique to GORM. We'll talk about what polymorphism is and the problem that it's trying to solve when we get there. For now, let's start out by talking about how to create one-to-one relationships using GORM.

Querying the Database
Hello and welcome back to this discussion about GORM: An object relational mapper for Go. In the last module we learned a lot about how to manipulate the data in the database by working with how to create, update, and delete objects that are stored there. In this module we're going to move on to one of the primary uses of relational databases and learn how to get data back out of it and into your application. As you might imagine if you've dealt with relational databases before, there are quite a few different options that we have for how to retrieve data from the database. We're going to start by talking about basic queries. This is just going to be the very introductory stuff that we're going to get some data back out of our database, but we're not going to have a whole lot of control over what comes out. We'll then move on to working with Where clauses and learn how to shape those from our Go application so that we can constrain the data that's coming back from our queries more precisely. By default, GORM does not inflate the entire graph of objects that are related to a parent entity, so we're going to need to learn how to load data eagerly in those situations where we want those child objects to come with the parent. Then we'll work with selecting result subsets. This is often used in scenarios such as pagination, when you want to restrict the results to a certain number of objects or offset into the result set by a certain amount. We'll then move on to shaping results. In this section, we'll be talking about returning a subset of the data represented on one or more tables in a structure that doesn't necessarily map directly to the structures that we've defined in our Go application. And finally, we'll wrap up with the ultimate solution when all else fails, and that's the ability to pass raw SQL down to the database. Let's start by learning how to work with some basic queries.

Modifying Schemas
Hello, my name is Michael Van Sickle for Pluralsight. Welcome back to this course where we're talking about GORM, an object relational mapper for Go. Over the last for example modules we've talked about how to set up your database, add data to it, and get that data back out to support your Go application. Well, if everything goes according to plan, then your application's going to take off and your user base is going to continue to grow. That inevitably will lead to additional features being requested and often a new feature request requires that the database be changed in some way. In this module, we're going to talk about how we can modify an existing schema while preserving the data in the database. We're going to start by learning how to create new entities. Now this isn't so different than what we've looked at in the past, but there's a couple of unique aspects that you might want to keep in mind and we'll talk about htose here. After that we'll talk about how we can add fields and indexes to existing entities in a way that preserves the data that's already stored on those tables. We'll also talk about removing entities. Of course, as features are added, other features are removed as your knowledge of how the customers are going to use your product grows and so sometimes aspects of your database are no longer needed as features from your application become obsolete. Finally we'll learn about how to modify and drop fields. Now you might expect us to talk about that in the same section as adding fields and indexes, but as we'll see GORM considers those very, very different things because of the impact that these operations inevitably have on the underlying database. So let's start out by learning how to create new entities.

Advanced Topics
Hello, my name is Michael Van Sickle for Pluralsight. Welcome to this final module where we're talking about GORM, an object relational mapper for Go. Over the last several modules you've learned everything that you need for day to day operations that involve GORM as the interface between your Go application and the underlying database. In this module, we're going to cover some advanced topics that while you might not need to use them in every project or every day, will come in handy at certain times. We're going to start by learning about callbacks. We talked about callbacks a little bit a few modules back, but now we're going to give them the treatment that they deserve and talk about all of the callbacks that are available and how they can be used. We'll then move on to scopes. We haven't talked about this one except for in a couple of slides back in module 2, but now we're going to focus on what scopes are and how we can use them to provide reusable contexts for our queries. And finally, we'll talk about GORM's logger. We've spent a lot of time looking at debug output from the logger, but we haven't really talk about how to use it and how to configure it in your own application. We'll talk about that in the final part of this module. Okay, now let's go look at some callbacks.