README
¶
codesjoy-modelgen
codesjoy-modelgen introspects MySQL/PostgreSQL metadata and generates:
- Single combined file per table:
<singular_table>_model_gen.go - GORM model + optional
AIPTable()method + optional compatibility wrapper
Install
go install github.com/codesjoy/pkg/tools/codesjoy-modelgen@latest
Usage
codesjoy-modelgen \
--dsn "user:pass@tcp(127.0.0.1:3306)/demo?parseTime=true" \
--schema demo \
--tables users,orders \
--out-dir ./internal/model \
--gen-aipsql=true \
--timestamp-mode unix_sec \
--override ./override.yaml
Required flags:
--dsn- MySQL example:
user:pass@tcp(127.0.0.1:3306)/demo?parseTime=true - PostgreSQL example:
postgres://user:pass@127.0.0.1:5432/demo?sslmode=disable
- MySQL example:
Optional flags:
--out-dir(default./)--schema--tables--override--package(defaults to the cleaned--out-dirdirectory name; when--out-diris./, uses the current working directory name)--gen-aipsql(defaulttrue)--timestamp-mode(unix_sec|unix_milli|unix_nano, defaultunix_sec; only affects integer-like timestamp columns)--dry-run--force
Override YAML
include_tables: []
exclude_tables: []
gen_aipsql: true
timestamp_mode: unix_sec
tables:
users:
skip: false
model_name: User
aipsql_builder: NewUserAIPTable
gen_aipsql: true
timestamp_mode: unix_sec
columns:
password_hash:
skip: true
is_active:
go_field: IsActive
go_type: bool
json_name: is_active
field_path: is_active
filterable: true
sortable: true
implicit_filter: false
bool_type: true
key_value: false
match_modes: [exact]
gorm_tag_append: "default:true"
timestamp_mode: unix_milli
Behavior notes
- By default, scalar columns are
Filterable. - Primary-key or indexed columns are
Sortableby default. - Text columns default to
WithMatchModes(aipsql.MatchModeExact).- PostgreSQL
character varying/charactercolumns are treated as text by default.
- PostgreSQL
- Composite indexes are generated from multi-column physical indexes.
- Timestamp role fields (
created*/updated*/deleted*) choose Go types from the physical database type first. --timestamp-modeonly controls integer-like timestamp precision:unix_sec,unix_milli, orunix_nano.- Timestamp role fields are normalized to
CreatedAt,UpdatedAt,DeletedAtby default (override-able withgo_field). - datetime-like
created*/updated*columns ->time.Time deleted*columns are soft-delete aware:- datetime-like physical columns ->
gorm.DeletedAt+index - integer-like physical columns ->
soft_delete.DeletedAt+softDelete[:milli|:nano]
- datetime-like physical columns ->
deleted*columns are hidden from AIPTable by default; setfilterable=trueorsortable=trueorimplicit_filter=truein override to expose them.- With
gen_aipsql=true, generated file includes:func (Model) AIPTable() *aipsql.Tablefunc New<Model>AIPTable() *aipsql.Tablecompatibility wrapper.
- Existing files without the generated header are protected unless
--forceis set. - Legacy
*_gen.go,<table>_model_gen.go, and<table>_aipsql_gen.gofiles are not deleted automatically; the generator prints migrate hints when they are detected.
Test
go test ./...
Integration Tests (Docker)
The integration suite is Docker-backed and uses Testcontainers to start MySQL and PostgreSQL.
Run from the module root:
go test -tags=integration -v ./testing/integration/...
Requirements:
- Docker Desktop (or another compatible Docker daemon) must be available.
- Integration tests are opt-in; default
go test ./...andmake MODULES="tools/codesjoy-modelgen" testremain unit-test focused. - Directory conventions:
- unit/golden fixtures:
testdata/golden/ - Docker integration tests:
testing/integration/
- unit/golden fixtures:
- AIP index coverage in integration tests includes:
- composite index generation with stable column ordering
- hidden-column-driven composite index pruning
- override-based deleted column exposure restoring composite indexes
- single-column/unique/expression index filtering from
CompositeIndexes - column-level
index_hintpassthrough (WithIndexHint(...))
Example
See example/ for a Docker-runnable PostgreSQL walkthrough and a committed sample generated output.
Documentation
¶
There is no documentation for this package.
Click to show internal directories.
Click to hide internal directories.