Files
gormx/options.go
T
2024-01-10 23:08:32 +08:00

58 lines
1.1 KiB
Go

package gormx
type clientOptions struct {
Db string
Mysql *MysqlOptions
Sqlite *SqliteOptions
Prefix string // 表名前缀,`Article` 的表名应该是 `it_articles`
SingularTable bool // 使用单数表名,启用该选项,此时,`Article` 的表名应该是 `it_article`
}
type MysqlOptions struct {
User string
Password string
Host string
Port int
Database string
}
type SqliteOptions struct {
DbPath string
}
func defaultOptions() clientOptions {
return clientOptions{
SingularTable: true,
}
}
type Option func(*clientOptions)
func SetMysql(user, password, host, database string, port int) Option {
return func(o *clientOptions) {
o.Db = "mysql"
o.Mysql = &MysqlOptions{
User: user,
Host: host,
Port: port,
Database: database,
Password: password,
}
}
}
func SetSqlite(dbPath string) Option {
return func(o *clientOptions) {
o.Db = "sqlite"
o.Sqlite = &SqliteOptions{
DbPath: dbPath,
}
}
}
func SetTablePrefix(perfix string) Option {
return func(o *clientOptions) {
o.Prefix = perfix
}
}