database

package
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextWithTransaction added in v0.4.0

func ContextWithTransaction(ctx context.Context, tx *gorm.DB) context.Context

ContextWithTransaction creates a context containing a transaction

func InitDefaultTenant added in v0.4.0

func InitDefaultTenant(db *gorm.DB) error

InitDefaultTenant initializes the default tenant if it doesn't exist

func TransactionFromContext added in v0.4.0

func TransactionFromContext(ctx context.Context) *gorm.DB

TransactionFromContext extracts a transaction from the context

Types

type Database

type Database interface {
	// Close closes the database connection.
	Close() error

	// SaveMessage saves a message to the database.
	SaveMessage(ctx context.Context, message *Message) error
	// GetMessages gets messages for a specific session.
	GetMessages(ctx context.Context, sessionID string) ([]*Message, error)
	// GetMessagesWithPagination gets messages for a specific session with pagination.
	GetMessagesWithPagination(ctx context.Context, sessionID string, page, pageSize int) ([]*Message, error)
	// CreateSession creates a new session with the given sessionId.
	CreateSession(ctx context.Context, sessionId string) error
	// SessionExists checks if a session exists.
	SessionExists(ctx context.Context, sessionID string) (bool, error)
	// GetSessions gets all chat sessions with their latest message.
	GetSessions(ctx context.Context) ([]*Session, error)
	// UpdateSessionTitle updates the title of a session.
	UpdateSessionTitle(ctx context.Context, sessionID string, title string) error
	// DeleteSession deletes a session by ID.
	DeleteSession(ctx context.Context, sessionID string) error

	CreateUser(ctx context.Context, user *User) error
	GetUserByUsername(ctx context.Context, username string) (*User, error)
	UpdateUser(ctx context.Context, user *User) error
	DeleteUser(ctx context.Context, id uint) error
	ListUsers(ctx context.Context) ([]*User, error)

	CreateTenant(ctx context.Context, tenant *Tenant) error
	GetTenantByName(ctx context.Context, name string) (*Tenant, error)
	GetTenantByID(ctx context.Context, id uint) (*Tenant, error)
	UpdateTenant(ctx context.Context, tenant *Tenant) error
	DeleteTenant(ctx context.Context, id uint) error
	ListTenants(ctx context.Context) ([]*Tenant, error)

	AddUserToTenant(ctx context.Context, userID, tenantID uint) error
	RemoveUserFromTenant(ctx context.Context, userID, tenantID uint) error
	GetUserTenants(ctx context.Context, userID uint) ([]*Tenant, error)
	GetTenantUsers(ctx context.Context, tenantID uint) ([]*User, error)
	DeleteUserTenants(ctx context.Context, userID uint) error

	Transaction(ctx context.Context, fn func(ctx context.Context) error) error
}

Database defines the methods for database operations.

func NewDatabase added in v0.2.6

func NewDatabase(cfg *config.DatabaseConfig) (Database, error)

NewDatabase creates a new database based on configuration

func NewMySQL added in v0.2.6

func NewMySQL(cfg *config.DatabaseConfig) (Database, error)

NewMySQL creates a new MySQL instance

func NewPostgres added in v0.2.6

func NewPostgres(cfg *config.DatabaseConfig) (Database, error)

NewPostgres creates a new Postgres instance

func NewSQLite added in v0.2.6

func NewSQLite(cfg *config.DatabaseConfig) (Database, error)

NewSQLite creates a new SQLite instance

type Message

type Message struct {
	ID         string    `json:"id"`
	SessionID  string    `json:"session_id"`
	Content    string    `json:"content"`
	Sender     string    `json:"sender"`
	Timestamp  time.Time `json:"timestamp"`
	ToolCalls  string    `json:"toolCalls,omitempty"`
	ToolResult string    `json:"toolResult,omitempty"`
}

Message represents a chat message

type MySQL added in v0.2.6

type MySQL struct {
	// contains filtered or unexported fields
}

MySQL implements the Database interface using MySQL

func (*MySQL) AddUserToTenant added in v0.4.0

func (db *MySQL) AddUserToTenant(ctx context.Context, userID, tenantID uint) error

