postgres

package
v0.10.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2024 License: MIT Imports: 13 Imported by: 6

Documentation

Overview

Package postgres manages our database connection. As part of the connection process, we also ensure that all migrations have been run on the proper database. The situation where the database is simply a target for some testing has been considered as well. In this scenario, we are dropping the public schema.

There is a very basic set of getter methods that have been implemented as well. An interface has been provided such that it can be mocked out for testing that does not need an actual database running in the environment.

Code generated by MockGen. DO NOT EDIT. Source: postgres/service.go

Generated by this command:

mockgen -source postgres/service.go -destination postgres/mocks.go -package postgres

Package postgres is a generated GoMock package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Connect

func Connect(config *CxnConfig, env trails.Environment) (*gorm.DB, error)

Connect creates a database connection through GORM according to the connection config.

Run migrations by passing DB into MigrateUp.

func MigrateUp added in v0.9.3

func MigrateUp(db *gorm.DB, migrations []Migration) error

func WipeDB added in v0.2.0

func WipeDB(db *gorm.DB) error

WipeDB queries for all of the tables and then drops the data in this tables.

Types

type CxnConfig

type CxnConfig struct {
	IsTestDB    bool
	URL         string
	Host        string
	Port        string
	Name        string
	User        string
	Password    string
	SSLMode     string
	MaxIdleCxns int
}

CxnConfig holds connection information used to connect to a PostgreSQL database.

type DatabaseService

type DatabaseService interface {
	CountByQuery(model any, query map[string]any) (int64, error)
	FetchByQuery(models any, query string, params []any) error
	FindByID(model any, ID any) error
	FindByQuery(model any, query map[string]any) error
	PagedByQuery(models any, query string, params []any, order string, page int, perPage int, preloads ...string) (PagedData, error)
	PagedByQueryFromSession(models any, session *gorm.DB, page int, perPage int) (PagedData, error)
}

DatabaseService sets up the interface to be used at the handler/middelware level. These should be straightforward calls that allow us to skip creating a procedure method for the most basic database interactions. At the procedural layer, the *gorm.DB struct is available directly for more complex composition. This has the intended functionality that we are not testing the database in handlers, while it is tested directly at the procedural layer.

type DatabaseServiceImpl

type DatabaseServiceImpl struct {
	DB *gorm.DB
}

DatabaseServiceImpl satisfies the above DatabaseService interface.

func NewService

func NewService(DB *gorm.DB) *DatabaseServiceImpl

NewService hydrates the gorm database for the implementation struct methods.

func (*DatabaseServiceImpl) CountByQuery

func (service *DatabaseServiceImpl) CountByQuery(model any, query map[string]any) (int64, error)

CountByQuery recives a database model and query and fetches a count for the given params.

func (*DatabaseServiceImpl) FetchByQuery

func (service *DatabaseServiceImpl) FetchByQuery(models any, query string, params []any) error

FetchByQuery receives a slice of database models as a pointer and fetches all records matching the query.

func (*DatabaseServiceImpl) FindByID

func (service *DatabaseServiceImpl) FindByID(model any, ID any) error

FindByID receives a database model as a pointer and fetches it using the primary ID.

func (*DatabaseServiceImpl) FindByQuery

func (service *DatabaseServiceImpl) FindByQuery(model any, query map[string]any) error

FindByQuery receives a database model as a pointer and fetches it using the given query.

func (*DatabaseServiceImpl) Insert

func (service *DatabaseServiceImpl) Insert(model any) error

Insert receives a database model and inserts it into the database.

func (*DatabaseServiceImpl) PagedByQuery

func (service *DatabaseServiceImpl) PagedByQuery(models any, query string, params []any, order string, page int, perPage int, preloads ...string) (PagedData, error)

PagedByQuery receives a slice of database models and paging information to build a paged database query.

func (*DatabaseServiceImpl) PagedByQueryFromSession

func (service *DatabaseServiceImpl) PagedByQueryFromSession(models any, session *gorm.DB, page int, perPage int) (PagedData, error)

