dbmanager

package module
v0.0.0-...-5d0b8b2 Latest Latest
Warning

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

Go to latest
Published: May 22, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

README

go-dbmanager

A Go library for handling database and database-user life cycles.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func QuoteIdentifier

func QuoteIdentifier(name string) string

QuoteIdentifier quotes an "identifier" (e.g. a table or a column name) to be used as part of an SQL statement. For example:

tblname := "my_table"
data := "my_data"
quoted := pq.QuoteIdentifier(tblname)
err := db.Exec(fmt.Sprintf("INSERT INTO %s VALUES ($1)", quoted), data)

Any double quotes in name will be escaped. The quoted identifier will be case sensitive when used in a query. If the input string contains a zero byte, the result will be truncated immediately before it.

This is a copy of the PostgreSQL libpq function so that we don't need to import the entire pq package.

func WithDatabase

func WithDatabase(database string) func(*Connection)

WithDatabase sets the database in the connection configuration

func WithHost

func WithHost(host string) func(*Connection)

WithHost sets the host in the connection configuration

func WithPassword

func WithPassword(password string) func(*Connection)

WithPassword sets the password in the connection configuration

func WithPort

func WithPort(port string) func(*Connection)

WithPort sets the port in the connection configuration

func WithSSL

func WithSSL(ssl bool) func(*Connection)

WithSSL sets the SSL flag in the connection configuration

func WithUsername

func WithUsername(username string) func(*Connection)

WithUsername sets the username in the connection configuration

Types

type Connection

type Connection struct {
	Host     string
	Database string
	Port     string
	Username string
	Password string
	SSLMode  string
	SSL      bool
}

Connection represents the configuration for establishing a connection

type Connector

type Connector interface {
	Connect() error
	Disconnect() error
}

Connector represents a database connection

type Database

type Database struct {
	Name              string             `json:"name"`
	DefaultPrivileges []DefaultPrivilege `json:"default_privileges"`
	Owner             string             `json:"owner"`
}

Database represents the configuration for creating a database

type DefaultPrivilege

type DefaultPrivilege struct {
	Role      string   `json:"role"`
	Schema    string   `json:"schema"`
	Grant     []string `json:"grant"`
	On        string   `json:"on"`
	To        string   `json:"to"`
	WithGrant bool     `json:"with_grant"`
}

DefaultPrivilege contains the default privileges in a database for a user or role.

type Grant

type Grant struct {
	// Optional: Specify the target database
	Database string `json:"database"`

	// Optional: Specify the target schema
	Schema string `json:"schema"`

	// Optional: Specify the target Sequence
	Sequence string `json:"sequence"`

	// Optional: Specify the target table
	Table string `json:"table"`

	// Optional: Specify the target parameter (PostgreSQL only)
	Parameter string `json:"parameter"`

	// Required: List of privileges (e.g., "ALL", "CONNECT", "USAGE", "SELECT", etc.)
	Privileges []string `json:"privileges"`

	// Optional: Grant option
	WithGrant bool `json:"with_grant"`
}

Grant represents a set of permissions granted to a user.

type Manager

type Manager interface {
	Connector
	CreateDatabase(databaseConfig Database) error
	CreateUser(userConfig User) error
	GrantPermissions(user User) error
	Manage(databases []Database, users []User) error
}

Manager is the main interface for managing database servers

func New

func New(engine string, options ...func(*Connection)) (Manager, error)

New creates a new Manager instance based on the provided engine.

type User

type User struct {
	Name     string      `json:"name"`
	Password string      `json:"password"`
	Options  UserOptions `json:"options"`
	Grants   []Grant     `json:"grants"`
	Roles    []string    `json:"roles"`
}

User represents the configuration for creating a user

type UserOptions

type UserOptions struct {
	// Login specifies whether the user is allowed to log in to the database. Applicable to PostgreSQL only.
	Login bool `json:"login"`

	// Superuser specifies whether the user will be a superuser. Applicable to PostgreSQL only.
	Superuser bool `json:"superuser"`

	// CreateDatabase specifies whether the user will be allowed to create databases. Applicable to PostgreSQL only.
	CreateDatabase bool `json:"create_database"`

	// CreateRole specifies whether the user will be allowed to create roles. Applicable to PostgreSQL only.
	CreateRole bool `json:"create_role"`

	// Inherit specifies whether the user will inherit privileges of roles that it is a member of. Applicable to PostgreSQL only.
	Inherit bool `json:"inherit"`

	// Replication specifies whether the user will be allowed to initiate streaming replication. Applicable to PostgreSQL only.
	Replication bool `json:"replication"`

	// BypassRLS specifies whether the user will be allowed to bypass row level security policies. Applicable to PostgreSQL only.
	BypassRLS bool `json:"bypass_rls"`
}

UserOptions represents the configuration for creating a user

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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