Models Introduction
Models are structs that map to database tables. Each model instance represents a row.
Nimbus models embed database.Model
for ID, timestamps, and soft deletes, and use GORM under the hood for queries and hooks,
while exposing framework-agnostic APIs for table names, relations, and metadata.
What is a model?
A model combines:
- Table mapping — Struct fields map to columns (Nimbus convention via
Table()or automatic pluralization) - CRUD operations — Create, Read, Update, Delete via GORM
- Relationships — hasMany, belongsTo, manyToMany with eager loading
- Hooks — BeforeSave, AfterCreate, etc.
Base model
Embed database.Model for standard fields (including soft deletes):
type Model struct {
ID uint
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt // soft delete (used by GORM + Nimbus helpers)
}
Defining a model
type Post struct {
database.Model
Title string
Content string
Status string
}
// Optional: override logical table name (used by Nimbus helpers).
func (Post) Table() string { return "posts" }
When no Table() is defined,
Nimbus derives the table name from the struct name using snake_case + pluralization:
Blog → blogs,
UserProfile → user_profiles.
Fillable metadata
For mass-assignment and scaffolding, models can optionally implement
Fillable() []string to declare which
fields are safe to assign from input. Nimbus helpers like admin UIs or request binders
can use this metadata.
func (Post) Fillable() []string {
return []string{"Title", "Content", "Status"}
}
Creating a model
Use the CLI:
nimbus make:model Post