vfs

package
v0.1.21 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: 5 Imported by: 0

Documentation

Overview

Package vfs opens a SQLite database embedded in a Go binary via modernc.org/sqlite/vfs. Import this package only when the SQLite database file is distributed as part of the binary (embed.FS); for regular file-based databases use the parent dbsqlc/sqlite package instead.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Default

func Default() *sql.DB

Default returns the default *sql.DB instance created by InitDefault. Returns nil if InitDefault has not been called yet.

Example

ExampleDefault shows how to retrieve the singleton *sql.DB created by InitDefault. Returns nil if InitDefault has not been called yet.

package main

import (
	"fmt"

	sqlitevfs "github.com/phcp-tech/common-library-golang/dbsqlc/sqlite/vfs"
)

func main() {
	db := sqlitevfs.Default()
	fmt.Println(db != nil)
}

func InitDefault

func InitDefault(sqliteFS *embed.FS) error

InitDefault initializes the default singleton VFS SQLite connection. Only the first call takes effect; subsequent calls are silently ignored (sync.Once). For a non-singleton connection use New directly.

Example

ExampleInitDefault shows the singleton pattern: call InitDefault once at application startup. Subsequent calls are silently ignored (sync.Once). After a successful call, Default returns the initialised *sql.DB.

In production code, replace the empty embed.FS declaration with:

//go:embed config/sqlite.db
var sqliteFS embed.FS
package main

import (
	"embed"
	"fmt"

	sqlitevfs "github.com/phcp-tech/common-library-golang/dbsqlc/sqlite/vfs"
)

func main() {
	var sqliteFS embed.FS // placeholder — embed with //go:embed config/sqlite.db

	if err := sqlitevfs.InitDefault(&sqliteFS); err != nil {
		fmt.Println("error:", err)
		return
	}

	db := sqlitevfs.Default() // *sql.DB, pass to sqlc-generated Queries
	_ = db
}

func New

func New(sqliteFS *embed.FS) (*sql.DB, error)

New mounts the provided embedded filesystem as a SQLite VFS and opens a *sql.DB connection to the embedded database file at "config/sqlite.db". The embedded FS is registered with modernc.org/sqlite/vfs and the resulting VFS name is used to construct the SQLite URI. Returns the open *sql.DB or an error if VFS registration or database opening fails.

Example

ExampleNew shows how to open an embedded SQLite database via VFS. The database file must be embedded at "config/sqlite.db" within the FS.

In production code, replace the empty embed.FS declaration with:

//go:embed config/sqlite.db
var sqliteFS embed.FS
package main

import (
	"embed"
	"fmt"

	sqlitevfs "github.com/phcp-tech/common-library-golang/dbsqlc/sqlite/vfs"
)

func main() {
	var sqliteFS embed.FS // placeholder — embed with //go:embed config/sqlite.db

	db, err := sqlitevfs.New(&sqliteFS)
	if err != nil {
		fmt.Println("error:", err)
		return
	}
	defer db.Close()

	// pass db to sqlc-generated Queries
	_ = db
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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