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",
}
相关推荐
妙码生花1 天前
PHP 各框架下和 Go 的性能比较
前端·后端·go
江湖十年1 天前
Go 还是 Golang?可能你一直都搞错了!
后端·面试·go
数字化小张2 天前
Go语言并发编程——goroutine与channel实战
go
码艺-Alimjan2 天前
Tauri 2.x + Vue 3 桌面应用开发实战:从踩坑到完美落地
前端·javascript·vue.js·rust·typescript·go
mldong3 天前
Go 开发者也有自己的轻量工作流引擎了:go get 一行,5 分钟跑通一条审批流
后端·go
newerp3 天前
Golang 调度循环:Go runtime 如何永不停歇地找人干活
后端·程序员·go
小满zs4 天前
Go语言第十一章(协程)
后端·go
newerp4 天前
Golang 函数调用的瞬间:栈帧、拷贝栈与逃逸分析
后端·程序员·go
newerp4 天前
Golang 网络轮询器 netpoll:把阻塞 IO 变成事件通知
后端·程序员·go