TORM-数据库支持


TORM支持多种主流数据库,提供统一的API接口,让您可以轻松在不同数据库间切换。

🗄️ 支持的数据库

数据库 驱动 状态 版本要求
MySQL mysql ✅ 完全支持 5.7+ / 8.0+
PostgreSQL postgres ✅ 完全支持 11+
SQLite sqlite ✅ 完全支持 3.8+
MongoDB mongodb ✅ 完全支持 4.4+
SQL Server sqlserver 🚧 基础支持 2017+

🔧 MySQL

配置示例

go 复制代码
config := &db.Config{
    Driver:   "mysql",
    Host:     "localhost",
    Port:     3306,
    Database: "myapp",
    Username: "root",
    Password: "password",
    Charset:  "utf8mb4",
    
    Options: map[string]string{
        "parseTime": "true",
        "loc":       "Local",
    },
}

特性支持

  • ✅ 完整的SQL支持
  • ✅ 事务处理
  • ✅ 连接池
  • ✅ JSON字段
  • ✅ 全文索引
  • ✅ 外键约束

🐘 PostgreSQL

配置示例

go 复制代码
config := &db.Config{
    Driver:   "postgres",
    Host:     "localhost",
    Port:     5432,
    Database: "myapp",
    Username: "postgres",
    Password: "password",
    SSLMode:  "disable",
}

特性支持

  • ✅ 完整的SQL支持
  • ✅ JSONB支持
  • ✅ 数组类型
  • ✅ 高级索引
  • ✅ 窗口函数

📁 SQLite

配置示例

go 复制代码
config := &db.Config{
    Driver:   "sqlite",
    Database: "app.db",
    
    Options: map[string]string{
        "foreign_keys": "on",
    },
}

特性支持

  • ✅ 轻量级部署
  • ✅ 零配置
  • ✅ 嵌入式应用
  • ⚠️ 并发限制

🍃 MongoDB

配置示例

go 复制代码
config := &db.Config{
    Driver:   "mongodb",
    Host:     "localhost",
    Port:     27017,
    Database: "myapp",
}

特性支持

  • ✅ 文档存储
  • ✅ 聚合管道
  • ✅ 索引支持
  • ✅ 事务支持(副本集)

🔄 数据库切换

TORM的设计允许您轻松在不同数据库间切换:

go 复制代码
// 开发环境 - SQLite
devConfig := &db.Config{
    Driver:   "sqlite",
    Database: "dev.db",
}

// 生产环境 - MySQL
prodConfig := &db.Config{
    Driver:   "mysql",
    Host:     "prod-db.example.com",
    Database: "myapp",
    Username: "app_user",
    Password: "secure_password",
}
相关推荐
corpse20109 分钟前
FastMonitor - 网络流量监控与威胁检测工具--各种报错!!!
go
源代码•宸2 天前
Leetcode—1929. 数组串联&&Q1. 数组串联【简单】
经验分享·后端·算法·leetcode·go
nil2 天前
记录protoc生成代码将optional改成omitepty问题
后端·go·protobuf
Way2top3 天前
Go语言动手写Web框架 - Gee第五天 中间件
后端·go
Way2top3 天前
Go语言动手写Web框架 - Gee第四天 分组控制
后端·go
Grassto3 天前
从 `go build` 开始:Go 第三方包加载流程源码导读
golang·go·go module
源代码•宸3 天前
Golang基础语法(go语言结构体、go语言数组与切片、go语言条件句、go语言循环)
开发语言·经验分享·后端·算法·golang·go
華勳全栈4 天前
两天开发完成智能体平台
java·spring·go
stark张宇5 天前
Go语言核心三剑客:数组、切片与结构体使用指南
后端·go
Aevget5 天前
智能高效Go开发工具GoLand v2025.3全新上线——新增资源泄漏分析
开发语言·ide·后端·golang·go