Skip to content

Commit

Permalink
feat(explorer/indexer): orm client funcationality and indexer config (#…
Browse files Browse the repository at this point in the history
…189)

Creating an endpoint to mock and test inserts into db

task: none
  • Loading branch information
DanFlannel authored Jan 30, 2024
1 parent bf67db0 commit 08df050
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 18 deletions.
38 changes: 25 additions & 13 deletions explorer/db/ent_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,23 @@ import (
type EntServerClient interface {
CreateNewEntClient(databaseURL string) (*ent.Client, error)
CreateSQLLiteEntClient() (*ent.Client, error)
CreateNewEntClientWithSchema(ctx context.Context, databaseURL string) (*ent.Client, error)
}

type ClientImpl struct {
type Client struct {
EntServerClient
port int
}

func NewClient(port int) *ClientImpl {
client := ClientImpl{
port: port,
}
func NewClient() *Client {
client := Client{}

return &client
}

func (ClientImpl) CreateNewEntClient(ctx context.Context, databaseURL string) (*ent.Client, error) {
// Creates an ORM client without creating the tables.
func (Client) CreateNewEntClient(databaseURL string) (*ent.Client, error) {
db, err := sql.Open("pgx", databaseURL)
if err != nil {
log.Error(ctx, "Failed to connect to postgres database: %v", err)
return nil, errors.Wrap(err, "failed to open postgres connection")
}

Expand All @@ -44,30 +42,44 @@ func (ClientImpl) CreateNewEntClient(ctx context.Context, databaseURL string) (*

client := ent.NewClient(ent.Driver(drv))

return client, nil
}

// Creates an ORM client and creates the tables on initialization.
func (Client) CreateNewEntClientWithSchema(ctx context.Context, databaseURL string) (*ent.Client, error) {
db, err := sql.Open("pgx", databaseURL)
if err != nil {
return nil, errors.Wrap(err, "failed to open postgres connection")
}

// Create an ent.Driver from `db`.
drv := entsql.OpenDB(dialect.Postgres, db)

client := ent.NewClient(ent.Driver(drv))

// Run the automatic migration tool to create all schema resources.
if err := client.Schema.Create(ctx); err != nil {
log.Error(ctx, "Failed creating schema resources: %v", err)
return nil, errors.Wrap(err, "failed to do schema creation for postgresql db")
}

return client, nil
}

func (ClientImpl) CreateSQLiteEntClient(ctx context.Context) (*ent.Client, error) {
// Creates an in-memory ORM client with tables.
func (Client) CreateSQLiteEntClient(ctx context.Context) (*ent.Client, error) {
client, err := ent.Open(dialect.SQLite, "file:ent?mode=memory&cache=shared&_fk=1")
if err != nil {
log.Error(ctx, "Failed opening connection to sqlite: %v", err)
return nil, errors.Wrap(err, "failed to open sqlite connection")
}
defer func(client *ent.Client) {
err := client.Close()
if err != nil {
log.Error(ctx, "Failed closing ent client: %v", err)
log.Error(ctx, "Closing ent client", err)
}
}(client)

// Run the automatic migration tool to create all schema resources.
if err := client.Schema.Create(ctx); err != nil {
log.Error(ctx, "Failed creating schema resources: %v", err)
return nil, errors.Wrap(err, "failed to do schema creation for sqlite db")
}

Expand Down
25 changes: 21 additions & 4 deletions explorer/indexer/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,31 @@ package app
import (
"context"

"github.com/omni-network/omni/explorer/db"
"github.com/omni-network/omni/lib/log"
)

type Config struct{}
func Run(c context.Context, conf ExplorerIndexerConfig) error {
log.Info(c, "Config: %v", conf)
ctx, cancel := context.WithCancel(c)

func Run(ctx context.Context, conf Config) error {
log.Info(ctx, "Starting Indexer")
log.Info(ctx, "Config: %v", conf)
go func() {
var err error

// create ent client
client := db.NewClient()

entClient, err := client.CreateNewEntClientWithSchema(ctx, conf.DBUrl)

if err != nil {
log.Error(ctx, "Ent client %v", err, entClient)
return
}

cancel()
}()

<-ctx.Done()

return nil
}
18 changes: 18 additions & 0 deletions explorer/indexer/app/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package app

const (
Port = 8081
DBURL = "postgres://omni:password@db:5432/omni_db"
)

type ExplorerIndexerConfig struct {
Port int
DBUrl string
}

func DefaultExplorerAPIConfig() ExplorerIndexerConfig {
return ExplorerIndexerConfig{
Port: Port,
DBUrl: DBURL,
}
}
2 changes: 1 addition & 1 deletion explorer/indexer/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func newRunCmd() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
log.Info(ctx, "Indexer started")
conf := app.Config{}
conf := app.DefaultExplorerAPIConfig()

err := app.Run(ctx, conf)
if err != nil {
Expand Down

0 comments on commit 08df050

Please sign in to comment.