database

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2025 License: MIT Imports: 32 Imported by: 0

README

Station Manager: database module

Regenerating the database models

The build uses sqlboiler to generate the database models. To regenerate the models, each database must be rebuilt separately, but both follow roughly the same steps.

SQLite
  1. Delete the existing development database file at build/db/data.db.
  2. Run the database/sqlite/example/main.go file.
  3. cd database/sqlite
  4. sqlboiler sqlite3
Postgres
  1. Delete the existing development database file at build/db/postgres_data - this will require the use of sudo.

  2. Start the Postgres Docker container.

     sudo systemctl start docker.service`
     docker-compose up
    
  3. Run the database/postgres/example/main.go file.

  4. cd database/postgres

  5. export PSQL_PASS=[some password] - obviously replace [some password] with the correct password.

  6. sqlboiler psql

Adapters usage

This module uses the adapters package to map between types and DB models. The new API is destination-first: Into(dst, src). Generic helpers are provided in database/helpers.go.

Examples:

// QSO insert (sqlite)
model, err := s.AdaptTypeToSqliteModelQso(qso)
if err != nil { /* handle */ }
err = model.Insert(ctx, h, boil.Infer())

// QSO fetch (postgres)
m, _ := pgmodels.FindQso(ctx, h, id)
out, err := s.AdaptPostgresModelToTypeQso(m)

Context-aware CRUD

Each CRUD method has a ...Context(ctx, ...) variant. If the caller doesn’t supply a deadline, a default timeout is applied by the service. Transactions use BeginTxContext with a configurable timeout.

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
qso, err := svc.InsertQsoContext(ctx, q)

Documentation

Index

Constants

View Source
const (
	ServiceName    = types.DatabaseServiceName
	PostgresDriver = "postgres"
	SqliteDriver   = "sqlite"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Database

type Database interface {
	Open() error
	Close() error
	Ping() error
	Migrate() error
	BeginTxContext(context.Context) (*sql.Tx, context.CancelFunc, error)
	ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
	QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
}

type Service

type Service struct {
	ConfigService  *config.Service  `di.inject:"configservice"`
	Logger         *logging.Service `di.inject:"loggingservice"`
	DatabaseConfig *types.DatastoreConfig
	// contains filtered or unexported fields
}

func (*Service) AdaptPostgresModelToTypeLogbook added in v0.0.8

func (s *Service) AdaptPostgresModelToTypeLogbook(m *pgmodels.Logbook) (types.Logbook, error)

AdaptPostgresModelToTypeLogbook adapts postgres model Logbook to types.Logbook.

func (*Service) AdaptPostgresModelToTypeQso added in v0.0.8

func (s *Service) AdaptPostgresModelToTypeQso(m *pgmodels.Qso) (types.Qso, error)

AdaptPostgresModelToTypeQso adapts postgres model to types.Qso.

func (*Service) AdaptSqliteModelToTypeLogbook added in v0.0.8

func (s *Service) AdaptSqliteModelToTypeLogbook(m *sqmodels.Logbook) (types.Logbook, error)

AdaptSqliteModelToTypeLogbook adapts sqlite model Logbook to types.Logbook.

func (*Service) AdaptSqliteModelToTypeQso added in v0.0.8

func (s *Service) AdaptSqliteModelToTypeQso(m *sqmodels.Qso) (types.Qso, error)

AdaptSqliteModelToTypeQso adapts sqlite model to types.Qso.

func (*Service) AdaptTypeToPostgresModelLogbook added in v0.0.8

func (s *Service) AdaptTypeToPostgresModelLogbook(lb types.Logbook) (*pgmodels.Logbook, error)

AdaptTypeToPostgresModelLogbook adapts a types.Logbook into a postgres model Logbook.

func (*Service) AdaptTypeToPostgresModelQso added in v0.0.8

func (s *Service) AdaptTypeToPostgresModelQso(q types.Qso) (*pgmodels.Qso, error)

AdaptTypeToPostgresModelQso adapts a types.Qso into a postgres model Qso.

func (*Service) AdaptTypeToSqliteModelLogbook added in v0.0.8

func (s *Service) AdaptTypeToSqliteModelLogbook(lb types.Logbook) (*sqmodels.Logbook, error)

AdaptTypeToSqliteModelLogbook adapts a types.Logbook into a sqlite model Logbook.

func (*Service) AdaptTypeToSqliteModelQso added in v0.0.8

func (s *Service) AdaptTypeToSqliteModelQso(q types.Qso) (*sqmodels.Qso, error)

AdaptTypeToSqliteModelQso adapts a types.Qso into a sqlite model Qso.

func (*Service) BeginTxContext

func (s *Service) BeginTxContext(ctx context.Context) (*sql.Tx, context.CancelFunc, error)

BeginTxContext starts a new transaction.

func (*Service) Close

func (s *Service) Close() error

Close closes the database connection.

func (*Service) DeleteQso added in v0.0.8

func (s *Service) DeleteQso(id int64) error

DeleteQso delegates to DeleteQsoContext with a background context.

func (*Service) DeleteQsoContext added in v0.0.8

func (s *Service) DeleteQsoContext(ctx context.Context, id int64) error

DeleteQsoContext deletes a QSO with a caller-provided context.

func (*Service) ExecContext

func (s *Service) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

func (*Service) FetchQsoById added in v0.0.7

func (s *Service) FetchQsoById(id int64) (types.Qso, error)

func (*Service) FetchQsoByIdContext added in v0.0.8

func (s *Service) FetchQsoByIdContext(ctx context.Context, id int64) (types.Qso, error)

func (*Service) Initialize

func (s *Service) Initialize() error

Initialize initializes the database service. No constructor is provided as this service is to be initialized within an IOC/DI container.

func (*Service) InsertLogbook added in v0.0.8

func (s *Service) InsertLogbook(logbook types.Logbook) (types.Logbook, error)

func (*Service) InsertLogbookContext added in v0.0.8

func (s *Service) InsertLogbookContext(ctx context.Context, logbook types.Logbook) (types.Logbook, error)

func (*Service) InsertQso added in v0.0.7

func (s *Service) InsertQso(qso types.Qso) (types.Qso, error)

InsertQso inserts a QSO using a background context with default timeout semantics.

func (*Service) InsertQsoContext added in v0.0.8

func (s *Service) InsertQsoContext(ctx context.Context, qso types.Qso) (types.Qso, error)

InsertQsoContext inserts a QSO with caller-provided context. If the context has no deadline, a default timeout is applied.

func (*Service) LogStats added in v0.0.8

func (s *Service) LogStats(prefix string)

func (*Service) Migrate

func (s *Service) Migrate() error

Migrate runs the database migrations.

func (*Service) Open

func (s *Service) Open() error

Open opens the database connection.

func (*Service) Ping

func (s *Service) Ping() error

Ping pings the database connection.

func (*Service) QueryContext

func (s *Service) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

func (*Service) UpdateQso added in v0.0.8

func (s *Service) UpdateQso(qso types.Qso) error

UpdateQso delegates to UpdateQsoContext with a background context.

func (*Service) UpdateQsoContext added in v0.0.8

func (s *Service) UpdateQsoContext(ctx context.Context, qso types.Qso) error

UpdateQsoContext updates an existing QSO with caller-provided context.

Directories

Path Synopsis
examples command
example command

Jump to

Keyboard shortcuts

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