groupstore

package module
v0.2.0 Latest Latest
Warning

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

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

README

GroupStore Open in Gitpod

Tests Status Go Report Card PkgGoDev

GroupStore is a robust group management package built in Go, following a clean architecture design. It provides a flexible solution for managing group relationships in a database, enabling you to group any type of entity - from user groups to product categories and beyond.

License

This project is licensed under the GNU Affero General Public License v3.0 (AGPL-3.0). You can find a copy of the license at https://www.gnu.org/licenses/agpl-3.0.en.html

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

Features

  • Generic group relationship management

Usage

GroupStore can be used to manage group relationships, i.e:

  • User groups and permissions
  • Product categories and subcategories
  • Organizational hierarchies
  • Tag systems and categorization
  • Any entity grouping needs

Examples

Here are some practical examples of how to use GroupStore:

Basic Group Management
// Create a new group
newGroup := groupstore.NewGroup()
newGroup.SetTitle("Administrators")
newGroup.SetHandle("admin")
newGroup.SetStatus(groupstore.GROUP_STATUS_ACTIVE)

// Save the group
err := store.GroupCreate(ctx, newGroup)
if err != nil {
    // handle error
}

// List all groups
groups, err := store.GroupList(ctx)
if err != nil {
    // handle error
}

// Retrieve a group by ID
adminGroup, err := store.GroupFindByID(ctx, "123456")
if err != nil {
    // handle error
}

// Retrieve a group by handle
adminGroup, err := store.GroupFindByHandle(ctx, "admin")
if err != nil {
    // handle error
}
Adding a User to a Group
// First get the group by its handle
adminGroup, err := store.GroupFindByHandle(ctx, "admin")
if err != nil {
    // handle error
}

// Create a new user group relationship
userRelation := groupstore.NewRelation()
userRelation.SetGroupID(adminGroup.ID())      // ID of the group
userRelation.SetEntityType("user")           // Type of entity (user in this case)
userRelation.SetEntityID("123456")          // ID of the user

// Save the relationship
err = store.RelationCreate(ctx, userRelation)
if err != nil {
    // handle error
}

// List all users in the group
users, err := store.RelationList(ctx, groupstore.RelationQuery().GroupID(adminGroup.ID()))
if err != nil {
    // handle error
}

Documentation

Index

Constants

View Source
const COLUMN_CREATED_AT = "created_at"
View Source
const COLUMN_ENTITY_ID = "entity_id"
View Source
const COLUMN_ENTITY_TYPE = "entity_type"
View Source
const COLUMN_GROUP_ID = "group_id"
View Source
const COLUMN_HANDLE = "handle"
View Source
const COLUMN_ID = "id"
View Source
const COLUMN_MEMO = "memo"
View Source
const COLUMN_METAS = "metas"
View Source
const COLUMN_SOFT_DELETED_AT = "soft_deleted_at"
View Source
const COLUMN_STATUS = "status"
View Source
const COLUMN_TITLE = "title"
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 GROUP_STATUS_ACTIVE = "active"
View Source
const GROUP_STATUS_DELETED = "deleted"
View Source
const GROUP_STATUS_INACTIVE = "inactive"

Variables

This section is empty.

Functions

This section is empty.

Types

type GroupInterface

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

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

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

	Handle() string
	SetHandle(handle string) GroupInterface

	ID() string
	SetID(id string) GroupInterface

	Memo() string
	SetMemo(memo string) GroupInterface

	Meta(name string) string
	SetMeta(name string, value string) error
	Metas() (map[string]string, error)
	SetMetas(metas map[string]string) error

	Status() string
	SetStatus(status string) GroupInterface

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

	Title() string
	SetTitle(title string) GroupInterface

	UpdatedAt() string
	UpdatedAtCarbon() *carbon.Carbon
	SetUpdatedAt(updatedAt string) GroupInterface
}

func NewGroup

func NewGroup() GroupInterface

func NewGroupFromExistingData

