Mixed ORM that allows easy access to different types of databases by configuring data sources. Currently supports mysql, mongodb and sqlite, implemented using gorm and mongo-driver.
Configuration files are parsed using viper.
In addition to initializing the ORM with a configuration file, you can also initialize the ORM directly through the DBConfig struct:
package main
import (
"github.com/lfhy/morm"
"github.com/lfhy/morm/conf"
)
func main() {
// Initialize via struct configuration
dbConfig := &conf.DBConfig{
Type: "sqlite",
LogConfig: &conf.LogConfig{
Log: "./db.log",
LogLevel: "4",
},
SQLiteConfig: &conf.SQLiteConfig{
AutoCreateTable: true,
FilePath: "./test.db",
ConnMaxLifetime: "1h",
MaxIdleConns: "10",
MaxOpenConns: "100",
},
}
// Initialize ORM with configuration struct
orm := morm.InitWithDBConfig(dbConfig)
// Use ORM for database operations...
}[db]
log = './db.log' # Log file path
loglevel = '4' # Log level
type = 'mysql' # Default ORM type
[mongodb]
# Database to connect to for mongodb
database = 'testorm'
# Connection pool size
option_pool_size = '200'
# Mongodb proxy connection method
proxy = 'socks5://127.0.0.1:7890'
# Mongodb connection URI mongodb://[username]:[password]@[address]/[parameters]
uri = 'mongodb://mgouser:mgopass@127.0.0.1:27027/?authSource=admin'
[mysql]
# MySQL database to connect to
database = 'testorm'
# Database encoding
charset = 'utf8mb4'
# Connection maximum lifetime
conn_max_lifetime = '1h'
# MySQL connection host
host = '127.0.0.1'
# MySQL connection port
port = '3306'
# Maximum idle connections
max_idle_conns = '10'
# Maximum open connections
max_open_conns = '100'
# MySQL authentication user
user = 'orm'
# MySQL authentication password
password = 'password'
[sqlite]
# SQLite database file path
file_path = './data.db'
# Connection maximum lifetime
conn_max_lifetime = '1h'
# Maximum idle connections
max_idle_conns = '10'
# Maximum open connections
max_open_conns = '100'Configuration file reading uses viper, so multiple configuration file formats are supported, such as json, yaml, toml, ini, etc. For details, please refer to viper documentation.
You can also pass in a viper instance for reading.
package main
import (
"fmt"
"github.com/lfhy/morm"
)
// Database struct
// If data is in mongo, annotate according to mongo-driver
// In other gorm databases (mysql, sqlite), annotate according to gorm
type DBSturct struct {
ID string `bson:"_id" gorm:"id"`
Name string `bson:"name" gorm:"name"`
}
// Table name or collection name
func (DBSturct) TableName() string {
return "dbtable"
}
func main() {
// Initialize configuration file
configPath := "/path/to/config.toml"
err := morm.InitORMConfig(configPath)
if err != nil {
fmt.Printf("Configuration file loading error:%v\n", err)
panic(err)
}
// Use custom logger: db.SetLogger
// Database initialization
// You can also handle errors yourself
// orm, err := morm.InitWithError(configPath)
// You can also pass the configuration file directly
// orm := morm.Init(configPath)
// If you've already parsed with viper, you can also use the corresponding configuration instance morm.UseViperConfig(viperConfig)
orm := morm.Init()
// Create query
var db DBSturct
db.ID = "123"
err = orm.Model(&db).Find().One(&db)
if err != nil {
fmt.Printf("Query failed:%v\n", err)
return
}
fmt.Printf("Query result:%+v\n", db)
}- Add test cases