mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-01 14:46:36 +08:00
refactor(core): align with cordis spatiotemporal composability architecture
- Purify core micro-kernel by removing context hardcoded helpers and reverse dependencies - Eliminate init() side effects in infra plugins with reversible lifecycle disposal - Completely isolate plugins by removing cross-plugin imports and using core/contracts - Introduce TaskService and RiskControlService contracts for unified cross-plugin APIs - Regenerate Swagger documentation and update developer guide matrix - Achieve 0 violations in check_cordis_architecture.sh and 100% test pass
This commit is contained in:
@@ -7,9 +7,10 @@ package database
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"Wavelet/core"
|
||||
"Wavelet/core/contracts"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Option configures the database plugin.
|
||||
@@ -60,7 +61,11 @@ func (p *Plugin) Name() string {
|
||||
func (p *Plugin) Apply(ctx *core.Context) error {
|
||||
targetDB := p.db
|
||||
if targetDB == nil {
|
||||
targetDB = DB(context.Background())
|
||||
var err error
|
||||
targetDB, err = InitDB()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
svc := &dbServiceImpl{
|
||||
@@ -68,10 +73,21 @@ func (p *Plugin) Apply(ctx *core.Context) error {
|
||||
namedDBs: p.namedDBs,
|
||||
}
|
||||
|
||||
if sqlDB, err := targetDB.DB(); err == nil && sqlDB != nil {
|
||||
ctx.OnDispose(func() error {
|
||||
return sqlDB.Close()
|
||||
})
|
||||
}
|
||||
|
||||
core.Provide[contracts.DBService](ctx, svc)
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewService wraps a GORM DB instance into a contracts.DBService.
|
||||
func NewService(primary *gorm.DB) contracts.DBService {
|
||||
return &dbServiceImpl{primary: primary}
|
||||
}
|
||||
|
||||
type dbServiceImpl struct {
|
||||
primary *gorm.DB
|
||||
namedDBs map[string]*gorm.DB
|
||||
|
||||
Reference in New Issue
Block a user