func NewGroupFromExistingData(data map[string]string) GroupInterface

type GroupQueryInterface

type GroupQueryInterface interface {
	Validate() error

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

	HasCountOnly() bool
	IsCountOnly() bool
	SetCountOnly(countOnly bool) GroupQueryInterface

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

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

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

	HasID() bool
	ID() string
	SetID(id string) GroupQueryInterface

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

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

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

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

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

	HasSoftDeletedIncluded() bool
	SoftDeletedIncluded() bool
	SetSoftDeletedIncluded(softDeletedIncluded bool) GroupQueryInterface

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

	HasStatusIn() bool
	StatusIn() []string
	SetStatusIn(statusIn []string) GroupQueryInterface

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

func NewGroupQuery

func NewGroupQuery() GroupQueryInterface

type NewStoreOptions

type NewStoreOptions struct {
	// GroupTableName is the name of the group table
	GroupTableName string

	// GroupEntityRelationTableName is the name of the entity to group relation table
	GroupEntityRelationTableName string

	// DB is the underlying database connection
	DB *sql.DB

	// DbDriverName is the database driver name/type
	DbDriverName string

	// AutomigrateEnabled indicates whether to automatically migrate the database
	AutomigrateEnabled bool

	// DebugEnabled enables or disables the debug mode
	DebugEnabled bool

	// SqlLogger is the sql statement logger when debug mode is enabled, defaults to the default logger
	SqlLogger *slog.Logger
}

NewStoreOptions define the options for creating a new block store

type RelationInterface added in v0.2.0

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

	IsSoftDeleted() bool

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

	EntityType() string
	SetEntityType(entityType string) RelationInterface

	EntityID() string
	SetEntityID(entityID string) RelationInterface

	ID() string
	SetID(id string) RelationInterface

	Memo() string
	SetMemo(memo string) RelationInterface

	Meta(name string) string
	SetMeta(name string, value string) error
	Metas() (map[string]string, error)
	SetMetas(metas map[string]string) error

	GroupID() string
	SetGroupID(groupID string) RelationInterface

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

	UpdatedAt() string
	UpdatedAtCarbon() *carbon.Carbon
	SetUpdatedAt(updatedAt string) RelationInterface
}

func NewGroupEntityRelationFromExistingData added in v0.2.0

func NewGroupEntityRelationFromExistingData(data map[string]string) RelationInterface

func NewRelation added in v0.2.0

func NewRelation() RelationInterface

type RelationQueryInterface added in v0.2.0

type RelationQueryInterface interface {
	Validate() error

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

	HasCountOnly() bool
	IsCountOnly() bool
	SetCountOnly(countOnly bool) RelationQueryInterface

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

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

	HasEntityID() bool
	EntityID() string
	SetEntityID(entityID string) RelationQueryInterface

	HasEntityType() bool
	EntityType() string
	SetEntityType(entityType string) RelationQueryInterface

	HasID() bool
	ID() string
	SetID(id string) RelationQueryInterface

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

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

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

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

	HasGroupID() bool
	GroupID() string
	SetGroupID(groupID string) RelationQueryInterface

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

	HasSoftDeletedIncluded() bool
	SoftDeletedIncluded() bool
	SetSoftDeletedIncluded(softDeletedIncluded bool) RelationQueryInterface
	// contains filtered or unexported methods
}

func NewRelationQuery added in v0.2.0

func NewRelationQuery() RelationQueryInterface

type StoreInterface