AddUserToTenant adds a user to a tenant

func (*MySQL) Close added in v0.2.6

func (db *MySQL) Close() error

Close closes the database connection

func (*MySQL) CreateSession added in v0.2.6

func (db *MySQL) CreateSession(ctx context.Context, sessionId string) error

func (*MySQL) CreateTenant added in v0.4.0

func (db *MySQL) CreateTenant(ctx context.Context, tenant *Tenant) error

CreateTenant creates a new tenant

func (*MySQL) CreateUser added in v0.3.1

func (db *MySQL) CreateUser(ctx context.Context, user *User) error

CreateUser creates a new user

func (*MySQL) DeleteSession added in v0.5.1

func (db *MySQL) DeleteSession(ctx context.Context, sessionID string) error

DeleteSession deletes a session by ID

func (*MySQL) DeleteTenant added in v0.4.0

func (db *MySQL) DeleteTenant(ctx context.Context, id uint) error

DeleteTenant deletes a tenant by ID

func (*MySQL) DeleteUser added in v0.3.1

func (db *MySQL) DeleteUser(ctx context.Context, id uint) error

DeleteUser deletes a user by ID

func (*MySQL) DeleteUserTenants added in v0.4.0

func (db *MySQL) DeleteUserTenants(ctx context.Context, userID uint) error

DeleteUserTenants deletes all tenant associations for a user

func (*MySQL) GetMessages added in v0.2.6

func (db *MySQL) GetMessages(ctx context.Context, sessionID string) ([]*Message, error)

func (*MySQL) GetMessagesWithPagination added in v0.2.6

func (db *MySQL) GetMessagesWithPagination(ctx context.Context, sessionID string, page, pageSize int) ([]*Message, error)

func (*MySQL) GetSessions added in v0.2.6

func (db *MySQL) GetSessions(ctx context.Context) ([]*Session, error)

func (*MySQL) GetTenantByID added in v0.4.0

func (db *MySQL) GetTenantByID(ctx context.Context, id uint) (*Tenant, error)

GetTenantByID retrieves a tenant by ID

func (*MySQL) GetTenantByName added in v0.4.0

func (db *MySQL) GetTenantByName(ctx context.Context, name string) (*Tenant, error)

GetTenantByName retrieves a tenant by name

func (*MySQL) GetTenantUsers added in v0.4.0

func (db *MySQL) GetTenantUsers(ctx context.Context, tenantID uint) ([]*User, error)

GetTenantUsers gets all users for a tenant

func (*MySQL) GetUserByUsername added in v0.3.1

func (db *MySQL) GetUserByUsername(ctx context.Context, username string) (*User, error)

GetUserByUsername retrieves a user by username

func (*MySQL) GetUserTenants added in v0.4.0

func (db *MySQL) GetUserTenants(ctx context.Context, userID uint) ([]*Tenant, error)

GetUserTenants gets all tenants for a user

func (*MySQL) ListTenants added in v0.4.0

func (db *MySQL) ListTenants(ctx context.Context) ([]*Tenant, error)

ListTenants retrieves all tenants

func (*MySQL) ListUsers added in v0.3.2

func (db *MySQL) ListUsers(ctx context.Context) ([]*User, error)

ListUsers retrieves all users

func (*MySQL) RemoveUserFromTenant added in v0.4.0

func (db *MySQL) RemoveUserFromTenant(ctx context.Context, userID, tenantID uint) error

RemoveUserFromTenant removes a user from a tenant

func (*MySQL) SaveMessage added in v0.2.6

func (db *MySQL) SaveMessage(ctx context.Context, message *Message) error

func (*MySQL) SessionExists added in v0.2.6

func (db *MySQL) SessionExists(ctx context.Context, sessionID string) (bool, error)

func (*MySQL) Transaction added in v0.4.0

func (db *MySQL) Transaction(ctx context.Context, fn func(ctx context.Context) error) error

Transaction implements Database.Transaction

func (*MySQL) UpdateSessionTitle added in v0.2.6

func (db *MySQL) UpdateSessionTitle(ctx context.Context, sessionID string, title string) error

