What we are going to build
As part of our introduction to PostgreSQL in Go, we will create a mini ranking of websites that we will store in the database. So, Website
will be our domain object. In addition to creating and reading objects from the database, we also want to be able to delete and update Website
records. Therefore, the application we will make should perform all CRUD (create, read, update, delete) operations.
Database operations will be defined in the Repository interface you have seen before. We will create three concrete implementations of this interface - the first using the database/sql
package, the second using the pgx
client package, and the third using the GORM ORM.
type Website struct {
ID int64
Name string
URL string
Rank int64
}
type Repository interface {
Migrate(ctx context.Context) error
Create(ctx context.Context, website Website) (*Website, error)
All(ctx context.Context) ([]Website, error)
GetByName(ctx context.Context, name string) (*Website, error)
Update(ctx context.Context, id int64, updated Website) (*Website, error)
Delete(ctx context.Context, id int64) error
}