codegen

command module
v1.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 22, 2026 License: Apache-2.0 Imports: 15 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README ΒΆ

Codegen πŸš€

Coverage Go Report Card Go Reference Version

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 with codegen 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.go is 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 (or 0, 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 in migrations.
  • 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 EXISTS is MariaDB-only and a syntax error on MySQL. Guard through information_schema plus PREPARE/EXECUTE instead.

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.go
  • wire.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.go
  • internal/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 -d the generated code will not compile (no required module provides package .../internal/database). Add one with codegen database init -d mariadb before 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: bun or sqlbuilder
  • -d: Driver (optional, defaults to mysql when -dsn is 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:

  • Fiber - Express-inspired web framework
  • Wire - Compile-time dependency injection
  • gRPC - High performance RPC framework

Documentation ΒΆ

The Go Gopher

There is no documentation for this package.

Directories ΒΆ

Path Synopsis
internal
pkg
mod

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 捒网址