func (*MySQL) UpdateTenant added in v0.4.0

func (db *MySQL) UpdateTenant(ctx context.Context, tenant *Tenant) error

UpdateTenant updates an existing tenant

func (*MySQL) UpdateUser added in v0.3.1

func (db *MySQL) UpdateUser(ctx context.Context, user *User) error

UpdateUser updates an existing user

type Postgres added in v0.2.6

type Postgres struct {
	// contains filtered or unexported fields
}

Postgres implements the Database interface using PostgreSQL

func (*Postgres) AddUserToTenant added in v0.4.0

func (db *Postgres) AddUserToTenant(ctx context.Context, userID, tenantID uint) error

AddUserToTenant adds a user to a tenant

func (*Postgres) Close added in v0.2.6

func (db *Postgres) Close() error

Close closes the database connection

func (*Postgres) CreateSession added in v0.2.6

func (db *Postgres) CreateSession(ctx context.Context, sessionId string) error

CreateSession creates a new session with the given sessionId

func (*Postgres) CreateTenant added in v0.4.0

func (db *Postgres) CreateTenant(ctx context.Context, tenant *Tenant) error

CreateTenant creates a new tenant

func (*Postgres) CreateUser added in v0.3.1

func (db *Postgres) CreateUser(ctx context.Context, user *User) error

CreateUser creates a new user

func (*Postgres) DeleteSession added in v0.5.1

func (db *Postgres) DeleteSession(ctx context.Context, sessionID string) error

DeleteSession deletes a session by ID

func (*Postgres) DeleteTenant added in v0.4.0

func (db *Postgres) DeleteTenant(ctx context.Context, id uint) error

DeleteTenant deletes a tenant by ID

func (*Postgres) DeleteUser added in v0.3.1

func (db *Postgres) DeleteUser(ctx context.Context, id uint) error

DeleteUser deletes a user by ID

func (*Postgres) DeleteUserTenants added in v0.4.0

func (db *Postgres) DeleteUserTenants(ctx context.Context, userID uint) error

DeleteUserTenants deletes all tenant associations for a user

func (*Postgres) GetMessages added in v0.2.6

func (db *Postgres) GetMessages(ctx context.Context, sessionID string) ([]*Message, error)

GetMessages retrieves all messages for a session

func (*Postgres) GetMessagesWithPagination added in v0.2.6

func (db *Postgres) GetMessagesWithPagination(ctx context.Context, sessionID string, page, pageSize int) ([]*Message, error)

GetMessagesWithPagination retrieves messages for a specific session with pagination

func (*Postgres) GetSessions added in v0.2.6

func (db *Postgres) GetSessions(ctx context.Context) ([]*Session, error)

GetSessions retrieves all chat sessions with their latest message

func (*Postgres) GetTenantByID added in v0.4.0

func (db *Postgres) GetTenantByID(ctx context.Context, id uint) (*Tenant, error)

GetTenantByID retrieves a tenant by ID

func (*Postgres) GetTenantByName added in v0.4.0

func (db *Postgres) GetTenantByName(ctx context.Context, name string) (*Tenant, error)

GetTenantByName retrieves a tenant by name

func (*Postgres) GetTenantUsers added in v0.4.0

func (db *Postgres) GetTenantUsers(ctx context.Context, tenantID uint) ([]*User, error)

GetTenantUsers gets all users for a tenant

func (*Postgres) GetUserByUsername added in v0.3.1

func (db *Postgres) GetUserByUsername(ctx context.Context, username string) (*User, error)

GetUserByUsername retrieves a user by username

func (*Postgres) GetUserTenants added in v0.4.0

func (db *Postgres) GetUserTenants(ctx context.Context, userID uint) ([]*Tenant, error)

GetUserTenants gets all tenants for a user

func (*Postgres) ListTenants added in v0.4.0

func (db *Postgres) ListTenants(ctx context.Context) ([]*Tenant, error)

ListTenants retrieves all tenants

func (*Postgres) ListUsers added in v0.3.2

func (db *Postgres) ListUsers(ctx context.Context) ([]*User, error)

ListUsers retrieves all users

