bbolt

package
v1.1.4 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2025 License: Apache-2.0 Imports: 16 Imported by: 0

README

BBolt System Driver for Apito Engine

This is a BBolt database driver implementation for the Apito Engine system. It uses the ApitoBolt SDK v0.1.1 to provide a MongoDB-like interface on top of BBolt for system-level operations.

Features

The BBolt driver implements the complete ApitoSystemDB interface and provides:

User Management
  • Create, update, and retrieve system users
  • User authentication and authorization support
  • User search functionality
Project Management
  • Create, update, and delete projects
  • Project search and filtering
  • User-project role assignments
  • Project schema and settings management
Team & Organization Management
  • Team creation and member management
  • Organization structure support
  • Role-based permissions
Webhooks
  • Webhook creation and management
  • Project-specific webhook filtering
  • Webhook deletion and updates
Audit Logging
  • Comprehensive audit trail
  • Activity logging with user and project context
  • Searchable audit logs
Usage Tracking
  • Project usage metrics
  • Bandwidth and API call tracking
  • Monthly usage reports
Token Management
  • Token blacklisting
  • Security token validation
Subscription & Billing (Pro Features)
  • Subscription management
  • Invoice creation and tracking
  • Usage-based billing support

Architecture

The driver follows the Apito Engine's dual plugin architecture pattern:

  • Core Implementation: Implements the ApitoSystemDB interface from open-core/interfaces/system_db.go
  • Pro Extensions: Supports additional pro features through the ProApitoSystemDB interface
  • BBolt Storage: Uses ApitoBolt SDK for efficient embedded database operations
  • Collection-Based: Organizes data into logical collections (users, projects, webhooks, etc.)

File Structure

bbolt/
├── init.go          # Driver initialization and migration
├── functions.go     # Project-related operations
├── query.go         # User and search operations
├── mutation.go      # Data modification operations
├── misc.go          # Helper functions and utilities
├── models.go        # Internal data models
├── driver_test.go   # Test suite
└── README.md        # This documentation

Usage

Basic Setup
import "gitlab.com/apito.io/engine/database/system/driver/bbolt"

// Create driver credentials
driverCred := &models.DriverCredentials{
    Database: "/path/to/apito_system.db",
}

// Initialize the driver
driver, err := bbolt.GetProSystemBBoltDriver(driverCred, cacheDriver)
if err != nil {
    log.Fatal("Failed to initialize BBolt driver:", err)
}
defer driver.Close()
Example Operations
ctx := context.Background()

// Create a user
user := &models.SystemUser{
    FirstName: "John",
    LastName:  "Doe",
    Email:     "john@example.com",
}
createdUser, err := driver.CreateSystemUser(ctx, user)

// Create a project
project := &models.Project{
    Name:        "My Project",
    Description: "A sample project",
}
createdProject, err := driver.CreateProject(ctx, createdUser.ID, project)

// Search projects
searchParams := &models.CommonSystemParams{
    ProjectID: createdProject.ID,
}
results, err := driver.SearchProjects(ctx, searchParams)

Collections

The driver automatically creates and manages the following collections:

  • users - System users with email indexing
  • projects - Projects with organization indexing
  • organizations - Organization structures
  • teams - Team definitions
  • team_memberships - User-project team relationships
  • webhooks - Project webhooks with project_id indexing
  • audit_logs - Audit trail with project_id and user_id indexing
  • token_blacklist - Blacklisted tokens
  • usages - Usage tracking with project_id indexing
  • subscriptions - Subscription data with user_id indexing
  • invoices - Invoice records

Performance Considerations

  • Embedded Database: BBolt provides excellent performance for single-node deployments
  • Indexing: Automatic secondary indexes on frequently queried fields
  • Memory Efficient: Low memory footprint compared to full database systems
  • ACID Compliance: Full ACID transaction support through BBolt
  • File-Based: Single file database for easy backup and deployment

Limitations

  • Single Node Only: BBolt is designed for single-node deployments
  • Read/Write Locks: Write operations are serialized (but this is usually fine for system operations)
  • Simplified Queries: Some complex query operations are simplified compared to full SQL databases
  • No Built-in Replication: For high availability, consider using external replication tools

