Compare commits

4 Commits

Author SHA1 Message Date
006af98328 removing forced paths 2025-08-28 21:11:59 -04:00
8c317fc3b6 Merge pull request 'updating methods' (#1) from fix/connect-methodology into main
Reviewed-on: #1
2025-07-15 23:01:10 +00:00
efc047c965 updating methods
- removing connect method
- db is connected to on call to New
2025-07-15 19:00:33 -04:00
5e9ba524e9 adding better error handling around connections 2025-07-15 18:22:46 -04:00
2 changed files with 4 additions and 25 deletions

View File

@@ -7,9 +7,3 @@ func WithFile(name string) Option {
d.file = name d.file = name
} }
} }
func WithPath(path string) Option {
return func(d *database) {
d.path = path
}
}

View File

@@ -4,7 +4,6 @@ import (
"context" "context"
"database/sql" "database/sql"
"fmt" "fmt"
"os"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
_ "github.com/ncruces/go-sqlite3/driver" _ "github.com/ncruces/go-sqlite3/driver"
@@ -13,12 +12,10 @@ import (
type database struct { type database struct {
file string file string
path string
sqlite *sqlx.DB sqlite *sqlx.DB
} }
type Database interface { type Database interface {
Connect() error
Close() error Close() error
Exec(ctx context.Context, query string, args ...any) (sql.Result, error) Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
Query(ctx context.Context, query string, args ...any) (*sqlx.Rows, error) Query(ctx context.Context, query string, args ...any) (*sqlx.Rows, error)
@@ -27,35 +24,23 @@ type Database interface {
} }
func New(opts ...Option) (Database, error) { func New(opts ...Option) (Database, error) {
home, err := os.UserHomeDir()
if err != nil {
return nil, err
}
defaultFile := "lite-db" defaultFile := "lite-db"
db := &database{ db := &database{
file: defaultFile, file: defaultFile,
path: home,
} }
for _, opt := range opts { for _, opt := range opts {
opt(db) opt(db)
} }
return db, nil slite, err := sqlx.Open("sqlite3", fmt.Sprintf("file:%s", db.file))
}
// connects to database file
func (d *database) Connect() error {
db, err := sqlx.Open("sqlite3", fmt.Sprintf("file:%s/%s", d.path, d.file))
if err != nil { if err != nil {
return err return nil, err
} }
db.sqlite = slite
d.sqlite = db return db, nil
return nil
} }
// closes database file connection // closes database file connection