mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2026-01-16 16:04:25 +00:00
feat(dbmanager): ✨ add support for existing SQL connections
* Introduced NewConnectionFromDB function to create connections from existing *sql.DB instances. * Added ExistingDBProvider to wrap existing database connections for dbmanager features. * Implemented tests for NewConnectionFromDB and ExistingDBProvider functionalities.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package dbmanager
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"github.com/bitechdev/ResolveSpec/pkg/dbmanager/providers"
|
||||
@@ -49,3 +50,18 @@ func createProvider(dbType DatabaseType) (Provider, error) {
|
||||
// Provider is an alias to the providers.Provider interface
|
||||
// This allows dbmanager package consumers to use Provider without importing providers
|
||||
type Provider = providers.Provider
|
||||
|
||||
// NewConnectionFromDB 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
|
||||
//
|
||||
// Parameters:
|
||||
// - name: A unique name for this connection
|
||||
// - dbType: The database type (DatabaseTypePostgreSQL, DatabaseTypeSQLite, or DatabaseTypeMSSQL)
|
||||
// - db: An existing *sql.DB connection
|
||||
//
|
||||
// Returns a Connection that wraps the existing *sql.DB
|
||||
func NewConnectionFromDB(name string, dbType DatabaseType, db *sql.DB) Connection {
|
||||
provider := providers.NewExistingDBProvider(db, name)
|
||||
return newSQLConnection(name, dbType, ConnectionConfig{Name: name, Type: dbType}, provider)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user