db

package
v0.0.0-...-37b99e7 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2023 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package db for database layer connection handling

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MongoConnect

func MongoConnect(ctx *context.Context, dbUser, dbPass, dbHost, dbTimeout string, dnsSeed bool) (*mongo.Client, context.Context, context.CancelFunc, error)

MongoConnect establishes a connection to a MongoDB cluster https://www.geeksforgeeks.org/how-to-use-go-with-mongodb/

Types

type APIKey

type APIKey struct {
	Created time.Time `yaml:"created" json:"created" sql:"created"`
	Updated time.Time `yaml:"updated" json:"updated" sql:"updated"`
	Name    string    `yaml:"name" json:"name" sql:"name"`
	ID      uuid.UUID `yaml:"id" json:"id" sql:"id"`
	Role    uuid.UUID `yaml:"role" json:"role" sql:"role"`
}

APIKey ApiKey for authentication

type Conn

type Conn struct {
	Pool    *pgxpool.Pool
	Context context.Context
}

Conn database connection pool and context

func Connect

func Connect(ctx *context.Context, dbUser, dbPass, dbHost, dbName, dbParams string) (*Conn, error)

Connect connect to a Postgres compatible database.

func (Conn) Close

func (d Conn) Close()

Close close the connection pool

func (Conn) Exec

func (d Conn) Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)

Exec execute a command via the connection pool

func (Conn) GenerateAPIKey

func (d Conn) GenerateAPIKey(name string, tags []string) (string, string, error)

GenerateAPIKey generate an api key and a public key for a new host

func (*Conn) GetAPIKey

func (d *Conn) GetAPIKey(host, keyID string) (APIKey, error)

GetAPIKey retrieves an API key from the database TODO: Add validation to the function

func (Conn) Query

func (d Conn) Query(ctx context.Context, sql string, optionsAndArgs ...any) (pgx.Rows, error)

Query execute a query via the connection pool

func (Conn) QueryRow

func (d Conn) QueryRow(ctx context.Context, sql string, optionsAndArgs ...any) pgx.Row

QueryRow execute a query via the connection pool. Return a row.

func (Conn) ValidateAPIKey

func (d Conn) ValidateAPIKey(id, token string) (string, error)

ValidateAPIKey validates an API Key for a host

func (*Conn) ValidateConnection

func (d *Conn) ValidateConnection() error

ValidateConnection validates the pool with a ping

type DBIface

type DBIface interface {
	Close()
	Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)
	Query(ctx context.Context, sql string, optionsAndArgs ...any) (pgx.Rows, error)
	QueryRow(ctx context.Context, sql string, optionsAndArgs ...any) pgx.Row
	GenerateAPIKey(name string, tags []string) (string, string, error)
	ValidateAPIKey(id, token string) (string, error)
}

Jump to

Keyboard shortcuts

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