Testing

Run the test suite:

cd database/system/driver/bbolt
go test -v

The tests cover:

  • Basic CRUD operations
  • User and project management
  • Search functionality
  • Audit logging
  • Database migration

Dependencies

  • ApitoBolt v0.1.1 - BBolt wrapper with MongoDB-like API
  • BBolt - Underlying embedded key-value database
  • Standard Apito Engine models and interfaces

Migration from Other Drivers

The BBolt driver implements the same interface as other system drivers (ArangoDB, PostgreSQL, etc.), making migration straightforward:

  1. Update driver configuration to use BBolt
  2. Run migration to create collections and indexes
  3. Optionally migrate existing data using export/import tools

Configuration

Database Path
  • Default: apito_system.db in current directory
  • Configurable via DriverCredentials.Database field
  • Supports both relative and absolute paths
Caching
  • Supports external cache drivers via CacheDBInterface
  • Cache integration for improved performance
  • Optional - can operate without caching

Monitoring and Maintenance

Database Statistics
stats, err := driver.GetDatabaseStats()
// Returns collection counts and basic metrics
Backup
err := driver.BackupDatabase("/path/to/backup.db")
// Note: Full backup implementation pending
Health Checks

The driver automatically validates database integrity during initialization and provides error reporting for any issues.

Contributing

When contributing to the BBolt driver:

  1. Follow the existing code patterns
  2. Add tests for new functionality
  3. Update documentation for interface changes
  4. Ensure compatibility with the ApitoSystemDB interface
  5. Consider performance implications of new features

License

This driver is part of the Apito Engine and follows the same license terms.

Documentation

Overview

Package bbolt provides a BBolt database driver implementation for the Apito system. It uses the ApitoBolt SDK (github.com/apito-io/apitoBolt) to provide a MongoDB-like interface on top of BBolt for system-level operations like user management, project management, team management, webhooks, audit logs, and more.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MigrationStatus

type MigrationStatus struct {
	ID        string `json:"id"`
	XKey      string `json:"_key"`
	Completed bool   `json:"completed"`
	Version   string `json:"version"`
	CreatedAt string `json:"created_at"`
	UpdatedAt string `json:"updated_at"`
}

MigrationStatus represents the migration state in the database

type ProBBoltSystemDriver

type ProBBoltSystemDriver struct {
	DB          *apitobolt.Store
	CacheDriver oci.CacheDBInterface
	// contains filtered or unexported fields
}

func GetSystemBBoltDriver

func GetSystemBBoltDriver(driverCred *models.DriverCredentials, cacheDriver oci.CacheDBInterface) (*ProBBoltSystemDriver, error)

func (*ProBBoltSystemDriver) AddATeamMemberToProject

func (d *ProBBoltSystemDriver) AddATeamMemberToProject(ctx context.Context, req *models.TeamMemberAddRequest) error

func (*ProBBoltSystemDriver) AddSystemUserMetaInfo

AddSystemUserMetaInfo is deprecated according to interface comments

func (*ProBBoltSystemDriver) AddTeamMetaInfo

func (d *ProBBoltSystemDriver) AddTeamMetaInfo(ctx context.Context, docs []*models.SystemUser) ([]*models.SystemUser, error)

func (*ProBBoltSystemDriver) AddWebhookToProject

func (d *ProBBoltSystemDriver) AddWebhookToProject(ctx context.Context, doc *models.Webhook) (*models.Webhook, error)

func (*ProBBoltSystemDriver) AssignProjectToOrganization

func (d *ProBBoltSystemDriver) AssignProjectToOrganization(ctx context.Context, orgId, userId, projectId string) error

func (*ProBBoltSystemDriver) AssignTeamToOrganization

func (d *ProBBoltSystemDriver) AssignTeamToOrganization(ctx context.Context, orgId, userId, teamId string) error

func (*ProBBoltSystemDriver) BackupDatabase

func (d *ProBBoltSystemDriver) BackupDatabase(backupPath string) error

Helper function to backup database

func (*ProBBoltSystemDriver) BlacklistAToken

func (d *ProBBoltSystemDriver) BlacklistAToken(ctx context.Context, token map[string]interface{}) error

