db

package
v0.0.0-...-caa39d1 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MessageStatusPending MessageStatus = "pending"
	MessageStatusSending MessageStatus = "sending"
	MessageStatusSent    MessageStatus = "sent"
	MessageStatusFailed  MessageStatus = "failed"
	MaxMessageLength     int           = 160
)

Variables

View Source
var (
	ErrMessageTooLong = errors.New("message content exceeds maximum length")
)

Functions

func Connect

func Connect(dsn string) (*bun.DB, error)

Connect returns a DB connection.

func CreateMessage

func CreateMessage(ctx context.Context, db bun.IDB, message *Message) error

CreateMessage inserts a new message into the database

func GetTotalSentMessagesCount

func GetTotalSentMessagesCount(ctx context.Context, db bun.IDB) (int, error)

GetTotalSentMessagesCount returns the total count of sent messages

func UpdateMessageStatus

func UpdateMessageStatus(ctx context.Context, db bun.IDB, messageID int64, status MessageStatus, sentAt *time.Time, webhookMessageID *string, webhookResponse *string) error

UpdateMessageStatus updates the status of a message and optionally sets sent_at and message_id

Types

type Message

type Message struct {
	bun.BaseModel `bun:"table:messages"`

	ID              int64         `bun:"id,pk,autoincrement" json:"id"`
	To              string        `bun:"to,notnull" json:"to"`
	Content         string        `bun:"content,notnull" json:"content"`
	Status          MessageStatus `bun:"status,notnull,default:'pending'" json:"status"`
	SentAt          *time.Time    `bun:"sent_at,nullzero" json:"sent_at,omitempty"`
	MessageID       *string       `bun:"message_id,nullzero" json:"message_id,omitempty"`
	WebhookResponse *string       `bun:"webhook_response,type:jsonb,nullzero" json:"webhook_response,omitempty"`
	CreatedAt       time.Time     `bun:"created_at,notnull,default:current_timestamp" json:"created_at"`
	UpdatedAt       time.Time     `bun:"updated_at,notnull,default:current_timestamp" json:"updated_at"`
}

func ClaimNextMessage

func ClaimNextMessage(ctx context.Context, db bun.IDB) (*Message, error)

ClaimNextMessage atomically claims the next available message for processing

func GetMessageByID

func GetMessageByID(ctx context.Context, db bun.IDB, id int64) (*Message, error)

GetMessageByID retrieves a single message by its ID

func GetSentMessages

func GetSentMessages(ctx context.Context, db bun.IDB, limit, offset int) ([]*Message, error)

GetSentMessages retrieves all sent messages with pagination

type MessageStatus

type MessageStatus string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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