跳转到内容

配置选项

Pie 提供了丰富的配置选项来满足不同部署环境和性能需求。

// 基础配置
engine, err := pie.NewEngine(
context.Background(),
"mydb",
pie.WithURI("mongodb://localhost:27017"),
)
// 完整连接配置
engine, err := pie.NewEngine(ctx, "mydb",
pie.WithURI("mongodb://localhost:27017"),
pie.WithAuth("username", "password"),
pie.WithSSL(true),
pie.WithReplicaSet("rs0"),
pie.WithReadPreference("secondary"),
pie.WithWriteConcern("majority"),
pie.WithReadConcern("majority"),
)
engine, err := pie.NewEngine(ctx, "mydb",
pie.WithMaxPoolSize(100), // 最大连接数
pie.WithMinPoolSize(5), // 最小连接数
pie.WithMaxIdleTime(30*time.Minute), // 最大空闲时间
)
engine, err := pie.NewEngine(ctx, "mydb",
pie.WithConnectTimeout(10*time.Second), // 连接超时
pie.WithSocketTimeout(30*time.Second), // 套接字超时
pie.WithServerSelectionTimeout(5*time.Second), // 服务器选择超时
)
// 读偏好配置
engine, err := pie.NewEngine(ctx, "mydb",
pie.WithReadPreference("primary"), // 主节点
pie.WithReadPreference("secondary"), // 从节点
pie.WithReadPreference("primaryPreferred"), // 主节点优先
pie.WithReadPreference("secondaryPreferred"), // 从节点优先
pie.WithReadPreference("nearest"), // 最近节点
)
// 写关注配置
engine, err := pie.NewEngine(ctx, "mydb",
pie.WithWriteConcern("majority"), // 大多数节点确认
pie.WithWriteConcern("1"), // 单个节点确认
pie.WithWriteConcern("0"), // 无确认
)
// 读关注配置
engine, err := pie.NewEngine(ctx, "mydb",
pie.WithReadConcern("local"), // 本地读关注
pie.WithReadConcern("available"), // 可用读关注
pie.WithReadConcern("majority"), // 大多数读关注
pie.WithReadConcern("linearizable"), // 线性化读关注
)
// 用户名密码认证
engine, err := pie.NewEngine(ctx, "mydb",
pie.WithAuth("username", "password"),
)
// 认证数据库
engine, err := pie.NewEngine(ctx, "mydb",
pie.WithAuth("username", "password"),
pie.WithAuthSource("admin"),
)
// SSL 配置
engine, err := pie.NewEngine(ctx, "mydb",
pie.WithSSL(true),
pie.WithSSLInsecure(false),
pie.WithSSLCertFile("/path/to/cert.pem"),
pie.WithSSLKeyFile("/path/to/key.pem"),
pie.WithSSLCAFile("/path/to/ca.pem"),
)
// 查询配置
engine, err := pie.NewEngine(ctx, "mydb",
pie.WithQueryLog(os.Stdout), // 查询日志
pie.WithQueryTimeout(30*time.Second), // 查询超时
)
// 缓存配置
engine, err := pie.NewEngine(ctx, "mydb",
pie.WithCache(pie.NewMemoryCache(), &pie.CacheConfig{
TTL: 5 * time.Minute,
MaxSize: 1000,
}),
)
// 命名映射配置
engine, err := pie.NewEngine(ctx, "mydb",
pie.WithMapper(&pie.SnakeMapper{}), // 蛇形命名
pie.WithMapper(&pie.CamelMapper{}), // 驼峰命名
pie.WithMapper(&pie.SameMapper{}), // 相同命名
)
// 自定义映射器
type CustomMapper struct{}
func (m CustomMapper) TableName(structName string) string {
return "t_" + strings.ToLower(structName)
}
func (m CustomMapper) FieldName(fieldName string) string {
return strings.ToLower(fieldName)
}
engine, err := pie.NewEngine(ctx, "mydb",
pie.WithMapper(CustomMapper{}),
)
// 日志配置
engine, err := pie.NewEngine(ctx, "mydb",
pie.WithLogger(log.New(os.Stdout, "PIE: ", log.LstdFlags)),
pie.WithLogLevel(pie.LogLevelInfo),
pie.WithQueryLog(os.Stdout),
)
// 监控配置
engine, err := pie.NewEngine(ctx, "mydb",
pie.WithMetrics(true), // 启用指标
pie.WithHealthCheck(true), // 启用健康检查
pie.WithPingInterval(30*time.Second), // Ping 间隔
)
选项类型默认值描述
WithURIstring-MongoDB 连接 URI
WithAuthstring, string-用户名和密码
WithAuthSourcestring”admin”认证数据库
WithSSLboolfalse启用 SSL
WithSSLInsecureboolfalse跳过 SSL 验证
WithSSLCertFilestring-SSL 证书文件
WithSSLKeyFilestring-SSL 私钥文件
WithSSLCAFilestring-SSL CA 文件
选项类型默认值描述
WithMaxPoolSizeuint64100最大连接数
WithMinPoolSizeuint640最小连接数
WithMaxIdleTimetime.Duration0最大空闲时间
WithConnectTimeouttime.Duration30s连接超时
WithSocketTimeouttime.Duration0套接字超时
WithServerSelectionTimeouttime.Duration30s服务器选择超时
选项类型默认值描述
WithReadPreferencestring”primary”读偏好
WithWriteConcernstring”majority”写关注
WithReadConcernstring”local”读关注
选项类型默认值描述
WithQueryLogio.Writer-查询日志输出
WithQueryTimeouttime.Duration-查询超时
WithCacheCache, *CacheConfig-缓存配置
func createDevEngine() (*pie.Engine, error) {
return pie.NewEngine(ctx, "dev_db",
pie.WithURI("mongodb://localhost:27017"),
pie.WithQueryLog(os.Stdout),
pie.WithMapper(&pie.SnakeMapper{}),
)
}
func createTestEngine() (*pie.Engine, error) {
return pie.NewEngine(ctx, "test_db",
pie.WithURI("mongodb://localhost:27017"),
pie.WithMaxPoolSize(10),
pie.WithMinPoolSize(1),
pie.WithMapper(&pie.SameMapper{}),
)
}
func createProdEngine() (*pie.Engine, error) {
return pie.NewEngine(ctx, "prod_db",
pie.WithURI("mongodb://prod-cluster:27017"),
pie.WithAuth(os.Getenv("DB_USER"), os.Getenv("DB_PASS")),
pie.WithSSL(true),
pie.WithReplicaSet("rs0"),
pie.WithReadPreference("secondaryPreferred"),
pie.WithWriteConcern("majority"),
pie.WithMaxPoolSize(100),
pie.WithMinPoolSize(10),
pie.WithCache(pie.NewRedisCache("redis:6379", "", 0), &pie.CacheConfig{
TTL: 10 * time.Minute,
}),
)
}
func validateConfig(engine *pie.Engine) error {
// 检查连接
if err := engine.Ping(ctx); err != nil {
return fmt.Errorf("database connection failed: %w", err)
}
// 检查权限
if err := engine.CheckPermissions(ctx); err != nil {
return fmt.Errorf("insufficient permissions: %w", err)
}
return nil
}
func testConfiguration() error {
engine, err := createProdEngine()
if err != nil {
return err
}
defer engine.Disconnect(ctx)
// 测试基本操作
session := pie.Table[User](engine)
_, err = session.Count(ctx)
if err != nil {
return fmt.Errorf("basic query failed: %w", err)
}
// 测试事务
err = engine.WithTransaction(ctx, func(txCtx context.Context) error {
return nil
})
if err != nil {
return fmt.Errorf("transaction test failed: %w", err)
}
return nil
}
func createEngineForEnvironment() (*pie.Engine, error) {
env := os.Getenv("ENVIRONMENT")
switch env {
case "development":
return createDevEngine()
case "testing":
return createTestEngine()
case "production":
return createProdEngine()
default:
return nil, fmt.Errorf("unknown environment: %s", env)
}
}
type Config struct {
Database struct {
URI string `yaml:"uri"`
Database string `yaml:"database"`
Auth struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
} `yaml:"auth"`
} `yaml:"database"`
Cache struct {
Type string `yaml:"type"`
TTL int `yaml:"ttl"`
} `yaml:"cache"`
}
func loadConfig() (*Config, error) {
data, err := os.ReadFile("config.yaml")
if err != nil {
return nil, err
}
var config Config
err = yaml.Unmarshal(data, &config)
return &config, err
}
func validateEngineConfig(config *Config) error {
if config.Database.URI == "" {
return errors.New("database URI is required")
}
if config.Database.Database == "" {
return errors.New("database name is required")
}
return nil
}