-
Notifications
You must be signed in to change notification settings - Fork 0
Code‐generator
CRUDGen is a code generation tool for rapidly scaffolding CRUD (Create, Read, Update, Delete) layers from a PostgreSQL database table using Golang. It supports model, handler, service, and route generation.
git clone https://github.com/yourusername/crudgen.git
cd crudgen
go mod tidy
go run main.go --conn "postgres://user:pass@localhost:5432/db?sslmode=disable" --table users
This will generate Go code in
/generatedor a specified output directory.
You can store database and app configurations in config/config.yaml:
database:
host: localhost
port: 5432
user: postgres
password: password
dbname: mydb
sslmode: disable
app:
name: crudgen
environment: development
log_level: info
server:
port: 8080
timeout: 30
api:
base_url: /api/v1
In your Go app, call:
err := config.LoadConfig("config/config.yaml")
if err != nil {
log.Fatal(err)
}
go run main.go --conn "<POSTGRES_CONN_STRING>" --table "<TABLE_NAME>"
| Flag | Description |
|---|---|
| --conn | PostgreSQL connection string |
| --table | Table name to generate code for |
| --output | (optional) Output directory |
Example:
go run main.go --conn "postgres://postgres:pass@localhost:5432/mydb?sslmode=disable" --table "products"
Templates are stored in the /templates directory. You can customize these:
-
model.tmpl: generates model structs -
handler.tmpl: generates HTTP handlers -
service.tmpl: business logic functions -
route.tmpl: routing logic -
dto.tmpl: optional DTOs -
main.tmpl: starting point
Each template uses Go's text/template syntax and supports .Name, .Fields, and more.
-
Fork the repo.
-
Create a new branch.
-
Make your changes.
-
Open a pull request.
Follow our CONTRIBUTING.md and ensure code style and formatting is respected.
Q: Does it support relationships (e.g., foreign keys)?
A: Not yet. It's on our roadmap.
Q: Can I generate code for multiple tables at once?
A: Not currently. You must run the tool per table.
Q: Can I change the output structure?
A: Yes. You can edit or replace the templates in /templates.
-
CLI to generate models, handlers, services
-
Templating with Go’s
text/template -
YAML-based config
-
Multi-table support
-
Foreign key relations
-
Swagger doc generation
-
RESTful test harness
Would you like a .github/wiki/ markdown structure version of this (so it appears in the repo instead of using GitHub's Wiki tab)?