sqlite3ha

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2025 License: MIT Imports: 12 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

Connector option DSN param Description Default
WithName name Unique node name $HOSTNAME
WithReplicationURL replicationURL NATS connection URL. (nats://localhost:4222)
WithEmbeddedNatsConfig
  • natsPort
  • natsStoreDir
  • natsConfigFile
NATS embedded server config
WithDisableDDLSync disableDDLSync Disable replication of DDL commands
WithPublisherTimeout publisherTimeout Publisher timeout 15s
WithChangeSetInterceptor Customize the replication behaviour
WithExtensions SQLite extensions to load

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

This section is empty.

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) 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)

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)

Directories

Path Synopsis
_examples
node1 command
node2 command

Jump to

Keyboard shortcuts

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