userstore

package module
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2026 License: GPL-3.0 Imports: 17 Imported by: 2

README

UserStore Open in Gitpod

Tests Status Go Report Card PkgGoDev

UserStore is a robust user management package.

Supports multiple database storages (SQLite, MySQL, or PostgreSQL)

Features

  • User and Role management
  • Soft delete support
  • Meta data storage for custom fields
  • Password hashing and verification
  • Method chaining for fluent API
  • Transaction support
  • Query builder for complex searches

License

This project is licensed under the GNU General Public License version 3 (GPL-3.0). You can find a copy of the license at https://www.gnu.org/licenses/gpl-3.0.en.html

For commercial use, please use my contact page to obtain a commercial license.

Installation

go get github.com/dracory/userstore

Setup

userStore, err = userstore.NewStore(userstore.NewStoreOptions{
	DB:                 databaseInstance,
    UserTableName:      "user",
	AutomigrateEnabled: true,
	DebugEnabled:       false,
})

if err != nil {
	return errors.Join(errors.New("userstore.NewStore"), err)
}

API Convention

All getter methods use the Get prefix and all setter methods use the Set prefix:

  • Getters: GetID(), GetName(), GetEmail(), GetStatus(), etc.
  • Setters: SetID(), SetName(), SetEmail(), SetStatus(), etc.

Examples

Creating a User
user := userstore.NewUser().
    SetStatus(userstore.USER_STATUS_ACTIVE).
    SetFirstName("John").
    SetLastName("Doe").
    SetEmail("test@test.com")

err := userStore.UserCreate(user)

if err != nil {
	return errors.New("user failed to create")
}
Reading User Properties
id := user.GetID()
email := user.GetEmail()
firstName := user.GetFirstName()
lastName := user.GetLastName()
status := user.GetStatus()
Creating a Role
role := userstore.NewRole().
    SetName("Administrator").
    SetHandle("admin").
    SetStatus(userstore.USER_STATUS_ACTIVE)

err := userStore.RoleCreate(role)

if err != nil {
	return errors.New("role failed to create")
}
Finding Users
// Find by ID
user, err := userStore.UserFindByID(context.Background(), userID)

// List users with query
query := userstore.NewUserQuery().
    SetStatus(userstore.USER_STATUS_ACTIVE).
    SetLimit(10)

users, err := userStore.UserList(context.Background(), query)
Updating Users
user.SetFirstName("Jane")
user.SetEmail("jane@example.com")

err := userStore.UserUpdate(context.Background(), user)
Soft Deleting Users
err := userStore.UserSoftDelete(context.Background(), user)

Documentation

Index

Constants

View Source
const COLUMN_BUSINESS_NAME = "business_name"
View Source
const COLUMN_COUNTRY = "country"
View Source
const COLUMN_CREATED_AT = "created_at"
View Source
const COLUMN_EMAIL = "email"
View Source
const COLUMN_FIRST_NAME = "first_name"
View Source
const COLUMN_HANDLE = "handle"
View Source
const COLUMN_ID = "id"
View Source
const COLUMN_LAST_NAME = "last_name"
View Source
const COLUMN_MEMO = "memo"
View Source
const COLUMN_METAS = "metas"
View Source
const COLUMN_MIDDLE_NAMES = "middle_names"
View Source
const COLUMN_NAME = "name"
View Source
const COLUMN_PASSWORD = "password"
View Source
const COLUMN_PHONE = "phone"
View Source
const COLUMN_PROFILE_IMAGE_URL = "profile_image_url"
View Source
const COLUMN_ROLE = "role"
View Source
const COLUMN_SOFT_DELETED_AT = "soft_deleted_at"
View Source
const COLUMN_STATUS = "status"
View Source
const COLUMN_TIMEZONE = "timezone"
View Source
const COLUMN_UPDATED_AT = "updated_at"
View Source
const ERROR_EMPTY_ARRAY = "array cannot be empty"
View Source
const ERROR_EMPTY_STRING = "string cannot be empty"
View Source
const ERROR_NEGATIVE_NUMBER = "number cannot be negative"
View Source
const ROLE_STATUS_ACTIVE = "active"
View Source
const ROLE_STATUS_DELETED = "deleted"
View Source
const ROLE_STATUS_INACTIVE = "inactive"
View Source
const USER_ROLE_ADMINISTRATOR = "administrator"
View Source
const USER_ROLE_MANAGER = "manager"
View Source
const USER_ROLE_SUPERUSER = "superuser"
View Source
const USER_ROLE_USER = "user"
View Source
const USER_STATUS_ACTIVE = "active"
View Source
const USER_STATUS_DELETED = "deleted"
View Source
const USER_STATUS_INACTIVE = "inactive"
View Source
const USER_STATUS_UNVERIFIED = "unverified"