func (*Postgres) RemoveUserFromTenant added in v0.4.0

func (db *Postgres) RemoveUserFromTenant(ctx context.Context, userID, tenantID uint) error

RemoveUserFromTenant removes a user from a tenant

func (*Postgres) SaveMessage added in v0.2.6

func (db *Postgres) SaveMessage(ctx context.Context, message *Message) error

SaveMessage saves a message to the database

func (*Postgres) SessionExists added in v0.2.6

func (db *Postgres) SessionExists(ctx context.Context, sessionID string) (bool, error)

SessionExists checks if a session exists

func (*Postgres) Transaction added in v0.4.0

func (db *Postgres) Transaction(ctx context.Context, fn func(ctx context.Context) error) error

Transaction implements Database.Transaction

func (*Postgres) UpdateSessionTitle added in v0.2.6

func (db *Postgres) UpdateSessionTitle(ctx context.Context, sessionID string, title string) error

UpdateSessionTitle updates the title of a session

func (*Postgres) UpdateTenant added in v0.4.0

func (db *Postgres) UpdateTenant(ctx context.Context, tenant *Tenant) error

UpdateTenant updates an existing tenant

func (*Postgres) UpdateUser added in v0.3.1

func (db *Postgres) UpdateUser(ctx context.Context, user *User) error

UpdateUser updates an existing user

type SQLite added in v0.2.6

type SQLite struct {
	// contains filtered or unexported fields
}

SQLite implements the Database interface using SQLite

func (*SQLite) AddUserToTenant added in v0.4.0

func (db *SQLite) AddUserToTenant(ctx context.Context, userID, tenantID uint) error

AddUserToTenant adds a user to a tenant

func (*SQLite) Close added in v0.2.6

func (db *SQLite) Close() error

Close closes the database connection

func (*SQLite) CreateSession added in v0.2.6

func (db *SQLite) CreateSession(ctx context.Context, sessionId string) error

func (*SQLite) CreateTenant added in v0.4.0

func (db *SQLite) CreateTenant(ctx context.Context, tenant *Tenant) error

CreateTenant creates a new tenant

func (*SQLite) CreateUser added in v0.3.1

func (db *SQLite) CreateUser(ctx context.Context, user *User) error

CreateUser creates a new user

func (*SQLite) DeleteSession added in v0.5.1

func (db *SQLite) DeleteSession(ctx context.Context, sessionID string) error

DeleteSession deletes a session by ID

func (*SQLite) DeleteTenant added in v0.4.0

func (db *SQLite) DeleteTenant(ctx context.Context, id uint) error

DeleteTenant deletes a tenant by ID

func (*SQLite) DeleteUser added in v0.3.1

func (db *SQLite) DeleteUser(ctx context.Context, id uint) error

DeleteUser deletes a user by ID

func (*SQLite) DeleteUserTenants added in v0.4.0

func (db *SQLite) DeleteUserTenants(ctx context.Context, userID uint) error

DeleteUserTenants deletes all tenant associations for a user

func (*SQLite) GetMessages added in v0.2.6

func (db *SQLite) GetMessages(ctx context.Context, sessionID string) ([]*Message, error)

func (*SQLite) GetMessagesWithPagination added in v0.2.6

func (db *SQLite) GetMessagesWithPagination(ctx context.Context, sessionID string, page, pageSize int) ([]*Message, error)

func (*SQLite) GetSessions added in v0.2.6

func (db *SQLite) GetSessions(ctx context.Context) ([]*Session, error)

func (*SQLite) GetTenantByID added in v0.4.0

func (db *SQLite) GetTenantByID(ctx context.Context, id uint) (*Tenant, error)

GetTenantByID retrieves a tenant by ID

func (*SQLite) GetTenantByName added in v0.4.0

func (db *SQLite) GetTenantByName(ctx context.Context, name string) (*Tenant, error)

GetTenantByName retrieves a tenant by name

func (*SQLite) GetTenantUsers added in v0.4.0

func (db *SQLite) GetTenantUsers(ctx context.Context, tenantID uint) ([]*User, error)

GetTenantUsers gets all users for a tenant

