Skip to content

(WIP) Implement Generics API #7424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open

(WIP) Implement Generics API #7424

wants to merge 10 commits into from

Conversation

jinzhu
Copy link
Member

@jinzhu jinzhu commented Apr 17, 2025

feat(gorm): add generic CRUD & query interfaces

This commit introduces a type-safe, generic API layer on top of GORM that
allows you to perform Create, Read, Update, Delete and raw SQL operations

Helper constructor:

  func G[T any](db *DB, opts ...clause.Expression) Interface[T]

Basic usage:

// Create a user
u := User{Name: "Alice"}
if err := gorm.G[User](DB).Create(ctx, &u); err != nil {
  // handle error
}

// Query by name and count
cnt, err := gorm.G[User](DB).
  Where("name = ?", "Alice").
  Count(ctx, "*")

// Raw + Find
users, err := gorm.G[User](DB).
  Raw("SELECT * FROM users WHERE created_at > ?", since).
  Find(ctx)

// Update one field
rows, err := gorm.G[User](DB).
  Where("id = ?", u.ID).
  Update(ctx, "email", "alice@example.com")

// Delete
deleted, err := gorm.G[User](DB).
  Where("id = ?", u.ID).
  Delete(ctx)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant