Skip to Content
fusion-orm
Getting Started

Quick start

Open a database, migrate a model, and run your first queries with fusion-orm.

Install

go get github.com/kiplagatcollins/fusion-orm

Define a model

Struct tags drive everything: column names, constraints, relations.

type User struct {
	ID   int64  `fusion:"pk,autoincrement"`
	Name string `fusion:"notnull"`
}

Open and migrate

Open takes a Dialector — sqlite.Open(dsn) or postgres.Open(dsn) — plus an optional *fusion.Config. The migrator inspects the live schema and issues the diff.

db, err := fusion.Open(sqlite.Open("app.db"))
if err != nil {
	panic(err)
}
m, err := db.Migrator()
if err != nil {
	panic(err)
}
if err := m.AutoMigrate(context.Background(), &User{}); err != nil {
	panic(err)
}

Create and read back

Finishers execute. The primary key and timestamps are backfilled onto your struct.

user := User{Name: "Ada"}
if err := db.Create(&user).Error; err != nil {
	panic(err)
}

var got User
if err := db.First(&got, user.ID).Error; err != nil {
	panic(err)
}
fmt.Println(got.Name) // Ada

Chain a query

Chainables accumulate clauses; a finisher triggers the build. ToSQL shows exactly what will run — the same rendering the executor uses.

sql := db.ToSQL(func(tx *fusion.DB) *fusion.DB {
	return tx.Model(&User{}).Where("name = ?", "Ada").Find(&[]User{})
})
SELECT * FROM "users" WHERE name = 'Ada'

Error handling

Every branchable failure is a sentinel — check with errors.Is, never by string matching.

var got User
err := db.First(&got, 999).Error
if errors.Is(err, fusion.ErrRecordNotFound) {
	// no row matched
}

Find vs First

First, Take, and Last return ErrRecordNotFound when nothing matches. Find returns an empty slice and a nil error instead.

Next steps

On this page