local.go

26 lines
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
package engines

import (
	"database/sql"
	"fmt"

	"congo.gg/pkg/database"
	_ "github.com/tursodatabase/go-libsql"
)

// NewLocal creates a new local file database engine.
func NewLocal(path string) (*database.Database, error) {
	// Use file: prefix for local file databases
	// WAL mode and busy timeout are set for better concurrency
	dsn := "file:" + path + "?_journal_mode=WAL&_busy_timeout=5000"

	db, err := sql.Open("libsql", dsn)
	if err != nil {
		return nil, fmt.Errorf("open local database: %w", err)
	}

	// libSQL/SQLite handles concurrency internally
	db.SetMaxOpenConns(1)

	return &database.Database{DB: db}, nil
}