Cheat sheet
The whole surface on one screen. Each row links to the guide that explains it. For exact signatures, see pkg.go.dev/liteorm.org.
Open a backend
Section titled “Open a backend”db, err := sqlite.Open("app.db") // also OpenEncrypted(path, key), OpenConfig(cfg)db, err := postgres.Open(ctx, dsn) // native pgxdb, err := mysql.Open(ctx, dsn)db, err := mssql.Open(ctx, dsn)defer db.Close()*liteorm.DB and a transaction (db.Begin(ctx)) are both a liteorm.Session — pass either to any front-end. See Backends.
orm.Repo[T] — the declarative repository
Section titled “orm.Repo[T] — the declarative repository”repo := orm.NewRepo[T](sess). Full method surface (see ORM, soft delete):
| Group | Methods |
|---|---|
| Create | Create · CreateInBatches(vs, n) |
| Insert-or-update | Save (by identity) · Upsert(v, query.OnConflict(...).DoUpdate(...)) · FirstOrCreate(v, conds...) · FirstOrInit(v, conds...) (no write) |
| Read by key | Get(keys...) (→ ErrNoRows) · GetByKeys(keys...) (batch, single-PK) |
| Read (scoped) | Find · First · Count · Exists · FindInBatches(n, fn) |
| Read scopes (chain, return a view) | Where · Filter(preds...) · OrderBy · Limit · Offset · Scopes(...) · IncludeDeleted · OnlyDeleted |
| Update | Update · Updates(v, cols...) (partial) |
| Delete | Delete · ForceDelete · Restore (un-soft-delete) |
| Write scoping (return a view) | Select(cols...) · Omit(cols...) |
Keyed Update/Delete/Restore return liteorm.ErrNoRows when no row matches. Create/Update/Delete fire hooks.
Associations (orm)
Section titled “Associations (orm)”orm.Load[P, C](ctx, sess, parents, "Field", opts...) — one batched query, N+1-safe; opts: LoadWhere("...", args...), LoadOrderBy("..."). Nested: orm.LoadPath[P](ctx, sess, roots, "A.B") or orm.NewPreloader[P](sess).With("A").With("B.C").Load(ctx, roots). Write the non-owner side with orm.Assoc[P, C](sess, "Field", &owner) → Append/Delete/Replace/Clear/Count; m2m primitives orm.Attach/orm.Detach. See Associations.
Schema & migrations
Section titled “Schema & migrations”orm.AutoMigrate[T](ctx, sess, orm.WithForeignKeys()) // one model (opt-in FK constraints)orm.AutoMigrateAll(ctx, sess, A{}, B{}, C{}) // a whole set, in dependency orderReviewable changes: orm.Diff[T] → Changes{Added, Removed, Changed}; orm.GenerateMigration[T] → up/down SQL (executes nothing). Introspect: orm.IntrospectColumns / IntrospectIndexes / IntrospectTables. Runner — migrate.New(sess) → Up/UpTo/Down/DownTo/Status/Version/Force; migrate.Load(fs.FS); migrate.WritePair(dir, ver, name, up, down). See Migrations.
query — the explicit builder
Section titled “query — the explicit builder”query.Select[T](sess) then a terminal (see Query builder):
| Group | Surface |
|---|---|
| Terminals | All · First · Count · Exists · Iter (stream, iter.Seq2) |
| Filter / order / page | Where(frag, args...) · Filter(preds...) · OrderBy / Order(Asc/Desc/OrderExpr) · Limit · Offset · Distinct / DistinctOn |
| Joins | InnerJoin · LeftJoin · RightJoin · CrossJoin · Join · JoinSub · JoinLateral |
| Group | GroupBy / GroupByCols · Having |
| Set ops | Union / UnionAll · Intersect(All) · Except(All) |
| Locking | ForUpdate · ForShare · SkipLocked · NoWait |
| CTE / subquery source | With · WithRecursive · From · query.FromSubquery |
| Project / scalar | Project · query.Pluck(ctx, b, col) → []V · query.Into[T,R](ctx, b, fields...) |
| Aggregates | query.Sum/Avg/Min/Max/CountCol(ctx, b, col) · SumAs/AvgAs/… in Into |
| Window / scalar subq | RowNumber/Rank/DenseRank/Lag/Lead/WindowSum….Over(...) · ScalarSubquery |
Predicates: query.Col[V]("col").Eq/Ne/Gt/Ge/Lt/Le/In/NotIn/Like/IsNull/IsNotNull · query.And/Or/Not · Exists/NotExists · Postgres query.JSON(...) / query.Array[E](...).
CRUD: query.NewRepo[T](sess) → Insert · InsertMany (bulk) · Get · Find(preds...) · Update · Delete · Upsert. Multi-row writes: query.Update[T](sess).Set/SetExpr(...).Where/Filter(...).Exec(ctx) (returns affected count) and query.Delete[T](sess)...Exec. Escape hatch: query.Raw[T](ctx, sess, sql, args...).
Errors
Section titled “Errors”liteorm.ErrNoRows · ErrUniqueViolation · ErrForeignKey · ErrNotNull · ErrCheck · ErrDeadlock · ErrSerializationTest with errors.Is(err, …) or the helpers: liteorm.IsUniqueViolation · IsForeignKeyViolation · IsNotNullViolation · IsCheckViolation · IsNotFound · IsRetryable (deadlock/serialization). See Errors.
See also
Section titled “See also”- Getting started · Recipes (common tasks) · Coming from GORM
- Backends · Dialects