Skip to content

burugo/thing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

246 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Thing ORM

Go Report Card Build Status Go Reference MIT License Go Version

Thing is a Go ORM focused on fast CRUD, ID-based loading, list queries, and built-in cache invalidation. It uses generics for type-safe model access and supports SQLite, MySQL, and PostgreSQL.

For detailed usage, model tags, relationships, cache-friendly query patterns, and raw SQL guidance, see the usage guides:

Install

go get github.com/burugo/thing

Quick Start

package main

import (
	"log"

	"github.com/burugo/thing"
	"github.com/burugo/thing/drivers/db/sqlite"
)

type User struct {
	thing.BaseModel
	Name  string `db:"name,index"`
	Email string `db:"email,unique"`
}

func main() {
	db, err := sqlite.NewSQLiteAdapter("app.db")
	if err != nil {
		log.Fatal(err)
	}

	if err := thing.Configure(db, nil); err != nil { // nil uses the built-in memory cache
		log.Fatal(err)
	}

	if err := thing.AutoMigrate(&User{}); err != nil {
		log.Fatal(err)
	}

	users, err := thing.Use[*User]()
	if err != nil {
		log.Fatal(err)
	}

	alice := &User{Name: "Alice", Email: "alice@example.com"}
	if err := users.Save(alice); err != nil {
		log.Fatal(err)
	}

	cachedUser, err := users.ByID(alice.ID)
	if err != nil {
		log.Fatal(err)
	}
	_ = cachedUser

	activeUsers, err := users.
		Where("name LIKE ?", "A%").
		Order("id DESC").
		Fetch(0, 20)
	if err != nil {
		log.Fatal(err)
	}
	_ = activeUsers
}

What Thing Optimizes For

  • Save, SaveMany, ByID, ByIDs, Delete, DeleteMany, and SoftDelete
  • Paginated list queries through Query(...).Fetch(offset, limit)
  • Count caching through Query(...).Count()
  • Model cache reuse when list results are hydrated
  • Cache invalidation for common WHERE and ORDER BY patterns
  • Relationship preloading without hand-written joins

Thing intentionally keeps the ORM surface small. For reporting queries, database-specific SQL, or one-off joins, use the raw DBAdapter / *sql.DB accessors and treat those reads as outside the ORM cache path.

Cache-Friendly Pattern

Prefer this shape:

ids := []int64{1, 2, 3}
usersByID, err := users.ByIDs(ids)

or:

page, err := users.Where("id IN (?)", ids).Fetch(0, len(ids))

Avoid replacing repeated application reads with SELECT * ... JOIN ... when the same data can be loaded as an ID list and hydrated through ByID / ByIDs. That is the path where Thing can reuse model cache and maintain query cache invalidation.

More examples are in Cache Utilization / 提高缓存利用率.

Documentation

Development

Run the test suite:

go test ./...

License

MIT

About

A full-featured, fast Go ORM with Redis caching, generics, and a simple, developer-friendly API for MySQL, PostgreSQL, and SQLite.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages