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 19 commits into
base: master
Choose a base branch
from
Open

(WIP) Implement Generics API #7424

wants to merge 19 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)

@jinzhu jinzhu requested review from a631807682 and Copilot May 21, 2025 14:58
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements a type-safe, generic CRUD and query API layer on top of GORM. Key changes include:

  • Introduction of generic constructor and API methods for CRUD and raw SQL operations.
  • Updates to join handling and inline condition building across query and preload callbacks.
  • Dependency updates and minor test adjustments.

Reviewed Changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/joins_test.go Removed DB.Debug() calls from preload and join tests.
tests/go.mod Updated dependency versions and removed toolchain declarations.
tests/connpool_test.go Inherited Logger from DB to ensure consistent logging.
statement.go Refactored join struct to include Alias and Expression; unified table references in conditions.
scan.go Updated nested relation name handling in scan to account for join alias changes.
clause/joins.go Added JoinTarget helper functions to support new join APIs.
chainable_api.go Improved inline documentation for chainable API usage.
callbacks/query.go Changed join alias resolution by referencing join.Alias and adjusted join expression handling.
callbacks/preload.go Modified the way inline conditions are applied to improve query composition.
Comments suppressed due to low confidence (1)

callbacks/preload.go:288

  • The change in how inline conditions are applied alters the original query behavior; please verify that merging these conditions with existing WHERE clauses is intentional.
if len(inlineConds) > 0 { tx = tx.Where(inlineConds[0], inlineConds[1:]...) }

@@ -146,9 +146,13 @@ func BuildQuerySQL(db *gorm.DB) {

if isRelations {
genJoinClause := func(joinType clause.JoinType, parentTableName string, relation *schema.Relationship) clause.Join {
Copy link
Preview

Copilot AI May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable 'join' is not defined in the closure; consider using the appropriate parameter or property (e.g., relation's alias) for alias resolution.

Suggested change
genJoinClause := func(joinType clause.JoinType, parentTableName string, relation *schema.Relationship) clause.Join {
genJoinClause := func(join clause.Join, joinType clause.JoinType, parentTableName string, relation *schema.Relationship) clause.Join {

Copilot uses AI. Check for mistakes.

@@ -244,6 +245,12 @@ func Scan(rows Rows, db *DB, mode ScanMode) {
matchedFieldCount[column] = 1
}
} else if names := utils.SplitNestedRelationName(column); len(names) > 1 { // has nested relation
for _, join := range db.Statement.Joins {
if join.Alias == names[0] {
names = append(strings.Split(join.Name, "."), names[len(names)-1])
Copy link
Preview

Copilot AI May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Appending the split join name to the existing names slice may lead to an unexpected structure; consider reassigning names if the intent is to replace the existing value.

Suggested change
names = append(strings.Split(join.Name, "."), names[len(names)-1])
names = append([]string{}, append(strings.Split(join.Name, "."), names[len(names)-1])...)

Copilot uses AI. Check for mistakes.

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