sqlite

package
v0.1.22 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Attach

func Attach(dbfile string, dbname string) *gorm.DB

Attach attaches an additional SQLite database file (dbfile) to the global database connection under the schema alias dbname using the SQLite ATTACH DATABASE statement. Returns the resulting *gorm.DB so errors can be checked via .Error.

func Dialector

func Dialector(conf *Config) (gorm.Dialector, error)

Dialector returns a SQLite GORM dialector from conf.

func InitDefault

func InitDefault(conf *Config) error

InitDefault opens SQLite and stores it as the dbgorm default database.

Example

ExampleInitDefault shows the default-instance pattern. Call InitDefault once at application startup; dbgorm.Default() returns the shared *gorm.DB for the process.

package main

import (
	"fmt"

	dbgorm "github.com/phcp-tech/common-library-golang/dbgorm"
	"github.com/phcp-tech/common-library-golang/dbgorm/sqlite"
)

func main() {
	err := sqlite.InitDefault(&sqlite.Config{
		Path: "file::memory:?cache=shared",
	})
	fmt.Println(err == nil)
	fmt.Println(dbgorm.Default() != nil)
}

func NewSQLite

func NewSQLite(conf *Config) (*gorm.DB, error)

NewSQLite opens a SQLite-backed GORM database and applies the standard PRAGMA settings (WAL, foreign keys, busy timeout, cache size). The parent directory of a file-based path is created automatically if absent.

Example

ExampleNewSQLite shows how to open an in-memory SQLite GORM database. In-memory databases are useful for tests and ephemeral data — all data is lost when the connection is closed.

package main

import (
	"fmt"

	"github.com/phcp-tech/common-library-golang/dbgorm/sqlite"
)

func main() {
	db, err := sqlite.NewSQLite(&sqlite.Config{
		Path: "file::memory:?cache=shared",
	})
	fmt.Println(err == nil)
	fmt.Println(db != nil)
}
Output:
true
true
Example (File)

ExampleNewSQLite_file shows how to open a file-based SQLite database. SQLite creates the file automatically if it does not exist.

package main

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/phcp-tech/common-library-golang/dbgorm/sqlite"
)

func main() {
	path := filepath.Join(os.TempDir(), "dbgorm_example.db")
	defer os.Remove(path)

	db, err := sqlite.NewSQLite(&sqlite.Config{
		Path: "file:" + path + "?cache=shared",
	})
	fmt.Println(err == nil)
	fmt.Println(db != nil)
}
Output:
true
true

Types

type Config

type Config struct {
	Path string

	// gorm.Config fields for connection pool settings.
	Logger *slog.Logger
}

Config contains SQLite connection settings.

Directories

Path Synopsis
Package component provides GORM SQLite lifecycle integration for bootstrap.
Package component provides GORM SQLite lifecycle integration for bootstrap.
Package vfs opens a SQLite database embedded inside a Go binary using modernc.org/sqlite/vfs and Go's embed.FS.
Package vfs opens a SQLite database embedded inside a Go binary using modernc.org/sqlite/vfs and Go's embed.FS.

Jump to

Keyboard shortcuts

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