github.com/wfusion/gofusion@v1.1.14/common/infra/drivers/orm/interface.go (about)

     1  package orm
     2  
     3  import (
     4  	"context"
     5  
     6  	"gorm.io/gorm"
     7  	"gorm.io/gorm/logger"
     8  
     9  	"github.com/wfusion/gofusion/common/utils"
    10  )
    11  
    12  type Dialect interface {
    13  	New(ctx context.Context, option Option, opts ...utils.OptionExtender) (db *DB, err error)
    14  }
    15  
    16  type newOption struct {
    17  	logger logger.Interface
    18  }
    19  
    20  // Option db option
    21  //nolint: revive // struct tag too long issue
    22  type Option struct {
    23  	Driver          driver  `yaml:"driver" json:"driver" toml:"driver"`
    24  	Dialect         dialect `yaml:"dialect" json:"dialect" toml:"dialect"`
    25  	DB              string  `yaml:"db" json:"db" toml:"db"`
    26  	Host            string  `yaml:"host" json:"host" toml:"host"`
    27  	Port            uint    `yaml:"port" json:"port" toml:"port"`
    28  	User            string  `yaml:"user" json:"user" toml:"user"`
    29  	Password        string  `yaml:"password" json:"password" toml:"password" encrypted:""`
    30  	Timeout         string  `yaml:"timeout" json:"timeout" toml:"timeout" default:"5s"`
    31  	ReadTimeout     string  `yaml:"read_timeout" json:"read_timeout" toml:"read_timeout" default:"2s"`
    32  	WriteTimeout    string  `yaml:"write_timeout" json:"write_timeout" toml:"write_timeout" default:"2s"`
    33  	MaxIdleConns    int     `yaml:"max_idle_conns" json:"max_idle_conns" toml:"max_idle_conns" default:"20"`
    34  	MaxOpenConns    int     `yaml:"max_open_conns" json:"max_open_conns" toml:"max_open_conns" default:"20"`
    35  	ConnMaxLifeTime string  `yaml:"conn_max_life_time" json:"conn_max_life_time" toml:"conn_max_life_time" default:"30m"`
    36  	ConnMaxIdleTime string  `yaml:"conn_max_idle_time" json:"conn_max_idle_time" toml:"conn_max_idle_time" default:"15m"`
    37  }
    38  
    39  type DB struct {
    40  	*gorm.DB
    41  	dialector gorm.Dialector
    42  }
    43  
    44  func (d *DB) GetProxy() *gorm.DB {
    45  	return d.DB
    46  }
    47  
    48  func (d *DB) GetDialector() gorm.Dialector {
    49  	return d.dialector
    50  }
    51  
    52  func (d *DB) WithContext(ctx context.Context) *DB {
    53  	return &DB{
    54  		DB:        d.DB.WithContext(ctx),
    55  		dialector: d.dialector,
    56  	}
    57  }