package repository import ( "context" "fmt" "net/url" "git.cryptic.systems/volker.raschek/flucky/pkg/types" "git.cryptic.systems/volker.raschek/go-logger" ) 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) Import(ctx context.Context, src Repository) 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 } // New returns a new database backend interface func New(databaseURL *url.URL, flogger logger.Logger) (Repository, error) { switch databaseURL.Scheme { case "postgres": repo, err := NewPostgres(PostgresOpts{ DatabaseURL: databaseURL, Logger: flogger, }) if err != nil { return nil, err } // Initialize database scheme if not exists err = repo.Migrate(context.Background()) if err != nil { return nil, err } return repo, nil case "sqlite3": repo, err := NewSQLite(SQLiteOpts{ DatabaseURL: databaseURL, Logger: flogger, }) if err != nil { return nil, err } // Initialize database scheme if not exists err = repo.Migrate(context.Background()) if err != nil { return nil, err } return repo, nil default: return nil, fmt.Errorf("Unsupported repository scheme: %v", databaseURL.Scheme) } }