database

package
v0.0.0-...-1703ca6 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2025 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	OrderByAsc  = OrderBy("ASC")
	OrderByDesc = OrderBy("DESC")
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Actor

type Actor struct {
	ID         uuid.UUID      `gorm:"column:id;primarykey"`
	ExternalId string         `gorm:"column:external_id;unique,index"`
	Email      string         `gorm:"column:email;index"`
	Admin      bool           `gorm:"column:admin"`
	SuperAdmin bool           `gorm:"column:super_admin"`
	CreatedAt  time.Time      `gorm:"column:created_at"`
	UpdatedAt  time.Time      `gorm:"column:updated_at"`
	DeletedAt  gorm.DeletedAt `gorm:"column:deleted_at;index"`
}

Actor is some entity taking action within the system.

func (*Actor) IsAdmin

func (a *Actor) IsAdmin() bool

IsAdmin is a helper to wrap the Admin attribute

func (*Actor) IsNormalActor

func (a *Actor) IsNormalActor() bool

IsNormalActor indicates that an actor is not an admin or superadmin

func (*Actor) IsSuperAdmin

func (a *Actor) IsSuperAdmin() bool

IsSuperAdmin is a helper to wrap the SuperAdmin attribute

func (*Actor) ToJwtActor

func (a *Actor) ToJwtActor() jwt.Actor

type ActorOrderByField

type ActorOrderByField string
const (
	ActorOrderByCreatedAt  ActorOrderByField = "created_at"
	ActorOrderByUpdatedAt  ActorOrderByField = "updated_at"
	ActorOrderByEmail      ActorOrderByField = "email"
	ActorOrderByExternalId ActorOrderByField = "external_id"
	ActorOrderByDeletedAt  ActorOrderByField = "deleted_at"
)

type Connection

type Connection struct {
	ID        uuid.UUID       `gorm:"column:id;primaryKey"`
	State     ConnectionState `gorm:"column:state"`
	CreatedAt time.Time       `gorm:"column:created_at"`
	UpdatedAt time.Time       `gorm:"column:updated_at"`
	DeletedAt gorm.DeletedAt  `gorm:"column:deleted_at;index"`
}

type ConnectionOrderByField

type ConnectionOrderByField string
const (
	ConnectionOrderByCreatedAt ConnectionOrderByField = "created_at"
)

type ConnectionState

type ConnectionState string
const (
	ConnectionStateCreated ConnectionState = "created"
	ConnectionStateReady   ConnectionState = "ready"
)

type DB

type DB interface {
	Migrate(ctx context.Context) error
	Ping(ctx context.Context) bool

	GetActor(ctx context.Context, id uuid.UUID) (*Actor, error)
	GetActorByExternalId(ctx context.Context, externalId string) (*Actor, error)
	CreateActor(ctx context.Context, actor *Actor) error
	UpsertActor(ctx context.Context, actor *jwt.Actor) (*Actor, error)
	ListActorsBuilder() ListActorsBuilder
	ListActorsFromCursor(ctx context.Context, cursor string) (ListActorsExecutor, error)

	GetConnection(ctx context.Context, id uuid.UUID) (*Connection, error)
	CreateConnection(ctx context.Context, c *Connection) error
	ListConnectionsBuilder() ListConnectionsBuilder
	ListConnectionsFromCursor(ctx context.Context, cursor string) (ListConnectionsExecutor, error)

	/*
	 * OAuth2 tokens
	 */
	GetOAuth2Token(ctx context.Context, connectionId uuid.UUID) (*OAuth2Token, error)
	InsertOAuth2Token(
		ctx context.Context,
		connectionId uuid.UUID,
		refreshedFrom *uuid.UUID,
		encryptedRefreshToken string,
		encryptedAccessToken string,
		accessTokenExpiresAt *time.Time,
		scopes string,
	) (*OAuth2Token, error)

	HasNonceBeenUsed(ctx context.Context, nonce uuid.UUID) (hasBeenUsed bool, err error)
	CheckNonceValidAndMarkUsed(ctx context.Context, nonce uuid.UUID, retainRecordUntil time.Time) (wasValid bool, err error)
}

