https://github.com/coghost/gokvsqlite
sqlite(with gorm) implementation for gokv
https://github.com/coghost/gokvsqlite
Last synced: 10 months ago
JSON representation
sqlite(with gorm) implementation for gokv
- Host: GitHub
- URL: https://github.com/coghost/gokvsqlite
- Owner: coghost
- License: gpl-3.0
- Created: 2025-07-17T04:34:49.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-07-17T06:18:18.000Z (11 months ago)
- Last Synced: 2025-07-18T12:46:35.663Z (11 months ago)
- Language: Go
- Size: 18.6 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gokv-sqlite (GORM-powered)
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**.
---
## Features
- ✅ 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
---
## Installation
```bash
go get github.com/coghost/gokvsqlite
```
---
## Basic Usage (SQLite)
```go
package 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")
}
```
---
## Use with MySQL / MariaDB / PostgreSQL
Just replace the `gorm.Open()` logic with another driver.
### Example: MySQL / MariaDB
```go
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")
```
### Example: PostgreSQL
```go
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/sqlite`
> - `go get gorm.io/driver/mysql`
> - `go get gorm.io/driver/postgres`
---
## Thread Safety
Internally uses a `sync.RWMutex` to allow safe concurrent access.
## 🧪 Testing
This package includes full tests using [`stretchr/testify`](https://github.com/stretchr/testify):
```bash
go test -v ./...
```
---
## 🧩 Integration with gokv
This implementation is compatible with the [`gokv.Store`](https://pkg.go.dev/github.com/philippgille/gokv#Store) interface.
You can use it with libraries or systems that rely on the `gokv` interface abstraction.
---
## 🧱 Table Schema
The table (default: `kv_store`) is auto-migrated and stores:
| Column | Type | Description |
| ------ | ---- | ------------------ |
| `k` | TEXT | Primary key |
| `v` | TEXT | JSON-encoded value |
---
## License
GPL-3.0 License © 2025 [Coghost](https://github.com/coghost)