update cli

v3
veypi 6 months ago
parent 71924315b4
commit 0e8ef78957

@ -9,7 +9,6 @@ package cfg
import ( import (
"github.com/glebarez/sqlite" "github.com/glebarez/sqlite"
"github.com/veypi/OneBD/rest/middlewares/crud"
"gorm.io/driver/mysql" "gorm.io/driver/mysql"
"gorm.io/driver/postgres" "gorm.io/driver/postgres"
"gorm.io/gorm" "gorm.io/gorm"
@ -17,7 +16,6 @@ import (
) )
var db *gorm.DB var db *gorm.DB
var StaticObjs = crud.New()
func DB() *gorm.DB { func DB() *gorm.DB {
if db == nil { if db == nil {

@ -1,36 +0,0 @@
//
// Copyright (C) 2024 veypi <i@veypi.com>
// 2025-03-04 16:08:06
// Distributed under terms of the MIT license.
//
package main
import (
"oa/cfg"
"github.com/veypi/utils/flags"
"github.com/veypi/utils/logv"
)
var CMD = flags.New("app", "the backend server of app")
var CfgDump = CMD.SubCommand("cfg", "generate cfg file")
var configFile = CMD.String("f", "./dev.yaml", "the config file")
func init() {
CMD.StringVar(&cfg.Config.Host, "host", "0.0.0.0", "host")
CMD.IntVar(&cfg.Config.Port, "p", 4000, "port")
CMD.StringVar(&cfg.Config.LoggerLevel, "l", "info", "log level")
CMD.StringVar(&cfg.Config.DSN, "dsn", "root:123456@tcp(127.0.0.1:3306)/test?charset=utf8&parseTime=True&loc=Local", "data source name")
CMD.StringVar(&cfg.Config.DB, "db", "mysql", "data source type: mysql/postgre/sqlite")
CMD.Before = func() error {
flags.LoadCfg(*configFile, cfg.Config)
CMD.Parse()
logv.SetLevel(logv.AssertFuncErr(logv.ParseLevel(cfg.Config.LoggerLevel)))
return nil
}
CfgDump.Command = func() error {
flags.DumpCfg(*configFile, cfg.Config)
return nil
}
}

@ -1,26 +0,0 @@
//
// Copyright (C) 2024 veypi <i@veypi.com>
// 2025-03-04 16:08:06
// Distributed under terms of the MIT license.
//
package main
import (
"oa/cfg"
"oa/models"
"github.com/veypi/OneBD/rest/middlewares/crud"
)
var cmdDB = CMD.SubCommand("db", "database operations")
func init() {
cmdDB.SubCommand("migrate", "migrate database").Command = func() error {
return crud.AutoMigrate(cfg.DB(), cfg.StaticObjs)
}
cmdDB.SubCommand("drop", "drop database").Command = func() error {
return crud.AutoDrop(cfg.DB(), cfg.StaticObjs)
}
cmdDB.SubCommand("init", "init db data").Command = models.Init
}

@ -10,13 +10,42 @@ package main
import ( import (
app "oa" app "oa"
"oa/cfg" "oa/cfg"
"oa/models"
"github.com/veypi/OneBD/rest" "github.com/veypi/OneBD/rest"
"github.com/veypi/utils/flags"
"github.com/veypi/utils/logv" "github.com/veypi/utils/logv"
) )
func main() { var CMD = flags.New("app", "the backend server of app")
var cmdCfg = CMD.SubCommand("cfg", "generate cfg file")
var cmdDB = CMD.SubCommand("db", "database operations")
var configFile = CMD.String("f", "./dev.yaml", "the config file")
func init() {
CMD.StringVar(&cfg.Config.Host, "host", "0.0.0.0", "host")
CMD.IntVar(&cfg.Config.Port, "p", 4000, "port")
CMD.StringVar(&cfg.Config.LoggerLevel, "l", "info", "log level")
CMD.StringVar(&cfg.Config.DSN, "dsn", "root:123456@tcp(127.0.0.1:3306)/test?charset=utf8&parseTime=True&loc=Local", "data source name")
CMD.StringVar(&cfg.Config.DB, "db", "mysql", "data source type: mysql/postgre/sqlite")
CMD.Before = func() error {
flags.LoadCfg(*configFile, cfg.Config)
CMD.Parse()
logv.SetLevel(logv.AssertFuncErr(logv.ParseLevel(cfg.Config.LoggerLevel)))
return nil
}
CMD.Command = runWeb CMD.Command = runWeb
cmdCfg.Command = func() error {
flags.DumpCfg(*configFile, cfg.Config)
return nil
}
cmdDB.SubCommand("migrate", "migrate database").Command = models.Migrate
cmdDB.SubCommand("drop", "drop database").Command = models.Drop
cmdDB.SubCommand("init", "init db data").Command = models.Init
}
func main() {
CMD.Parse() CMD.Parse()
err := CMD.Run() err := CMD.Run()
if err != nil { if err != nil {

@ -12,14 +12,16 @@ import (
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/veypi/OneBD/rest/middlewares/crud"
"github.com/veypi/utils/logv" "github.com/veypi/utils/logv"
"gorm.io/gorm" "gorm.io/gorm"
) )
type BaseModel struct { type BaseModel struct {
// ID uint `json:"id" gorm:"primaryKey" methods:"get,patch,delete" parse:"path"` ID string `json:"id" gorm:"primaryKey;type:varchar(32)" methods:"get,patch,delete" parse:"path"`
ID string `json:"id" gorm:"primaryKey;type:varchar(32)" methods:"get,patch,delete" parse:"path"` CreatedAt time.Time `json:"created_at" methods:"*list" parse:"query"`
BaseDate UpdatedAt time.Time `json:"updated_at" methods:"*list" parse:"query"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
} }
func (m *BaseModel) BeforeCreate(tx *gorm.DB) error { func (m *BaseModel) BeforeCreate(tx *gorm.DB) error {
@ -29,19 +31,24 @@ func (m *BaseModel) BeforeCreate(tx *gorm.DB) error {
return nil return nil
} }
type BaseDate struct {
CreatedAt time.Time `json:"created_at" methods:"*list" parse:"query"` var AllModels = crud.New()
UpdatedAt time.Time `json:"updated_at" methods:"*list" parse:"query"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
}
func init() { func init() {
// cfg.CmdDB.SubCommand("init", "init db data").Command = InitDBData AllModels.Append(User{}, AppUser{}, Resource{}, Access{}, Role{}, UserRole{}, Token{}, App{})
cfg.StaticObjs.Append(User{}, AppUser{}, Resource{}, Access{}, Role{}, UserRole{}, Token{}, App{}) AllModels.One2Many(App{}, Resource{})
cfg.StaticObjs.One2Many(App{}, Resource{}) AllModels.One2Many(App{}, Role{})
cfg.StaticObjs.One2Many(App{}, Role{}) AllModels.One2Many(App{}, Access{})
cfg.StaticObjs.Many2Many(AppUser{}, App{}, User{}) AllModels.Many2Many(AppUser{}, App{}, User{})
cfg.StaticObjs.Many2Many(UserRole{}, App{}, User{}, Role{}) AllModels.Many2Many(UserRole{}, App{}, User{}, Role{})
}
func Migrate() error {
return crud.AutoMigrate(cfg.DB(), AllModels)
}
func Drop() error {
return crud.AutoDrop(cfg.DB(), AllModels)
} }
func Init() error { func Init() error {

Loading…
Cancel
Save