Gorm-Plus

repository·main·Indexed 43 days ago

https://github.com/acmestack/gorm-plus

An enhancement tool for GORM that provides generic querying, simplified CRUD operations, and URL-based search capabilities. It features BuildQuery for converting URL parameters into database queries with support for equality, pattern matching, and logical grouping, as well as a pagination plugin and pointer field querying.

Tokens
830
Snippets
4
Records
6
Agent score
5%

What's inside Gorm-Plus

  1. Core Features of Gorm-Plus

    main

    Gorm-Plus enhances GORM with several out-of-the-box capabilities:

    1. Non-intrusive Enhancement: Maintains original GORM features and compatibility while adding new ones.
    2. Powerful CRUD & Universal Querying: Simplifies complex conditional queries without tedious configuration.
    3. Pointer Field Querying: Supports querying via pointer fields to prevent errors caused by incorrect field names.
    4. Generic Queries: Built-in generic query support allows for flexible return type encapsulation.
    5. Built-in Pagination: Includes a pagination plugin to handle large datasets efficiently.
  2. Initialize Gorm-Plus with GORM

    main

    Gorm-Plus is designed to be non-intrusive. To integrate it into your existing application, initialize it by passing your existing *gorm.DB instance to gplus.Init() after opening the database connection.

    // ... gorm.Open setup ...
    
    // Initialize gplus
    gplus.Init(gormDb)
  3. Use BuildQuery for dynamic URL-based searching

    main

    Gorm-Plus provides a powerful way to convert URL query parameters into complex database queries using gplus.BuildQuery[T]. This allows you to implement single-table search functionality with minimal code.

    func handleRequest(w http.ResponseWriter, r *http.Request) {
      queryParams := r.URL.Query()
      // Build query from URL params and execute selection
      list, _ := gplus.SelectList(gplus.BuildQuery[User](queryParams))
      
      marshal, _ := json.Marshal(list)
      w.Write(marshal)
    }
  4. Perform generic list selection with SelectList

    main

    Use gplus.SelectList[T] to perform a select query for a specific model type T. It returns a slice of the model and a *gorm.DB instance containing error and metadata information (like RowsAffected).

    // Example: Selecting all users
    users, resultDb := gplus.SelectList[User](nil)
    if resultDb.Error != nil {
        log.Println("error:", resultDb.Error)
    }
    log.Println("RowsAffected:", resultDb.RowsAffected)
    for _, user := range users {
        log.Println("user:", user)
    }
  5. Query syntax for BuildQuery

    main

    When using gplus.BuildQuery, you can construct various query conditions via URL parameters using the q key and special operators.

    Supported Patterns:

    • Equality: q=field=value (e.g., q=username=zhangsan)
    • Pattern Matching: q=field~>=prefix (e.g., q=username~>=zhang for names starting with 'zhang')
    • Comparison: q=field>value (e.g., q=age>20)
    • Multiple Conditions (AND): Use multiple q parameters (e.g., q=username=zhangsan&q=password=123456)
    • Logical Grouping (OR): Use group identifiers (A, B, etc.) and the gcond parameter to define OR logic (e.g., q=A.username=zhangsan&q=B.username=lisi&gcond=A|B)