Variables

This section is empty.

Functions

func GenerateShortID added in v1.7.0

func GenerateShortID() string

GenerateShortID generates a new 9-character shortened ID using TimestampMicro

func IsShortID added in v1.7.0

func IsShortID(id string) bool

IsShortID checks if an ID appears to be shortened (9-21 chars, alphanumeric)

func NormalizeID added in v1.7.0

func NormalizeID(id string) string

NormalizeID normalizes an ID for lookup (lowercase)

func RoleNoImageUrl

func RoleNoImageUrl() string

func ShortenID added in v1.7.0

func ShortenID(id string) string

ShortenID shortens any numeric ID string using Crockford Base32

func UnshortenID added in v1.7.0

func UnshortenID(shortID string) (string, error)

UnshortenID attempts to unshorten a Crockford Base32 ID

func UserNoImageUrl

func UserNoImageUrl() string

Types

type NewStoreOptions

type NewStoreOptions struct {
	UserTableName      string
	DB                 *sql.DB
	DbDriverName       string
	AutomigrateEnabled bool
	DebugEnabled       bool
}

NewStoreOptions define the options for creating a new block store

type RoleInterface

type RoleInterface interface {
	Data() map[string]string
	DataChanged() map[string]string
	MarkAsNotDirty()

	GetCreatedAt() string
	GetCreatedAtCarbon() *carbon.Carbon
	SetCreatedAt(createdAt string) RoleInterface

	GetHandle() string
	SetHandle(handle string) RoleInterface

	GetID() string
	SetID(id string) RoleInterface

	GetName() string
	SetName(name string) RoleInterface

	GetMemo() string
	SetMemo(memo string) RoleInterface

	GetMeta(name string) string
	SetMeta(name string, value string) error
	GetMetas() (map[string]string, error)
	SetMetas(metas map[string]string) error

	GetStatus() string
	SetStatus(status string) RoleInterface

	GetSoftDeletedAt() string
	GetSoftDeletedAtCarbon() *carbon.Carbon
	SetSoftDeletedAt(softDeletedAt string) RoleInterface

	GetUpdatedAt() string
	GetUpdatedAtCarbon() *carbon.Carbon
	SetUpdatedAt(updatedAt string) RoleInterface
}

func NewRole

func NewRole() RoleInterface

func NewRoleFromExistingData

func NewRoleFromExistingData(data map[string]string) RoleInterface

type RoleQueryInterface

type RoleQueryInterface interface {
	Validate() error

	Columns() []string
	SetColumns(columns []string) RoleQueryInterface

	HasCountOnly() bool
	IsCountOnly() bool
	SetCountOnly(countOnly bool) RoleQueryInterface

	HasHandle() bool
	Handle() string
	SetHandle(handle string) RoleQueryInterface

	HasID() bool
	GetID() string
	SetID(id string) RoleQueryInterface

	HasLimit() bool
	Limit() int
	SetLimit(limit int) RoleQueryInterface

	HasOffset() bool
	Offset() int
	SetOffset(offset int) RoleQueryInterface

	HasOrderBy() bool
	OrderBy() string
	SetOrderBy(orderBy string) RoleQueryInterface

	HasSortDirection() bool
	SortDirection() string
	SetSortDirection(sortDirection string) RoleQueryInterface

	HasSoftDeletedIncluded() bool
	SoftDeletedIncluded() bool
	SetSoftDeletedIncluded(softDeletedIncluded bool) RoleQueryInterface

	HasStatus() bool
	Status() string
	SetStatus(status string) RoleQueryInterface

	HasTitleLike() bool
	TitleLike() string
	SetTitleLike(titleLike string) RoleQueryInterface
	// contains filtered or unexported methods
}

func NewRoleQuery

func NewRoleQuery() RoleQueryInterface

type StoreInterface

type StoreInterface interface {
	AutoMigrate() error
	EnableDebug(debug bool)
	DB() *sql.DB

	UserCreate(ctx context.Context, user UserInterface) error
	UserCount(ctx context.Context, options UserQueryInterface) (int64, error)
	UserDelete(ctx context.Context, user UserInterface) error
	UserDeleteByID(ctx context.Context, id string) error
	UserFindByEmail(ctx context.Context, email string) (UserInterface, error)
	UserFindByID(ctx context.Context, userID string) (UserInterface, error)
	UserList(ctx context.Context, query UserQueryInterface) ([]UserInterface, error)
	UserSoftDelete(ctx context.Context, user UserInterface) error
	UserSoftDeleteByID(ctx context.Context, id string) error
	UserUpdate(ctx context.Context, user UserInterface) error
}

