chatstore

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2026 License: AGPL-3.0 Imports: 16 Imported by: 0

README

ChatStore Open in Gitpod

Tests Status Go Report Card PkgGoDev

A Go library for storing and retrieving chat messages.

License

This project is dual-licensed under the following terms:

  • For non-commercial use, you may choose either the GNU Affero General Public License v3.0 (AGPLv3) or a separate commercial license (see below). You can find a copy of the AGPLv3 at: https://www.gnu.org/licenses/agpl-3.0.txt

  • For commercial use, a separate commercial license is required. Commercial licenses are available for various use cases. Please contact me via my contact page to obtain a commercial license.

Installation

go get github.com/dracory/chatstore

Usage Examples

Here are some examples demonstrating how to use the chatstore library.

Example 1: Creating a Chat Store

This example shows how to create a chat store.

// Initialize the database connection.
db, err := sql.Open("sqlite3", "./chatstore.db") // Replace with your database details
if err != nil {
    log.Fatalf("Failed to open database: %v", err)
}
defer db.Close()

// Create the store with database options.
store, err := chatstore.CreateStore(chatstore.NewStoreOptions{
    DB:                 db,
    TableChatName:      "chat_table",
    TableMessageName:   "message_table",
    AutomigrateEnabled: true,
})
if err != nil {
    log.Fatalf("Failed to create store: %v", err)
}
Example 2. Creating a Chat

This example shows how to create a chat.

chat := chatstore.NewChat().
		SetName("Test Chat").
		SetOwnerID(testUser_O1).
		SetStatus(CHAT_STATUS_ACTIVE)

err = store.ChatCreate(chat)
if err != nil {
    log.Fatalf("Failed to create chat: %v", err)
}

fmt.Println("Chat created successfully!")
Example 3: Creating a Chat Message

This example shows how to create and store a single chat message.

message := chatstore.NewMessage().
		SetChatID(chat.ID()).
		SetSenderID(user1.ID()).
		SetRecipientID(user2.ID()).
		SetText("Message 1")

err = store.MessageCreate(message)
if err != nil {
    log.Fatalf("Failed to create message: %v", err)
}

fmt.Println("Message stored successfully!")

Documentation

Index

Constants

View Source
const (
	COLUMN_CHAT_ID         = "chat_id"
	COLUMN_CREATED_AT      = "created_at"
	COLUMN_ID              = "id"
	COLUMN_MEMO            = "memo"
	COLUMN_METAS           = "metas"
	COLUMN_RECIPIENT_ID    = "recipient_id"
	COLUMN_SENDER_ID       = "sender_id"
	COLUMN_OWNER_ID        = "owner_id"
	COLUMN_SOFT_DELETED_AT = "soft_deleted_at"
	COLUMN_STATUS          = "status"
	COLUMN_TEXT            = "text"
	COLUMN_TITLE           = "title"
	COLUMN_UPDATED_AT      = "updated_at"
)

Column names for the chat and message tables

View Source
const (
	CHAT_STATUS_ACTIVE   = "active"
	CHAT_STATUS_INACTIVE = "inactive"
	CHAT_STATUS_DELETED  = "deleted"

	CHAT_SYSTEM_ID = "00000000000000000000000000000001"
)

Status constants

View Source
const (
	MESSAGE_STATUS_ACTIVE   = "active"
	MESSAGE_STATUS_INACTIVE = "inactive"
	MESSAGE_STATUS_DELETED  = "deleted"
)

Message status constants

View Source
const MAX_DATETIME = "9999-12-31 23:59:59"

MAX_DATETIME is a far-future datetime used as the default soft-delete sentinel.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChatInterface

type ChatInterface interface {
	IsSoftDeleted() bool

	ID() string
	SetID(id string) ChatInterface

	OwnerID() string
	SetOwnerID(id string) ChatInterface

	Status() string
	SetStatus(status string) ChatInterface

	Title() string
	SetTitle(title string) ChatInterface

	Memo() string
	SetMemo(memo string) ChatInterface

	Meta(key string) (string, error)
	SetMeta(key string, value string) error

	Metas() (map[string]string, error)
	SetMetas(metas map[string]string) error
	UpsertMetas(metas map[string]string) error

	CreatedAt() string
	CreatedAtCarbon() *carbon.Carbon
	SetCreatedAt(createdAt string) ChatInterface

	SoftDeletedAt() string
	SoftDeletedAtCarbon() *carbon.Carbon
	SetSoftDeletedAt(softDeletedAt string) ChatInterface

	UpdatedAt() string
	UpdatedAtCarbon() *carbon.Carbon
	SetUpdatedAt(updatedAt string) ChatInterface

	MarkAsNotDirty()
}

