README
ΒΆ
SQL-CRUD β CLI Tool for Golang Backend Projects
SQL-CRUD is a powerful command-line tool that streamlines the setup of Golang backend projects using GORM and Echo for SQL-based applications. It automates the generation of boilerplate code, caching logic, and documentation, significantly accelerating development.
β¨ Features
- Generates GORM models, services, and controllers based on metadata provided in a
config.json
file. - Automatically creates RESTful CRUD APIs for each model.
- Integrates with the Echo framework for routing and middleware.
- Adds in-memory caching using Ristretto for enhanced performance.
- Generates OpenAPI (Swagger) documentation using Swag.
- Simplifies project scaffolding and enforces consistent architecture.
- Currently supports MySQL, SQLite, and PostgreSQL databases.
β οΈ Note: Models with JSON datatypes are not fully supported in service layer generation. The tool can generate the base model, but you'll need to manually handle business logic in the generated services.
π¦ Use Case
Ideal for developers looking to:
- Quickly bootstrap a Golang backend project with a standardized structure.
- Reduce manual setup time for CRUD operations and documentation.
- Leverage caching for improved read performance.
- Enforce separation of concerns with clean service and controller layers.
Installation
To install SQL-CRUD, run:
go install github.com/semay-cli/sql-crud@latest
π Project Structure (Generated)
sql-crud-sample/
βββ manager/
β βββ app.go
β βββ middleware.go
β βββ [manager.go](http://_vscodecontentref_/1)
β βββ migrate.go
βββ blue_admin/
β βββ scheduler
β βββ controllers
β βββ models
β βββ services
β βββ services
β βββ testsetting/
β βββ middleware.go
β βββ setup.go
βββ observe/
βββ configs/
β βββ .dev.env
β βββ .env
βββ logs/
βββ database/
βββ project.json
βββ main.go
π Folder Structure Explanation
manager/
Contains application-level setup and utility functions.
app.go
β Initializes core application components like database, cache, and router.middleware.go
β Global middleware definitions (e.g., CORS, logging).manager.go
β Handles app-wide logic such as dependency injection and route group registration.migrate.go
β Runs GORM-based database migrations.
blue_admin/
This directory represents a self-contained application module β in this case, "blue_admin". It contains all the business logic specific to that app, and follows a clean modular structure. Each application (like blue_admin
, crm
, etc.) can maintain its own version of this layout.
If you add another app such as crm/
, it will mirror this structure and share the rest of the project components (like manager/
, configs/
, database/
, etc.).
Shared Structure Across Multiple Apps:
Each app (blue_admin/
, crm/
, etc.) will typically include:
scheduler/
β Scheduled tasks (e.g., cron jobs) specific to the app.controllers/
β HTTP handler functions generated from the app's models.models/
β GORM model definitions based onproject.json
or scoped configs.services/
β Business logic layer, caching via Ristretto, and DB access.testsetting/
β App-specific test configurations or fixtures.middleware.go
β Middlewares that apply to this specific app/module.setup.go
β Initialization logic to wire up this app into the main application (register routes, dependencies, etc.).
β This modular approach makes it easy to manage and scale multiple apps in a monorepo-style project, while keeping shared concerns centralized (e.g., in
manager/
,configs/
,database/
, etc.).
observe/
Handles observability features for the entire application, including monitoring, metrics, and tracing integrations.
/metrics
endpoint β Automatically exposed for Prometheus scraping. This provides runtime metrics such as HTTP request counts, response durations, memory usage, and more.- Tracing with Jaeger β If tracing is initialized, the app is configured to:
- Export spans to Jaeger.
- Only trace failed requests by default (non-2xx responses).
- Sample 10% of the traffic using the default configuration.
π‘ You can adjust the trace sampling rate using the
TRACER_SAMPLE
environment variable. IfTRACER_SAMPLE
is not defined, a 10% default sampling rate is used.
β This module ensures observability is centralized and configurable, without polluting business logic or service layers.
configs/
Houses environment-specific configuration files used to initialize the application with the correct settings (e.g., database URLs, ports, caching parameters, etc.).
.env
β Base configuration that applies across all environments (common defaults)..dev.env
β Environment-specific overrides for development.
π The
.dev.env
file naming is dynamic based on the environment. For example:
.staging.env
.production.env
.test.env
These files follow the pattern .<env>.env
, and the active environment is determined by the --env
flag when starting your application.
β Example:
go run main.go run --env=staging
logs/
Directory where application logs are written (if logging to file is enabled).
database/
Contains the logic for establishing and configuring database connections using GORM, with support for MySQL, PostgreSQL, and SQLite. It also integrates GORM with OpenTelemetry for tracing and uses structured logging for better observability.
π Summary of Responsibilities:
- Dynamic Driver Selection: Based on the environment variable
<APP_NAME>_DB_TYPE
, it dynamically selects and configures the appropriate GORM driver (mysql
,postgres
, orsqlite
). - Connection Strings: Reads DSN (connection string) from env variables like
<APP_NAME>_MYSQL_URI
,<APP_NAME>_POSTGRES_URI
, or<APP_NAME>_SQLLITE_URI
based on the selected DB type. - Custom GORM Logger:
- Logs slow queries.
- Outputs logs to a file named
<app_name>_gorm.log
. - Uses
logger.Info
level with timestamped entries.
- Connection Pool Configuration:
- Sets maximum open connections and connection lifetime for each DB type to optimize performance.
- OpenTelemetry Tracing:
- Attaches the
otel
tracing plugin to the GORM session to support distributed tracing. - Helps trace slow or failing database calls when observability is enabled.
- Attaches the
β The logic is designed to support multi-tenant or multi-app setups by using the app name as a prefix for environment variable lookup, making it scalable and modular.
project.json
This file serves as the main input metadata for the SQL-CRUD CLI tool. It defines project-wide configuration and tracks individual application modules for code generation.
- The file is automatically generated by the CLI.
- It keeps track of the project name, registered apps, and auth configuration.
- If
auth_app_name
is not provided, it defaults toblue-admin
. - If
auth_app_type
is not specified, it defaults tosso
(Single Sign-On). - The
models
field is populated with your application's data models and is used as the source of truth for generating GORM models, services, and controllers.
π Sample project.json
{
"project_name": "github.com/semay-cli/sql-play",
"app_names": [
"blue-admin"
],
"current_app_name": "",
"back_tick": "`",
"auth_app_name": "blue-admin",
"auth_app_type": "sso"
}
config.json
This file defines model metadata used by the SQL-CRUD CLI tool to auto-generate:
- GORM models
- RESTful controllers
- Service layers
- OpenAPI documentation
Each application module (e.g., blue-admin
, crm
) has its own config.json
file that is automatically generated when the app is initialized.
π§ Key Notes:
app_name
: The name of the app the config belongs to.project_name
: The Go module path (used for import paths).models
: An array of model definitions, each with fields, types, relationships, and CRUD flags.- This metadata drives code scaffolding (models, services, routes, docs).
π Sample config.json
{
"project_name": "github.com/semay-cli/sql-play",
"app_name": "blue-admin",
"models": [
{
"name": "Group",
"search_fields": ["name", "description", "active"],
"rln_model": ["User$mtm$user_groups", "Scope$mtm$group_scopes"],
"fields": [
{
"name": "ID",
"type": "uint",
"annotation": "gorm:\"primaryKey;autoIncrement:true\" json:\"id,omitempty\"",
"curd_flag": "true$false$false$true$false$false"
},
{
"name": "Name",
"type": "string",
"annotation": "gorm:\"not null; unique;\" json:\"name,omitempty\"",
"curd_flag": "true$true$true$true$false$false"
}
// ...more fields...
]
}
// ...more models like App, User, Scope, Resource, JWTSalt...
]
}
π Quick Start
Follow the steps below to get your project up and running:
1. Create a Project Directory
mkdir <folder-name>
2. Install sql-crud
CLI
sql-crud-install -u <github-username> -a blue-admin -p <folder-name>
Note: To use the admin UI that ships with
sql-crud
, you must set the app name toblue-admin
, as it includes the built-in role management interface.
3. Configure the Database URI
Edit the .dev.env
file in the project root and set the SQL database URI:
DATABASE_URI=your-database-uri-here
4. Run Migrations and migrate data models meta data
go run main.go migrate -t create -e dev -a blue-admin
go run main.go migrate -a blue-admin
Note for Windows users using SQLite:
Before running the command above, set the environment variable:
set CGO_ENABLED=1
5. Create a Superuser
go run main.go superuser
This will create a default superuser with the following credentials:
- Email:
superuser@mail.com
- Password:
default@123
6. Start the Development Server
go run main.go run --env=dev
7. π₯οΈ Accessing the UI
-
The Admin UI is available at:
http://localhost:<port>/admin/
-
The Swagger Documentation UI can be found at:
http://localhost:<port>/<app_name>/docs/
Note:
If your app name contains hyphens (-
), they will be converted to underscores (_
) in the Swagger docs URL.
π Notes and Pitfalls
-
App Cache Instance
Each app has its own cache instance. The default cache uses Resiretto for local management.
You should carefully consider the total cache size you want for your global app environment, then divide that size equally among the number of apps. This ensures fair and efficient cache allocation. -
Adding New Apps
Every time you add a new app, you must run the configuration and global echo app generation commands again. This step is necessary to register the new app correctly. -
File Overwrite Warning
If a file with the same name already exists, running thesql-crud
generation commands will completely replace the existing file.
Be cautious when running these commands on heavily customized files to avoid losing your changes. -
Unit Tests Coverage
The current generated unit tests cover CRUD functionality only.
Unit test generation for relationships between entities is planned and will be coming soon. -
Custom Middleware for Token and Role Validation
You have to implement your own middleware for token and role validation.
(Soon to be added: a default admin role middleware that works out of the box.)
βΉοΈ Additional Information
For more details and to explore all available commands, you can always run:
sql-crud --help
π License
This project is licensed under the MIT License.
Documentation
ΒΆ
There is no documentation for this package.