1234567891011121314151617181920212223242526272829303132333435363738394041 |
- package db
- import (
- "errors"
- "os"
- "github.com/gravitl/netmaker/config"
- "gorm.io/gorm"
- )
- var ErrUnsupportedDB = errors.New("unsupported db type")
- // connector helps connect to a database,
- // along with any initializations required.
- type connector interface {
- connect() (*gorm.DB, error)
- }
- // GetDB - gets the database type
- func GetDB() string {
- database := "sqlite"
- if os.Getenv("DATABASE") != "" {
- database = os.Getenv("DATABASE")
- } else if config.Config.Server.Database != "" {
- database = config.Config.Server.Database
- }
- return database
- }
- // newConnector detects the database being
- // used and returns the corresponding connector.
- func newConnector() (connector, error) {
- switch GetDB() {
- case "sqlite":
- return &sqliteConnector{}, nil
- case "postgres":
- return &postgresConnector{}, nil
- default:
- return nil, ErrUnsupportedDB
- }
- }
|