DEV Community

Cover image for Introducing Go DB ORM (v1.0.1) — A Type-Safe, Fluent ORM for Go
Md Anik Islam
Md Anik Islam

Posted on

Introducing Go DB ORM (v1.0.1) — A Type-Safe, Fluent ORM for Go

I’ve been working on an ORM for Go which is lightweight, easy to use, type-safe, and developer-friendly at its core.

The main goal of GoDB ORM is simple:

Make database handling in Go feel clean, predictable, and enjoyable — without heavy abstraction or hidden magic.

It is designed to feel like native Go, not a framework that hides what’s happening under the hood.

It also balances:

  1. Performance
  2. Simplicirt
  3. Flexibility

This is the first version of this package. So, it might not have all the features you expect from an ORM, and there could be some bugs in it.

But please give it a try. Your contributions, bug reports, feature requests, and feedback will be much appreciated.

Key Features:
1. Type-Safe Query Builder (Generic API)
No interface{} chaos — everything is strongly typed:

users, err := client.Query[User]().
    Where("city = ?", "Dhaka").
    Where("age > ?", 18).
    OrderBy("created_at DESC").
    Limit(10).
    Offset(20).
    All()
Enter fullscreen mode Exit fullscreen mode
  1. Compile-time safety
  2. No type casting
  3. Predictable results

2. Fluent & Clean API
Readable query chaining like natural language:

client.Model(&User{}).
    Where("active = ?", true).
    OrderBy("created_at DESC").
    Find()
Enter fullscreen mode Exit fullscreen mode
  1. Simple learning curve
  2. Familiar to GORM users
  3. Clean production code style

3. Schema-Driven Development (CLI Powered)
Define your database schema like this:

model User {
    id         int      @id
    name       string
    email      string
    city       string?
    created_at datetime
}

Enter fullscreen mode Exit fullscreen mode

Then generate everything automatically:

godborm migrate
godborm generate

  1. Auto migrations
  2. Auto Go struct generation
  3. Faster development workflow

4. Smart Relations with Include()

Load related data easily:

client.Query[Invoice]().
    Include("User:name,email").
    Include("Items:item_name,quantity").
    All()
Enter fullscreen mode Exit fullscreen mode
  1. Prevents unnecessary data loading
  2. Reduces DB cost
  3. Supports field-level selection

5. Smart CRUD Operations

user := &User{Name: "Alice", Email: "alice@example.com"}
client.Model(user).Save()   // Insert

user.Name = "Bob"
client.Model(user).Save()   // Update

client.Model(user).Delete() // Delete
Enter fullscreen mode Exit fullscreen mode
  1. Automatic insert/update detection
  2. Less boilerplate

6. Transactions Made Simple

err := client.WithTx(func(tx *client.Tx) error {
    tx.Create(&order)
    tx.Update(&inventory)
    return nil
})
Enter fullscreen mode Exit fullscreen mode
  1. Safe rollback handling
  2. Clean transactional flow

7. Raw SQL Support (Escape Hatch)

client.Raw("SELECT * FROM users WHERE email LIKE $1", "%@gmail.com").
    Scan(&users)
Enter fullscreen mode Exit fullscreen mode
  1. Full control when needed
  2. No restrictions

Lets Start Go DB ORM

Getting started takes only a few steps:

1. Install CLI

go install github.com/Anik2069/go-db-orm/cmd/godborm@latest
Enter fullscreen mode Exit fullscreen mode

2. Initialize ORM

godborm init
Enter fullscreen mode Exit fullscreen mode

This generates:
godborm.json
/schema folder

3. Define Your Schema

model User {
    id         int      @id
    name       string
    email      string
    city       string?
    created_at datetime
}

model Post {
    id       int      @id
    title    string
    user_id  int      @foreign(User.id)
}
Enter fullscreen mode Exit fullscreen mode

4. Run Migrations

godborm migrate

Enter fullscreen mode Exit fullscreen mode

5. Generate Models

godborm generate --package main
Enter fullscreen mode Exit fullscreen mode

6. Connect in Code

import "github.com/Anik2069/go-db-orm/godborm/client"

func main() {
    err := client.ConnectWithConfig()
    if err != nil {
        panic(err)
    }
    defer client.Close()
}
Enter fullscreen mode Exit fullscreen mode
🛠 Config
{
    "schema": "./schema",
    "migrations": "./migrations",
    "driver": "postgres",
    "dsn": "postgresql://user:pass@localhost:5432/dbname?sslmode=disable"
}
Enter fullscreen mode Exit fullscreen mode

Conclusion
This project is still evolving and actively improving. Contributions, feedback, bug reports, and feature ideas are highly welcome. If you’re interested in improving GoDB ORM, feel free to explore and contribute:

GitHub: https://github.com/Anik2069/go-db-orm

Let’s build something useful for the Go ecosystem together

Top comments (0)