Introduction
What is Monstarillo?¶
Monstarillo is a database-driven code generator that transforms your database schema into production-ready code for any programming language. Instead of manually writing repetitive CRUD operations, model classes, and API endpoints, Monstarillo automates this process using customizable templates.
Core Concepts¶
🗃️ Database as Source of Truth¶
Your database schema defines the structure, and Monstarillo reads this to understand:
- Tables and columns with their data types
- Primary keys and relationships between tables
- Constraints and indexes for proper code generation
📋 Template-Based Generation¶
Monstarillo uses Go's powerful template engine to generate code: - Flexible templating - Create templates for any language or framework - Reusable patterns - Write once, generate for all tables - Custom logic - Add conditions, loops, and data transformations
⚙️ Configuration-Driven Workflow¶
A simple templates.json
file controls everything:
- Which templates to run and where to find them
- Where to output files and how to name them
- Which tables to process or ignore
How It Works¶
graph LR
A[Database Schema] --> B[Monstarillo]
C[Templates] --> B
D[Configuration] --> B
B --> E[Generated Code]
style A fill:#e1f5fe
style C fill:#f3e5f5
style D fill:#e8f5e8
style E fill:#fff3e0
- Connect to your PostgreSQL, MySQL, or Oracle database
- Extract metadata about tables, columns, and relationships
- Process each table through your templates
- Generate code files according to your configuration
Example: Generate API for 10 Tables¶
If your database has 10 tables, and you have 3 templates (model, controller, test), Monstarillo will: - Run 30 template executions (10 tables × 3 templates) - Generate 30 code files automatically - Maintain consistent patterns across all generated code
Template Context¶
Each template receives rich metadata about your database through the MonstarilloContext
:
type MonstarilloContext struct {
Tables []models.Table // All database tables
Tags []models.Tag // Custom configuration tags
CurrentTable models.Table // Current table being processed
CurrentGuiTable models.GuiListTable // GUI-specific table data
UnitTestValuesFile string // Test data configuration
GuiListTables models.GuiListTables // All GUI table data
}
Key Context Properties¶
CurrentTable
- The table currently being processed by the template
Contains columns, data types, relationships, and metadata for generating table-specific code like models or controllers.
Tables
- Complete list of all database tables
Useful for generating cross-table code like foreign key relationships, navigation, or comprehensive documentation.
Tags
- Custom variables from your templates.json
Define reusable values like output paths, project names, or framework settings that templates can access.
UnitTestValuesFile
- Path to test data configuration
Enables templates to generate realistic test data based on column types and constraints.
Configuration: templates.json¶
The templates.json
file is your control center - it tells Monstarillo what to generate and where to put it.
Template Definition¶
Each template specifies how to transform database tables into code files:
{
"templates": [
{
"templateFile": "/templates/go-api/struct.tmpl",
"generatedFileName": "{{ .CurrentTable.GetCamelCaseTableName }}.go",
"generatedFolderName": "models",
"outputPath": "/output/my-project",
"overwriteFile": true,
"minimumGeneratedFileLength": 0
}
]
}
Template Properties:
Property | Description | Example |
---|---|---|
templateFile |
Path to your template file | /templates/model.tmpl |
generatedFileName |
Name pattern for output files | {{ .CurrentTable.Name }}.go |
generatedFolderName |
Subdirectory for generated files | models |
outputPath |
Root directory for all output | /output/my-project |
overwriteFile |
Replace existing files? | true or false |
minimumGeneratedFileLength |
Skip files smaller than this | 0 (keep all files) |
Dynamic File Names
Use template variables in generatedFileName
to create unique files for each table:
- {{ .CurrentTable.Name }}.go
→ users.go
, orders.go
- {{ .CurrentTable.GetCamelCaseTableName }}Controller.java
→ UserController.java
Configuration Tags¶
Tags are reusable variables that keep your configuration DRY and maintainable:
{
"tags": [
{
"tagName": "ProjectRoot",
"value": "/home/patrick/my-project"
},
{
"tagName": "PackageName",
"value": "com.mycompany.api"
}
]
}
Using Tags in Configuration:
{
"outputPath": "{{ getTag .Tags \"ProjectRoot\" }}/src/main/java",
"templateFile": "{{ getTag .Tags \"TemplateRoot\" }}/java-api/controller.tmpl"
}
Table Filtering¶
Control which database tables to process:
Next Steps¶
Ready to see Monstarillo in action? The tutorial walks you through generating your first API from a sample database.
For detailed API information about tables, columns, and data types, see the API Reference.