func NewStore

func NewStore(opts NewStoreOptions) (StoreInterface, error)

NewStore creates a new block store

type UserInterface

type UserInterface interface {
	Data() map[string]string
	DataChanged() map[string]string
	MarkAsNotDirty()
	Get(columnName string) string
	Set(columnName string, value string)

	IsActive() bool
	IsInactive() bool
	IsSoftDeleted() bool
	IsUnverified() bool

	IsAdministrator() bool
	IsManager() bool
	IsSuperuser() bool

	IsRegistrationCompleted() bool

	GetBusinessName() string
	SetBusinessName(businessName string) UserInterface

	GetCountry() string
	SetCountry(country string) UserInterface

	GetCreatedAt() string
	GetCreatedAtCarbon() *carbon.Carbon
	SetCreatedAt(createdAt string) UserInterface

	GetEmail() string
	SetEmail(email string) UserInterface

	GetID() string
	SetID(id string) UserInterface

	GetFirstName() string
	SetFirstName(firstName string) UserInterface

	GetLastName() string
	SetLastName(lastName string) UserInterface

	GetMemo() string
	SetMemo(memo string) UserInterface

	GetMeta(name string) string
	SetMeta(name string, value string) error
	GetMetas() (map[string]string, error)
	SetMetas(metas map[string]string) error
	UpsertMetas(metas map[string]string) error

	GetMiddleNames() string
	SetMiddleNames(middleNames string) UserInterface

	GetPassword() string
	PasswordCompare(password string) bool
	SetPassword(password string) UserInterface
	SetPasswordAndHash(password string) error

	GetPhone() string
	SetPhone(phone string) UserInterface

	GetProfileImageUrl() string
	SetProfileImageUrl(profileImageUrl string) UserInterface

	GetRole() string
	SetRole(role string) UserInterface

	GetSoftDeletedAt() string
	GetSoftDeletedAtCarbon() *carbon.Carbon
	SetSoftDeletedAt(deletedAt string) UserInterface

	GetTimezone() string
	SetTimezone(timezone string) UserInterface

	GetStatus() string
	SetStatus(status string) UserInterface

	GetUpdatedAt() string
	GetUpdatedAtCarbon() *carbon.Carbon
	SetUpdatedAt(updatedAt string) UserInterface
}

func NewUser

func NewUser() UserInterface

func NewUserFromExistingData

func NewUserFromExistingData(data map[string]string) UserInterface

type UserQueryInterface

type UserQueryInterface interface {
	Validate() error

	Columns() []string
	SetColumns(columns []string) UserQueryInterface

	HasCountOnly() bool
	IsCountOnly() bool
	SetCountOnly(countOnly bool) UserQueryInterface

	HasCreatedAtGte() bool
	CreatedAtGte() string
	SetCreatedAtGte(createdAtGte string) UserQueryInterface

	HasCreatedAtLte() bool
	CreatedAtLte() string
	SetCreatedAtLte(createdAtLte string) UserQueryInterface

	HasEmail() bool
	Email() string
	SetEmail(email string) UserQueryInterface

	HasID() bool
	GetID() string
	SetID(id string) UserQueryInterface

	HasIDIn() bool
	IDIn() []string
	SetIDIn(idIn []string) UserQueryInterface

	HasMetaLike() bool
	MetaLike() string
	SetMetaLike(metaLike string) UserQueryInterface

	HasLimit() bool
	Limit() int
	SetLimit(limit int) UserQueryInterface

	HasOffset() bool
	Offset() int
	SetOffset(offset int) UserQueryInterface

	HasOrderBy() bool
	OrderBy() string
	SetOrderBy(orderBy string) UserQueryInterface

	HasSortDirection() bool
	SortDirection() string
	SetSortDirection(sortDirection string) UserQueryInterface

	HasSoftDeletedIncluded() bool
	SoftDeletedIncluded() bool
	SetSoftDeletedIncluded(softDeletedIncluded bool) UserQueryInterface

	HasStatus() bool
	Status() string
	SetStatus(status string) UserQueryInterface

	HasStatusIn() bool
	StatusIn() []string
	SetStatusIn(statusIn []string) UserQueryInterface
	// contains filtered or unexported methods
}

func NewUserQuery

func NewUserQuery() UserQueryInterface

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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