The fantastic ORM library for Golang aims to be developer friendly.
Overview
- Full-Featured ORM
- Associations (Has One, Has Many, Belongs To, Many To Many, Polymorphism, Single-table inheritance)
- Hooks (Before/After Create/Save/Update/Delete/Find)
- Eager loading with
Preload,Joins - Transactions, Nested Transactions, Save Point, RollbackTo to Saved Point
- Context, Prepared Statement Mode, DryRun Mode
- Batch Insert, FindInBatches, Find/Create with Map, CRUD with SQL Expr and Context Valuer
- SQL Builder, Upsert, Locking, Optimizer/Index/Comment Hints, Named Argument, SubQuery
- Composite Primary Key, Indexes, Constraints
- Auto Migrations
- Logger
- Extendable, flexible plugin API: Database Resolver (Multiple Databases, Read/Write Splitting) / Prometheus…
- Every feature comes with tests
- Developer Friendly
Install
go get -u gorm.io/gormgo get -u gorm.io/driver/sqlite
Quick Start
package mainimport ("gorm.io/gorm""gorm.io/driver/sqlite")type Product struct {gorm.ModelCode stringPrice uint}func main() {db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})if err != nil {panic("failed to connect database")}// Migrate the schemadb.AutoMigrate(&Product{})// Createdb.Create(&Product{Code: "D42", Price: 100})// Readvar product Productdb.First(&product, 1) // find product with integer primary keydb.First(&product, "code = ?", "D42") // find product with code D42// Update - update product's price to 200db.Model(&product).Update("Price", 200)// Update - update multiple fieldsdb.Model(&product).Updates(Product{Price: 200, Code: "F42"}) // non-zero fieldsdb.Model(&product).Updates(map[string]interface{}{"Price": 200, "Code": "F42"})// Delete - delete productdb.Delete(&product, 1)}