func (*ProBBoltSystemDriver) CheckProjectName

func (d *ProBBoltSystemDriver) CheckProjectName(ctx context.Context, name string) error

func (*ProBBoltSystemDriver) CheckProjectWithRoles

func (d *ProBBoltSystemDriver) CheckProjectWithRoles(ctx context.Context, userId, projectId string) (*models.ProjectWithRoles, error)

func (*ProBBoltSystemDriver) CheckTeamMemberExists

func (d *ProBBoltSystemDriver) CheckTeamMemberExists(ctx context.Context, projectId string, memberID string) error

func (*ProBBoltSystemDriver) CheckTokenBlacklisted

func (d *ProBBoltSystemDriver) CheckTokenBlacklisted(ctx context.Context, tokenId string) error

func (*ProBBoltSystemDriver) Close

func (d *ProBBoltSystemDriver) Close() error

func (*ProBBoltSystemDriver) CreateOrganization

func (d *ProBBoltSystemDriver) CreateOrganization(ctx context.Context, org *models.Organization) (*models.Organization, error)

func (*ProBBoltSystemDriver) CreateProject

func (d *ProBBoltSystemDriver) CreateProject(ctx context.Context, userId string, project *models.Project) (*models.Project, error)

func (*ProBBoltSystemDriver) CreateSystemUser

func (d *ProBBoltSystemDriver) CreateSystemUser(ctx context.Context, user *models.SystemUser) (*models.SystemUser, error)

func (*ProBBoltSystemDriver) CreateTeam

func (d *ProBBoltSystemDriver) CreateTeam(ctx context.Context, team *models.Team) (*models.Team, error)

func (*ProBBoltSystemDriver) DeleteProjectFromSystem

func (d *ProBBoltSystemDriver) DeleteProjectFromSystem(ctx context.Context, projectId string) error

func (*ProBBoltSystemDriver) DeleteWebhook

func (d *ProBBoltSystemDriver) DeleteWebhook(ctx context.Context, projectId, hookId string) error

func (*ProBBoltSystemDriver) FindOrganizationAdmin

func (d *ProBBoltSystemDriver) FindOrganizationAdmin(ctx context.Context, orgId string) (*models.SystemUser, error)

func (*ProBBoltSystemDriver) FindUserOrganizations

func (d *ProBBoltSystemDriver) FindUserOrganizations(ctx context.Context, userId string) ([]*models.Organization, error)

func (*ProBBoltSystemDriver) FindUserProjects

func (d *ProBBoltSystemDriver) FindUserProjects(ctx context.Context, userId string) ([]*models.Project, error)

func (*ProBBoltSystemDriver) FindUserProjectsWithRoles

func (d *ProBBoltSystemDriver) FindUserProjectsWithRoles(ctx context.Context, userId string) ([]*models.ProjectWithRoles, error)

func (*ProBBoltSystemDriver) FindUserTeams

func (d *ProBBoltSystemDriver) FindUserTeams(ctx context.Context, userId string) ([]*models.Team, error)

FindUserTeams retrieves all teams for a given user

func (*ProBBoltSystemDriver) GetDatabaseStats

func (d *ProBBoltSystemDriver) GetDatabaseStats() (map[string]interface{}, error)

Helper function to get database statistics

func (*ProBBoltSystemDriver) GetOrganizations

func (*ProBBoltSystemDriver) GetProject

func (d *ProBBoltSystemDriver) GetProject(ctx context.Context, id string) (*models.Project, error)

func (*ProBBoltSystemDriver) GetProjectTeams

func (d *ProBBoltSystemDriver) GetProjectTeams(ctx context.Context, projectId string) (*models.Team, error)

func (*ProBBoltSystemDriver) GetProjects

func (d *ProBBoltSystemDriver) GetProjects(ctx context.Context, keys []string) ([]*models.Project, error)

func (*ProBBoltSystemDriver) GetSystemUser

func (d *ProBBoltSystemDriver) GetSystemUser(ctx context.Context, id string) (*models.SystemUser, error)

func (*ProBBoltSystemDriver) GetSystemUserByEmail

