qmgo
qmgo copied to clipboard
Add AsyncTransaction
Doing a transaction in a callback makes writing go code very cumbersome and maybe a bit javascript like. (which the official mongo go sdk definitely has that feeling). They have a less visible / documented way using session context.
I added a StartAsyncTransaction method to the session that returns a new context that as long as passed in with insert/updates will be associated with the transaction.
Example usage:
session, err := cli.Session()
if err != nil {
return err
}
sCtx, err := session.StartAsyncTransaction(ctx)
if err != nil {
return err
}
defer session.EndSession(sCtx)
defer session.AbortAsyncTransaction(sCtx)
if _, err := cli.InsertOne(sCtx, bson.D{{"abc", int32(1)}}); err != nil {
return err
}
if err := session.CommitAsyncTransaction(sCtx); err != nil {
return err
}
Any idea if or when this PR will be considered for being added?
Thanks for accepting my change! :pray: