database

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextWithTransaction

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

ContextWithTransaction creates a context containing a transaction

func InitDefaultTenant

func InitDefaultTenant(db *gorm.DB) error

InitDefaultTenant initializes the default tenant if it doesn't exist

func TransactionFromContext

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

	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

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

NewDatabase creates a new database based on configuration

func NewMySQL

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

NewMySQL creates a new MySQL instance

func NewPostgres

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

NewPostgres creates a new Postgres instance

func NewSQLite

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

NewSQLite creates a new SQLite instance

type MySQL

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

MySQL implements the Database interface using MySQL

func (*MySQL) AddUserToTenant

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

AddUserToTenant adds a user to a tenant

func (*MySQL) Close

func (db *MySQL) Close() error

Close closes the database connection

func (*MySQL) CreateTenant

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

CreateTenant creates a new tenant

func (*MySQL) CreateUser

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

CreateUser creates a new user

func (*MySQL) DeleteTenant

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

DeleteTenant deletes a tenant by ID

func (*MySQL) DeleteUser

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

DeleteUser deletes a user by ID

func (*MySQL) DeleteUserTenants

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

DeleteUserTenants deletes all tenant associations for a user

func (*MySQL) GetTenantByID

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

GetTenantByID retrieves a tenant by ID

func (*MySQL) GetTenantByName

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

GetTenantByName retrieves a tenant by name

func (*MySQL) GetTenantUsers

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

GetTenantUsers gets all users for a tenant

func (*MySQL) GetUserByUsername

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

GetUserByUsername retrieves a user by username

func (*MySQL) GetUserTenants

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

GetUserTenants gets all tenants for a user

func (*MySQL) ListTenants

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

ListTenants retrieves all tenants

func (*MySQL) ListUsers

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

ListUsers retrieves all users

func (*MySQL) RemoveUserFromTenant

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

RemoveUserFromTenant removes a user from a tenant

func (*MySQL) Transaction

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

Transaction implements Database.Transaction

func (*MySQL) UpdateTenant

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

UpdateTenant updates an existing tenant

func (*MySQL) UpdateUser

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

UpdateUser updates an existing user

type Postgres

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

Postgres implements the Database interface using PostgreSQL

func (*Postgres) AddUserToTenant

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

AddUserToTenant adds a user to a tenant

func (*Postgres) Close

func (db *Postgres) Close() error

Close closes the database connection

func (*Postgres) CreateTenant

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

CreateTenant creates a new tenant

func (*Postgres) CreateUser

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

CreateUser creates a new user

func (*Postgres) DeleteTenant

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

DeleteTenant deletes a tenant by ID

func (*Postgres) DeleteUser

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

DeleteUser deletes a user by ID

func (*Postgres) DeleteUserTenants

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

DeleteUserTenants deletes all tenant associations for a user

func (*Postgres) GetTenantByID

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

GetTenantByID retrieves a tenant by ID

func (*Postgres) GetTenantByName

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

GetTenantByName retrieves a tenant by name

func (*Postgres) GetTenantUsers

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

GetTenantUsers gets all users for a tenant

func (*Postgres) GetUserByUsername

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

GetUserByUsername retrieves a user by username

func (*Postgres) GetUserTenants

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

GetUserTenants gets all tenants for a user

func (*Postgres) ListTenants

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

ListTenants retrieves all tenants

func (*Postgres) ListUsers

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

ListUsers retrieves all users

func (*Postgres) RemoveUserFromTenant

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

RemoveUserFromTenant removes a user from a tenant

func (*Postgres) Transaction

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

Transaction implements Database.Transaction

func (*Postgres) UpdateTenant

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

UpdateTenant updates an existing tenant

func (*Postgres) UpdateUser

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

UpdateUser updates an existing user

type SQLite

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

SQLite implements the Database interface using SQLite

func (*SQLite) AddUserToTenant

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

AddUserToTenant adds a user to a tenant

func (*SQLite) Close

func (db *SQLite) Close() error

Close closes the database connection

func (*SQLite) CreateTenant

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

CreateTenant creates a new tenant

func (*SQLite) CreateUser

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

CreateUser creates a new user

func (*SQLite) DeleteTenant

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

DeleteTenant deletes a tenant by ID

func (*SQLite) DeleteUser

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

DeleteUser deletes a user by ID

func (*SQLite) DeleteUserTenants

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

DeleteUserTenants deletes all tenant associations for a user

func (*SQLite) GetTenantByID

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

GetTenantByID retrieves a tenant by ID

func (*SQLite) GetTenantByName

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

GetTenantByName retrieves a tenant by name

func (*SQLite) GetTenantUsers

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

GetTenantUsers gets all users for a tenant

func (*SQLite) GetUserByUsername

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

GetUserByUsername retrieves a user by username

func (*SQLite) GetUserTenants

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

GetUserTenants gets all tenants for a user

func (*SQLite) ListTenants

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

ListTenants retrieves all tenants

func (*SQLite) ListUsers

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

ListUsers retrieves all users

func (*SQLite) RemoveUserFromTenant

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

RemoveUserFromTenant removes a user from a tenant

func (*SQLite) Transaction

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

Transaction implements Database.Transaction

func (*SQLite) UpdateTenant

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

UpdateTenant updates an existing tenant

func (*SQLite) UpdateUser

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

UpdateUser updates an existing user

type Tenant

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

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

type UserRole string

UserRole represents the role of a user

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

type UserTenant

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