Skip to content

Commit fd35cb1

Browse files
author
Ezequiel Raynaudo
authored
Merge pull request #17 from Zondax/add/GetMongoDoc
Update on mongodb wrapper Minor refactor
2 parents de78365 + 2caa8a7 commit fd35cb1

8 files changed

Lines changed: 135 additions & 132 deletions

File tree

connections/data_source.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
ds "github.com/Zondax/zindexer/connections/data_store"
66
"github.com/coinbase/rosetta-sdk-go/client"
7+
"go.mongodb.org/mongo-driver/mongo"
78
"gorm.io/gorm"
89
"time"
910
)
@@ -14,10 +15,11 @@ const (
1415

1516
type DataSource struct {
1617
// data sources
17-
DbConn *gorm.DB
18-
RosettaClient *client.APIClient
19-
NodeClient interface{}
20-
DataStore ds.DataStoreClient
18+
DatabasePostgres *gorm.DB
19+
DatabaseMongo *mongo.Client
20+
RosettaClient *client.APIClient
21+
NodeClient interface{}
22+
DataStore ds.DataStoreClient
2123
// common
2224
Ctx context.Context
2325
RetryDelay time.Duration
@@ -49,9 +51,15 @@ func WithRetryDelay(delay time.Duration) SourceOption {
4951
}
5052
}
5153

52-
func WithDBConnection(dbConn *gorm.DB) SourceOption {
54+
func WithPostgresDB(dbConn *gorm.DB) SourceOption {
5355
return func(w *DataSource) {
54-
w.DbConn = dbConn
56+
w.DatabasePostgres = dbConn
57+
}
58+
}
59+
60+
func WithMongoDB(dbConn *mongo.Client) SourceOption {
61+
return func(w *DataSource) {
62+
w.DatabaseMongo = dbConn
5563
}
5664
}
5765

@@ -69,10 +77,10 @@ func WithNodeClient(node interface{}) SourceOption {
6977

7078
func WithDataStore(cfg ds.DataStoreConfig) SourceOption {
7179
return func(w *DataSource) {
72-
client, err := ds.NewDataStoreClient(cfg)
80+
storeClient, err := ds.NewDataStoreClient(cfg)
7381
if err != nil {
7482
panic(err)
7583
}
76-
w.DataStore = client
84+
w.DataStore = storeClient
7785
}
7886
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
package database
1+
package graphql
22

33
import (
44
"encoding/json"
55
"fmt"
6+
"github.com/Zondax/zindexer/connections/database"
67
"github.com/hasura/go-graphql-client"
78
"go.uber.org/zap"
89
"net/http"
@@ -28,7 +29,7 @@ type GraphqlSubscriptionClient struct {
2829
func NewGraphqlQueryClient(host string, token string) GraphqlClient {
2930
transport := http.DefaultTransport
3031
if token != "" {
31-
transport = AuthHeaderTransport{Transport: http.DefaultTransport, Token: token}
32+
transport = database.AuthHeaderTransport{Transport: http.DefaultTransport, Token: token}
3233
}
3334

3435
customHttpClient := http.Client{Transport: transport}

connections/database/mongoDB.go

Lines changed: 0 additions & 48 deletions
This file was deleted.

connections/database/mongoDB_test.go

Lines changed: 0 additions & 52 deletions
This file was deleted.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package mongodb
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"github.com/Zondax/zindexer/connections/database"
7+
"go.uber.org/zap"
8+
"time"
9+
10+
"go.mongodb.org/mongo-driver/bson"
11+
"go.mongodb.org/mongo-driver/mongo"
12+
"go.mongodb.org/mongo-driver/mongo/options"
13+
"go.mongodb.org/mongo-driver/mongo/readpref"
14+
)
15+
16+
type MongoConnection struct {
17+
db *mongo.Client
18+
}
19+
20+
func NewMongoConnection(params *database.DBConnectionParams) (*MongoConnection, error) {
21+
uri := params.URI
22+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
23+
defer cancel()
24+
25+
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
26+
if err != nil {
27+
return nil, err
28+
}
29+
30+
// Ping the primary
31+
if err := client.Ping(ctx, readpref.Primary()); err != nil {
32+
return nil, err
33+
}
34+
35+
return &MongoConnection{db: client}, nil
36+
}
37+
38+
func (c *MongoConnection) GetDB() *mongo.Client {
39+
return c.db
40+
}
41+
42+
func Connect(params *database.DBConnectionParams) (*mongo.Client, error) {
43+
conn, err := NewMongoConnection(params)
44+
if err != nil {
45+
return nil, err
46+
}
47+
48+
return conn.GetDB(), nil
49+
}
50+
51+
func (c *MongoConnection) GetMongoDoc(collection *mongo.Collection, docId string) (bson.M, error) {
52+
zap.S().Debug("document with id:%v \n", docId)
53+
opts := options.FindOne()
54+
var result bson.M
55+
readErr := collection.FindOne(
56+
context.TODO(),
57+
bson.D{{Key: "_id", Value: docId}},
58+
opts,
59+
).Decode(&result)
60+
61+
if readErr != nil {
62+
// ErrNoDocuments means that the filter did not match any documents in
63+
// the collection.
64+
if readErr == mongo.ErrNoDocuments {
65+
return nil, fmt.Errorf("no document found")
66+
}
67+
return nil, readErr
68+
}
69+
70+
return result, nil
71+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package mongodb
2+
3+
import (
4+
"context"
5+
"crypto/rand"
6+
"fmt"
7+
"github.com/Zondax/zindexer/connections/database"
8+
"go.mongodb.org/mongo-driver/bson"
9+
"os"
10+
"testing"
11+
)
12+
13+
func TestBuffer_InsertRead(t *testing.T) {
14+
params := &database.DBConnectionParams{URI: os.Getenv("MONGO_URI")}
15+
client, err := NewMongoConnection(params)
16+
if err != nil {
17+
fmt.Println(err)
18+
t.Fail()
19+
}
20+
21+
coll := client.GetDB().Database("test").Collection("sample")
22+
key, _ := rand.Prime(rand.Reader, 32)
23+
res, err := coll.InsertOne(context.TODO(), bson.D{{Key: "name", Value: "Alice"}, {Key: "_id", Value: key.String()}})
24+
if err != nil {
25+
fmt.Println(err)
26+
t.Fail()
27+
}
28+
fmt.Printf("inserted document with ID %v\n", res.InsertedID)
29+
30+
// Get the inserted file
31+
result, err := client.GetMongoDoc(coll, key.String())
32+
if err != nil {
33+
fmt.Printf("Failed to get with error %v", err)
34+
t.Fail()
35+
return
36+
}
37+
38+
fmt.Printf("found document %v\n", result)
39+
}
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
package database
1+
package postgres
22

33
import (
44
"fmt"
5+
"github.com/Zondax/zindexer/connections/database"
56
"gorm.io/driver/postgres"
67
"gorm.io/gorm"
78
)
@@ -14,7 +15,7 @@ type DBConnectionConfig struct {
1415
Gorm *gorm.Config
1516
}
1617

17-
func NewPostgresConnection(params *DBConnectionParams, config DBConnectionConfig) (*GormConnection, error) {
18+
func NewPostgresConnection(params *database.DBConnectionParams, config DBConnectionConfig) (*GormConnection, error) {
1819
dsn, err := params.GetDSN()
1920
if err != nil {
2021
return nil, fmt.Errorf("failed to retrieve dsn")
@@ -24,22 +25,20 @@ func NewPostgresConnection(params *DBConnectionParams, config DBConnectionConfig
2425
if config.Gorm != nil {
2526
dbConfig = *config.Gorm
2627
}
27-
conn, err := gorm.Open(postgres.Open(dsn), &dbConfig)
28+
c, err := gorm.Open(postgres.Open(dsn), &dbConfig)
2829

2930
if err != nil {
3031
return nil, fmt.Errorf("failed to dial connect to db '%s@%s:%s': %v", params.Name, params.Host, params.Port, err)
3132
}
3233

33-
return &GormConnection{
34-
db: conn,
35-
}, nil
34+
return &GormConnection{db: c}, nil
3635
}
3736

3837
func (c *GormConnection) GetDB() *gorm.DB {
3938
return c.db
4039
}
4140

42-
func ConnectDB(params DBConnectionParams, config DBConnectionConfig) (*gorm.DB, error) {
41+
func Connect(params database.DBConnectionParams, config DBConnectionConfig) (*gorm.DB, error) {
4342
conn, err := NewPostgresConnection(&params, config)
4443
if err != nil {
4544
return nil, err
Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package database
33
import (
44
"fmt"
55
"net/http"
6-
7-
"gorm.io/gorm"
86
)
97

108
type AuthHeaderTransport struct {
@@ -17,19 +15,6 @@ func (adt AuthHeaderTransport) RoundTrip(req *http.Request) (*http.Response, err
1715
return adt.Transport.RoundTrip(req)
1816
}
1917

20-
type DBConnection interface {
21-
GetDB() *gorm.DB
22-
}
23-
24-
type IDBQueryClient interface {
25-
GetDB() interface{}
26-
Connect() error
27-
}
28-
29-
type DBQueryClient struct {
30-
Client IDBQueryClient
31-
}
32-
3318
type DBConnectionParams struct {
3419
User string
3520
Password string

0 commit comments

Comments
 (0)