

GitHub - Fs02/grimoire: Database access layer for golang.
source link: https://github.com/Fs02/grimoire
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md
grimoire
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("root@(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
Recommend
-
38
The first augmented reality spellbook for D&D 5eAR Grimoire makes it possible to see your D&D spells on the real tabletop. Go to any area of effect spell and see it in augmented reality! The app also i...
-
17
Test Case Of Data Access Layer Function in Play:- Reading Time: < 1 minuteIn this blog I am trying to show how to creat...
-
8
Authority Role Based Access Control (RBAC) Go package with database persistence Features Create Roles Create Permissions Assign Permissions to Roles Assign Multiple Roles to Users...
-
9
Go SQL DB 中文 "Go SQL DB" is a relational database that supports SQL queries for research purposes. The main goal is to show the basic principles and key...
-
12
Building a To-do List App with Python: Data Access Layer with SQLAlchemyOctober 15th 2021 new story
-
11
Spring Boot Data Access Layer Best PracticesIn this article, we review best practices that are very effective to optimize spring boot data access layer.Spring boot JPA has added some interface on JP...
-
4
javascriptBuild a Data Access Layer with PostgreSQL and Node.jsCamilo Reyes on Jun 1, 2022...
-
8
Data Access Layer makes it easier to change your Database? Skip to main content...
-
23
Codeforces Round #819 (Div. 1 + Div. 2) and Grimoire of Code Annual Contest 2022 Codeforces Round #819 (Div. 1 + Div. 2) and Grimoir...
-
7
Copied problem in Codeforces Round #819 (Div. 1 + Div. 2) and Grimoire of Code Annual Contest 2022 Copied problem in Codeforces Roun...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK