zdravko/tools/generate/main.go

45 lines
1 KiB
Go
Raw Normal View History

2024-02-11 10:56:21 +00:00
package main
import (
"code.tjo.space/mentos1386/zdravko/internal/config"
2024-02-11 10:56:21 +00:00
"code.tjo.space/mentos1386/zdravko/internal/models"
"gorm.io/driver/sqlite"
2024-02-11 10:56:21 +00:00
"gorm.io/gen"
"gorm.io/gorm"
2024-02-11 10:56:21 +00:00
)
func main() {
config := config.NewServerConfig()
2024-02-12 08:25:11 +00:00
2024-02-11 10:56:21 +00:00
// Initialize the generator with configuration
g := gen.NewGenerator(gen.Config{
2024-02-11 11:57:57 +00:00
OutPath: "internal/models/query",
2024-02-11 10:56:21 +00:00
Mode: gen.WithDefaultQuery | gen.WithQueryInterface,
FieldNullable: true,
})
db, err := gorm.Open(sqlite.Open(config.DatabasePath), &gorm.Config{})
if err != nil {
panic(err)
}
2024-02-11 10:56:21 +00:00
// Use the above `*gorm.DB` instance to initialize the generator,
// which is required to generate structs from db when using `GenerateModel/GenerateModelAs`
g.UseDB(db)
// Generate default DAO interface for those specified structs
g.ApplyBasic(
models.Worker{},
models.HealthcheckHttp{},
models.HealthcheckHttpHistory{},
models.HealthcheckTcp{},
models.HealthcheckTcpHistory{},
models.Cronjob{},
models.CronjobHistory{},
models.OAuth2State{},
)
2024-02-11 10:56:21 +00:00
// Execute the generator
g.Execute()
}