Description
Grimoire is a flexible ORM for golang. It features flexible query API and builtin validation. It currently supports MySQL, PostgreSQL and SQLite3 but a custom adapter can be implemented easily using the Adapter interface.
grimoire alternatives and similar packages
Based on the "ORM" category.
Alternatively, view grimoire alternatives based on common mentions on social networks and blogs.
-
upper.io/db
Data access layer for PostgreSQL, CockroachDB, MySQL, SQLite and MongoDB with ORM-like features. -
xorm
xorm是一个简单而强大的Go语言ORM库,通过它可以使数据库操作非常简便。本库是基于原版xorm的定制增强版本,为xorm提供类似ibatis的配置文件及动态SQL支持,支持AcitveRecord操作 -
Prisma
Prisma Client Go is an auto-generated and fully type-safe database client -
reform
A better ORM for Go, based on non-empty interfaces and code generation. -
go-sqlbuilder
A flexible and powerful SQL string builder library plus a zero-config ORM. -
go-queryset
100% type-safe ORM for Go (Golang) with code generation and MySQL, PostgreSQL, Sqlite3, SQL Server support. GORM under the hood. -
REL
:gem: Modern ORM for Golang - Testable, Extendable and Crafted Into a Clean and Elegant API -
golobby/orm
A lightweight yet powerful, fast, customizable, type-safe object-relational mapper for the Go programming language. -
Marlow
Generated ORM from project structs for compile time safety assurances. -
gomodel
A lightweight, fast, orm-like library helps interactive with database. -
lore
Light Object-Relational Environment (LORE) provides a simple and lightweight pseudo-ORM/pseudo-struct-mapping environment for Go
Access the most powerful time series database as a service
Do you think we are missing an alternative of grimoire or a related project?
README
grimoire
:warning: Grimoire V2 is available as REL and Changeset package.
Grimoire is a database access layer inspired by Ecto. It features a flexible query API and built-in validation. It currently supports MySQL, PostgreSQL, and SQLite3 but a custom adapter can be implemented easily using the Adapter interface.
Features:
- Query Builder
- Association Preloading
- Struct style create and update
- Changeset Style create and update
- Builtin validation using changeset
- Multi adapter support
- Logger
Motivation
Common go ORM accepts struct as a value for modifying records which has a problem of unable to differentiate between an empty, nil, or undefined value. It's a tricky problem especially when you want to have an endpoint that supports partial updates. Grimoire attempts to solve that problem by integrating Changeset system inspired from Elixir's Ecto. Changeset is a form like entity which allows us to not only solve that problem but also help us with casting, validations, and constraints check.
Install
go get github.com/Fs02/grimoire
Quick Start
package main
import (
"time"
"github.com/Fs02/grimoire"
"github.com/Fs02/grimoire/adapter/mysql"
"github.com/Fs02/grimoire/changeset"
"github.com/Fs02/grimoire/params"
)
type Product struct {
ID int
Name string
Price int
CreatedAt time.Time
UpdatedAt time.Time
}
// ChangeProduct prepares data before database operation.
// Such as casting value to appropriate types and perform validations.
func ChangeProduct(product interface{}, params params.Params) *changeset.Changeset {
ch := changeset.Cast(product, params, []string{"name", "price"})
changeset.ValidateRequired(ch, []string{"name", "price"})
changeset.ValidateMin(ch, "price", 100)
return ch
}
func main() {
// initialize mysql adapter.
adapter, err := mysql.Open("[email protected](127.0.0.1:3306)/db?charset=utf8&parseTime=True&loc=Local")
if err != nil {
panic(err)
}
defer adapter.Close()
// initialize grimoire's repo.
repo := grimoire.New(adapter)
var product Product
// Inserting Products.
// Changeset is used when creating or updating your data.
ch := ChangeProduct(product, params.Map{
"name": "shampoo",
"price": 1000,
})
if ch.Error() != nil {
// handle error
}
// Changeset can also be created directly from json string.
jsonch := ChangeProduct(product, params.ParseJSON(`{
"name": "soap",
"price": 2000,
}`))
// Create products with changeset and return the result to &product,
if err = repo.From("products").Insert(&product, ch); err != nil {
// handle error
}
// or panic when insertion pailed
repo.From("products").MustInsert(&product, jsonch)
// Querying Products.
// Find a product with id 1.
repo.From("products").Find(1).MustOne(&product)
// Updating Products.
// Update products with id=1.
repo.From("products").Find(1).MustUpdate(&product, ch)
// Deleting Products.
// Delete Product with id=1.
repo.From("products").Find(1).MustDelete()
}
Examples
Documentation
Guides: https://fs02.github.io/grimoire
API Documentation: https://godoc.org/github.com/Fs02/grimoire
License
Released under the MIT License
*Note that all licence references and agreements mentioned in the grimoire README section above
are relevant to that project's source code only.