sqlink

command module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: MIT Imports: 5 Imported by: 0

README ΒΆ

Go Version Go Report Card

SQLink is a modern, type-safe, and lightweight ORM for Go, built with the power of generics.


✨ Features

  • Type-Safe by Design: Uses Go generics to catch database field and type errors at compile time.
  • Fluent & Chainable API: Write clean, readable, and expressive database queries.
  • High Performance: Smart schema caching minimizes reflection overhead for production speed.
  • Flexible Querying: Supports complex AND/OR conditions, IN, and NOT.
  • Standard Library Core: Built on top of the native database/sql for broad compatibility.

πŸš€ Installation

To get started, use go get to add Go-Sqlink to your project.

go get github.com/Kaguya233qwq/sqlink

You'll also need a database driver. This example uses go-sqlite3.

go get github.com/mattn/go-sqlite3

πŸ’‘ Usage

Define Your Model

Define your model using standard Go structs. Use the db tag to specify the column name. orm.Field[T] is a generic wrapper that gracefully handles nullable values.

import "github.com/Kaguya233qwq/sqlink/orm"

type User struct {
    ID      orm.Field[int]    `db:"id"`
    Name    orm.Field[string] `db:"name"`
    Age     orm.Field[int]    `db:"age"`
    IsAdmin orm.Field[bool]   `db:"is_admin"`
}
Connect and Migrate

Open a database connection and create the table schema with AutoMigrate.

import (
    "github.com/Kaguya233qwq/sqlink/orm"
    _ "github.com/mattn/go-sqlite3"
)

func main() {
    db, err := orm.OpenDB("test.db")
	if err != nil {
		panic(err)
	}
	defer db.Close()

    // Enable the colorful logger
    db.Logger = orm.NewDefaultLogger(os.Stdout)
    
    // Create the 'User' table if it doesn't exist
    if err := db.AutoMigrate(User{}); err != nil {
        panic(err)
    }
}
Create Records

Use the Create method to insert new records.

newUser := &User{
    Name:    orm.Field[string]{Value: "alice", Valid: true},
    Age:     orm.Field[int]{Value: 25, Valid: true},
    IsAdmin: orm.Field[bool]{Value: false, Valid: true},
}

err := orm.Query[User](db).Create(newUser)
if err != nil {
    // Handle error
}
Query Records

Querying is type-safe and fluent. Access your model's schema via the .Schema field.

Get the First Record
var alice User
q := orm.Query[User](db)
err := q.Where(q.Schema.Name.Is("alice")).First(&alice)
if err != nil {
    if errors.Is(err, sql.ErrNoRows) {
        fmt.Println("User not found")
    }
    // Handle other errors
}

fmt.Printf("Found: %s, Age: %d\n", alice.Name.Value, alice.Age.Value)
Get Multiple Records
var allUsers []User
q := orm.Query[User](db)
// Find all users older than 20
err := q.Where(q.Schema.Age.Gt(20)).Find(&allUsers)
if err != nil {
    // Handle error
}

for _, user := range allUsers {
    fmt.Println(user.Name.Value)
}
Complex Queries

Combine conditions with orm.And, orm.Or, and orm.Not.

// Find admins OR users older than 30 named 'bob'
var results []User
q := orm.Query[User](db)
err := q.Where(
    orm.Or(
        q.Schema.IsAdmin.Is(true),
        orm.And(
            q.Schema.Age.Gt(30),
            q.Schema.Name.Is("bob"),
        ),
    ),
).Find(&results)
Update and Delete

Updating and deleting records is just as straightforward.

q := orm.Query[User](db)
// Update
err := q.Where(userSchema.Name.Is("alice")).Update(map[string]interface{}{"age": 26})
// Delete
err = q.Where(userSchema.Name.Is("alice")).Delete()

πŸ™ Acknowledgements

This project was inspired by the elegance of modern ORMs and the power of Go generics. A special thanks to the creators of projects like GORM and Bun for paving the way.

πŸ“„ License

Go-Sqlink is released under the MIT License. See the LICENSE file for more information.

Documentation ΒΆ

The Go Gopher

There is no documentation for this package.

Directories ΒΆ

Path Synopsis

Jump to

Keyboard shortcuts

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