HawkORM

package module
v0.0.0-...-bf8f870 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 4, 2023 License: MIT Imports: 3 Imported by: 0

README

HawkORM

A toy ORM for golang. Currently only MySQL is supported.

Installation

go get github.com/HarrisonEagle/HawkORM

Progress

  • Select
    • ✅select all objects
    • ✅select objects with conditions
    • ✅select first
    • ✅select last
    • 🔄left join, inner join, outer join
  • Insert
    • ✅insert an object
    • ✅batch insert
    • 🔄insert with associations
  • Update
    • ✅update with conditions
    • ✅batch update
  • Delete
    • ✅delete with conditions
    • ✅delete an object
    • ✅delete with multiple objects
  • Transaction
  • 🔄Comming soon...

Usage

For example, user data object defined like this:

type User struct {
 ID        string `hawkorm:"primarykey"`
 Name      string
 Email     string
 CreatedAt time.Time
 UpdatedAt time.Time
}
SELECT
// connect to db
// remember to add parseTime=true to parse time correctlt
testDB, err := HawkORM.OpenMySQL("user:pass@localhost:3306/databasename?parseTime=true")
if err != nil{
	log.Fatalf("connect to db failed!")
}

// equal to: SELECT id, name, email, created_at, updated_at FROM users WHERE (id = "userId" OR name = "testname") ORDER BY id ASC LIMIT 1
var users []User
testDB.Select(&User{}).WhereOr(&User{ID: "userId", Name: "testname"}).First(&users)
INSERT
insertTest := []User{
 {ID: "userid1", Name: "user1", Email: "[email protected]"},
 {ID: "userid2", Name: "user2", Email: "[email protected]"},
}
// equal to: INSERT INTO users (id, name, email) VALUES ("userid1", "user1", "[email protected]"), ("userid2", "user2", "[email protected]")
res, err := testDB.Insert(&User{}).SetData(insertTest).Exec()
UPDATE
// equal to: UPDATE users SET name = "rename" WHERE id = "testuserid3" AND email = "[email protected]" 
_, err := testDB.Update(&User{}).Where(&User{ID: "testuserid3", Email: "[email protected]"}).SetData(User{Name: "rename"}).Exec()
DELETE
// equal to: DELETE FROM users WHERE id = "testuserid4" AND email = "[email protected]"
_, err := testDB.Delete(&User{}).Where(&User{ID: "testuserid4", Email: "[email protected]"}).Exec()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func OpenMySQL

func OpenMySQL(config string) (driver.Database, error)

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL