forked from rbaliyan/ledger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtx.go
More file actions
37 lines (33 loc) · 1.14 KB
/
tx.go
File metadata and controls
37 lines (33 loc) · 1.14 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
package ledger
import "context"
type txKey struct{}
// WithTx returns a new context carrying the given transaction value.
// SQL backends accept *sql.Tx, MongoDB backends accept mongo.Session.
//
// When a context with a transaction is passed to Store methods, the store
// participates in the caller's transaction instead of creating its own.
// This enables atomic writes across the ledger and other tables/collections.
//
// Example (SQL):
//
// tx, _ := db.BeginTx(ctx, nil)
// ctx = ledger.WithTx(ctx, tx)
// store.Append(ctx, "orders", entry) // uses caller's tx
// tx.Commit()
//
// Example (MongoDB):
//
// session, _ := client.StartSession()
// session.WithTransaction(ctx, func(sc context.Context) (any, error) {
// ctx := ledger.WithTx(sc, session)
// store.Append(ctx, "orders", entry) // uses caller's session
// return nil, nil
// })
func WithTx(ctx context.Context, tx any) context.Context {
return context.WithValue(ctx, txKey{}, tx)
}
// TxFromContext returns the transaction stored in the context, or nil.
// This function is intended for Store implementors.
func TxFromContext(ctx context.Context) any {
return ctx.Value(txKey{})
}