db

package
v0.5.7 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2022 License: AGPL-3.0 Imports: 13 Imported by: 1

Documentation

Overview

Package db is an middleware that manages multiple database pools and provides applications with an way to access the database

Index

Constants

View Source
const ContextKey string = "db"

ContextKey is the key that is used in a gin.Context to get the Middleware

Variables

This section is empty.

Functions

func GetConnection added in v0.5.5

func GetConnection(c *gin.Context, db string) (*pgxpool.Conn, error)

GetConnection is an simple helper function that returns an connection to the db

func SetupMigrator

func SetupMigrator(prefix string, connection *pgx.Conn, migrations fs.FS) (mig *migrate.Migrator, err error)

SetupMigrator sets up the migrator to migrate the database.

Types

type ConnGet

type ConnGet func(string) *pgxpool.Conn

ConnGet is an function that returns an connection instance.

type Middleware

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

Middleware return a handler that sets the db into the context of every request. uri is an url in the form dbtype:connectargs

func NewMiddleware

func NewMiddleware() *Middleware

NewMiddleware return an initialized Middleware Object.

func (*Middleware) AddDB

func (m *Middleware) AddDB(name, uri string) (err error)

AddDB adds an db connection to the middleware.

func (*Middleware) Defaults

func (*Middleware) Defaults() middleware.Config

Defaults returns an default config for connections

func (*Middleware) GetConn

func (m *Middleware) GetConn(name string) *pgxpool.Conn

GetConn returns a connection from the specified database or if not found one of the default database.

func (*Middleware) Gin

func (m *Middleware) Gin(c *gin.Context)

Gin is the Entrypoint for Gin.

func (*Middleware) PostSetup added in v0.4.1

func (m *Middleware) PostSetup(sites []any) (err error)

PostSetup is an function for getting the migrations of the site

func (*Middleware) Setup

func (m *Middleware) Setup(mc middleware.Config)

Setup adds the connections from the configfile into the middleware

func (*Middleware) Teardown

func (m *Middleware) Teardown()

Teardown closes the DBConnection

type MigrationSite

type MigrationSite interface {
	Database() string
	Migrations(*pgx.Conn) (*migrate.Migrator, error)
}

MigrationSite is an interface for Sites that use an database. this enables sites to provide database migrations.

type PoolSite added in v0.5.4

type PoolSite interface {
	MigrationSite
	Pool(*pgxpool.Pool)
}

PoolSite is an interface for site that need access to the pool outside of requests

Jump to

Keyboard shortcuts

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