mirror of
https://github.com/safedep/vet.git
synced 2025-12-11 17:44:20 -06:00
feat: Add ent sqlite3 based storage driver
This commit is contained in:
parent
e98690ca42
commit
6d9af94dc0
54
pkg/storage/ent_sqlite.go
Normal file
54
pkg/storage/ent_sqlite.go
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
package storage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/safedep/vet/ent"
|
||||||
|
)
|
||||||
|
|
||||||
|
type EntSqliteClientConfig struct {
|
||||||
|
// Path to the sqlite database file
|
||||||
|
Path string
|
||||||
|
|
||||||
|
// Read Only mode
|
||||||
|
ReadOnly bool
|
||||||
|
|
||||||
|
// Skip schema creation
|
||||||
|
SkipSchemaCreation bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type entSqliteClient struct {
|
||||||
|
client *ent.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewEntSqliteClient(config EntSqliteClientConfig) (Storage[*ent.Client], error) {
|
||||||
|
mode := "rwc"
|
||||||
|
if config.ReadOnly {
|
||||||
|
mode = "r"
|
||||||
|
}
|
||||||
|
|
||||||
|
dbConn := fmt.Sprintf("file:%s?mode=%s&_fk=1", mode, config.Path)
|
||||||
|
client, err := ent.Open("sqlite3", dbConn)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to open sqlite3 connection: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !config.SkipSchemaCreation {
|
||||||
|
if err := client.Schema.Create(context.Background()); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to create schema resources: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &entSqliteClient{
|
||||||
|
client: client,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *entSqliteClient) Client() (*ent.Client, error) {
|
||||||
|
return c.client, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *entSqliteClient) Close() error {
|
||||||
|
return c.client.Close()
|
||||||
|
}
|
||||||
16
pkg/storage/storage.go
Normal file
16
pkg/storage/storage.go
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package storage
|
||||||
|
|
||||||
|
// Storage interface defines a generic contract for implementing
|
||||||
|
// a storage driver in the system. This usually means wrapping
|
||||||
|
// a DB client with our defined interface.
|
||||||
|
type Storage[T any] interface {
|
||||||
|
// Returns a client to the underlying storage driver.
|
||||||
|
// We will NOT hide the underlying storage driver because for an ORM
|
||||||
|
// which already abstracts DB connection, its unnecessary work to abstract
|
||||||
|
// an ORM and then implement function wrappers for all ORM operations.
|
||||||
|
Client() (T, error)
|
||||||
|
|
||||||
|
// Close any open connections, file descriptors and free
|
||||||
|
// any resources used by the storage driver
|
||||||
|
Close() error
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user