Skip to content

Code‐generator

Imoleayo Moses edited this page May 7, 2025 · 1 revision

🗂️ Wiki Table of Contents

  1. Home

  2. Getting Started

  3. Configuration

  4. Command-Line Usage

  5. Templates

  6. Contributing

  7. FAQ

  8. Roadmap


🏠 Home

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.


🚀 Getting Started

1. Clone the Repository

git clone https://github.com/yourusername/crudgen.git
cd crudgen

2. Install Dependencies

go mod tidy

3. Run the Generator

go run main.go --conn "postgres://user:pass@localhost:5432/db?sslmode=disable" --table users

This will generate Go code in /generated or a specified output directory.


⚙️ Configuration

config.yaml

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

Loading Config

In your Go app, call:

err := config.LoadConfig("config/config.yaml")
if err != nil {
    log.Fatal(err)
}

💻 Command-Line Usage

go run main.go --conn "<POSTGRES_CONN_STRING>" --table "<TABLE_NAME>"

Flags

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

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.


🤝 Contributing

  1. Fork the repo.

  2. Create a new branch.

  3. Make your changes.

  4. Open a pull request.

Follow our CONTRIBUTING.md and ensure code style and formatting is respected.


❓ FAQ

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.


🛣️ Roadmap

  • 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)?