🏎️ Zima ORM library that is simple, ultra-fast and self-mockable for Go
Measured on 2026-11-25: Apple M4 Pro, macOS 26.6.2, Go 1.25.6, GOMAXPROCS=1. All six libraries use in-memory SQLite with one connection. ZORM uses commit 1573041, including the cache fixes and optimizations. Values use the median of five runs.
| Test Case | ZORM | BUN | ENT | GORM | SQLX | XORM |
|---|---|---|---|---|---|---|
| InsertSingle | 🟡 1.03x | 🟠 2.22x | 🟠 2.41x | 🟠 3.99x | 🟢 1.00x | 🟡 1.55x |
| InsertBatch | 🟢 1.00x | 🟡 1.61x | 🟠 2.82x | 🟡 1.89x | 🟡 1.57x | 🟡 1.91x |
| GetByID | 🟢 1.00x | 🟠 2.54x | 🟠 3.05x | 🟠 3.28x | 🟡 1.79x | 🟠 3.85x |
| GetByIDs | 🟢 1.00x | 🟡 1.48x | 🟡 1.71x | 🟡 1.85x | 🟡 1.34x | 🟠 2.36x |
| Update | 🟢 1.00x | 🟡 1.71x | 🔴 5.73x | 🟠 4.63x | 🟡 1.26x | 🟠 2.78x |
| Delete | 🟢 1.00x | 🟡 1.62x | 🟡 1.87x | 🟠 4.51x | 🟡 1.16x | 🟠 2.66x |
| Count | 🟢 1.00x | 🟡 1.67x | 🔴 8.58x | 🟠 2.32x | 🟡 1.37x | 🟠 2.54x |
| GetAll | 🟢 1.00x | 🟡 1.09x | 🟡 1.17x | 🟡 1.47x | 🟡 1.07x | 🟠 2.18x |
Time ratio: Each operation’s fastest median is 1.00x; lower is better. These are elapsed-time multiples, not throughput multipliers.
The v1.0.0 release includes a benchmark archive with library versions, absolute timings, raw results, benchmark corrections, and reproduction commands.
- Measured ORM Performance: The table above compares eight operations across six libraries; raw timings and allocations are available in the report
- Reuse Enabled by Default: SQL and metadata caching with regression tests for cache isolation and concurrency
- Connection Pool Management: Configurable pool with optimal defaults for high concurrency
- Read-Write Separation: Automatic routing of read/write operations for better performance
- Map Support: Use
map[string]interface{}without struct definitions - Auto Naming: CamelCase to snake_case conversion for database fields
- Flexible Tags: Support
zorm:"field_name,auto_incr"format - Atomic DDL: Create, alter, and drop tables with atomic operations
- One-Line Operations: Simple Insert, Update, Select, Delete APIs
- Transaction Support: Built-in transaction management with context support
- Join Queries: Advanced JOIN operations with flexible ON conditions
- SQL Audit: Complete audit logging for all database operations
- Easy to use: SQL-Like (One-Line-CRUD)
- KISS: Keep it small and beautiful (not big and comprehensive)
- Universal: Support struct, map, pb and basic types
- Testable: Support self-mock (because parameters as return values, most mock frameworks don't support)
- A library that is not test-oriented is not a good library
- As-Is: Try not to make hidden settings to prevent misuse
- Solve core pain points:
- Manual SQL is error-prone, data assembly takes too much time
- time.Time cannot be read/written directly
- SQL function results cannot be scanned directly
- Database operations cannot be easily mocked
- QueryRow's sql.ErrNoRows problem
- Directly replace the built-in Scanner, completely take over data reading type conversion
- Core principles:
- Don't map a table to a model like other ORMs
- (In zorm, you can use Fields filter to achieve this)
- Try to keep it simple, map one operation to one model!
- Other advantages:
- More natural where conditions (only add parentheses when needed, compared to gorm)
- In operation accepts various types of slices
- Switching from other ORM libraries requires no historical code modification, non-invasive modification
Below is a comparison with mainstream ORM libraries (please don't hesitate to open issues for corrections)
| Library | zorm (me) | gorm | xorm | Notes | |
| Usability | No type specification needed | ✅ | ❌ | ❌ | zorm doesn't need low-frequency DDL in tags |
| No model specification needed | ✅ | ❌ | ❌ | gorm/xorm modification operations need to provide "template" | |
| No primary key specification needed | ✅ | ❌ | ❌ | gorm/xorm prone to misoperation, such as deleting/updating entire table | |
| Low learning cost | ✅ | ❌ | ❌ | If you know SQL, you can use zorm | |
| Reuse native connections | ✅ | ❌ | ❌ | zorm has minimal refactoring cost | |
| Full type conversion | ✅ | maybe | ❌ | Eliminate type conversion errors | |
| Reuse query commands | ✅ | ❌ | ❌ | zorm uses the same function for batch and single operations | |
| Map type support | Operate database with map | ✅ | ❌ | ❌ | Without defining struct, flexible handling of dynamic fields |
| Testability | Self-mock | ✅ | ❌ | ❌ | zorm is very convenient for unit testing |
| Performance | Measured performance | See benchmarks above | — | — | Results depend on the driver, workload, and concurrency |
| Reflection | reflect2 | reflect | reflect | zorm zero use of ValueOf | |
| Cache Optimization | 🚀 | ✅ | ✅ | SQL and metadata caching; see the measured results and regression tests | |
-
Import package
import z "github.com/IceWhaleTech/zorm"
-
Define Table object
t := z.Table(d.DB, "t_usr") t1 := z.TableContext(ctx, d.DB, "t_usr")
d.DBis a database connection object that supports Exec/Query/QueryRowt_usrcan be a table name or nested query statementctxis the Context object to pass, defaults to context.Background() if not provided- Reuse functionality is enabled by default; use
NoReuse()to disable SQL and metadata caching
-
(Optional) Define model object
type Info struct { ID int64 `zorm:"id,auto_incr"` // Auto-increment primary key Name string // Auto-converted to "name" Tag string // Auto-converted to "tag" }
-
Execute operations
-
CRUD interfaces return (affected rows, error)
-
Type
Vis an abbreviation formap[string]interface{}, similar togin.H -
Insert
// o can be object/slice/ptr slice n, err = t.Insert(&o) n, err = t.InsertIgnore(&o) n, err = t.ReplaceInto(&o) // Insert only partial fields (others use defaults) n, err = t.Insert(&o, z.Fields("name", "tag")) // Resolve primary key conflicts (using excluded values - SQLite UPSERT) // This is SQLite's official UPSERT syntax, equivalent to MySQL's ON DUPLICATE KEY UPDATE // Example: INSERT INTO users (id, name, age) VALUES (1, 'Alice', 18) // ON CONFLICT(id) DO UPDATE SET name = excluded.name, age = excluded.age; n, err = t.Insert(&o, z.Fields("id", "name", "age"), z.OnConflictDoUpdateSet([]string{"id"}, []string{"name", "age"})) // Use map insert (no need to define struct) userMap := map[string]interface{}{ "name": "John Doe", "email": "john@example.com", "age": 30, } n, err = t.Insert(userMap) // Support embedded struct type User struct { Name string `zorm:"name"` Email string `zorm:"email"` Address struct { Street string `zorm:"street"` City string `zorm:"city"` } `zorm:"-"` // embedded struct } n, err = t.Insert(&user) // Support field ignore type User struct { Name string `zorm:"name"` Password string `zorm:"-"` // ignore this field Email string `zorm:"email"` } n, err = t.Insert(&user) // Auto-increment primary key type User struct { ID int64 `zorm:"id,auto_incr"` // Auto-increment primary key Name string `zorm:"name"` Age int `zorm:"age"` } user := User{Name: "Alice", Age: 25} n, err = t.Insert(&user) // user.ID will be automatically set to the generated ID // Support both pointer and non-pointer types user := User{Name: "Bob", Age: 30} // Non-pointer users := []User{{Name: "Charlie", Age: 35}} // Non-pointer slice n, err = t.Insert(user) // Non-pointer struct n, err = t.Insert(&users) // Non-pointer slice
-
Select
// o can be object/slice/ptr slice n, err := t.Select(&o, z.Where("name = ?", name), z.GroupBy("id"), z.Having(z.Gt("id", 0)), z.OrderBy("id", "name"), z.Limit(1)) // Use basic type + Fields to get count (n value is 1, because result has only 1 row) var cnt int64 n, err = t.Select(&cnt, z.Fields("count(1)"), z.Where("name = ?", name)) // Also support arrays var ids []int64 n, err = t.Select(&ids, z.Fields("id"), z.Where("name = ?", name)) // Can force index n, err = t.Select(&ids, z.Fields("id"), z.IndexedBy("idx_xxx"), z.Where("name = ?", name)) // Advanced Join Queries // Simple join with string ON condition var results []UserOrder n, err := t.Select(&results, z.Fields("users.id", "users.name", "orders.amount"), z.InnerJoin("orders", "users.id = orders.user_id"), z.Where("orders.status = ?", "completed"), ) // Complex join with condition objects n, err = t.Select(&results, z.Fields("users.id", "users.name", "orders.amount"), z.LeftJoin("orders", z.Eq("users.id", z.U("orders.user_id"))), )
-
Select to Map (no struct needed)
// single row to map var m map[string]interface{} n, err := t.Select(&m, z.Fields("id", "name", "age"), z.Where(z.Eq("id", 1))) // multiple rows to []map var ms []map[string]interface{} n, err = t.Select(&ms, z.Fields("id", "name", "age"), z.Where(z.Gt("age", 18)))
-
Update
// o can be object/slice/ptr slice n, err = t.Update(&o, z.Where(z.Eq("id", id))) // Use map update n, err = t.Update(z.V{ "name": "new_name", "tag": "tag1,tag2,tag3", "age": z.U("age+1"), // Use z.U to handle non-variable updates }, z.Where(z.Eq("id", id))) // Use map update partial fields n, err = t.Update(z.V{ "name": "new_name", "tag": "tag1,tag2,tag3", }, z.Fields("name"), z.Where(z.Eq("id", id))) n, err = t.Update(&o, z.Fields("name"), z.Where(z.Eq("id", id)))
-
CRUD with Reuse (enabled by default)
// Reuse is on by default; repeated calls at the same call-site reuse SQL/metadata // Update example type User struct { ID int64 `zorm:"id"`; Name string `zorm:"name"`; Age int `zorm:"age"` } for _, u := range users { _, _ = t.Update(&u, z.Fields("name", "age"), z.Where(z.Eq("id", u.ID))) } // Insert example for _, u := range users { _, _ = t.Insert(&u) }
-
Delete
// Delete by condition n, err = t.Delete(z.Where("name = ?", name)) n, err = t.Delete(z.Where(z.Eq("id", id)))
-
Exec (Raw SQL)
// Execute raw SQL with parameters n, err = t.Exec("UPDATE users SET status = ? WHERE id = ?", "active", 123) n, err = t.Exec("DELETE FROM logs WHERE created_at < ?", time.Now().AddDate(0, 0, -30)) n, err = t.Exec("CREATE INDEX idx_name ON users (name)")
-
Variable conditions
conds := []interface{}{z.Cond("1=1")} // prevent empty where condition if name != "" { conds = append(conds, z.Eq("name", name)) } if id > 0 { conds = append(conds, z.Eq("id", id)) } // Execute query operation n, err := t.Select(&o, z.Where(conds...))
-
Join queries
type Info struct { ID int64 `zorm:"users.id"` // field definition with table name Name string `zorm:"users.name"` Tag string `zorm:"tags.tag"` } t := z.Table(d.DB, "users") n, err := t.Select(&o, z.InnerJoin("tags", "users.id = tags.user_id"), z.Where(z.Eq("users.id", id)))
-
Get inserted auto-increment id
// Modern approach: Use auto_incr tag type Info struct { ID int64 `zorm:"id,auto_incr"` // Auto-increment primary key Name string `zorm:"name"` Age int `zorm:"age"` } o := Info{ Name: "OrcaZ", Age: 30, } n, err = t.Insert(&o) id := o.ID // get the inserted id automatically set
Note: The old
ZormLastIdfield is still supported for backward compatibility, but the modernauto_incrtag approach is recommended. -
New features example: Map types and Embedded Struct
// 1. Use map type (no need to define struct) userMap := map[string]interface{}{ "name": "John Doe", "email": "john@example.com", "age": 30, "created_at": time.Now(), } n, err := t.Insert(userMap) // 2. Support embedded struct type Address struct { Street string `zorm:"street"` City string `zorm:"city"` Zip string `zorm:"zip"` } type User struct { ID int64 `zorm:"id"` Name string `zorm:"name"` Email string `zorm:"email"` Address Address `zorm:"-"` // embedded struct Password string `zorm:"-"` // ignore field } user := User{ Name: "Jane Doe", Email: "jane@example.com", Address: Address{ Street: "123 Main St", City: "New York", Zip: "10001", }, Password: "secret", // this field will be ignored } n, err := t.Insert(&user) // 3. Complex nested structure type Profile struct { Bio string `zorm:"bio"` Website string `zorm:"website"` } type UserWithProfile struct { ID int64 `zorm:"id"` Name string `zorm:"name"` Profile Profile `zorm:"-"` // nested embedding }
-
Currently using other ORM frameworks (new interfaces can be switched first)
// [gorm] db is a *gorm.DB t := z.Table(db.DB(), "tbl") // [xorm] db is a *xorm.EngineGroup t := z.Table(db.Master().DB().DB, "tbl") // or t := z.Table(db.Slave().DB().DB, "tbl")
| Option | Description |
|---|---|
| Debug | Print SQL statements |
| Reuse | Cache SQL and metadata by call location, database, table, model, configuration, and argument shape (enabled by default) |
| NoReuse | Disable SQL and metadata reuse |
| ToTimestamp | Use timestamp for Insert, not formatted string |
| Audit | Enable SQL audit logging and performance monitoring |
Option usage example:
n, err = t.Debug().Insert(&o)
n, err = t.ToTimestamp().Insert(&o)
// Reuse functionality is enabled by default, no manual call needed
// To disable SQL and metadata reuse, call:
n, err = t.NoReuse().Insert(&o)
// Enable audit with chain-style method
userTable := zorm.Table(db, "users").Audit(nil, nil) // Uses default loggers
// Or with custom loggers
auditLogger := zorm.NewJSONAuditLogger()
telemetryCollector := zorm.NewDefaultTelemetryCollector()
userTable := zorm.Table(db, "users").Audit(auditLogger, telemetryCollector)
// Chain multiple options
advancedTable := zorm.Table(db, "users").
Debug(). // Enable debug mode
Audit(nil, nil) // Enable audit loggingNotes:
Audit(nil, nil)now wraps the current DB with a working audit/telemetry layer instead of a placeholder no-op.NewDefaultTelemetryCollectorWithLimit(n)can be used to keep only the latestntelemetry records in memory.Table(...),TableContext(...),NewReadWriteDB(...), andNewAuditableDB(...)now enable a bounded prepared-statement cache automatically when the underlying DB supportsPrepareContext;NewStmtCacheDB(db, n)is available for explicit wrapping.
| Example | Description |
|---|---|
| Where("id=? and name=?", id, name) | Regular formatted version |
| Where(Eq("id", id), Eq("name", name)...) | Default to and connection |
| Where(And(Eq("x", x), Eq("y", y), Or(Eq("x", x), Eq("y", y)...)...)...) | And & Or |
For non-panicking input validation, use TryWhere(...).
| Name | Example | Description |
|---|---|---|
| Logical AND | And(...) | Any number of parameters, only accepts relational operators below |
| Logical OR | Or(...) | Any number of parameters, only accepts relational operators below |
| Normal condition | Cond("id=?", id) | Parameter 1 is formatted string, followed by placeholder parameters |
| Equal | Eq("id", id) | Two parameters, id=? |
| Not equal | Neq("id", id) | Two parameters, id<>? |
| Greater than | Gt("id", id) | Two parameters, id>? |
| Greater than or equal | Gte("id", id) | Two parameters, id>=? |
| Less than | Lt("id", id) | Two parameters, id<? |
| Less than or equal | Lte("id", id) | Two parameters, id<=? |
| Between | Between("id", start, end) | Three parameters, between start and end |
| Like | Like("name", "x%") | Two parameters, name like "x%" |
| GLOB | GLOB("name", "?x*") | Two parameters, name glob "?x*" |
| Multiple value selection | In("id", ids) | Two parameters, ids is basic type slice |
| Example | Description |
|---|---|
| GroupBy("id", "name"...) | - |
| Example | Description |
|---|---|
| Having("id=? and name=?", id, name) | Regular formatted version |
| Having(Eq("id", id), Eq("name", name)...) | Default to and connection |
| Having(And(Eq("x", x), Eq("y", y), Or(Eq("x", x), Eq("y", y)...)...)...) | And & Or |
For non-panicking input validation, use TryHaving(...).
| Example | Description |
|---|---|
| OrderBy("id desc", "name asc"...) | - |
| Example | Description |
|---|---|
| Limit(1) | Page size 1 |
| Limit(3, 2) | Page size 3, offset position 2 (Note the difference from MySQL) |
For non-panicking input validation, use TryLimit(...).
| Example | Description |
|---|---|
| OnConflictDoUpdateSet([]string{"id"}, []string{"name", "age"}) | SQLite UPSERT syntax using excluded values. Equivalent to MySQL's ON DUPLICATE KEY UPDATE. Uses excluded. prefix to reference conflicting row values. |
| Example | Description |
|---|---|
| Insert(map[string]interface{}{"name": "John", "age": 30}) | Use map to insert data |
| Support all CRUD operations | Select, Insert, Update, Delete all support map |
| Example | Description |
|---|---|
| struct embeds other struct | Automatically handle composite object fields |
| zorm:"-" tag | Mark embedded struct |
| Example | Description |
|---|---|
Password string zorm:"-" |
Ignore this field, not participate in database operations |
| Suitable for sensitive fields | Such as passwords, temporary fields, etc. |
| Example | Description |
|---|---|
| IndexedBy("idx_biz_id") | Solve index selectivity issues |
- Call
ZormMockto specify operations to mock - Use
ZormMockFinishto check if mock was hit
-
First five parameters are
tbl,fun,caller,file,pkg-
Set to empty for default matching
-
Support wildcards '?' and '*', representing match one character and multiple characters respectively
-
Case insensitive
Parameter Name Description tbl Table name Database table name fun Method name Select/Insert/Update/Delete caller Caller method name Need to include package name file File name File path where used pkg Package name Package name where used
-
-
Last three parameters are
return data,return affected rowsanderror -
Can only be used in test files
Function to test:
package x
func test(db *sql.DB) (X, int, error) {
var o X
tbl := z.Table(db, "tbl")
n, err := tbl.Select(&o, z.Where("`id` >= ?", 1), z.Limit(100))
return o, n, err
}In the x.test method querying tbl data, we need to mock database operations
// Must set mock in _test.go file
// Note caller method name needs to include package name
z.ZormMock("tbl", "Select", "*.test", "", "", &o, 1, nil)
// Call the function under test
o1, n1, err := test(db)
So(err, ShouldBeNil)
So(n1, ShouldEqual, 1)
So(o1, ShouldResemble, o)
// Check if all hits
err = z.ZormMockFinish()
So(err, ShouldBeNil)All operations are automatically monitored with telemetry data:
- Duration tracking: Measure operation execution time
- Cache hit rates: Monitor reuse effectiveness
- Memory usage: Track allocation patterns
- Error rates: Monitor operation success/failure rates
Supported struct tags:
zorm:"field_name"- Field name mappingzorm:"field_name,auto_incr"- Auto-increment primary keyzorm:"auto_incr"- Use converted field name with auto-incrementzorm:"-"- Ignore field- No tag - Auto-convert camelCase to snake_case
- v1.0.0 Release Notes and Benchmark Archive - Cache fixes, dependency updates, version comparison, six-ORM results, and reproduction commands
- Historical Performance Report (2025-09-28)
Compared with commit 72401e1, five alternating runs of the current code (1573041) reduced four SQLite query timings by 27.7%–34.0%; struct Select went from 2,586 to 1,708 ns/op, with allocations from 36 to 22. Default Update overhead decreased 7.4%. Insert was approximately unchanged. Update with a new table per call used 26.1% fewer allocation bytes but took 2.9% longer. These results include the SQLite driver upgrade; full results and validation are in the benchmark archive.
SQL binding caches use separate instances for each database, table, model type, and configuration; Insert also separates insertion modes and batch lengths. Entries retain the existing call-site/SQL-shape key. Table objects remember their most recent instance, and each call site retains a hint for its first scope. Every hint validates the complete scope; mismatches use the shared registry.
Field metadata is shared by actual Go model type and remains independent of table and database identity. Binding metadata is complete before publication and stays read-only on hits. Regression tests cover cross-table operations, changing batch sizes, model/configuration/database isolation, pointer-slice binding, concurrent updates, and reuse across newly created table objects.
In the separate comparison against an intermediate version that already includes the correctness fixes, five paired runs reduced Update with a new table per call from 404.4 to 357.3 ns/op (11.6%), and allocation bytes from 184 to 136 B/op (26.1%); the allocation count stayed at 5. Alternating between two table names reduced time from 406.3 to 393.6 ns/op. Both compared versions include the correctness fixes. The benchmark archive contains all results, including workloads without a speedup.
Earlier cache-line padding candidates have not been measured again against the current code and are excluded from the release performance claims.
Run benchmarks and regression tests using the commands in the benchmark archive. Root-package tests delete test.db during initialization, so run them in a temporary copy.
The existence of this project is thanks to all contributors.
Please give us a 💖star💖 to support us, thank you.
And thank you to all our supporters! 🙏