ChatInterface defines the interface for a chat record.

func NewChat

func NewChat() ChatInterface

NewChat creates a new chat.

func NewChatFromExistingData

func NewChatFromExistingData(data map[string]string) ChatInterface

NewChatFromExistingData creates a new chat from a raw column map (e.g. query results).

type ChatQueryInterface

type ChatQueryInterface interface {
	// Validation method
	Validate() error

	// Count related methods
	IsCountOnlySet() bool
	GetCountOnly() bool
	SetCountOnly(countOnly bool) ChatQueryInterface

	// Soft delete related query methods
	IsWithSoftDeletedSet() bool
	GetWithSoftDeleted() bool
	SetWithSoftDeleted(withSoftDeleted bool) ChatQueryInterface

	IsOnlySoftDeletedSet() bool
	GetOnlySoftDeleted() bool
	SetOnlySoftDeleted(onlySoftDeleted bool) ChatQueryInterface

	// Field query methods
	IsOwnerIDSet() bool
	GetOwnerID() string
	SetOwnerID(ownerID string) ChatQueryInterface

	IsCreatedAtGteSet() bool
	GetCreatedAtGte() string
	SetCreatedAtGte(createdAt string) ChatQueryInterface

	IsCreatedAtLteSet() bool
	GetCreatedAtLte() string
	SetCreatedAtLte(createdAt string) ChatQueryInterface

	IsIDSet() bool
	GetID() string
	SetID(id string) ChatQueryInterface

	IsIDInSet() bool
	GetIDIn() []string
	SetIDIn(ids []string) ChatQueryInterface

	IsLimitSet() bool
	GetLimit() int
	SetLimit(limit int) ChatQueryInterface

	IsOffsetSet() bool
	GetOffset() int
	SetOffset(offset int) ChatQueryInterface

	IsOrderBySet() bool
	GetOrderBy() string
	SetOrderBy(orderBy string) ChatQueryInterface

	IsOrderDirectionSet() bool
	GetOrderDirection() string
	SetOrderDirection(orderDirection string) ChatQueryInterface

	IsStatusSet() bool
	GetStatus() string
	SetStatus(status string) ChatQueryInterface

	IsStatusInSet() bool
	GetStatusIn() []string
	SetStatusIn(statuses []string) ChatQueryInterface

	IsUpdatedAtGteSet() bool
	GetUpdatedAtGte() string
	SetUpdatedAtGte(updatedAt string) ChatQueryInterface

	IsUpdatedAtLteSet() bool
	GetUpdatedAtLte() string
	SetUpdatedAtLte(updatedAt string) ChatQueryInterface
}

ChatQueryInterface defines the interface for querying chats

func ChatQuery

func ChatQuery() ChatQueryInterface

ChatQuery creates a new chat query

func NewChatQuery added in v1.1.0

func NewChatQuery() ChatQueryInterface

NewChatQuery creates a new chat query

type MessageInterface

type MessageInterface interface {
	IsSoftDeleted() bool

	ID() string
	SetID(id string) MessageInterface

	ChatID() string
	SetChatID(chatID string) MessageInterface

	SenderID() string
	SetSenderID(id string) MessageInterface

	RecipientID() string
	SetRecipientID(id string) MessageInterface

	Status() string
	SetStatus(status string) MessageInterface

	Memo() string
	SetMemo(memo string) MessageInterface

	Meta(key string) (string, error)
	SetMeta(key string, value string) error

	Metas() (map[string]string, error)
	SetMetas(metas map[string]string) error
	UpsertMetas(metas map[string]string) error

	Text() string
	SetText(text string) MessageInterface

	CreatedAt() string
	CreatedAtCarbon() *carbon.Carbon
	SetCreatedAt(createdAt string) MessageInterface

	SoftDeletedAt() string
	SoftDeletedAtCarbon() *carbon.Carbon
	SetSoftDeletedAt(softDeletedAt string) MessageInterface

	UpdatedAt() string
	UpdatedAtCarbon() *carbon.Carbon
	SetUpdatedAt(updatedAt string) MessageInterface

	MarkAsNotDirty()
}

MessageInterface defines the interface for a message record.

func NewMessage

func NewMessage() MessageInterface

NewMessage creates a new message.

func NewMessageFromExistingData

func NewMessageFromExistingData(data map[string]string) MessageInterface

NewMessageFromExistingData creates a new message from a raw column map (e.g. query results).

type MessageQueryInterface

