diff --git a/session/database/service.go b/session/database/service.go index 56e808ad..212fb36c 100644 --- a/session/database/service.go +++ b/session/database/service.go @@ -49,6 +49,20 @@ func NewSessionService(dialector gorm.Dialector, opts ...gorm.Option) (session.S return &databaseService{db: db}, nil } +// NewSessionServiceGorm creates a new [session.Service] implementation that uses a +// relational database (e.g., PostgreSQL, Spanner, SQLite) via an already initialized GORM DB. +// This allows reusing the same database across multiple ADK services and your application. +// +// It requires an initialized [*gorm.DB] to specify the database connection. +// +// It returns the new [session.Service] or an error if the provided *gorm.DB is nil. +func NewSessionServiceGorm(db *gorm.DB) (session.Service, error) { + if db == nil { + return nil, fmt.Errorf("session service: gorm.DB cannot be nil") + } + return &databaseService{db: db}, nil +} + // AutoMigrate runs the GORM auto-migration tool to ensure the database schema // matches the internal storage models (e.g., storageSession, storageEvent). //