DEV Community

Mimgyong Chen
Mimgyong Chen

Posted on

✨ Proposal: Simplify MongoDB Transaction Handling in mongox with Wrapper APIs

Hi everyone,

I’m working on a Go library called go-mongox, which extends the official MongoDB Go driver with generics, type safety, and fluent APIs. Recently, we’ve been exploring ways to simplify transaction handling, which can be quite verbose and error-prone in the official driver.

To address this, we’re proposing two high-level transaction wrapper APIs:

    // Simplified transaction handling with automatic session management
    func (c *Client) RunTransaction(
        ctx context.Context,
        fn func(ctx context.Context) (any, error),
        txnOptions ...options.Lister[options.TransactionOptions],
    ) (any, error)

    // Advanced transaction handling with manual session control
    func (c *Client) WithManualTransaction(
        ctx context.Context,
        fn func(ctx context.Context, session *mongo.Session, txnOptions ...options.Lister[options.TransactionOptions]) error,
        txnOptions ...options.Lister[options.TransactionOptions],
    ) error

Enter fullscreen mode Exit fullscreen mode

These methods aim to:

  • Reduce boilerplate by automating session lifecycle management.
  • Provide a consistent and ergonomic API for common transaction use cases.
  • Offer flexibility for advanced scenarios with manual session control.

We’ve also included usage examples and design goals in the full proposal here: ✨ Feature Proposal: Simplify Transaction Handling with Wrapper APIs

We’d love your feedback on:

  • Are the proposed APIs intuitive? Any suggestions for better naming or design?
  • Are there additional features you’d like to see, such as retry strategies, hooks, or metrics?
  • Any edge cases or limitations we should consider?

Looking forward to hearing your thoughts and ideas! 🙌

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.