{"modulePath":"github.com/bitechdev/ResolveSpec","version":"v1.3.14","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package dbmanager\n\n## Constants\n\n```go\nconst DefaultApplicationName = \"ResolveSpec\"\n```\nDefaultApplicationName is used when a connection does not set ApplicationName.\n\n## Variables\n\n```go\nvar (\n\t// ErrConnectionNotFound is returned when a connection with the given name doesn't exist\n\tErrConnectionNotFound = errors.New(\"connection not found\")\n\n\t// ErrInvalidConfiguration is returned when the configuration is invalid\n\tErrInvalidConfiguration = errors.New(\"invalid configuration\")\n\n\t// ErrConnectionClosed is returned when attempting to use a closed connection\n\tErrConnectionClosed = errors.New(\"connection is closed\")\n\n\t// ErrNotSQLDatabase is returned when attempting SQL operations on a non-SQL database\n\tErrNotSQLDatabase = errors.New(\"not a SQL database\")\n\n\t// ErrNotMongoDB is returned when attempting MongoDB operations on a non-MongoDB connection\n\tErrNotMongoDB = errors.New(\"not a MongoDB connection\")\n\n\t// ErrUnsupportedDatabase is returned when the database type is not supported\n\tErrUnsupportedDatabase = errors.New(\"unsupported database type\")\n\n\t// ErrNoDefaultConnection is returned when no default connection is configured\n\tErrNoDefaultConnection = errors.New(\"no default connection configured\")\n\n\t// ErrAlreadyConnected is returned when attempting to connect an already connected connection\n\tErrAlreadyConnected = errors.New(\"already connected\")\n)\n```\nCommon errors\n\n## Functions\n\n```go\nfunc RecordReconnectAttempt(name string, dbType DatabaseType, success bool)\n```\nRecordReconnectAttempt records a reconnection attempt\n\n```go\nfunc ResetInstance()\n```\nResetInstance resets the singleton instance (primarily for testing purposes). WARNING: This should only be used in tests. Calling this in production code while the manager is in use can lead to undefined behavior.\n\n```go\nfunc SetupManager(cfg ManagerConfig) error\n```\nSetupManager initializes the singleton database manager with the provided configuration. This function must be called before GetInstance(). Returns an error if the manager is already initialized or if configuration is invalid.\n\n## Types\n\n```go\ntype ConfigurationError struct {\n\tField string\n\tErr   error\n}\n```\nConfigurationError wraps configuration-related errors\n\n```go\nfunc NewConfigurationError(field string, err error) *ConfigurationError\n```\nNewConfigurationError creates a new ConfigurationError\n\n```go\nfunc (e *ConfigurationError) Error() string\n```\n\n```go\nfunc (e *ConfigurationError) Unwrap() error\n```\n\n```go\ntype Connection interface {\n\t// Metadata\n\tName() string\n\tType() DatabaseType\n\n\t// ORM Access (SQL databases only)\n\tBun() (*bun.DB, error)\n\tGORM() (*gorm.DB, error)\n\tNative() (*sql.DB, error)\n\tDB() (*sql.DB, error)\n\n\t// Common Database interface (for SQL databases)\n\tDatabase() (common.Database, error)\n\n\t// MongoDB Access (MongoDB only)\n\tMongoDB() (*mongo.Client, error)\n\n\t// Lifecycle\n\tConnect(ctx context.Context) error\n\tClose() error\n\tHealthCheck(ctx context.Context) error\n\tReconnect(ctx context.Context) error\n\n\t// Stats\n\tStats() *ConnectionStats\n}\n```\nConnection represents a single named database connection\n\n```go\nfunc NewConnectionFromDB(name string, dbType DatabaseType, db *sql.DB) Connection\n```\nNewConnectionFromDB creates a new Connection from an existing \\*sql.DB This allows you to use dbmanager features (ORM wrappers, health checks, etc.) with a database connection that was opened outside of dbmanager\n\nParameters:\n\n  - name: A unique name for this connection\n  - dbType: The database type (DatabaseTypePostgreSQL, DatabaseTypeSQLite, or DatabaseTypeMSSQL)\n  - db: An existing \\*sql.DB connection\n\nReturns a Connection that wraps the existing \\*sql.DB\n\n```go\ntype ConnectionConfig struct {\n\t// Name is the unique name of this connection\n\tName string `mapstructure:\"name\"`\n\n\t// Type is the database type (postgres, sqlite, mssql, mongodb)\n\tType DatabaseType `mapstructure:\"type\"`\n\n\t// DSN is the complete Data Source Name / connection string\n\t// If provided, this takes precedence over individual connection parameters\n\tDSN string `mapstructure:\"dsn\"`\n\n\t// Connection parameters (used if DSN is not provided)\n\tHost     string `mapstructure:\"host\"`\n\tPort     int    `mapstructure:\"port\"`\n\tUser     string `mapstructure:\"user\"`\n\tPassword string `mapstructure:\"password\"`\n\tDatabase string `mapstructure:\"database\"`\n\n\t// PostgreSQL/MSSQL specific\n\tSSLMode string `mapstructure:\"sslmode\"` // disable, require, verify-ca, verify-full\n\tSchema  string `mapstructure:\"schema\"`  // Default schema\n\n\t// ApplicationName identifies this client to the server (postgres\n\t// application_name, mssql app name, mongodb appName)\n\tApplicationName string `mapstructure:\"application_name\"`\n\n\t// SQLite specific\n\tFilePath string `mapstructure:\"filepath\"`\n\n\t// MongoDB specific\n\tAuthSource     string `mapstructure:\"auth_source\"`\n\tReplicaSet     string `mapstructure:\"replica_set\"`\n\tReadPreference string `mapstructure:\"read_preference\"` // primary, secondary, etc.\n\n\t// Connection pool settings (overrides global defaults)\n\tMaxOpenConns    *int           `mapstructure:\"max_open_conns\"`\n\tMaxIdleConns    *int           `mapstructure:\"max_idle_conns\"`\n\tConnMaxLifetime *time.Duration `mapstructure:\"conn_max_lifetime\"`\n\tConnMaxIdleTime *time.Duration `mapstructure:\"conn_max_idle_time\"`\n\n\t// Timeouts\n\tConnectTimeout time.Duration `mapstructure:\"connect_timeout\"`\n\tQueryTimeout   time.Duration `mapstructure:\"query_timeout\"`\n\n\t// Retry policy for the initial connect (inherited from the manager config)\n\tRetryAttempts int           `mapstructure:\"retry_attempts\"`\n\tRetryDelay    time.Duration `mapstructure:\"retry_delay\"`\n\tRetryMaxDelay time.Duration `mapstructure:\"retry_max_delay\"`\n\n\t// Features\n\tEnableTracing bool `mapstructure:\"enable_tracing\"`\n\tEnableMetrics bool `mapstructure:\"enable_metrics\"`\n\tEnableLogging bool `mapstructure:\"enable_logging\"`\n\n\t// DefaultORM specifies which ORM to use for the Database() method\n\t// Options: \"bun\", \"gorm\", \"native\"\n\tDefaultORM string `mapstructure:\"default_orm\"`\n\n\t// Tags for organization and filtering\n\tTags map[string]string `mapstructure:\"tags\"`\n}\n```\nConnectionConfig defines configuration for a single database connection\n\n```go\nfunc (cc *ConnectionConfig) ApplyDefaults(global *ManagerConfig)\n```\nApplyDefaults applies default values and global settings to the connection configuration\n\n```go\nfunc (cc *ConnectionConfig) BuildDSN() (string, error)\n```\nBuildDSN builds a connection string from individual parameters\n\n```go\nfunc (cc *ConnectionConfig) GetApplicationName() string\n```\n\n```go\nfunc (cc *ConnectionConfig) GetConnMaxIdleTime() *time.Duration\n```\n\n```go\nfunc (cc *ConnectionConfig) GetConnMaxLifetime() *time.Duration\n```\n\n```go\nfunc (cc *ConnectionConfig) GetConnectTimeout() time.Duration\n```\n\n```go\nfunc (cc *ConnectionConfig) GetDatabase() string\n```\n\n```go\nfunc (cc *ConnectionConfig) GetEnableLogging() bool\n```\n\n```go\nfunc (cc *ConnectionConfig) GetEnableMetrics() bool\n```\n\n```go\nfunc (cc *ConnectionConfig) GetFilePath() string\n```\n\n```go\nfunc (cc *ConnectionConfig) GetHost() string\n```\n\n```go\nfunc (cc *ConnectionConfig) GetMaxIdleConns() *int\n```\n\n```go\nfunc (cc *ConnectionConfig) GetMaxOpenConns() *int\n```\n\n```go\nfunc (cc *ConnectionConfig) GetName() string\n```\nGetter methods to implement providers.ConnectionConfig interface\n\n```go\nfunc (cc *ConnectionConfig) GetPassword() string\n```\n\n```go\nfunc (cc *ConnectionConfig) GetPort() int\n```\n\n```go\nfunc (cc *ConnectionConfig) GetQueryTimeout() time.Duration\n```\n\n```go\nfunc (cc *ConnectionConfig) GetReadPreference() string\n```\n\n```go\nfunc (cc *ConnectionConfig) GetRetryAttempts() int\n```\n\n```go\nfunc (cc *ConnectionConfig) GetRetryDelay() time.Duration\n```\n\n```go\nfunc (cc *ConnectionConfig) GetRetryMaxDelay() time.Duration\n```\n\n```go\nfunc (cc *ConnectionConfig) GetType() string\n```\n\n```go\nfunc (cc *ConnectionConfig) GetUser() string\n```\n\n```go\nfunc (cc *ConnectionConfig) Validate() error\n```\nValidate validates the connection configuration\n\n```go\ntype ConnectionError struct {\n\tName      string\n\tOperation string\n\tErr       error\n}\n```\nConnectionError wraps errors that occur during connection operations\n\n```go\nfunc NewConnectionError(name, operation string, err error) *ConnectionError\n```\nNewConnectionError creates a new ConnectionError\n\n```go\nfunc (e *ConnectionError) Error() string\n```\n\n```go\nfunc (e *ConnectionError) Unwrap() error\n```\n\n```go\ntype ConnectionStats struct {\n\tName              string\n\tType              DatabaseType\n\tConnected         bool\n\tLastHealthCheck   time.Time\n\tHealthCheckStatus string\n\n\t// SQL connection pool stats\n\tOpenConnections    int\n\tMaxOpenConnections int\n\tTotalOpened        int64 // physical connections ever opened\n\tInUse              int\n\tIdle               int\n\tWaitCount          int64\n\tWaitDuration       time.Duration\n\tMaxIdleClosed      int64\n\tMaxLifetimeClosed  int64\n}\n```\nConnectionStats contains statistics about a database connection\n\n```go\ntype DatabaseType string\n```\nDatabaseType represents the type of database\n\n```go\nconst (\n\t// DatabaseTypePostgreSQL represents PostgreSQL database\n\tDatabaseTypePostgreSQL DatabaseType = \"postgres\"\n\n\t// DatabaseTypeSQLite represents SQLite database\n\tDatabaseTypeSQLite DatabaseType = \"sqlite\"\n\n\t// DatabaseTypeMSSQL represents Microsoft SQL Server database\n\tDatabaseTypeMSSQL DatabaseType = \"mssql\"\n\n\t// DatabaseTypeMongoDB represents MongoDB database\n\tDatabaseTypeMongoDB DatabaseType = \"mongodb\"\n)\n```\n\n```go\ntype Manager interface {\n\t// Connection retrieval\n\tGet(name string) (Connection, error)\n\tGetDefault() (Connection, error)\n\tGetAll() map[string]Connection\n\n\t// Default database management\n\tGetDefaultDatabase() (common.Database, error)\n\tSetDefaultDatabase(name string) error\n\n\t// Lifecycle\n\tConnect(ctx context.Context) error\n\tClose() error\n\tHealthCheck(ctx context.Context) error\n\n\t// Stats\n\tStats() *ManagerStats\n}\n```\nManager manages multiple named database connections\n\n```go\nfunc GetInstance() (Manager, error)\n```\nGetInstance returns the singleton instance of the database manager. Returns an error if SetupManager has not been called yet.\n\n```go\nfunc NewManager(cfg ManagerConfig) (Manager, error)\n```\nNewManager creates a new database connection manager\n\n```go\ntype ManagerConfig struct {\n\t// DefaultConnection is the name of the default connection to use\n\tDefaultConnection string `mapstructure:\"default_connection\"`\n\n\t// Connections is a map of connection name to connection configuration\n\tConnections map[string]ConnectionConfig `mapstructure:\"connections\"`\n\n\t// Global connection pool defaults\n\tMaxOpenConns    int           `mapstructure:\"max_open_conns\"`\n\tMaxIdleConns    int           `mapstructure:\"max_idle_conns\"`\n\tConnMaxLifetime time.Duration `mapstructure:\"conn_max_lifetime\"`\n\tConnMaxIdleTime time.Duration `mapstructure:\"conn_max_idle_time\"`\n\n\t// Retry policy\n\tRetryAttempts int           `mapstructure:\"retry_attempts\"`\n\tRetryDelay    time.Duration `mapstructure:\"retry_delay\"`\n\tRetryMaxDelay time.Duration `mapstructure:\"retry_max_delay\"`\n\n\t// Health checks. A zero HealthCheckInterval selects the default (15s); a\n\t// negative value disables the background health checker.\n\tHealthCheckInterval time.Duration `mapstructure:\"health_check_interval\"`\n\n\t// Deprecated: ignored. The manager never closes a pool to recover from an\n\t// error because database/sql already replaces broken connections; closing\n\t// it would invalidate every handle handed out. Use Connection.Reconnect for\n\t// an explicit, handle-preserving refresh.\n\tEnableAutoReconnect bool `mapstructure:\"enable_auto_reconnect\"`\n}\n```\nManagerConfig contains configuration for the database connection manager\n\n```go\nfunc DefaultManagerConfig() ManagerConfig\n```\nDefaultManagerConfig returns a ManagerConfig with sensible defaults\n\n```go\nfunc FromConfig(cfg config.DBManagerConfig) ManagerConfig\n```\nFromConfig converts config.DBManagerConfig to internal ManagerConfig\n\n```go\nfunc (c *ManagerConfig) ApplyDefaults()\n```\nApplyDefaults applies default values to the manager configuration\n\n```go\nfunc (c *ManagerConfig) Validate() error\n```\nValidate validates the manager configuration\n\n```go\ntype ManagerStats struct {\n\tTotalConnections int\n\tHealthyCount     int\n\tUnhealthyCount   int\n\tConnectionStats  map[string]*ConnectionStats\n}\n```\nManagerStats contains statistics about the connection manager\n\n```go\ntype ORMType string\n```\nORMType represents the ORM to use for database operations\n\n```go\nconst (\n\t// ORMTypeBun represents Bun ORM\n\tORMTypeBun ORMType = \"bun\"\n\n\t// ORMTypeGORM represents GORM\n\tORMTypeGORM ORMType = \"gorm\"\n\n\t// ORMTypeNative represents native database/sql\n\tORMTypeNative ORMType = \"native\"\n)\n```\n\n```go\ntype Provider = providers.Provider\n```\nProvider is an alias to the providers.Provider interface This allows dbmanager package consumers to use Provider without importing providers\n\n","path":"github.com/bitechdev/ResolveSpec/pkg/dbmanager","name":"dbmanager","synopsis":"","isRedistributable":true}