func (*SQLite) GetUserByUsername added in v0.3.1

func (db *SQLite) GetUserByUsername(ctx context.Context, username string) (*User, error)

GetUserByUsername retrieves a user by username

func (*SQLite) GetUserTenants added in v0.4.0

func (db *SQLite) GetUserTenants(ctx context.Context, userID uint) ([]*Tenant, error)

GetUserTenants gets all tenants for a user

func (*SQLite) ListTenants added in v0.4.0

func (db *SQLite) ListTenants(ctx context.Context) ([]*Tenant, error)

ListTenants retrieves all tenants

func (*SQLite) ListUsers added in v0.3.2

func (db *SQLite) ListUsers(ctx context.Context) ([]*User, error)

ListUsers retrieves all users

func (*SQLite) RemoveUserFromTenant added in v0.4.0

func (db *SQLite) RemoveUserFromTenant(ctx context.Context, userID, tenantID uint) error

RemoveUserFromTenant removes a user from a tenant

func (*SQLite) SaveMessage added in v0.2.6

func (db *SQLite) SaveMessage(ctx context.Context, message *Message) error

func (*SQLite) SessionExists added in v0.2.6

func (db *SQLite) SessionExists(ctx context.Context, sessionID string) (bool, error)

func (*SQLite) Transaction added in v0.4.0

func (db *SQLite) Transaction(ctx context.Context, fn func(ctx context.Context) error) error

Transaction implements Database.Transaction

func (*SQLite) UpdateSessionTitle added in v0.2.6

func (db *SQLite) UpdateSessionTitle(ctx context.Context, sessionID string, title string) error

func (*SQLite) UpdateTenant added in v0.4.0

func (db *SQLite) UpdateTenant(ctx context.Context, tenant *Tenant) error

UpdateTenant updates an existing tenant

func (*SQLite) UpdateUser added in v0.3.1

func (db *SQLite) UpdateUser(ctx context.Context, user *User) error

UpdateUser updates an existing user

type Session

type Session struct {
	ID        string    `json:"id"`
	CreatedAt time.Time `json:"createdAt"`
	Title     string    `json:"title"`
}

Session represents a chat session

type Tenant added in v0.4.0

type Tenant struct {
	ID          uint      `json:"id" gorm:"primaryKey;autoIncrement"`
	Name        string    `json:"name" gorm:"type:varchar(50);uniqueIndex"`
	Prefix      string    `json:"prefix" gorm:"type:varchar(50);uniqueIndex"`
	Description string    `json:"description" gorm:"type:varchar(255)"`
	IsActive    bool      `json:"isActive" gorm:"not null;default:true"`
	CreatedAt   time.Time `json:"createdAt"`
	UpdatedAt   time.Time `json:"updatedAt"`
}

Tenant represents a tenant in the system

type User added in v0.3.1

type User struct {
	ID        uint      `json:"id" gorm:"primaryKey;autoIncrement"`
	Username  string    `json:"username" gorm:"type:varchar(50);uniqueIndex"`
	Password  string    `json:"-" gorm:"not null"` // Password is not exposed in JSON
	Role      UserRole  `json:"role" gorm:"not null;default:'normal'"`
	IsActive  bool      `json:"isActive" gorm:"not null;default:true"`
	CreatedAt time.Time `json:"createdAt"`
	UpdatedAt time.Time `json:"updatedAt"`
}

User represents an admin user

type UserRole added in v0.3.2

type UserRole string

UserRole represents the role of a user

const (
	RoleAdmin  UserRole = "admin"
	RoleNormal UserRole = "normal"
)

type UserTenant added in v0.4.0

type UserTenant struct {
	ID        uint      `json:"id" gorm:"primaryKey;autoIncrement"`
	UserID    uint      `json:"userId" gorm:"index:idx_user_tenant,unique;not null"`
	TenantID  uint      `json:"tenantId" gorm:"index:idx_user_tenant,unique;not null"`
	CreatedAt time.Time `json:"createdAt"`
	UpdatedAt time.Time `json:"updatedAt"`
}

UserTenant represents the relationship between a user and a tenant

Jump to

Keyboard shortcuts

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