morm

package module
v0.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 16, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

README

Morm

混合ORM(Mixed orm),配置数据源源即可轻松访问不同类型的数据库,目前支持mysqlmongodbsqlite,底层使用gormmongo-driver实现。

配置文件使用viper进行解析

配置文件参考

[db]
log = './db.log'    # 日志文件路径
loglevel = '4'  # 日志等级 
type = 'mysql' # 默认orm类型

[mongodb]
# mongodb连接的数据库
database = 'testorm'    
# 连接池大小
option_pool_size = '200'   
# mongodb代理连接方法
proxy = 'socks5://127.0.0.1:7890'  
# mongodb连接uri mongodb://[认证用户名]:[认证密码]@[连接地址]/[额外参数]
uri = 'mongodb://mgouser:mgopass@127.0.0.1:27027/?authSource=admin' 

[mysql]
# mysql连接数据库
database = 'testorm'
# 数据库编码
charset = 'utf8mb4'
# 连接最大生命时间
conn_max_lifetime = '1h'
# mysql连接主机
host = '127.0.0.1'
# mysql连接端口
port = '3306'
# 最大空闲连接数
max_idle_conns = '10'
# 最大连接数
max_open_conns = '100'
# mysql认证用户
user = 'orm'
# mysql认证密码
password = 'password'

[sqlite]
# sqlite数据库文件路径
file_path = './data.db'
# 连接最大生命时间
conn_max_lifetime = '1h'
# 最大空闲连接数
max_idle_conns = '10'
# 最大连接数
max_open_conns = '100'

配置文件读取使用了viper,所以支持多种配置文件格式,如jsonyamltomlini等,详情请参考viper文档。

也可以直接传入viper实例进行读取。

使用案例

package main

import (
	"fmt"

	"github.com/lfhy/morm"
)

// 数据库结构体
// 如果数据在mongo需要按mongo-driver进行标注
// 在其他gorm的(mysql,sqlite)按gorm进行标注
type DBSturct struct {
	ID   string `bson:"_id" gorm:"id"`
	Name string `bson:"name" gorm:"name"`
}

// 表名或集合名
func (DBSturct) TableName() string {
	return "dbtable"
}

func main() {
	// 初始化配置文件
	configPath := "/path/to/config.toml"
	err := morm.InitORMConfig(configPath)
	if err != nil {
		fmt.Printf("配置文件加载错误:%v\n", err)
		panic(err)
	}
	// 使用自定义日志:db.SetLogger
	// 数据库初始化
	// 也可以自己处理错误
	// orm, err := morm.InitWithError(configPath)
	// 也可以直接传入配置文件
	// orm := morm.Init(configPath)
	// 如果已经用viper解析了,那也可以使用对应的配置实例 morm.UseViperConfig(viperConfig)
	orm := morm.Init()

	// 创建查询
	var db DBSturct
	db.ID = "123"

	err = orm.Model(&db).Find().One(&db)
	if err != nil {
		fmt.Printf("查询失败:%v\n", err)
		return
	}
	fmt.Printf("查询结果:%+v\n", db)
}

TODO

  • 添加测试案例

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetConfig added in v0.3.1

func GetConfig() *viper.Viper

获取当前viper配置实例

func InitORMConfig added in v0.0.2

func InitORMConfig(configFilePath string) (err error)

初始化orm配置文件

func UseViperConfig added in v0.3.1

func UseViperConfig(config *viper.Viper)

设置viper配置

Types

type BoolORM

type BoolORM = orm.BoolORM

为了避免识别错误设置Bool类型为int类型

const (
	TrueInt  BoolORM = 1
	FalseInt BoolORM = -1
	// 空字符串
	EmptyStr = "-"
)

type BulkWriteOperation added in v0.1.9

type BulkWriteOperation = orm.BulkWriteOperation

type MongoBulkWriteOperation added in v0.1.9

type MongoBulkWriteOperation = orm.MongoBulkWriteOperation

type ORM

type ORM = orm.ORM

func Init added in v0.0.2

func Init(configPath ...string) ORM

初始化ORM连接(带错误panic)

func InitMongoDB added in v0.0.2

func InitMongoDB(configPath ...string) ORM

初始化MongoDB连接(带错误panic)

func InitMongoDBWithError added in v0.3.1

func InitMongoDBWithError(configPath ...string) (ORM, error)

初始化MongoDB连接(返回错误)

func InitMySQL added in v0.0.2

func InitMySQL(configPath ...string) ORM

初始化MySQL连接(带错误panic)

func InitMySQLWithError added in v0.3.1

func InitMySQLWithError(configPath ...string) (ORM, error)

初始化MySQL连接(返回错误)

func InitSQLite added in v0.1.0

func InitSQLite(configPath ...string) ORM

初始化SQLite连接(带错误panic)

func InitSQLiteWithError added in v0.3.1

func InitSQLiteWithError(configPath ...string) (ORM, error)

初始化SQLite连接(返回错误)

func InitWithError added in v0.3.1

func InitWithError(configPath ...string) (ORM, error)

初始化ORM连接(返回错误)

type ORMModel

type ORMModel = orm.ORMModel

type ORMQuary

type ORMQuary = orm.ORMQuary

Directories

Path Synopsis
db
example
find command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL