PKGBUILD/pkg/repository/repository.go

86 lines
3.1 KiB
Go
Raw Normal View History

2020-05-21 15:40:24 +00:00
package repository
import (
"context"
2020-11-06 22:09:26 +00:00
"fmt"
2020-05-21 15:40:24 +00:00
"net/url"
"git.cryptic.systems/volker.raschek/flucky/pkg/repository/postgres"
"git.cryptic.systems/volker.raschek/flucky/pkg/repository/sqlite3"
2020-06-10 19:13:05 +00:00
"git.cryptic.systems/volker.raschek/flucky/pkg/types"
"git.cryptic.systems/volker.raschek/go-logger"
2020-05-21 15:40:24 +00:00
)
type Repository interface {
AddDevices(ctx context.Context, devices ...*types.Device) error
AddMeasuredValues(ctx context.Context, measuredValues ...*types.MeasuredValue) error
AddOrUpdateDevices(ctx context.Context, devices ...*types.Device) error
AddOrUpdateMeasuredValues(ctx context.Context, measuredValues ...*types.MeasuredValue) error
AddOrUpdateSensors(ctx context.Context, sensors ...*types.Sensor) error
AddSensors(ctx context.Context, sensors ...*types.Sensor) error
Close() error
GetDeviceByID(ctx context.Context, deviceID string) (*types.Device, error)
GetDeviceByName(ctx context.Context, name string) (*types.Device, error)
GetDevices(ctx context.Context) ([]*types.Device, error)
GetHumidities(ctx context.Context) ([]*types.MeasuredValue, error)
GetHumidityByID(ctx context.Context, id string) (*types.MeasuredValue, error)
GetPressureByID(ctx context.Context, id string) (*types.MeasuredValue, error)
GetPressures(ctx context.Context) ([]*types.MeasuredValue, error)
GetSensorByID(ctx context.Context, sensorID string) (*types.Sensor, error)
GetSensorsByDeviceIDs(ctx context.Context, deviceIDs ...string) ([]*types.Sensor, error)
GetSensorsByModels(ctx context.Context, sensorModels ...string) ([]*types.Sensor, error)
GetSensorsByNames(ctx context.Context, sensorModels ...string) ([]*types.Sensor, error)
GetSensors(ctx context.Context) ([]*types.Sensor, error)
GetTemperatureByID(ctx context.Context, id string) (*types.MeasuredValue, error)
GetTemperatures(ctx context.Context) ([]*types.MeasuredValue, error)
Migrate(ctx context.Context) error
RemoveDevicesByIDs(ctx context.Context, deviceIDs ...string) error
RemoveDevicesByNames(ctx context.Context, names ...string) error
RemoveSensorsByIDs(ctx context.Context, sensorIDs ...string) error
RemoveSensorsByNames(ctx context.Context, names ...string) error
UpdateDevices(ctx context.Context, devices ...*types.Device) error
UpdateSensors(ctx context.Context, sensors ...*types.Sensor) error
2020-05-21 15:40:24 +00:00
}
// New returns a new database backend interface
func New(databaseURL *url.URL, flogger logger.Logger) (Repository, error) {
switch databaseURL.Scheme {
case "postgres":
repo, err := postgres.New(postgres.Opts{
DatabaseURL: databaseURL,
Logger: flogger,
})
if err != nil {
return nil, err
2020-05-21 15:40:24 +00:00
}
// Initialize database scheme if not exists
err = repo.Migrate(context.Background())
if err != nil {
return nil, err
2020-11-06 22:09:26 +00:00
}
return repo, nil
2020-11-06 22:09:26 +00:00
case "sqlite3":
repo, err := sqlite3.New(sqlite3.Opts{
DatabaseURL: databaseURL,
Logger: flogger,
})
2020-11-06 22:09:26 +00:00
if err != nil {
return nil, err
2020-11-06 22:09:26 +00:00
}
// Initialize database scheme if not exists
err = repo.Migrate(context.Background())
2020-11-06 22:09:26 +00:00
if err != nil {
return nil, err
2020-11-06 22:09:26 +00:00
}
return repo, nil
2020-11-06 22:09:26 +00:00
default:
return nil, fmt.Errorf("Unsupported repository scheme: %v", databaseURL.Scheme)
2020-11-06 22:09:26 +00:00
}
}