orm/

directory
v1.2.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 1, 2026 License: Apache-2.0

README ΒΆ

ORM

Object-Relational Mapping (ORM) tools and query builders for Go.

Overview

The orm package provides working examples and comprehensive documentation for five popular Go database abstraction libraries. Each tool represents a different philosophy for database interaction, from full-featured ORMs to lightweight query builders.

Supported Tools

Tool Type Documentation Best For
Ent ORM πŸ“– Guide Complex relationships, type safety
GORM ORM πŸ“– Guide Rapid development, familiar API
SQLC Code Generator πŸ“– Guide Type-safe SQL, compile-time safety
SQLx Query Builder πŸ“– Guide Minimal abstraction, SQL control
Beego ORM πŸ“– Guide Beego framework integration

Quick Comparison

Philosophy
Tool Approach Schema Definition Query Style
Ent Code-first Go structs with field builders Graph-based, type-safe
GORM Convention over config Go structs with tags ActiveRecord-style
SQLC SQL-first SQL schema files Raw SQL with codegen
SQLx Database/sql wrapper Manual SQL Raw SQL with scanning
Beego Framework-integrated Go structs with tags QueryBuilder + ORM
Feature Matrix
Feature Ent GORM SQLC SQLx Beego
Type Safety βœ…βœ…βœ… βœ…βœ… βœ…βœ…βœ… βœ… βœ…βœ…
Learning Curve Steep Easy Medium Easy Medium
Code Generation βœ… ❌ βœ… ❌ ❌
Auto Migration βœ… βœ… ❌ ❌ βœ…
Relationships βœ…βœ…βœ… βœ…βœ…βœ… Manual Manual βœ…βœ…
Performance βœ…βœ… βœ… βœ…βœ…βœ… βœ…βœ…βœ… βœ…βœ…
SQL Control βœ… βœ… βœ…βœ…βœ… βœ…βœ…βœ… βœ…βœ…
Community Size Medium Large Medium Large Small

Decision Guide

Choose Ent When
  • Complex relationships: Your domain has many entity relationships
  • Type safety critical: Compile-time guarantees are essential
  • Graph queries: You need to traverse relationships efficiently
  • Schema evolution: Automatic migration generation is valuable
  • Greenfield projects: Starting fresh with code-first approach

πŸ“– Read the Ent Guide

Choose GORM When
  • Rapid development: Need to build quickly with minimal boilerplate
  • Familiar API: Coming from Rails/ActiveRecord background
  • Rich features: Need hooks, associations, auto-migration out of the box
  • Large community: Want extensive documentation and plugins
  • Learning curve: Need something easy to pick up

πŸ“– Read the GORM Guide

Choose SQLC When
  • SQL expertise: You know SQL and want to write it directly
  • Performance critical: Need zero runtime overhead
  • Type safety: Want compile-time query verification
  • Database-specific: Need to use database-specific features
  • Simple queries: Mostly straightforward CRUD operations

πŸ“– Read the SQLC Guide

Choose SQLx When
  • Minimal abstraction: Want thin layer over database/sql
  • Full control: Need complete control over SQL
  • Small projects: Don't need heavy ORM machinery
  • Learning SQL: Want to improve SQL skills
  • Existing code: Migrating from database/sql

πŸ“– Read the SQLx Guide

Choose Beego ORM When
  • Beego framework: Already using Beego web framework
  • Framework integration: Want tight web framework coupling
  • QueryBuilder needed: Mix of ORM and query builder
  • Multi-database: Need to support multiple databases easily
  • China-focused: Development team is China-based

πŸ“– Read the Beego Guide

Getting Started

See individual guides for detailed installation, configuration, and usage:

  • Ent Guide - Schema definition, code generation, graph queries
  • GORM Guide - Models, associations, migrations, hooks
  • SQLC Guide - SQL queries, code generation, configuration
  • SQLx Guide - Struct scanning, named queries, transactions
  • Beego Guide - ORM features, QueryBuilder, framework integration

Performance Considerations

Tool Query Performance Memory Usage Startup Time
Ent Good Medium Medium (codegen)
GORM Fair High Fast
SQLC Excellent Low Medium (codegen)
SQLx Excellent Low Fast
Beego Good Medium Fast

Recommendations:

  • High performance: SQLC or SQLx for minimal overhead
  • Balanced: Ent for type safety with good performance
  • Rapid development: GORM for quick iteration
  • Control: SQLx for manual optimization

Common Patterns

Connection Management
// Ent
client, err := ent.Open("mysql", dsn)
defer client.Close()

// GORM
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
sqlDB, _ := db.DB()
defer sqlDB.Close()

// SQLC/SQLx
db, err := sqlx.Connect("mysql", dsn)
defer db.Close()

// Beego
orm.RegisterDataBase("default", "mysql", dsn)
Transaction Handling
// Ent
tx, err := client.Tx(ctx)
if err := txFunc(ctx, tx); err != nil {
    tx.Rollback()
} else {
    tx.Commit()
}

// GORM
db.Transaction(func(tx *gorm.DB) error {
    // operations
    return nil
})

// SQLx
tx, _ := db.Beginx()
defer tx.Rollback()
// operations
tx.Commit()

Migration Between Tools

From database/sql to SQLx

Easiest migration - SQLx extends database/sql interface.

From GORM to Ent

Requires schema redesign but improves type safety.

From Raw SQL to SQLC

Keep your SQL, gain type safety through codegen.

From Any Tool to SQLx

Always possible - SQLx works with any database/sql driver.

Next Steps

Choose a tool from the decision guide above and read its detailed documentation. Each guide includes installation, complete API reference, best practices, testing examples, and troubleshooting.

Resources

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL