models

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseModel

type BaseModel struct {
	DB                    *sqlx.DB
	SupportsJSONFunctions bool
}

type DBGetFilter

type DBGetFilter struct {
	// ID specifies a unique job identifier to filter by.
	// If provided, the query typically returns a single match.
	ID string

	// Keyword specifies a search term for partial matching.
	// This is usually applied against the job name or ID (e.g., "backup" matches "db-backup-01").
	Keyword string

	// Limit restricts the maximum number of records returned by the query.
	// A value of 0 typically implies no limit or uses the system default.
	Limit int

	// SortDesc determines the sorting order of the results based on the timestamp.
	// If true, results are ordered descending (newest first). If false, they are ordered ascending (oldest first).
	SortDesc bool

	// Offset specifies the number of records to skip before starting to return results.
	// Used in conjunction with Limit for pagination.
	Offset int
}

DBGetFilter defines the criteria used to query and filter job records from the database. It is typically passed to repository functions to refine the result set.

type JobFilter

type JobFilter struct {
	// ActiveOnly filters results to include only active jobs.
	// Cannot be combined with FinishOnly.
	ActiveOnly bool

	// FinishOnly filters results to include only finished jobs whether job is success or failed.
	// Cannot be combined with ActiveOnly.
	FinishOnly bool

	// MetadataFilter allows filtering jobs based on their metadata.
	// Keys are metadata field names, and values are the desired values.
	MetadataFilter map[string]string

	// GeneralKeywordSearch applies to JobFilter.ID OR JobFilter.Keyword
	GeneralKeywordSearch string

	// HideCommand prevents the command from being exposed in the job response.
	HideCommand bool `json:"hide_command,omitempty"`

	DBGetFilter
}

type JobModel

type JobModel struct {
	ID        string    `db:"id"`
	JobName   string    `db:"job_name"`
	Command   string    `db:"command"` // JSON string representation of []string
	Status    int       `db:"status"`  // Integer cast from JobStatusEnum
	ExitCode  int       `db:"exit_code"`
	Metadata  string    `db:"metadata"` // JSON string representation of PayloadRegularMetadata
	PID       int       `db:"pid"`
	CreatedAt time.Time `db:"created_at"` // Generated automatically (current_timestamp)
	UpdatedAt time.Time `db:"updated_at"` // Generated automatically (TRIGGER jobs_update_updated_at)
	BaseModel
}

JobModel represents a single row in the 'jobs' database table. Complex fields (such as slices or maps) are serialized and stored as JSON strings.

func NewJobModel

func NewJobModel(db *sqlx.DB, job payload.JobResponse) (*JobModel, error)

NewJobModel creates a database-ready JobModel from a JobResponse object. It serializes complex fields into JSON strings to prepare for database insertion.

func (*JobModel) BulkToPayload

func (j *JobModel) BulkToPayload(jm []*JobModel) (p []*payload.JobResponse, err error)

BulkToPayload converts the bulk of raw database model back into a bulk of JobResponse struct.

func (*JobModel) Count

func (j *JobModel) Count(filter *JobFilter) (int, error)

Get fetch the job data from the table and return it as a int of JobModel size.

func (*JobModel) Delete

func (j *JobModel) Delete()

func (*JobModel) Get

func (j *JobModel) Get(filter *JobFilter) ([]*JobModel, error)

Get fetch the job data from the table and return it as a array of JobModel.

func (*JobModel) MarkJobFinished

func (j *JobModel) MarkJobFinished() error

MarkFinished updates only the status and exit code of a job.

To use this function, the required property is `JobModel.ID` and `JobModel.ExitCode`.

func (*JobModel) Save

func (j *JobModel) Save() error

Save create new data in the table

func (*JobModel) ToPayload

func (j *JobModel) ToPayload() (*payload.JobResponse, error)

ToPayload converts the raw database model back into a JobResponse struct. It handles the deserialization of JSON fields to ensure the data is ready for the CLI.

func (*JobModel) Update

func (j *JobModel) Update() error

Update persists the current state of the JobModel to the database.

NOTE: Ensure that 'Command', 'PID', and 'Metadata' fields (strings) are updated with the latest JSON content before calling this method.

func (*JobModel) WaitJob

func (j *JobModel) WaitJob(ctx context.Context, cancel context.CancelFunc, filter *JobFilter) (*JobModel, error)

WaitJob will waiting the filter until it finished.

Jump to

Keyboard shortcuts

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