Skip to content
/ go-korm Public

Golang struct base orm model library

License

Notifications You must be signed in to change notification settings

lkjfrf/go-korm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-korm

Golang struct base orm model library

💡 Concept

korm is an Sequrlize / Hibernate inspired 'half' object-relational mapping.
'Half' mean we do not control database connection for now.

There are many struct base orm library written in Go.
Main difference in korm is that support .Create() .Get() .Insert() .Update() .Delete() from korm-model directly.

🛠 How to use

📖 Pre steps

  • Step 1 : Create Database connection (for pass it to korm)
    db, _ := sql.Open("mysql", "root:password@tcp(localhost:3306)/schema")
  • Step 2 : Define struct as database model (using korm tag)
    type Employee struct {
        Eid int32 `korm:"integer"`
        Name string `korm:"varchar(100)"`
        Team string `korm:"varchar(30)"`
    }
  • Step 3 : Create korm model based on step 2
    model := korm.NewModel[Employee]

📖 Create Table

// korm use first struct field for primary key by default
// Second parameter : set primary key or not
model.CreateTable(db, true)

📖 Insert into database

model.Data.Eid = 920809
model.Data.Name = "Abbie Oh"
model.Data.Team = "Dev Team 1"

model.Insert(db)

📖 Get from database

model.Data.Eid = 920809

// Second parameter : index of struct field.
// It mean get data from database with filter eid = 920809
model.Get(db, 0)

📖 Get all from database

//var result []Employee
result := model.GetAll(db)

📖 Update to database

model.Data.Eid = 920809
model.Data.Team = "Dev Team 2"

// Second parameter : index of struct field which want to update.
// It mean just update team to database.
model.Update(db, 2)

📖 Delete from database

model.Data.Eid = 920809

// Second parameter : index of struct field which use for delete operation.
model.Delete(db, 0)

About

Golang struct base orm model library

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages