Some checks failed
ci/woodpecker/push/billing_fees Pipeline was successful
ci/woodpecker/push/bff Pipeline was successful
ci/woodpecker/push/db Pipeline was successful
ci/woodpecker/push/chain_gateway Pipeline was successful
ci/woodpecker/push/fx_ingestor Pipeline was successful
ci/woodpecker/push/fx_oracle Pipeline was successful
ci/woodpecker/push/frontend Pipeline was successful
ci/woodpecker/push/payments_orchestrator Pipeline was successful
ci/woodpecker/push/bump_version Pipeline failed
ci/woodpecker/push/nats Pipeline was successful
ci/woodpecker/push/ledger Pipeline was successful
ci/woodpecker/push/notification Pipeline was successful
66 lines
1.7 KiB
Go
66 lines
1.7 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
|
|
mongoimpl "github.com/tech/sendico/pkg/db/internal/mongo"
|
|
"github.com/tech/sendico/pkg/merrors"
|
|
"github.com/tech/sendico/pkg/mlogger"
|
|
mongoDriver "go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/readpref"
|
|
)
|
|
|
|
// Connection represents a low-level database connection lifecycle.
|
|
type Connection interface {
|
|
Disconnect(ctx context.Context) error
|
|
Ping(ctx context.Context) error
|
|
}
|
|
|
|
// MongoConnection provides direct access to the underlying mongo client.
|
|
type MongoConnection struct {
|
|
client *mongoDriver.Client
|
|
database string
|
|
}
|
|
|
|
func (c *MongoConnection) Client() *mongoDriver.Client {
|
|
return c.client
|
|
}
|
|
|
|
func (c *MongoConnection) Database() *mongoDriver.Database {
|
|
return c.client.Database(c.database)
|
|
}
|
|
|
|
func (c *MongoConnection) Disconnect(ctx context.Context) error {
|
|
if ctx == nil {
|
|
ctx = context.Background()
|
|
}
|
|
return c.client.Disconnect(ctx)
|
|
}
|
|
|
|
func (c *MongoConnection) Ping(ctx context.Context) error {
|
|
if ctx == nil {
|
|
ctx = context.Background()
|
|
}
|
|
return c.client.Ping(ctx, readpref.Primary())
|
|
}
|
|
|
|
// ConnectMongo returns a low-level MongoDB connection without constructing repositories.
|
|
func ConnectMongo(logger mlogger.Logger, config *Config) (*MongoConnection, error) {
|
|
if config == nil {
|
|
return nil, merrors.InvalidArgument("database configuration is nil", "config")
|
|
}
|
|
if config.Driver != Mongo {
|
|
return nil, merrors.InvalidArgument("unsupported database driver: "+string(config.Driver), "config.driver")
|
|
}
|
|
|
|
client, _, settings, err := mongoimpl.ConnectClient(logger, config.Settings)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &MongoConnection{
|
|
client: client,
|
|
database: settings.Database,
|
|
}, nil
|
|
}
|