dbresolver

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: May 13, 2023 License: MIT Imports: 11 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetHeaderName

func SetHeaderName(name string)

Changes the expected header or query param for the API key.

Types

type ContextValue

type ContextValue string

Use custom type as context key to avoid collisions.

var ConnectionContextKey ContextValue = "connection"

The context key used to access each database connection from req context.

var DatabaseContextKey ContextValue = "database"

The context key used to access each database name from req context.

type DBDriver

type DBDriver struct {
	Driver   Driver
	Database string
}

type DBResolver

type DBResolver struct {
	// contains filtered or unexported fields
}

DBResolver stores all database connections and config. Call ResolveConnection to get the underlying database connection for an API key.

func New

func New(c DatabaseConfig, options ...Option) (*DBResolver, error)

Initialize a new DBResolver with a database config, driver, and *gorm.Config. The driver argument should be one of "sqlite", "mysql", or "postgres". Default ApiKey header/query expected is x-api-key. call dbresolver.SetHeaderName to change it.

func (*DBResolver) AutoMigrate

func (resolver *DBResolver) AutoMigrate(models []interface{}, errorCallback func(error) bool)

Runs *gorm.DB.AutoMigrate(...) on all databases, creating all the tables. The error callback is only called if error is not nil. The error callback allows you to ignore certain errors and return true. If the error callback returns false this function will panic.

func (*DBResolver) DB

func (resolver *DBResolver) DB(r *http.Request) *gorm.DB

DB retrieves the current database connection from the request context. This assumes that the handler was run after the middleware. Otherwise, this will panic.

func (*DBResolver) DBName

func (resolver *DBResolver) DBName(r *http.Request) string

ResolveDatabase resolves the database name from the request APIKey.

func (*DBResolver) Middleware

func (resolver *DBResolver) Middleware(next http.Handler) http.Handler

type DatabaseConfig

type DatabaseConfig map[string]map[string]string

DatabaseConfig links API keys to database objects in a map data structure.

func ConfigFromYAMLFile

func ConfigFromYAMLFile(filename string) (DatabaseConfig, error)

DatabaseConfigFromYAML parses a yaml file and returns a DatabaseConfig.

func ConfigFromYAMLString

func ConfigFromYAMLString(yamlStr string) (DatabaseConfig, error)

DatabaseConfigFromYAML parses a YAML-formatted string and returns a DatabaseConfig.

func (DatabaseConfig) DatabaseDrivers

func (dbconfig DatabaseConfig) DatabaseDrivers() []DBDriver

Returns a struct of DBDrivers with each having a Database and Driver field.

type Driver

type Driver string
const (
	Sqlite   Driver = "sqlite"
	MySQL    Driver = "mysql"
	Postgres Driver = "postgres"
)

type Option

type Option func(resolver *DBResolver)

func GormConfig

func GormConfig(c *gorm.Config) Option

Jump to

Keyboard shortcuts

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