mtdb

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: May 11, 2022 License: MIT Imports: 6 Imported by: 0

README

Minetest database repositories for the golang ecosystem

Coverage Status

Docs: https://pkg.go.dev/github.com/minetest-go/mtdb

Features

  • Read and write users/privs to and from the auth database

Supported databases:

  • Sqlite3
  • Postgres

Usage

Read from an existing auth.sqlite database:

import (
    "database/sql"
    _ "modernc.org/sqlite"
    "github/minetest-go/mtdb"
)

func main() {
    auth_db, err := sql.Open("sqlite", "file:auth.sqlite")
    if err != nil {
        panic(err)
    }

    // Enable the wal mode for concurrent access
    err = mtdb.EnableWAL(auth_db)
    if err != nil {
        panic(err)
    }

    auth_repo := mtdb.NewAuthRepository(auth_db, mtdb.DATABASE_SQLITE)
    priv_repo := mtdb.NewPrivilegeRepository(auth_db, mtdb.DATABASE_SQLITE)

    // Read a user
    admin_user, err := auth_repo.GetByUsername("admin")
    if err != nil {
        panic(err)
    }

    fmt.Printf("User: %s, Last-login: %d\n", admin_user.Name, admin_user.LastLogin)

    // read privileges
    admin_privs, err := priv_repo.GetByID(*admin_user.ID)
    for _, priv := range admin_privs {
        fmt.Printf("+ Priv: %s\n", priv.Privilege)
    }
}

License

Code: MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnableWAL

func EnableWAL(db *sql.DB) error

func MigrateAuthDB added in v1.1.1

func MigrateAuthDB(db *sql.DB, dbtype DatabaseType) error

Types

type AuthEntry

type AuthEntry struct {
	ID        *int64 `json:"id"`
	Name      string `json:"name"`
	Password  string `json:"password"`
	LastLogin int    `json:"last_login"`
}

type AuthRepository added in v1.1.1

type AuthRepository interface {
	GetByUsername(username string) (*AuthEntry, error)
	Create(entry *AuthEntry) error
	Update(entry *AuthEntry) error
	Delete(id int64) error
}

func NewAuthRepository

func NewAuthRepository(db *sql.DB, dbtype DatabaseType) AuthRepository

type DatabaseType

type DatabaseType string
const (
	DATABASE_SQLITE   DatabaseType = "sqlite"
	DATABASE_POSTGRES DatabaseType = "postgres"
)

type PostgresAuthRepository added in v1.1.1

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

func (*PostgresAuthRepository) Create added in v1.1.1

func (repo *PostgresAuthRepository) Create(entry *AuthEntry) error

func (*PostgresAuthRepository) Delete added in v1.1.1

func (repo *PostgresAuthRepository) Delete(id int64) error

func (*PostgresAuthRepository) GetByUsername added in v1.1.1

func (repo *PostgresAuthRepository) GetByUsername(username string) (*AuthEntry, error)

func (*PostgresAuthRepository) Update added in v1.1.1

func (repo *PostgresAuthRepository) Update(entry *AuthEntry) error

type PrivRepository added in v1.1.1

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

func NewPrivilegeRepository

func NewPrivilegeRepository(db *sql.DB, dbtype DatabaseType) *PrivRepository

func (*PrivRepository) Create added in v1.1.1

func (repo *PrivRepository) Create(entry *PrivilegeEntry) error

func (*PrivRepository) Delete added in v1.1.1

func (repo *PrivRepository) Delete(id int64, privilege string) error

func (*PrivRepository) GetByID added in v1.1.1

func (repo *PrivRepository) GetByID(id int64) ([]*PrivilegeEntry, error)

type PrivilegeEntry

type PrivilegeEntry struct {
	ID        int64  `json:"id"`
	Privilege string `json:"privilege"`
}

type SqliteAuthRepository added in v1.1.1

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

func (*SqliteAuthRepository) Create added in v1.1.1

func (repo *SqliteAuthRepository) Create(entry *AuthEntry) error

func (*SqliteAuthRepository) Delete added in v1.1.1

func (repo *SqliteAuthRepository) Delete(id int64) error

func (*SqliteAuthRepository) GetByUsername added in v1.1.1

func (repo *SqliteAuthRepository) GetByUsername(username string) (*AuthEntry, error)

func (*SqliteAuthRepository) Update added in v1.1.1

func (repo *SqliteAuthRepository) Update(entry *AuthEntry) error

Jump to

Keyboard shortcuts

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