-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmongo.go
More file actions
82 lines (72 loc) · 2.61 KB
/
mongo.go
File metadata and controls
82 lines (72 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package uow
import (
"context"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/mongo"
)
// MongoTx implements the Runner interface for MongoDB transactions. It manages
// the lifecycle of MongoDB sessions and transactions.
var _ Runner = &MongoTx{}
// MongoTx struct holds the MongoDB client and database name.
type MongoTx struct {
client *mongo.Client
dbName string
}
// NewMongoTx creates a new MongoTx instance. It takes a MongoDB client and
// database name as arguments. This function should be called to initialize
// a new transaction with MongoDB.
func NewMongoTx(client *mongo.Client, dbName string) *MongoTx {
return &MongoTx{
client: client,
dbName: dbName,
}
}
// Ctx starts a new MongoDB transaction. It uses the provided context and
// starts a new session and transaction within that session. If any errors
// occur during this process, they are wrapped and returned. This function
// is crucial for initiating transactions in the context.
func (m *MongoTx) Ctx(ctx context.Context) (context.Context, error) {
sess, err := m.client.StartSession()
if err != nil {
return nil, err
}
err = sess.StartTransaction()
if err != nil {
return nil, errors.Wrap(err, "error in starting transaction")
}
return mongo.NewSessionContext(ctx, sess), nil
}
// Get retrieves the MongoDB database. It checks if a session is present in the
// context. If a session exists, it retrieves the database from the session's
// client. Otherwise, it retrieves the database from the client directly. This
// function provides access to the database within the transaction's context.
func (m *MongoTx) Get(ctx context.Context) any {
sess := mongo.SessionFromContext(ctx)
if sess != nil {
return sess.Client().Database(m.dbName)
}
return m.client.Database(m.dbName)
}
// Rollback aborts the current transaction. It checks for the presence of a
// session in the context and aborts the transaction if one exists. The session
// is then ended. This function is essential for handling transaction failures.
func (m *MongoTx) Rollback(ctx context.Context) error {
sess := mongo.SessionFromContext(ctx)
if sess != nil {
defer sess.EndSession(ctx)
return sess.AbortTransaction(ctx)
}
return nil
}
// Commit commits the current transaction. It checks for the presence of a
// session in the context and commits the transaction if one exists. The session
// is then ended. This function is crucial for saving changes made within a
// transaction.
func (m *MongoTx) Commit(ctx context.Context) error {
sess := mongo.SessionFromContext(ctx)
if sess != nil {
defer sess.EndSession(ctx)
return sess.CommitTransaction(ctx)
}
return nil
}