type StoreInterface interface {
	// AutoMigrate auto migrates the database schema
	AutoMigrate() error

	// EnableDebug enables or disables the debug mode
	EnableDebug(debug bool)

	// DB returns the underlying database connection
	DB() *sql.DB

	// GroupCount returns the number of groups based on the given query options
	GroupCount(ctx context.Context, options GroupQueryInterface) (int64, error)

	// GroupCreate creates a new group
	GroupCreate(ctx context.Context, group GroupInterface) error

	// GroupDelete deletes a group
	GroupDelete(ctx context.Context, group GroupInterface) error

	// GroupDeleteByID deletes a group by its ID
	GroupDeleteByID(ctx context.Context, id string) error

	// GroupFindByHandle returns a group by its handle
	GroupFindByHandle(ctx context.Context, handle string) (GroupInterface, error)

	// GroupFindByID returns a group by its ID
	GroupFindByID(ctx context.Context, id string) (GroupInterface, error)

	// GroupList returns a list of groups based on the given query options
	GroupList(ctx context.Context, query GroupQueryInterface) ([]GroupInterface, error)

	// GroupSoftDelete soft deletes a group
	GroupSoftDelete(ctx context.Context, group GroupInterface) error

	// GroupSoftDeleteByID soft deletes a group by its ID
	GroupSoftDeleteByID(ctx context.Context, id string) error

	// GroupUpdate updates a group
	GroupUpdate(ctx context.Context, group GroupInterface) error

	// RelationCount returns the number of group entities mappings based on the given query options
	RelationCount(ctx context.Context, options RelationQueryInterface) (int64, error)

	// RelationCreate creates a new group entity mapping
	RelationCreate(ctx context.Context, relation RelationInterface) error

	// RelationDelete deletes a group entity mapping
	RelationDelete(ctx context.Context, relation RelationInterface) error

	// RelationDeleteByID deletes a group entity mapping by its ID
	RelationDeleteByID(ctx context.Context, id string) error

	// RelationFindByEntityAndGroup returns a group entity mapping by its entity type, entity ID and group ID
	RelationFindByEntityAndGroup(ctx context.Context, entityType string, entityID string, groupID string) (RelationInterface, error)

	// RelationFindByID returns a group entity mapping by its ID
	RelationFindByID(ctx context.Context, id string) (RelationInterface, error)

	// RelationList returns a list of group entity mappings based on the given query options
	RelationList(ctx context.Context, query RelationQueryInterface) ([]RelationInterface, error)

	// RelationSoftDelete soft deletes a group entity mapping
	RelationSoftDelete(ctx context.Context, relation RelationInterface) error

	// RelationSoftDeleteByID soft deletes a group entity mapping by its ID
	RelationSoftDeleteByID(ctx context.Context, id string) error

	// RelationUpdate updates a group entity mapping
	RelationUpdate(ctx context.Context, relation RelationInterface) 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

	BusinessName() string
	SetBusinessName(businessName string) UserInterface

	Country() string
	SetCountry(country string) UserInterface

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

	Email() string
	SetEmail(email string) UserInterface

	ID() string
	SetID(id string) UserInterface

	FirstName() string
	SetFirstName(firstName string) UserInterface

	LastName() string
	SetLastName(lastName string) UserInterface

	Memo() string
	SetMemo(memo string) UserInterface

	Meta(name string) string
	SetMeta(name string, value string) error
	Metas() (map[string]string, error)
	SetMetas(metas map[string]string) error
	UpsertMetas(metas map[string]string) error

	MiddleNames() string
	SetMiddleNames(middleNames string) UserInterface

	Password() string
	SetPassword(password string) UserInterface

	Phone() string
	SetPhone(phone string) UserInterface

	ProfileImageUrl() string
	SetProfileImageUrl(profileImageUrl string) UserInterface

	Group() string
	SetGroup(group string) UserInterface

	SoftDeletedAt() string
	SoftDeletedAtCarbon() *carbon.Carbon
	SetSoftDeletedAt(deletedAt string) UserInterface

	Timezone() string
	SetTimezone(timezone string) UserInterface

	Status() string
	SetStatus(status string) UserInterface

	PasswordCompare(password string) bool

	UpdatedAt() string
	UpdatedAtCarbon() *carbon.Carbon
	SetUpdatedAt(updatedAt string) UserInterface
}

Jump to

Keyboard shortcuts

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