func (d *ProBBoltSystemDriver) GetSystemUserByEmail(ctx context.Context, email string) (*models.SystemUser, error)

func (*ProBBoltSystemDriver) GetSystemUsers

func (d *ProBBoltSystemDriver) GetSystemUsers(ctx context.Context, keys []string) ([]*models.SystemUser, error)

func (*ProBBoltSystemDriver) GetTeams

func (d *ProBBoltSystemDriver) GetTeams(ctx context.Context, userId string) ([]*models.Team, error)

func (*ProBBoltSystemDriver) GetTeamsMembers

func (d *ProBBoltSystemDriver) GetTeamsMembers(ctx context.Context, projectId string) ([]*models.SystemUser, error)

func (*ProBBoltSystemDriver) GetWebHook

func (d *ProBBoltSystemDriver) GetWebHook(ctx context.Context, projectId, hookId string) (*models.Webhook, error)

func (*ProBBoltSystemDriver) ListAllProjects

func (d *ProBBoltSystemDriver) ListAllProjects(ctx context.Context, userId string) ([]*models.Project, error)

ListAllProjects returns all projects (admin only)

func (*ProBBoltSystemDriver) ListAllUsers

func (d *ProBBoltSystemDriver) ListAllUsers(ctx context.Context) ([]*models.SystemUser, error)

func (*ProBBoltSystemDriver) ListTeams

func (d *ProBBoltSystemDriver) ListTeams(ctx context.Context, projectId string) ([]*models.SystemUser, error)

ListTeams retrieves teams for a project

func (*ProBBoltSystemDriver) RemoveATeamFromOrganization

func (d *ProBBoltSystemDriver) RemoveATeamFromOrganization(ctx context.Context, orgId, userId, teamId string) error

func (*ProBBoltSystemDriver) RemoveATeamMemberFromProject

func (d *ProBBoltSystemDriver) RemoveATeamMemberFromProject(ctx context.Context, projectId string, memberID string) error

func (*ProBBoltSystemDriver) RemoveProjectFromOrganization

func (d *ProBBoltSystemDriver) RemoveProjectFromOrganization(ctx context.Context, orgId, userId, projectId string) error

func (*ProBBoltSystemDriver) RunMigration

func (d *ProBBoltSystemDriver) RunMigration(ctx context.Context) error

RunMigration creates the necessary collections/buckets for the system with migration tracking

func (*ProBBoltSystemDriver) SaveAuditLog

func (d *ProBBoltSystemDriver) SaveAuditLog(ctx context.Context, auditLog *models.AuditLogs) error

func (*ProBBoltSystemDriver) SaveRawData

func (d *ProBBoltSystemDriver) SaveRawData(ctx context.Context, collectionName string, data map[string]interface{}) error

func (*ProBBoltSystemDriver) SearchAuditLogs

func (*ProBBoltSystemDriver) SearchFunctions

func (*ProBBoltSystemDriver) SearchProjects

func (*ProBBoltSystemDriver) SearchResource

SearchResource searches for any type of resource

func (*ProBBoltSystemDriver) SearchUsers

func (*ProBBoltSystemDriver) SearchWebHooks

func (*ProBBoltSystemDriver) TransferSchema

func (d *ProBBoltSystemDriver) TransferSchema(ctx context.Context, from, to string) error

func (*ProBBoltSystemDriver) UpdateProject

func (d *ProBBoltSystemDriver) UpdateProject(ctx context.Context, project *models.Project, replace bool) error

func (*ProBBoltSystemDriver) UpdateSystemUser

func (d *ProBBoltSystemDriver) UpdateSystemUser(ctx context.Context, user *models.SystemUser, replace bool) error

type TeamMembership

type TeamMembership struct {
	ID          string   `json:"id"`
	XKey        string   `json:"_key"`
	ProjectID   string   `json:"project_id"`
	UserID      string   `json:"user_id"`
	Role        string   `json:"role"`
	Permissions []string `json:"permissions"`
	CreatedAt   string   `json:"created_at"`
	UpdatedAt   string   `json:"updated_at"`
}

TeamMembership represents a user's membership in a project team

Jump to

Keyboard shortcuts

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