README
ΒΆ
Codegen π
Generate Clean Architecture for REST API with support for Fiber Web Framework and gRPC in Golang. Speed up your development with automatic code generation.
β¨ Features
- ποΈ Clean Architecture - Automatically generates layered architecture structure
- π Fiber Framework Support - Optimized for the Fiber web framework
- π gRPC Support - Built-in support for gRPC services
- π CRUD Generation - Generate CRUD operations from JSON specifications
- ποΈ Database Schema Generation - Generate CRUD directly from a live MySQL/MariaDB schema
- π Optional Database - Projects are generated without database code unless you ask for it; MariaDB, MongoDB and InfluxDB 3 can be added at any time
- 𧬠SQL Migrations - MariaDB projects ship a bun-backed migration runner that applies embedded SQL files on boot
- π‘ MQTT Template - Scaffold an MQTT forward service instead of a REST API
- π οΈ OpenAPI + Scalar - Generate the OpenAPI spec from the routes and browse it with Scalar at
/docs - π§© Modular Design - Feature-based modules for better organization
- π§ Wire Integration - Dependency injection with Google Wire
- β‘ Fast Development - Speed up your development workflow
π¦ Installation
Latest version:
go install github.com/prongbang/codegen@v1.8.0
π© CLI Flags
| Flag | Alias | Description |
|---|---|---|
-new |
-n |
Project name (create a new project) |
-mod |
-m |
Module name, e.g. github.com/prongbang |
-template |
-t |
Project template (currently supported: mqtt) |
-feature |
-f |
Feature name (generate a feature module) |
-shared |
-sh |
Shared module name |
-package |
-p |
Package prototype name |
-spec |
-s |
JSON spec file for CRUD generation |
-dsn |
Database connection string β generate CRUD from a live database schema | |
-table |
-tb |
Table name (optional, defaults to the feature name) |
-driver |
-d |
See below β the meaning depends on what you are generating |
-orm |
ORM for generated data source: bun or sqlbuilder |
-driver does two different jobs:
| Used with | Meaning | Example |
|---|---|---|
-new |
Which databases to scaffold. Comma-separated. Omit it for a project with no database code. | -d mariadb,influxdb3 |
-f / -sh |
Which database the generated CRUD data source targets | -d mariadb |
π§ Commands
| Command | Description |
|---|---|
codegen -new <name> -mod <module> |
Create a new project |
codegen database init -d <databases> |
Add database code to an existing project |
codegen migration init |
Add the startup migration runner to an existing MariaDB project |
codegen migration new <name> |
Create the next migration file under migrations/sql |
codegen grpc init |
Add the gRPC scaffold |
codegen grpc server --new <service> |
Generate a gRPC server |
codegen grpc client --new <thirdparty>/<service> |
Generate a gRPC client |
codegen openapi -framework fiber ./... |
Print an OpenAPI spec for the current project |
Run codegen <command> --help for the flags of any command.
π Quick Start
A typical run, from nothing to a working CRUD endpoint:
# 1. Create the project (add -d here if you already know you need a database)
codegen -new test_project -mod github.com/prongbang
cd test-project
# 2. Add a database whenever you need one
codegen database init -d mariadb
# 3. Generate a CRUD feature from a JSON spec (the spec must include the primary key)
mkdir -p spec && echo '{"id": "uuid", "name": "text", "active": 1}' > spec/brand.json
codegen -f brand -s spec/brand.json -d mariadb -orm bun
# ...or straight from an existing table
codegen -f brand -dsn "user:password@tcp(127.0.0.1:3306)/dbname" -orm bun
# 4. Run it
go run cmd/api/main.go -env development
Generate an OpenAPI spec from the finished project (run inside the project):
codegen openapi -framework fiber ./... > docs/openapi.json
1. Create a New Project - API
Generate a new project with module structure:
codegen -new test_project -mod github.com/prongbang
Parameters:
-new: Project name-mod: Module name-d,-driver: (optional) databases to scaffold βmariadb,mongodb,influxdb3. Separate several with a comma. Omit it and the project is generated with no database code at all; you can add one later withcodegen database init.
# no database code
codegen -new test_project -mod github.com/prongbang
# with one or more databases
codegen -new test_project -mod github.com/prongbang -d mariadb
codegen -new test_project -mod github.com/prongbang -d mariadb,influxdb3
This creates the following structure:
.
βββ Makefile
βββ cmd
β βββ api
β βββ main.go
βββ configuration
β βββ configuration.go
β βββ development.yml
β βββ environment.go
β βββ production.yml
βββ deployments
β βββ Dockerfile
β βββ api-prod.yml
βββ docs
β βββ openapi.json # served by Scalar at /docs; regenerate with `make openapi-gen`
βββ go.mod
βββ go.sum
βββ internal
β βββ app
β β βββ api
β β β βββ api.go
β β β βββ health
β β β β βββ health.go
β β β β βββ handler.go
β β β β βββ provider.go
β β β β βββ router.go
β β β β βββ usecase.go
β β β βββ routers.go
β β βββ app.go
β βββ database # only with -d; one file per selected database
β β βββ db.go # -d mariadb
β β βββ drivers.go
β β βββ mariadb.go # -d mariadb
β β βββ mongodb.go # -d mongodb
β β βββ influxdb3.go # -d influxdb3
β β βββ wire.go
β β βββ wire_gen.go
β βββ middleware
β β βββ api_key.go
β β βββ jwt.go
β β βββ on_request.go
β βββ pkg
β βββ casbinx
β β βββ casbinx.go
β βββ response
β β βββ response.go
β βββ validator
β βββ validator.go
β
βββ pkg
β βββ requestx
β βββ request.go
βββ policy
β βββ model.conf
β βββ policy.csv
βββ spec
β βββ promotion.json
βββ wire.go
βββ wire_gen.go
Create a New Project - MQTT
Generate a project from a predefined template using the -template (-t) flag:
codegen -new test_project -mod github.com/prongbang -template mqtt
Parameters:
-new: Project name-mod: Module name-template: Template name (currently supported:mqtt)
The mqtt template scaffolds an MQTT forward service that subscribes to an
upstream MQTT broker and forwards messages to a built-in (mochi) MQTT server:
.
βββ Makefile
βββ cmd
β βββ api
β βββ main.go
βββ configuration
β βββ configuration.go
β βββ development.yml
β βββ environment.go
β βββ production.yml
βββ go.mod
βββ internal
βββ app
βββ mqtt
βββ broker
β βββ mqtt_broker.go
βββ forward
β βββ forward.go
β βββ mqtt_forward.go
β βββ payload.go
βββ subscribe
βββ mqtt_subscribe.go
After generating, run go mod tidy inside the project, then start it with
make run (or go run cmd/api/main.go -env development).
Add a Database Later
A project created without -d has no database code at all. Add one at any time β
run this from the project root (where go.mod is):
codegen database init -d mariadb
codegen database init -d influxdb3
codegen database init -d mariadb,mongodb,influxdb3
Supported: mariadb, mongodb, influxdb3.
What it changes
| File | Change |
|---|---|
internal/database/*.go |
Driver for each selected database, plus drivers.go, wire.go, wire_gen.go |
wire.go, wire_gen.go |
CreateApp gains its database.Drivers parameter |
cmd/api/main.go |
Builds the driver, closes it on shutdown, and runs migrations (MariaDB) |
migrations/, internal/database/migrations.go |
Migration runner and its SQL directory (MariaDB only) |
configuration/configuration.go |
Config struct fields for each database |
configuration/development.yml, production.yml |
Connection settings to fill in |
go.mod |
Database dependencies, then go mod tidy + wire |
So cmd/api/main.go goes from:
apps := myproject.CreateApp()
apps.StartAPI()
to:
dbDriver := database.NewDatabaseDriver()
defer dbDriver.Close()
apps := myproject.CreateApp(dbDriver)
apps.StartAPI()
Adding databases one at a time
It merges with what the project already has, so the existing databases are kept, and re-running it for one that is already set up does nothing:
codegen database init -d mongodb # project has mongodb
codegen database init -d influxdb3 # project has mongodb + influxdb3
codegen database init -d influxdb3 # "Nothing to do: influxdb3 already set up"
Fill in the connection settings
database init writes the settings with placeholder values β update
configuration/development.yml (and production.yml) before running:
mariadb:
host: "localhost"
port: 3306
database: "mariaDB"
user: "root"
pass: "password"
mongodb:
host: "localhost"
port: 27017
database: "mongoDB"
user: "root"
pass: "password"
influxdb3:
host: "http://localhost:8181"
token: ""
organization: ""
database: "influxDB"
Using the driver
Inject database.Drivers into any provider and take the connection you need:
func NewDataSource(driver database.Drivers) DataSource {
return &dataSource{Driver: driver}
}
driver.GetMariaDB() // *bun.DB (-d mariadb)
driver.GetMongoDB() // *mongo.Database (-d mongodb)
driver.GetInfluxDB3() // *influxdb3.Client (-d influxdb3)
[!NOTE] Only the databases you selected have a getter β
drivers.gois generated from the selected set, so asking for one you did not add is a compile error rather than a nil connection at runtime.
Database Migrations
A project with MariaDB ships a migration runner. SQL files live in
migrations/sql, are embedded into the binary, and are applied at boot by
database.StartupMigrations(dbDriver) in cmd/api/main.go.
migrations/
βββ embed.go //go:embed sql/*.sql
βββ README.md the conventions, generated with the project
βββ sql/
βββ 0001_init.up.sql no-op baseline, keeps the embed non-empty
internal/database/migrations.go the runner (bun/migrate)
Create the next migration β run this from the project root:
codegen migration new master_room_setup
# β migrations/sql/0002_master_room_setup.up.sql
It picks the next free version, matches the zero-padding already in use, and writes a skeleton with the rules in its header. Fill it in and rebuild β the runner only sees files compiled into the binary.
Projects that already have MariaDB but predate the runner get it with:
codegen migration init
That writes the four files above and adds the StartupMigrations call to
cmd/api/main.go. It never overwrites a file that already exists, so it is safe
to re-run.
How it behaves at runtime
- Every boot.
RUN_MIGRATIONS=false(or0,f) opts out β for a replica that must never touch the schema. - Fail-fast. Any error exits the process before the API starts, bounded by a 5-minute timeout.
- Locked. bun holds a lock in
migrations_locks, so concurrent replicas cannot race. Applied versions are recorded inmigrations. - Retried on failure. A migration is recorded only after its SQL succeeds, so a failed one runs again next boot. That is why every file must be idempotent and survive being re-run from its first statement.
Writing one
Two rules the generated migrations/README.md covers in full, and that are not
style preferences:
- Separate every statement with a line containing exactly
--bun:split. Each chunk is sent as one query; two statements in a chunk fail with errno 1064. ADD COLUMN IF NOT EXISTSis MariaDB-only and a syntax error on MySQL. Guard throughinformation_schemaplusPREPARE/EXECUTEinstead.
Append .tx before the suffix β 0002_seed.tx.up.sql β to run a file inside a
transaction. Useful for DML; DDL auto-commits regardless.
1.1 Initial gRPC
codegen grpc init
This creates the initial gRPC scaffold under internal/app/grpc and generates the default health service.
1.2 Generate gRPC Server
codegen grpc server --new device
This creates:
internal/app/grpc/device/v1
βββ device.proto
βββ provider.go
βββ server.go
It also updates:
internal/app/grpc/servers.gowire.go
Then it runs:
make gen service=device version=v1
wire
1.3 Generate gRPC Client
Use the format <thirdparty>/<service>:
codegen grpc client --new core/device
This creates:
internal/thirdparty
βββ Makefile
βββ core
βββ clients.go
βββ device
βββ v1
βββ client.go
βββ device.proto
It also updates:
internal/thirdparty/core/clients.gointernal/thirdparty/Makefile
Then it runs:
make gen service=device version=v1 thirdparty=core
2. Generate Features Prototype
Generate a new feature module (run inside internal/app/api of your project):
codegen -f promotion
This creates:
test-project/internal/app/api/promotion
βββ datasource.go
βββ handler.go
βββ permission.go
βββ promotion.go
βββ provider.go
βββ repository.go
βββ router.go
βββ usecase.go
3. Generate Features CRUD from JSON Spec
Generate CRUD operations from JSON specifications:
[!IMPORTANT] CRUD data sources talk to
internal/database, so the project needs a database first. On a project created without-dthe generated code will not compile (no required module provides package .../internal/database). Add one withcodegen database init -d mariadbbefore generating CRUD.
3.1 Define Spec File
Create spec/auth.json. The field named id (case-insensitive) becomes the
primary key, and the spec must contain one β without it the generated code will
not compile:
{
"id": "uuid",
"accessToken": "JWT",
"expired": 1234567,
"date": "2024-10-15T14:30:00Z"
}
Field values are samples that decide the Go type:
| Sample value | Go type |
|---|---|
"uuid", "text", any string |
string |
1234567 |
int64 |
1.5 |
float64 |
true |
bool |
"2024-10-15T14:30:00Z" |
*time.Time |
3.2 Generate CRUD
- SQL Builder
codegen -f auth -s spec/auth.json -d mariadb -orm sqlbuilder
- Bun
codegen -f auth -s spec/auth.json -d mariadb -orm bun
This generates complete CRUD operations based on your JSON structure.
test-project/internal/app/api/auth
βββ auth.go
βββ datasource.go
βββ handler.go
βββ permission.go
βββ provider.go
βββ repository.go
βββ router.go
βββ usecase.go
4. Generate Features CRUD from Database Schema
Instead of writing a JSON spec, point codegen at a live MySQL/MariaDB database
with -dsn and it reads the table schema from information_schema to generate
the model and CRUD code:
codegen -f brand -dsn "user:password@tcp(127.0.0.1:3306)/dbname" -orm bun
By default the table name is the snake_case of the feature name. If the table
name differs, override it with -table:
codegen -f company -dsn "user:password@tcp(127.0.0.1:3306)/dbname" -table company_group -orm bun
Parameters:
-dsn: Database connection string in go-sql-driver DSN format-table(-tb): Table name (optional, defaults to the feature name)-orm:bunorsqlbuilder-d: Driver (optional, defaults tomysqlwhen-dsnis set)
Column types are mapped automatically:
| MySQL type | Go type |
|---|---|
int, bigint, smallint, tinyint, year, bit |
int64 |
tinyint(1), boolean |
*bool |
decimal, float, double |
float64 |
date, datetime, timestamp, time |
*time.Time |
varchar, text, enum, json, others |
string |
The primary key is detected from the table's PRIMARY KEY definition, and
column names are used as-is for the generated SQL and db/bun tags. For
example, a brand table generates:
type Brand struct {
bun.BaseModel `bun:"table:brand,alias:b" json:"-" swaggerignore:"true"`
Id string `bun:"id,pk" json:"id" db:"id"`
Name string `bun:"name" json:"name" db:"name"`
Active int64 `bun:"active" json:"active" db:"active"`
CreatedAt *time.Time `bun:"created_at" json:"createdAt" db:"created_at"`
UpdatedAt *time.Time `bun:"updated_at" json:"updatedAt" db:"updated_at"`
}
This also works for shared modules with -sh:
codegen -sh brand -dsn "user:password@tcp(127.0.0.1:3306)/dbname" -orm bun
[!NOTE] The DSN is only used at generation time to read the schema β it is never written into the generated code.
5. Generate Shared Prototype
codegen -sh promotion
This generates shared prototype
test-project/internal/shared/promotion
βββ datasource.go
βββ promotion.go
βββ provider.go
βββ repository.go
6. Generate Shared CRUD
- SQL Builder
codegen -sh promotion -s spec/promotion.json -d mariadb -orm sqlbuilder
- Bun
codegen -sh promotion -s spec/promotion.json -d mariadb -orm bun
- From a database schema
codegen -sh promotion -dsn "user:password@tcp(127.0.0.1:3306)/dbname" -orm bun
This generates shared CRUD operations based on your JSON structure or database schema.
test-project/internal/shared/promotion
βββ datasource.go
βββ promotion.go
βββ provider.go
βββ repository.go
π€ Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
π License
This project is licensed under the MIT License - see the LICENSE file for details.
π Support
If you find this tool helpful, please consider buying me a coffee:
π Related Projects
- Fiber - Express-inspired web framework
- Wire - Compile-time dependency injection
- gRPC - High performance RPC framework
Documentation
ΒΆ
There is no documentation for this package.
