sqlite

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: MIT Imports: 14 Imported by: 0

README

GORM libSQL Driver

An unofficial driver for using libSQL/Turso with Gorm.

This is a fork of the official Gorm SQLite driver changed to work with libSQL/Turso. The primary change is the introduction of a Config struct.

Note: This driver does not contain a default SQLite driver like the original does. This allows you to import your choice of the Turso/libSQL Go client and pick the features that are important for you.

Full feature usage

This version allows you to use all of the features of Turso/libSQL, including embedded replicas, but requires cGo to be enabled to work as expected.

import (
	"database/sql"
	"fmt"
	"log"
	"os"
	"path/filepath"
	"time"

	"github.com/tursodatabase/go-libsql"
  	sqlite "github.com/ytsruh/gorm-libsql"
	"gorm.io/gorm"
)

// Create your Turso/libSQL connection
dbName := "local.db"
primaryUrl := os.Getenv("DB_URL")
authToken := os.Getenv("DB_AUTH_TOKEN")
dbPath := filepath.Join("./db", dbName)
syncInterval := time.Minute

connector, err := libsql.NewEmbeddedReplicaConnector(dbPath, primaryUrl,
	libsql.WithAuthToken(authToken),
	libsql.WithSyncInterval(syncInterval),
)
if err != nil {
	fmt.Println("Error creating connector:", err)
	os.Exit(1)
}

// Pass a database connection to Gorm using this driver
conn := sql.OpenDB(connector)
db, err := gorm.Open(sqlite.New(sqlite.Config{
	Conn: conn,
}), &gorm.Config{})
if err != nil {
	log.Fatalf("Error connecting to database: %v", err)
}

Pure go usage

For those willing to not use embedded replicas, you will not need cGo enabled. Turso/libSQL offers a pure Go driver that we can make use of.

The libSQL client must be imported as an unnamed import so we can make use of it's side effects (acting as the driver for Gorm)

import (
	"database/sql/driver"
	"errors"
	"fmt"
	"os"

	_ "github.com/tursodatabase/libsql-client-go/libsql"
	"gorm.io/gorm"
	sqlite "github.com/ytsruh/gorm-libsql"
)

dburl := os.Getenv("DATABASE_URL")
var err error

// Pass a DSN or Turso DB url to Gorm. The url must also include an authToken as a query parameter
conn, err := gorm.Open(sqlite.New(sqlite.Config{
	DSN:        dburl,
}), &gorm.Config{})

if err != nil {
	fmt.Println("Failed to connect to database")
	panic(err)
}

Documentation

Index

Constants

View Source
const DriverName = "libsql"

DriverName is the default driver name for SQLite / libSQL.

Variables

View Source
var (
	ErrConstraintsNotImplemented = errors.New("constraints not implemented on sqlite, consider using DisableForeignKeyConstraintWhenMigrating, more details https://github.com/go-gorm/gorm/wiki/GORM-V2-Release-Note-Draft#all-new-migrator")
)

Functions

func New

func New(config Config) gorm.Dialector

func Open

func Open(dsn string) gorm.Dialector

Types

type Config

type Config struct {
	DriverName string
	DSN        string
	Conn       gorm.ConnPool
}

type Dialector

type Dialector struct {
	DriverName string
	DSN        string
	Conn       gorm.ConnPool
}

func (Dialector) BindVarTo

func (dialector Dialector) BindVarTo(writer clause.Writer, stmt *gorm.Statement, v interface{})

func (Dialector) ClauseBuilders

func (dialector Dialector) ClauseBuilders() map[string]clause.ClauseBuilder

func (Dialector) DataTypeOf

func (dialector Dialector) DataTypeOf(field *schema.Field) string

func (Dialector) DefaultValueOf

func (dialector Dialector) DefaultValueOf(field *schema.Field) clause.Expression

func (Dialector) Explain

func (dialector Dialector) Explain(sql string, vars ...interface{}) string

func (Dialector) Initialize

func (dialector Dialector) Initialize(db *gorm.DB) (err error)

func (Dialector) Migrator

func (dialector Dialector) Migrator(db *gorm.DB) gorm.Migrator

func (Dialector) Name

func (dialector Dialector) Name() string

func (Dialector) QuoteTo

func (dialector Dialector) QuoteTo(writer clause.Writer, str string)

func (Dialector) RollbackTo

func (dialectopr Dialector) RollbackTo(tx *gorm.DB, name string) error

func (Dialector) SavePoint

func (dialectopr Dialector) SavePoint(tx *gorm.DB, name string) error

func (Dialector) Translate

func (dialector Dialector) Translate(err error) error

Translate it will translate the error to native gorm errors. We are not using go-sqlite3 error type intentionally here because it will need the CGO_ENABLED=1 and cross-C-compiler.

type ErrMessage

type ErrMessage struct {
	Code         int `json:"Code"`
	ExtendedCode int `json:"ExtendedCode"`
	SystemErrno  int `json:"SystemErrno"`
}

type Index

type Index struct {
	Seq     int
	Name    string
	Unique  bool
	Origin  string
	Partial bool
}

type Migrator

type Migrator struct {
	migrator.Migrator
}

func (Migrator) AlterColumn

func (m Migrator) AlterColumn(value interface{}, name string) error

func (Migrator) BuildIndexOptions

func (m Migrator) BuildIndexOptions(opts []schema.IndexOption, stmt *gorm.Statement) (results []interface{})

func (Migrator) ColumnTypes

func (m Migrator) ColumnTypes(value interface{}) ([]gorm.ColumnType, error)

ColumnTypes return columnTypes []gorm.ColumnType and execErr error

func (Migrator) CreateConstraint

func (m Migrator) CreateConstraint(value interface{}, name string) error

func (Migrator) CreateIndex

func (m Migrator) CreateIndex(value interface{}, name string) error

func (Migrator) CurrentDatabase

func (m Migrator) CurrentDatabase() (name string)

func (Migrator) DropColumn

func (m Migrator) DropColumn(value interface{}, name string) error

func (Migrator) DropConstraint

func (m Migrator) DropConstraint(value interface{}, name string) error

func (Migrator) DropIndex

func (m Migrator) DropIndex(value interface{}, name string) error

func (Migrator) DropTable

func (m Migrator) DropTable(values ...interface{}) error

func (Migrator) GetIndexes

func (m Migrator) GetIndexes(value interface{}) ([]gorm.Index, error)

GetIndexes return Indexes []gorm.Index and execErr error, See the doc

func (Migrator) GetTables

func (m Migrator) GetTables() (tableList []string, err error)

func (Migrator) HasColumn

func (m Migrator) HasColumn(value interface{}, name string) bool

func (Migrator) HasConstraint

func (m Migrator) HasConstraint(value interface{}, name string) bool

func (Migrator) HasIndex

func (m Migrator) HasIndex(value interface{}, name string) bool

func (Migrator) HasTable

func (m Migrator) HasTable(value interface{}) bool

func (Migrator) RenameIndex

func (m Migrator) RenameIndex(value interface{}, oldName, newName string) error

func (*Migrator) RunWithoutForeignKey

func (m *Migrator) RunWithoutForeignKey(fc func() error) error

Jump to

Keyboard shortcuts

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