sqlite3ha

package module
v0.11.9 Latest Latest
Warning

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

Go to latest
Published: May 6, 2026 License: MIT Imports: 22 Imported by: 1

README

go-sqlite3-ha

Go database/sql driver based on github.com/mattn/go-sqlite3, providing high availability for SQLite databases.

Features

  • Built on the robust foundation of go-sqlite3.
  • High availability support for SQLite databases.
  • Replication: Synchronize data across nodes using NATS.
  • Customize the replication strategy
  • Leaderless clusters: Read/Write from/to any node. Last-writer wins by default, but you can customize conflict resolutions by implementing ChangeSetInterceptor.
  • Embedded or External NATS: Choose between an embedded NATS server or an external one for replication.
  • Easy to integrate with existing Go projects.

Installation

go get github.com/litesql/go-sqlite3-ha

Usage

package main

import (
    "database/sql"
    _ "github.com/litesql/go-sqlite3-ha"
)

func main() {
    db, err := sql.Open("sqlite3-ha", "file:example.db?_journal=WAL&_timeout=5000&replicationURL=nats://broker:4222&name=node0")
    if err != nil {
        panic(err)
    }
    defer db.Close()

    // Use db to interact with your database
}
Using Connector
package main

import (
    "database/sql"
    sqlite3ha "github.com/litesql/go-sqlite3-ha"
)

func main() {
    c, err := sqlite3.NewConnector("file:example.db?_journal=WAL&_timeout=5000",
		ha.WithName("node1"),
		ha.WithEmbeddedNatsConfig(&ha.EmbeddedNatsConfig{
			Port: 4222,
		}))
	if err != nil {
		panic(err)
	}
	defer c.Close()

	db := sql.OpenDB(c)
	defer db.Close()

    // Use db to interact with your database
}

Configuration

See options available

Projects using go-ha

  • HA: Highly available leaderless SQLite cluster with HTTP and PostgreSQL Wire Protocol
  • PocketBase HA: Highly available leaderless PocketBase cluster

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrTimedOut = errors.New("Timed out")

Functions

func Backup

func Backup(ctx context.Context, db *sql.DB, w io.Writer) error

func NewConnector

func NewConnector(name string, opts ...ha.Option) (*ha.Connector, error)

Types

type Conn

type Conn struct {
	*sqlite3.SQLiteConn
	// contains filtered or unexported fields
}

func (*Conn) Begin added in v0.4.5

func (c *Conn) Begin() (driver.Tx, error)

func (*Conn) BeginTx added in v0.4.5

func (c *Conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error)

func (*Conn) Close added in v0.4.5

func (c *Conn) Close() error

func (*Conn) Exec

func (c *Conn) Exec(query string, args []driver.Value) (driver.Result, error)

func (*Conn) ExecContext

func (c *Conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error)

func (*Conn) IsValid added in v0.5.7

func (c *Conn) IsValid() bool

func (*Conn) Query added in v0.4.5

func (c *Conn) Query(query string, args []driver.Value) (driver.Rows, error)

func (*Conn) QueryContext added in v0.4.5

func (c *Conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error)

func (*Conn) ResetSession added in v0.5.7

func (c *Conn) ResetSession(ctx context.Context) error

type Driver

type Driver struct {
	Extensions  []string
	ConnectHook func(*sqlite3.SQLiteConn) error
	Options     []ha.Option
}

func (*Driver) Open

func (d *Driver) Open(name string) (driver.Conn, error)

func (*Driver) OpenConnector

func (d *Driver) OpenConnector(name string) (driver.Connector, error)

type ProxiedQuerierExecer added in v0.11.8

type ProxiedQuerierExecer interface {
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
}

Directories

Path Synopsis
_examples
node1 command
node2 command
shards command

Jump to

Keyboard shortcuts

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