type MessageQueryInterface interface {
	// Validation method
	Validate() error

	// Basic query methods
	IsCreatedAtGteSet() bool
	GetCreatedAtGte() string
	SetCreatedAtGte(createdAt string) MessageQueryInterface

	IsCreatedAtLteSet() bool
	GetCreatedAtLte() string
	SetCreatedAtLte(createdAt string) MessageQueryInterface

	IsIDSet() bool
	GetID() string
	SetID(id string) MessageQueryInterface

	IsIDInSet() bool
	GetIDIn() []string
	SetIDIn(ids []string) MessageQueryInterface

	IsIDNotInSet() bool
	GetIDNotIn() []string
	SetIDNotIn(ids []string) MessageQueryInterface

	IsLimitSet() bool
	GetLimit() int
	SetLimit(limit int) MessageQueryInterface

	IsChatIDSet() bool
	GetChatID() string
	SetChatID(chatID string) MessageQueryInterface

	IsChatIDInSet() bool
	GetChatIDIn() []string
	SetChatIDIn(chatIDs []string) MessageQueryInterface

	IsOffsetSet() bool
	GetOffset() int
	SetOffset(offset int) MessageQueryInterface

	IsOrderBySet() bool
	GetOrderBy() string
	SetOrderBy(orderBy string) MessageQueryInterface

	IsOrderDirectionSet() bool
	GetOrderDirection() string
	SetOrderDirection(orderDirection string) MessageQueryInterface

	IsRecipientIDSet() bool
	GetRecipientID() string
	SetRecipientID(recipientID string) MessageQueryInterface

	IsSenderIDSet() bool
	GetSenderID() string
	SetSenderID(senderID string) MessageQueryInterface

	IsStatusSet() bool
	GetStatus() string
	SetStatus(status string) MessageQueryInterface

	IsStatusInSet() bool
	GetStatusIn() []string
	SetStatusIn(statuses []string) MessageQueryInterface

	// Count related methods
	IsCountOnlySet() bool
	GetCountOnly() bool
	SetCountOnly(countOnly bool) MessageQueryInterface

	// Soft delete related query methods
	IsWithSoftDeletedSet() bool
	GetWithSoftDeleted() bool
	SetWithSoftDeleted(withSoftDeleted bool) MessageQueryInterface

	IsOnlySoftDeletedSet() bool
	GetOnlySoftDeleted() bool
	SetOnlySoftDeleted(onlySoftDeleted bool) MessageQueryInterface
}

MessageQueryInterface defines the interface for querying messages

func MessageQuery

func MessageQuery() MessageQueryInterface

MessageQuery creates a new message query

func NewMessageQuery added in v1.1.0

func NewMessageQuery() MessageQueryInterface

NewMessageQuery creates a new message query

type NewStoreOptions

type NewStoreOptions struct {
	TableChatName      string
	TableMessageName   string
	DB                 *sql.DB
	AutomigrateEnabled bool
	DebugEnabled       bool
	Logger             *slog.Logger
}

NewStoreOptions defines the options for creating a new chat store.

type StoreInterface

type StoreInterface interface {
	// GetChatTableName returns the chat table name
	GetChatTableName() string
	// SetChatTableName sets the chat table name
	SetChatTableName(tableName string)
	// GetMessageTableName returns the message table name
	GetMessageTableName() string
	// SetMessageTableName sets the message table name
	SetMessageTableName(tableName string)

	// MigrateDown drops the chat and message tables
	MigrateDown(ctx context.Context, tx ...*sql.Tx) error
	// MigrateUp creates the chat and message tables
	MigrateUp(ctx context.Context, tx ...*sql.Tx) error

	EnableDebug(enabled bool)

	ChatCount(options ChatQueryInterface) (int64, error)
	ChatCreate(chat ChatInterface) error
	ChatDelete(chat ChatInterface) error
	ChatDeleteByID(id string) error
	ChatFindByID(id string) (ChatInterface, error)
	ChatList(options ChatQueryInterface) ([]ChatInterface, error)
	ChatSoftDelete(chat ChatInterface) error
	ChatSoftDeleteByID(id string) error
	ChatUpdate(chat ChatInterface) error

	MessageCount(options MessageQueryInterface) (int64, error)
	MessageCreate(message MessageInterface) error
	MessageDelete(message MessageInterface) error
	MessageDeleteByID(id string) error
	MessageFindByID(id string) (MessageInterface, error)
	MessageList(options MessageQueryInterface) ([]MessageInterface, error)
	MessageSoftDelete(message MessageInterface) error
	MessageSoftDeleteByID(id string) error
	MessageUpdate(message MessageInterface) error
}

func NewStore

func NewStore(opts NewStoreOptions) (StoreInterface, error)

NewStore creates a new chat store.

Jump to

Keyboard shortcuts

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