db

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: May 22, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMissingFields    = errors.New("missing required fields")
	ErrConstraintUnique = errors.New("unique constraint violation")
	ErrUserNotFound     = errors.New("user not found") // Added for clarity
)

Functions

func TimeFormat

func TimeFormat(tt time.Time) string

TimeFormat converts a time.Time to RFC3339 string in UTC. This should be used when sending time values to SQLite since it doesn't have a native datetime type. All timestamps in the database should use this format. Example: "2024-03-11T15:04:05Z"

func TimeParse

func TimeParse(s string) (time.Time, error)

TimeParse parses a RFC3339 string into a time.Time. This should be used when reading timestamps from SQLite to convert them back to time.Time values. Returns an error if the input string is not in RFC3339 format.

Types

type DbApp

type DbApp interface {
	DbAuth
	DbQueue
	DbConfig
}

type DbAuth

type DbAuth interface {
	GetUserByEmail(email string) (*User, error)
	GetUserById(id string) (*User, error)
	CreateUserWithPassword(user User) (*User, error)
	CreateUserWithOauth2(user User) (*User, error)
	UpdateVerified(email string) (*User, error)
	UpdatePassword(userId string, newPassword string) error
	UpdateEmail(userId string, newEmail string) error
}

DbAuth defines database operations related to users and authentication.

type DbConfig

type DbConfig interface {
	// GetConfig retrieves encrypted config content and format by scope and generation
	// generation 0 = latest, 1 = previous, etc.
	GetConfig(scope string, generation int) ([]byte, string, error)
	// InsertConfig inserts a new configuration content blob for a given scope.
	InsertConfig(scope string, contentData []byte, format string, description string) error
	// Path returns the filesystem path of the SQLite database file.
	Path() string
}

DbConfig defines database operations related to configuration.

type DbLog

type DbLog interface {
	// InsertBatch inserts a batch of log entries into the database.
	InsertBatch(batch []Log) error
	// Ping verifies the connection to the database is alive and the schema for the given table is correct.
	Ping(tableName string) error
	// Close closes the underlying database connection or pool.
	Close() error
}

LogDB defines the interface for database operations related to logs.

type DbQueue

type DbQueue interface {
	InsertJob(job Job) error
	Claim(limit int) ([]*Job, error)
	MarkCompleted(jobID int64) error
	MarkFailed(jobID int64, errMsg string) error
	MarkRecurrentCompleted(completedJobID int64, newJob Job) error
}

DbQueue defines database operations related to the job queue.

type DbQueueAdmin

type DbQueueAdmin interface {
	ListJobs(limit int) ([]*Job, error)
	DeleteJob(jobID int64) error
}

DbQueueAdmin defines database operations for administering the job queue.

type Job

type Job struct {
	ID           int64           `json:"id"`
	JobType      string          `json:"job_type"`
	Payload      json.RawMessage `json:"payload"`       // Unique payload part
	PayloadExtra json.RawMessage `json:"payload_extra"` // Non-unique payload part
	Status       string          `json:"status"`
	Attempts     int             `json:"attempts"`
	MaxAttempts  int             `json:"max_attempts"`
	CreatedAt    time.Time       `json:"created_at"`
	UpdatedAt    time.Time       `json:"updated_at"`
	ScheduledFor time.Time       `json:"scheduled_for"`
	LockedBy     string          `json:"-"` // deprecated, marked as ignored in JSON
	LockedAt     time.Time       `json:"locked_at,omitempty"`
	CompletedAt  time.Time       `json:"completed_at,omitempty"`
	LastError    string          `json:"last_error,omitempty"`
	Recurrent    bool            `json:"recurrent"`
	Interval     time.Duration   `json:"interval"` // Go duration
}

Job represents a job in the processing queue

type Log

type Log struct {
	Level    int64  `json:"level"`    // Log level
	Message  string `json:"message"`  // Log message
	JsonData string `json:"jsonData"` // Pre-marshalled JSON for the 'data' field
	Created  string `json:"created"`  // Pre-formatted time string for 'created'
}

Log represents a log entry ready for database storage

type PayloadEmailVerification

type PayloadEmailVerification struct {
	Email string `json:"email"`
	// CooldownBucket is the time bucket number calculated from the current time divided by the cooldown duration.
	// This provides a basic rate limiting mechanism where only one email verification request is allowed per time bucket.
	// The bucket number is calculated as: floor(current Unix time / cooldown duration in seconds)
	CooldownBucket int `json:"cooldown_bucket"`
}

PayloadEmailVerification contains the email verification details

type User

type User struct {
	ID    string
	Email string
	Name  string
	// Non empty password means password authentication is active
	// Password can be empty for passwordless methods like oauth2, otp over email...
	Password string
	Avatar   string
	Created  time.Time
	Updated  time.Time
	Verified bool
	//deprecated
	// ExternalAuth identifies authentication methods (password authentication excluded)
	// Example of methods are "oauth2", "otp".
	// the structure is a comma separated string
	// in future a colon separated string (not implmented) could be used for mfa
	//
	// The only reason for this field is the use case of a user having password and oauth2 login with the same email,
	// if the user request a change of email, and after that tries to  log with the
	// the old email a new user is created which may surprise the user.
	// having this field, we now it has two auth methods and we can remember the user before changing email.
	//ExternalAuth    string
	Oauth2          bool
	EmailVisibility bool
}

User represents a user from the database. Timestamps (Created and Updated) use RFC3339 format in UTC timezone. Example: "2024-03-07T15:04:05Z"

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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