storage

package
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package storage interacts with the permissions-api database handling the metadata updates for roles and resources.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoRoleFound is returned when no role is found when retrieving or deleting a role.
	ErrNoRoleFound = errors.New("role not found")

	// ErrRoleAlreadyExists is returned when creating a role which already has an existing record.
	ErrRoleAlreadyExists = errors.New("role already exists")

	// ErrRoleNameTaken is returned when the role name provided already exists under the same resource id.
	ErrRoleNameTaken = errors.New("role name already taken")

	// ErrMethodUnavailable is returned when the provided method is called is unavailable in the current environment.
	// For example there is nothing to commit after getting a role so calling Commit on a Role after retrieving it will return this error.
	ErrMethodUnavailable = errors.New("method unavailable")

	// ErrorMissingContextTx represents an error where no context transaction was provided.
	ErrorMissingContextTx = errors.New("no transaction provided in context")

	// ErrorInvalidContextTx represents an error where the given context transaction is of the wrong type.
	ErrorInvalidContextTx = errors.New("invalid type for transaction context")
)
View Source
var Migrations embed.FS

Migrations contains an embedded filesystem with all the sql migration files

Functions

This section is empty.

Types

type DB

type DB interface {
	BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
	PingContext(ctx context.Context) error

	DBQuery
}

DB is the interface the database package requires from a database engine to run. *sql.DB implements these methods.

type DBQuery

type DBQuery interface {
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
}

DBQuery are required methods for querying the database.

type Option

type Option func(e *engine)

Option defines a storage engine configuration option.

func WithLogger

func WithLogger(logger *zap.SugaredLogger) Option

WithLogger sets the logger for the storage engine.

type Role

type Role struct {
	ID         gidx.PrefixedID
	Name       string
	ResourceID gidx.PrefixedID
	CreatedBy  gidx.PrefixedID
	UpdatedBy  gidx.PrefixedID
	CreatedAt  time.Time
	UpdatedAt  time.Time
}

Role represents a role in the database.

type RoleService

type RoleService interface {
	GetRoleByID(ctx context.Context, id gidx.PrefixedID) (Role, error)
	GetResourceRoleByName(ctx context.Context, resourceID gidx.PrefixedID, name string) (Role, error)
	ListResourceRoles(ctx context.Context, resourceID gidx.PrefixedID) ([]Role, error)
	CreateRole(ctx context.Context, actorID gidx.PrefixedID, roleID gidx.PrefixedID, name string, resourceID gidx.PrefixedID) (Role, error)
	UpdateRole(ctx context.Context, actorID, roleID gidx.PrefixedID, name string) (Role, error)
	DeleteRole(ctx context.Context, roleID gidx.PrefixedID) (Role, error)
	LockRoleForUpdate(ctx context.Context, roleID gidx.PrefixedID) error
}

RoleService represents a service for managing roles.

type Storage

type Storage interface {
	RoleService
	TransactionManager

	HealthCheck(ctx context.Context) error
}

Storage defines the interface the engine exposes.

func New

func New(db DB, options ...Option) Storage

New creates a new storage engine using the provided underlying DB.

type TransactionManager

type TransactionManager interface {
	BeginContext(context.Context) (context.Context, error)
	CommitContext(context.Context) error
	RollbackContext(context.Context) error
}

TransactionManager manages the state of sql transactions within a context

Directories

Path Synopsis
Package teststore is a testing helper package which initializes a new crdb database and runs migrations returning a new store which may be used during testing.
Package teststore is a testing helper package which initializes a new crdb database and runs migrations returning a new store which may be used during testing.

Jump to

Keyboard shortcuts

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