PagedByQueryFromSession receives a slice of database models and paging information to build a paged database query.

type Migration

type Migration struct {
	Executor func(*gorm.DB) error
	Key      string
}

Migration is used to hold the database key and function for creating the migration.

type MockDatabaseService added in v0.1.1

type MockDatabaseService struct {
	// contains filtered or unexported fields
}

MockDatabaseService is a mock of DatabaseService interface.

func NewMockDatabaseService added in v0.1.1

func NewMockDatabaseService(ctrl *gomock.Controller) *MockDatabaseService

NewMockDatabaseService creates a new mock instance.

func (*MockDatabaseService) CountByQuery added in v0.1.1

func (m *MockDatabaseService) CountByQuery(model any, query map[string]any) (int64, error)

CountByQuery mocks base method.

func (*MockDatabaseService) EXPECT added in v0.1.1

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockDatabaseService) FetchByQuery added in v0.1.1

func (m *MockDatabaseService) FetchByQuery(models any, query string, params []any) error

FetchByQuery mocks base method.

func (*MockDatabaseService) FindByID added in v0.1.1

func (m *MockDatabaseService) FindByID(model, ID any) error

FindByID mocks base method.

func (*MockDatabaseService) FindByQuery added in v0.1.1

func (m *MockDatabaseService) FindByQuery(model any, query map[string]any) error

FindByQuery mocks base method.

func (*MockDatabaseService) PagedByQuery added in v0.1.1

func (m *MockDatabaseService) PagedByQuery(models any, query string, params []any, order string, page, perPage int, preloads ...string) (PagedData, error)

PagedByQuery mocks base method.

func (*MockDatabaseService) PagedByQueryFromSession added in v0.1.1

func (m *MockDatabaseService) PagedByQueryFromSession(models any, session *gorm.DB, page, perPage int) (PagedData, error)

PagedByQueryFromSession mocks base method.

type MockDatabaseServiceMockRecorder added in v0.1.1

type MockDatabaseServiceMockRecorder struct {
	// contains filtered or unexported fields
}

MockDatabaseServiceMockRecorder is the mock recorder for MockDatabaseService.

func (*MockDatabaseServiceMockRecorder) CountByQuery added in v0.1.1

func (mr *MockDatabaseServiceMockRecorder) CountByQuery(model, query any) *gomock.Call

CountByQuery indicates an expected call of CountByQuery.

func (*MockDatabaseServiceMockRecorder) FetchByQuery added in v0.1.1

func (mr *MockDatabaseServiceMockRecorder) FetchByQuery(models, query, params any) *gomock.Call

FetchByQuery indicates an expected call of FetchByQuery.

func (*MockDatabaseServiceMockRecorder) FindByID added in v0.1.1

func (mr *MockDatabaseServiceMockRecorder) FindByID(model, ID any) *gomock.Call

FindByID indicates an expected call of FindByID.

func (*MockDatabaseServiceMockRecorder) FindByQuery added in v0.1.1

func (mr *MockDatabaseServiceMockRecorder) FindByQuery(model, query any) *gomock.Call

FindByQuery indicates an expected call of FindByQuery.

func (*MockDatabaseServiceMockRecorder) PagedByQuery added in v0.1.1

func (mr *MockDatabaseServiceMockRecorder) PagedByQuery(models, query, params, order, page, perPage any, preloads ...any) *gomock.Call

PagedByQuery indicates an expected call of PagedByQuery.

func (*MockDatabaseServiceMockRecorder) PagedByQueryFromSession added in v0.1.1

func (mr *MockDatabaseServiceMockRecorder) PagedByQueryFromSession(models, session, page, perPage any) *gomock.Call

PagedByQueryFromSession indicates an expected call of PagedByQueryFromSession.

type PagedData

type PagedData struct {
	Items      any   `json:"items"`
	Page       int   `json:"page"`
	PerPage    int   `json:"perPage"`
	TotalItems int64 `json:"totalItems"`
	TotalPages int   `json:"totalPages"`
}

PagedData is returned from the PagedByQuery method. It contains paged database records and pagination metadata.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL