A lightweight, concurrency-safe key-value store built on top of GORM and SQLite, with plug-and-play support for any GORM-compatible database such as MySQL, MariaDB, or PostgreSQL.
- ✅ Thread-safe Set/Get/Delete
- ✅ Uses GORM ORM layer
- ✅ Lightweight: stores data as JSON strings
- ✅ Supports custom table name
- ✅ Easily swap backend: SQLite / MySQL / PostgreSQL / etc.
- ✅ Auto-migration via GORM
go get github.com/coghost/gokvsqlitepackage main
import (
"fmt"
"github.com/coghost/gokvsqlite"
)
type User struct {
Name string
Age int
}
func main() {
// Create a new client with SQLite
client, err := sqlite.New("file=test.db?cache=shared", "kv_table")
if err != nil {
panic(err)
}
defer client.Close()
// Set a value
err = client.Set("user1", User{Name: "Alice", Age: 30})
if err != nil {
panic(err)
}
// Get the value
var user User
found, err := client.Get("user1", &user)
if err != nil {
panic(err)
}
if found {
fmt.Printf("Found user: %+v\n", user)
}
// Delete the key
_ = client.Delete("user1")
}Just replace the gorm.Open() logic with another driver.
import (
"gorm.io/driver/mysql"
"gorm.io/gorm"
"github.com/coghost/gokvsqlite"
)
dsn := "user:password@tcp(localhost:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
panic(err)
}
client, err := sqlite.NewWithDB(db, "my_kv_store")import (
"gorm.io/driver/postgres"
"gorm.io/gorm"
"github.com/coghost/gokvsqlite"
)
dsn := "host=localhost user=postgres password=yourpw dbname=yourdb port=5432 sslmode=disable"
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
client, err := sqlite.NewWithDB(db, "pg_kv_store")
⚠️ You must import the driver explicitly:
go get gorm.io/driver/sqlitego get gorm.io/driver/mysqlgo get gorm.io/driver/postgres
Internally uses a sync.RWMutex to allow safe concurrent access.
This package includes full tests using stretchr/testify:
go test -v ./...This implementation is compatible with the gokv.Store interface.
You can use it with libraries or systems that rely on the gokv interface abstraction.
The table (default: kv_store) is auto-migrated and stores:
| Column | Type | Description |
|---|---|---|
k |
TEXT | Primary key |
v |
TEXT | JSON-encoded value |
GPL-3.0 License © 2025 Coghost