func MustApplyBlankTestDbConfig

func MustApplyBlankTestDbConfig(testName string, cfg config.C) (config.C, DB)

MustApplyBlankTestDbConfig applies a test database configuration to the specified config root. The database is guaranteed to be blank and migrated. This method uses a temp file so that the database will be eventually cleaned up after the process exits. Note that the configuration in the root will be modified for the database and populated for the GlobalAESKey if it is not already populated.

Parameters: - testName: the name of the test. this can be a blank value but providing it make file names be identifiable by the test that generated them - root: the config to apply the database config to. This may be nil, in which case a new config is created. This method will overwrite the existing config.

Returns: - the config with information populated for the database. If a config was passed in, the same value is returned with data populated. - a database instance configured with the specified root. This database can be used directly, or if the root used again, it will connect to the same database instance.

func NewConnection

func NewConnection(c config.C) (DB, error)

NewConnection creates a new database connection from the specified configuration. The type of the database returned will be determined by the configuration.

func NewConnectionForRoot

func NewConnectionForRoot(root *config.Root) (DB, error)

NewConnectionForRoot creates a new database connection from the specified configuration. The type of the database returned will be determined by the configuration. Same as NewConnection.

func NewSqliteConnection

func NewSqliteConnection(dbConfig *config.DatabaseSqlite, secretKey config.KeyData) (DB, error)

NewSqliteConnection creates a new database connection to a SQLite database.

Parameters: - dbConfig: the configuration for the SQLite database - secretKey: the AES key used to secure cursors

type ListActorsBuilder

type ListActorsBuilder interface {
	ListActorsExecutor
	Limit(int32) ListActorsBuilder
	OrderBy(ActorOrderByField, OrderBy) ListActorsBuilder
	IncludeDeleted() ListActorsBuilder
}

type ListActorsExecutor

type ListActorsExecutor interface {
	FetchPage(context.Context) PageResult[Actor]
	Enumerate(context.Context, func(PageResult[Actor]) (keepGoing bool, err error)) error
}

type ListConnectionsExecutor

type ListConnectionsExecutor interface {
	FetchPage(context.Context) PageResult[Connection]
	Enumerate(context.Context, func(PageResult[Connection]) (keepGoing bool, err error)) error
}

type OAuth2Token

type OAuth2Token struct {
	ID                    uuid.UUID      `gorm:"column:id;primarykey"`
	ConnectionID          uuid.UUID      `gorm:"column:connection_id;not null"` // Foreign key to Connection
	RefreshedFromID       *uuid.UUID     `gorm:"column:refreshed_from_id"`
	EncryptedRefreshToken string         `gorm:"column:encrypted_refresh_token"`
	EncryptedAccessToken  string         `gorm:"column:encrypted_access_token"`
	AccessTokenExpiresAt  *time.Time     `gorm:"column:access_token_expires_at"`
	Scopes                string         `gorm:"column:scopes"`
	CreatedAt             time.Time      `gorm:"column:created_at"`
	DeletedAt             gorm.DeletedAt `gorm:"column:deleted_at;index"`
}

type OrderBy

type OrderBy string

func SplitOrderByParam

func SplitOrderByParam(p string) (field string, orderBy OrderBy, err error)

SplitOrderByParam is a helper function that can be used for query params to take the field plus direction as a single string value. e.g. "created_at DESC" This method will split the two parts and return the field and the order. If the direction is omitted, ASC will be assumed. If the param is empty or the param contains two parts but the order is invalid, this method will return an error

type PageResult

type PageResult[T any] struct {
	Results []T
	HasMore bool
	Cursor  string
	Error   error
}

type UsedNonce

type UsedNonce struct {
	ID          uuid.UUID `gorm:"column:id;primarykey"`
	RetainUntil time.Time `gorm:"column:retain_until;index"`
	CreatedAt   time.Time `gorm:"column:created_at"`
}

UsedNonce represents a onetime use value (UUID) that has already been used in the system and cannot be used again. When used outside the system, nonces should also use some sort of expiry mechanism such that when they are used there is a known time that they must be retained until so that the list of used nonces doesn't grow infinitely.

Jump to

Keyboard shortcuts

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