connector.go 859 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package db
  2. import (
  3. "errors"
  4. "os"
  5. "github.com/gravitl/netmaker/config"
  6. "gorm.io/gorm"
  7. )
  8. var ErrUnsupportedDB = errors.New("unsupported db type")
  9. // connector helps connect to a database,
  10. // along with any initializations required.
  11. type connector interface {
  12. connect() (*gorm.DB, error)
  13. }
  14. // GetDB - gets the database type
  15. func GetDB() string {
  16. database := "sqlite"
  17. if os.Getenv("DATABASE") != "" {
  18. database = os.Getenv("DATABASE")
  19. } else if config.Config.Server.Database != "" {
  20. database = config.Config.Server.Database
  21. }
  22. return database
  23. }
  24. // newConnector detects the database being
  25. // used and returns the corresponding connector.
  26. func newConnector() (connector, error) {
  27. switch GetDB() {
  28. case "sqlite":
  29. return &sqliteConnector{}, nil
  30. case "postgres":
  31. return &postgresConnector{}, nil
  32. default:
  33. return nil, ErrUnsupportedDB
  34. }
  35. }