Skip to content

Commit 5cb6ba1

Browse files
committed
Ensuring db lazily resolves
1 parent d372118 commit 5cb6ba1

2 files changed

Lines changed: 129 additions & 14 deletions

File tree

application.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,8 @@ func (app *StdApplication) injectServices(module Module) (Module, error) {
369369
return nil, err
370370
}
371371

372+
app.logger.Debug("Injecting dependencies", "dependencies", dependencies, "module", module.Name())
373+
372374
// If module supports constructor injection, use it
373375
if withConstructor, ok := module.(Constructable); ok {
374376
return app.constructModuleWithServices(withConstructor, requiredServices)

modules/database/module.go

Lines changed: 127 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,131 @@ import (
1212
// Module name constant
1313
const Name = "database"
1414

15+
// lazyDefaultService is a wrapper that lazily resolves the default database service
16+
type lazyDefaultService struct {
17+
module *Module
18+
}
19+
20+
func (l *lazyDefaultService) Connect() error {
21+
service := l.module.GetDefaultService()
22+
if service == nil {
23+
return errors.New("no default database service available")
24+
}
25+
return service.Connect()
26+
}
27+
28+
func (l *lazyDefaultService) Close() error {
29+
service := l.module.GetDefaultService()
30+
if service == nil {
31+
return errors.New("no default database service available")
32+
}
33+
return service.Close()
34+
}
35+
36+
func (l *lazyDefaultService) DB() *sql.DB {
37+
service := l.module.GetDefaultService()
38+
if service == nil {
39+
return nil
40+
}
41+
return service.DB()
42+
}
43+
44+
func (l *lazyDefaultService) Ping(ctx context.Context) error {
45+
service := l.module.GetDefaultService()
46+
if service == nil {
47+
return errors.New("no default database service available")
48+
}
49+
return service.Ping(ctx)
50+
}
51+
52+
func (l *lazyDefaultService) Stats() sql.DBStats {
53+
service := l.module.GetDefaultService()
54+
if service == nil {
55+
return sql.DBStats{}
56+
}
57+
return service.Stats()
58+
}
59+
60+
func (l *lazyDefaultService) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
61+
service := l.module.GetDefaultService()
62+
if service == nil {
63+
return nil, errors.New("no default database service available")
64+
}
65+
return service.ExecContext(ctx, query, args...)
66+
}
67+
68+
func (l *lazyDefaultService) Exec(query string, args ...interface{}) (sql.Result, error) {
69+
service := l.module.GetDefaultService()
70+
if service == nil {
71+
return nil, errors.New("no default database service available")
72+
}
73+
return service.Exec(query, args...)
74+
}
75+
76+
func (l *lazyDefaultService) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) {
77+
service := l.module.GetDefaultService()
78+
if service == nil {
79+
return nil, errors.New("no default database service available")
80+
}
81+
return service.PrepareContext(ctx, query)
82+
}
83+
84+
func (l *lazyDefaultService) Prepare(query string) (*sql.Stmt, error) {
85+
service := l.module.GetDefaultService()
86+
if service == nil {
87+
return nil, errors.New("no default database service available")
88+
}
89+
return service.Prepare(query)
90+
}
91+
92+
func (l *lazyDefaultService) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
93+
service := l.module.GetDefaultService()
94+
if service == nil {
95+
return nil, errors.New("no default database service available")
96+
}
97+
return service.QueryContext(ctx, query, args...)
98+
}
99+
100+
func (l *lazyDefaultService) Query(query string, args ...interface{}) (*sql.Rows, error) {
101+
service := l.module.GetDefaultService()
102+
if service == nil {
103+
return nil, errors.New("no default database service available")
104+
}
105+
return service.Query(query, args...)
106+
}
107+
108+
func (l *lazyDefaultService) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row {
109+
service := l.module.GetDefaultService()
110+
if service == nil {
111+
return nil
112+
}
113+
return service.QueryRowContext(ctx, query, args...)
114+
}
115+
116+
func (l *lazyDefaultService) QueryRow(query string, args ...interface{}) *sql.Row {
117+
service := l.module.GetDefaultService()
118+
if service == nil {
119+
return nil
120+
}
121+
return service.QueryRow(query, args...)
122+
}
123+
124+
func (l *lazyDefaultService) BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error) {
125+
service := l.module.GetDefaultService()
126+
if service == nil {
127+
return nil, errors.New("no default database service available")
128+
}
129+
return service.BeginTx(ctx, opts)
130+
}
131+
132+
func (l *lazyDefaultService) Begin() (*sql.Tx, error) {
133+
service := l.module.GetDefaultService()
134+
if service == nil {
135+
return nil, errors.New("no default database service available")
136+
}
137+
return service.Begin()
138+
}
139+
15140
// Module represents the database module
16141
type Module struct {
17142
config *Config
@@ -53,7 +178,7 @@ func (m *Module) RegisterConfig(app modular.Application) error {
53178
// Init initializes the database module
54179
func (m *Module) Init(app modular.Application) error {
55180
// Get the configuration
56-
provider, err := app.GetConfigSection("database")
181+
provider, err := app.GetConfigSection(m.Name())
57182
if err != nil {
58183
return fmt.Errorf("failed to get config section: %w", err)
59184
}
@@ -71,11 +196,6 @@ func (m *Module) Init(app modular.Application) error {
71196
return fmt.Errorf("failed to initialize database connections: %w", err)
72197
}
73198

74-
// Register services
75-
if err := m.registerServices(app); err != nil {
76-
return fmt.Errorf("failed to register database services: %w", err)
77-
}
78-
79199
return nil
80200
}
81201

@@ -121,7 +241,7 @@ func (m *Module) ProvidesServices() []modular.ServiceProvider {
121241
{
122242
Name: "database.service",
123243
Description: "Default database service",
124-
Instance: m.GetDefaultService(), // This can be nil if no connections are configured
244+
Instance: &lazyDefaultService{module: m}, // Lazy wrapper that resolves at runtime
125245
},
126246
}
127247

@@ -223,10 +343,3 @@ func (m *Module) initializeConnections() error {
223343

224344
return nil
225345
}
226-
227-
// registerServices registers the database services with the application
228-
func (m *Module) registerServices(app modular.Application) error {
229-
// Services are registered through ProvidesServices() by the modular framework
230-
// This method is kept for any additional service registration logic if needed in the future
231-
return nil
232-
}

0 commit comments

Comments
 (0)