91 lines
2.8 KiB
Markdown
91 lines
2.8 KiB
Markdown
# gormcol
|
|
|
|
Type-safe GORM column descriptors and model generation utilities.
|
|
|
|
## Library
|
|
|
|
The `gormcol` package provides two approaches for referencing database columns:
|
|
|
|
### Field descriptors
|
|
|
|
Define struct-like variables mapping Go field names to `table.column` pairs:
|
|
|
|
```go
|
|
var PsAccess = struct {
|
|
IDProfile gormcol.Field
|
|
IDAuthorizationRole gormcol.Field
|
|
}{
|
|
IDProfile: gormcol.Field{Table: "ps_access", Column: "id_profile"},
|
|
IDAuthorizationRole: gormcol.Field{Table: "ps_access", Column: "id_authorization_role"},
|
|
}
|
|
```
|
|
|
|
```go
|
|
gormcol.Column(PsAccess.IDAuthorizationRole) // "id_authorization_role"
|
|
gormcol.ColumnOnTable(PsAccess.IDAuthorizationRole) // "ps_access.id_authorization_role"
|
|
gormcol.TableField(PsAccess.IDAuthorizationRole) // "ps_access"
|
|
```
|
|
|
|
### Reflection-based utilities
|
|
|
|
Extract column/table names from GORM struct tags at compile time:
|
|
|
|
```go
|
|
type PsAccess struct {
|
|
IDProfile uint `gorm:"column:id_profile;primaryKey"`
|
|
IDAuthorizationRole uint `gorm:"column:id_authorization_role"`
|
|
}
|
|
|
|
gormcol.Col[PsAccess]("IDAuthorizationRole") // "id_authorization_role"
|
|
gormcol.Tbl[PsAccess]() // "ps_access" (via NamingStrategy)
|
|
gormcol.ColOnTbl[PsAccess]("IDAuthorizationRole") // "ps_access.id_authorization_role"
|
|
gormcol.PK[PsAccess]() // "id_profile"
|
|
gormcol.Columns[PsAccess]() // ["id_profile", "id_authorization_role"]
|
|
gormcol.ColByJSON[PsAccess]("id_profile") // "id_profile"
|
|
```
|
|
|
|
### Naming strategy
|
|
|
|
The reflection-based functions fall back to a configurable `NamingStrategy` when no explicit `gorm:"column:..."` tag is set. Default is `schema.NamingStrategy{}`. Override it with your project's GORM naming strategy:
|
|
|
|
```go
|
|
gormcol.NamingStrategy = db.DB.NamingStrategy
|
|
```
|
|
|
|
## CLI
|
|
|
|
The `cmd/` directory contains a standalone tool that generates GORM model files with column descriptors.
|
|
|
|
### Build
|
|
|
|
```bash
|
|
go build -o gormcol-gen ./cmd/
|
|
```
|
|
|
|
### Usage
|
|
|
|
```
|
|
gormcol-gen --dsn <connection-string> [options]
|
|
```
|
|
|
|
| Flag | Default | Description |
|
|
|------|---------|-------------|
|
|
| `--dsn` | *(required)* | MySQL/MariaDB DSN, e.g. `user:pass@tcp(localhost:3306)/dbname` |
|
|
| `--filter` | `(ps_\|b2b_).*` | Regex matching table names to generate |
|
|
| `--out` | `./app/model/prestadb` | Output directory for generated files |
|
|
| `--pkg` | `prestadb` | Go package name for generated files |
|
|
|
|
### Example
|
|
|
|
```bash
|
|
./gormcol-gen --dsn "user:pass@tcp(localhost:3306)/mydb" --filter "ps_.*" --out ./internal/model --pkg model
|
|
```
|
|
|
|
This connects to the database, generates a `.go` model file for each matching table, and appends `<Model>Cols` variables with typed `gormcol.Field` descriptors to each file.
|
|
|
|
## Dependencies
|
|
|
|
- `gorm.io/gorm`
|
|
- `gorm.io/gen`
|
|
- `gorm.io/driver/mysql`
|