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.
Package postgres is a generated GoMock package.
Index ¶
- func Connect(config *CxnConfig, migrations []Migration) (*gorm.DB, error)
- func WipeDB(db *gorm.DB) error
- type CxnConfig
- type DatabaseService
- type DatabaseServiceImpl
- func (service *DatabaseServiceImpl) CountByQuery(model interface{}, query map[string]interface{}) (int64, error)
- func (service *DatabaseServiceImpl) FetchByQuery(models interface{}, query string, params []interface{}) error
- func (service *DatabaseServiceImpl) FindByID(model interface{}, ID interface{}) error
- func (service *DatabaseServiceImpl) FindByQuery(model interface{}, query map[string]interface{}) error
- func (service *DatabaseServiceImpl) Insert(model interface{}) error
- func (service *DatabaseServiceImpl) PagedByQuery(models interface{}, query string, params []interface{}, order string, page int, ...) (PagedData, error)
- func (service *DatabaseServiceImpl) PagedByQueryFromSession(models interface{}, session *gorm.DB, page int, perPage int) (PagedData, error)
- type Migration
- type MockDatabaseService
- func (m *MockDatabaseService) CountByQuery(model interface{}, query map[string]interface{}) (int64, error)
- func (m *MockDatabaseService) EXPECT() *MockDatabaseServiceMockRecorder
- func (m *MockDatabaseService) FetchByQuery(models interface{}, query string, params []interface{}) error
- func (m *MockDatabaseService) FindByID(model, ID interface{}) error
- func (m *MockDatabaseService) FindByQuery(model interface{}, query map[string]interface{}) error
- func (m *MockDatabaseService) PagedByQuery(models interface{}, query string, params []interface{}, order string, ...) (PagedData, error)
- func (m *MockDatabaseService) PagedByQueryFromSession(models interface{}, session *gorm.DB, page, perPage int) (PagedData, error)
- type MockDatabaseServiceMockRecorder
- func (mr *MockDatabaseServiceMockRecorder) CountByQuery(model, query interface{}) *gomock.Call
- func (mr *MockDatabaseServiceMockRecorder) FetchByQuery(models, query, params interface{}) *gomock.Call
- func (mr *MockDatabaseServiceMockRecorder) FindByID(model, ID interface{}) *gomock.Call
- func (mr *MockDatabaseServiceMockRecorder) FindByQuery(model, query interface{}) *gomock.Call
- func (mr *MockDatabaseServiceMockRecorder) PagedByQuery(models, query, params, order, page, perPage interface{}, ...) *gomock.Call
- func (mr *MockDatabaseServiceMockRecorder) PagedByQueryFromSession(models, session, page, perPage interface{}) *gomock.Call
- type PagedData
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type CxnConfig ¶
type CxnConfig struct { Host string IsTestDB bool Port string Name string Password string URL string User string }
CxnConfig holds connection information used to connect to a PostgreSQL database.
type DatabaseService ¶
type DatabaseService interface { CountByQuery(model interface{}, query map[string]interface{}) (int64, error) FetchByQuery(models interface{}, query string, params []interface{}) error FindByID(model interface{}, ID interface{}) error FindByQuery(model interface{}, query map[string]interface{}) error PagedByQuery(models interface{}, query string, params []interface{}, order string, page int, perPage int, preloads ...string) (PagedData, error) PagedByQueryFromSession(models interface{}, 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 {
// contains filtered or unexported fields
}
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 interface{}, query map[string]interface{}) (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 interface{}, query string, params []interface{}) 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 interface{}, ID interface{}) error
FindByID receives a database model as a pointer and fetches it using the primary ID.
func (*DatabaseServiceImpl) FindByQuery ¶
func (service *DatabaseServiceImpl) FindByQuery(model interface{}, query map[string]interface{}) error
FindByQuery receives a database model as a pointer and fetches it using the given query.
func (*DatabaseServiceImpl) Insert ¶
func (service *DatabaseServiceImpl) Insert(model interface{}) error
Insert receives a database model and inserts it into the database.
func (*DatabaseServiceImpl) PagedByQuery ¶
func (service *DatabaseServiceImpl) PagedByQuery(models interface{}, query string, params []interface{}, 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 interface{}, 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 ¶
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 interface{}, query map[string]interface{}) (int64, error)
CountByQuery mocks base method
func (*MockDatabaseService) EXPECT ¶ added in v0.1.1
func (m *MockDatabaseService) EXPECT() *MockDatabaseServiceMockRecorder
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 interface{}, query string, params []interface{}) error
FetchByQuery mocks base method
func (*MockDatabaseService) FindByID ¶ added in v0.1.1
func (m *MockDatabaseService) FindByID(model, ID interface{}) error
FindByID mocks base method
func (*MockDatabaseService) FindByQuery ¶ added in v0.1.1
func (m *MockDatabaseService) FindByQuery(model interface{}, query map[string]interface{}) error
FindByQuery mocks base method
func (*MockDatabaseService) PagedByQuery ¶ added in v0.1.1
func (m *MockDatabaseService) PagedByQuery(models interface{}, query string, params []interface{}, 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 interface{}, 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 interface{}) *gomock.Call
CountByQuery indicates an expected call of CountByQuery
func (*MockDatabaseServiceMockRecorder) FetchByQuery ¶ added in v0.1.1
func (mr *MockDatabaseServiceMockRecorder) FetchByQuery(models, query, params interface{}) *gomock.Call
FetchByQuery indicates an expected call of FetchByQuery
func (*MockDatabaseServiceMockRecorder) FindByID ¶ added in v0.1.1
func (mr *MockDatabaseServiceMockRecorder) FindByID(model, ID interface{}) *gomock.Call
FindByID indicates an expected call of FindByID
func (*MockDatabaseServiceMockRecorder) FindByQuery ¶ added in v0.1.1
func (mr *MockDatabaseServiceMockRecorder) FindByQuery(model, query interface{}) *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 interface{}, preloads ...interface{}) *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 interface{}) *gomock.Call
PagedByQueryFromSession indicates an expected call of PagedByQueryFromSession
type PagedData ¶
type PagedData struct { Items interface{} `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.