Skip to content

coghost/gokvsqlite

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

go get github.com/coghost/gokvsqlite

Basic Usage (SQLite)

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

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

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:

go test -v ./...

🧩 Integration with gokv

This implementation is compatible with the 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

About

sqlite(with gorm) implementation for gokv

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages