mirror of
https://github.com/luxfi/mpc.git
synced 2026-09-24 00:26:24 +00:00
feat: SQLite default — zero external deps to start
db.New() auto-detects backend from DSN: - "" or "sqlite://path" or "file.db" → SQLite (default) - "postgres://..." → PostgreSQL (production upgrade) KV (Valkey/Redis) is optional — pass "" to skip. No postgres, no redis required to run mpcd out of the box. Removed pgxpool direct dependency from db package.
This commit is contained in:
@@ -17,7 +17,6 @@ require (
|
||||
github.com/hanzoai/kv-go/v9 v9.18.0
|
||||
github.com/hanzoai/orm v0.3.2
|
||||
github.com/hashicorp/consul/api v1.33.7
|
||||
github.com/jackc/pgx/v5 v5.9.1
|
||||
github.com/luxfi/crypto v1.17.45
|
||||
github.com/luxfi/database v1.17.44
|
||||
github.com/luxfi/fhe v1.7.7
|
||||
@@ -81,6 +80,7 @@ require (
|
||||
github.com/holiman/uint256 v1.3.2 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||
github.com/jackc/pgx/v5 v5.9.1 // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
||||
github.com/klauspost/compress v1.18.5 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
|
||||
|
||||
+44
-25
@@ -1,31 +1,57 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
kv "github.com/hanzoai/kv-go/v9"
|
||||
"github.com/hanzoai/orm"
|
||||
ormdb "github.com/hanzoai/orm/db"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
// Database holds the ORM entity store and the KV cache/session client.
|
||||
// Database holds the ORM entity store and optional KV cache.
|
||||
type Database struct {
|
||||
ORM orm.DB
|
||||
KV kv.UniversalClient
|
||||
pool *pgxpool.Pool // underlying pool for raw SQL fallback
|
||||
ORM orm.DB
|
||||
KV kv.UniversalClient
|
||||
}
|
||||
|
||||
// New creates a Database wired to PostgreSQL (via ORM) and Valkey/Redis (via KV).
|
||||
func New(postgresURL, kvURL string) (*Database, error) {
|
||||
sqlDB, err := ormdb.NewSQLDB(&ormdb.SQLConfig{
|
||||
DSN: postgresURL,
|
||||
MaxConns: 20,
|
||||
MinConns: 2,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// New creates a Database. Detects backend from DSN:
|
||||
// - "" or "sqlite://..." or file path → SQLite (default, zero-config)
|
||||
// - "postgres://..." → PostgreSQL
|
||||
//
|
||||
// kvURL is optional — pass "" to skip KV cache.
|
||||
func New(dsn, kvURL string) (*Database, error) {
|
||||
var ormInstance orm.DB
|
||||
|
||||
ormInstance := orm.AdaptDB(sqlDB)
|
||||
switch {
|
||||
case dsn == "" || strings.HasPrefix(dsn, "sqlite://") || strings.HasSuffix(dsn, ".db"):
|
||||
// SQLite — default, zero external deps.
|
||||
path := "mpc.db"
|
||||
if strings.HasPrefix(dsn, "sqlite://") {
|
||||
path = strings.TrimPrefix(dsn, "sqlite://")
|
||||
} else if strings.HasSuffix(dsn, ".db") {
|
||||
path = dsn
|
||||
}
|
||||
sqliteDB, err := ormdb.NewSQLiteDB(&ormdb.SQLiteDBConfig{Path: path})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sqlite: %w", err)
|
||||
}
|
||||
ormInstance = orm.AdaptDB(sqliteDB)
|
||||
|
||||
case strings.HasPrefix(dsn, "postgres://") || strings.HasPrefix(dsn, "postgresql://"):
|
||||
sqlDB, err := ormdb.NewSQLDB(&ormdb.SQLConfig{
|
||||
DSN: dsn,
|
||||
MaxConns: 20,
|
||||
MinConns: 2,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("postgres: %w", err)
|
||||
}
|
||||
ormInstance = orm.AdaptDB(sqlDB)
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported DSN: %q (use sqlite://, postgres://, or .db path)", dsn)
|
||||
}
|
||||
|
||||
var kvClient kv.UniversalClient
|
||||
if kvURL != "" {
|
||||
@@ -35,18 +61,11 @@ func New(postgresURL, kvURL string) (*Database, error) {
|
||||
}
|
||||
|
||||
return &Database{
|
||||
ORM: ormInstance,
|
||||
KV: kvClient,
|
||||
pool: sqlDB.Pool(),
|
||||
ORM: ormInstance,
|
||||
KV: kvClient,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Pool returns the underlying pgxpool.Pool for raw SQL operations.
|
||||
// Use this only for queries that have no ORM equivalent.
|
||||
func (d *Database) Pool() *pgxpool.Pool {
|
||||
return d.pool
|
||||
}
|
||||
|
||||
// Close releases all database resources.
|
||||
func (d *Database) Close() {
|
||||
if d.ORM != nil {
|
||||
|
||||
Reference in New Issue
Block a user