This is a creation in Article, where the information may have evolved or changed.
Project Address: Https://github.com/eaigner/hood
This is a very aesthetic ORM library.
Characteristics
- Chain-of-the-line API
- Transaction support
- Migration and namespace generation
- Model variables
- Model Time
- Database dialect interface
- No ambiguous fields
- Clean and testable code
Open Database
If the dialect is already registered you can open the database directly
hd, err := hood.Open("postgres", "user=
dbname=
")
You can also specify a dialect when you open the database
hd := hood.New(db, NewPostgres())
Schemas
You can declare that.
Type person struct {//auto-incrementing int field ' ID ' ID hood. Id//Custom primary key field ' first_name ', with presence validation FirstName string ' sql: ' pk ' validate: ' Presence ' String field ' last_name ' with size A, not NULL LastName string ' sql: ' Size ($), notnull "'//String field ' tag ' wi Th size 255, default value ' Customer ' Tag string ' sql: ' Size (255), Default (' Customer ') '//You can also combine tags, de Fault value ' orange ' combinedtags string ' sql: ' Size (+), default (' Orange ') ' Birthday time. Time//timestamp field ' birthday ' data []byte//data field ' data ' isadmin bool//Boolean F Ield ' is_admin ' notes string//Text field ' Notes '//You can alternatively define a var char as a string f Ield by setting a size Nick string ' sql: ' Size (+) '///validates number range Balance int ' Validate: ' Range (10:20) ' These fields is auto updated on save Created Hood. Created Updated Hood. Updated//... and othER built in types (int, uint, float ...)} Indexes is defined via the Indexed interface to avoid//polluting the table fields. Func (Table *person) Indexes (Indexes *hood. Indexes) {Indexes. ADD ("Tag_index", "tag")//Params:indexname, unique, columns ... indexes. Addunique ("Name_index", "first_name", "Last_Name")}
Data migration
You need to install the hood tool first and then run
go get github.com/eaigner/hoodcd $GOPATH/src/github.com/eaigner/hood./install.sh<
Example
Here is an example of use
Package main import ("Hood") func main () {//Open a DB connection, use New () alternatively for unregistered Dialec TS HD, Err: = Hood. Open ("Postgres", "User=hood dbname=hood_test sslmode=disable") if err! = Nil {panic (ERR)}//Create a T Able type Fruit struct {Id hood. Id Name string ' Validate: ' Presence ' ' Color string} err = HD. CreateTable (&fruit{}) if err! = Nil {panic (err)} Fruits: = []fruit{fruit{name: "Banana", Co Lor: "Yellow"}, Fruit{name: "Apple", Color: "Red"}, Fruit{name: "Grapefruit", Color: "Yellow"}, Fruit {Name: "Grape", Color: "Green"}, Fruit{name: "Pear", Color: "Yellow"},}//Start a transaction tx: = HD. Begin () IDs, err: = Tx. SaveAll (&fruits) if err! = Nil {panic (err)} FMT. PRINTLN ("Inserted IDs:", IDS)//[1 2 3 4 5]//Commit Changes err = TX. Commit () if err! = Nil {panic (ERR)}//Ids aRe automatically updated if fruits[0]. Id! = 1 | | FRUITS[1]. Id! = 2 | | FRUITS[2]. ID! = 3 {Panic ("id not set")}/If an ID was already set, a call to save would result in an update fruit S[0]. Color = "Green" ids, err = HD. SaveAll (&fruits) if err! = Nil {panic (err)} FMT. Println ("Updated IDs:", IDS)//[1 2 3 4 5] if fruits[0]. Id! = 1 | | FRUITS[1]. Id! = 2 | | FRUITS[2]. ID! = 3 {Panic ("id not set")}/Let's try to save a row this does not satisfy the required validations _, Err = HD. Save (&fruit{}) If Err = = Nil | | Err. Error ()! = "Value not set" {Panic ("does not satisfy validations, should not save")}//Find////Th E markers is DB agnostic, so can always use '? ' e.g. in Postgres they is replaced with $ $, $ $, ... var results []fruit err = HD. Where ("color", "=", "green"). ("name"). Limit (1). Find (&results) if err! = Nil {panic (err)} FMT. Println ("ResulTS: ", results)//[{1 banana Green}]//Delete IDs, err = HD. DeleteAll (&results) if err! = Nil {panic (err)} FMT. Println ("Deleted IDs:", IDS)//[1] results = nil Err = HD. Find (&results) if err! = Nil {panic (err)} FMT. Println ("Results:", results)//[{2 Apple red} {3 Grapefruit yellow} {4 Grape green} {5 pear yellow}]//Drop HD. Droptable (&fruit{})}