Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Attach ¶
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 InitDefault ¶
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)
}
Output:
func NewSQLite ¶
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 ¶
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. |