Skip to content

Add MongoDB support as storage back-end#27

Merged
doganarif merged 10 commits intodoganarif:mainfrom
ronanzindev:feature/mongodb-storage
May 20, 2025
Merged

Add MongoDB support as storage back-end#27
doganarif merged 10 commits intodoganarif:mainfrom
ronanzindev:feature/mongodb-storage

Conversation

@ronanzindev
Copy link
Contributor

@ronanzindev ronanzindev commented May 19, 2025

This PR adds support for using MongoDB as a storage back-end in GoVisual.

Based on #6

Problem solved:

GoVisual currenty supports only PostgreSQL,Redis and SQLite as storage back-ends. However, there was no option for document-base, high perfomace for write/read operations, schema flexibility for request/response structures and native JSON support for HTTP, like MongoDB

Summary of changes:

  • Implemented MongoDBStorage with same interface and begavior as the existing storages back-end.
  • Added support for configuring MongoDB via enviroment variables and options.
  • Updated the storage factory to handle the new mongodb type.
  • Added documentation for MongoDB usage and schema inside docs/storage-backends.md file.

Summary by Sourcery

Add MongoDB as a new back-end for GoVisual, providing a fast schema flexibility persistent storage option.

New Features:

  • Implemented MongoDB storage back-end with full support for request logging.

Enhancements:

  • Extended storage factory to support MongoDB as new storage type.
  • Added index on timestamp for faster log retrieval in MongoDB.

Summary by Sourcery

Add native support for MongoDB as a new storage backend by implementing MongoDBStore, wiring it into configuration and factory logic, updating documentation and examples, and adding tests.

New Features:

  • Add MongoDB storage backend implementing the Store interface with indexing and capacity management.

Enhancements:

  • Extend storage factory and configuration to support MongoDB storage type.
  • Provide a WithMongoDBStorage option and configure its connection parameters.

Documentation:

  • Document MongoDB usage, configuration options, API reference, and examples in storage-backends and configuration guides.

Tests:

  • Add integration tests for MongoDB store implementation.

Chores:

  • Update go.mod dependencies and docker-compose example to include MongoDB service.

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented May 19, 2025

Reviewer's Guide

Introduces MongoDB as a first-class storage backend by adding a MongoDBStore implementation conforming to the existing Store interface, wiring it through the factory and configuration options, and supporting it with documentation, examples, dependency updates, and integration tests.

File-Level Changes

Change Details Files
New MongoDBStore implementation with full Store interface support
  • Established NewMongoDBStore to connect, ping, and index timestamp field
  • Implemented Add (InsertOne) with capacity-based cleanup
  • Provided Get, GetAll, GetLatest, Clear, and Close methods following Store interface
internal/store/mongodb.go
Integrated MongoDBStore into storage factory
  • Added StorageTypeMongoDB enum constant
  • Updated NewStore to parse database.collection and return NewMongoDBStore
internal/store/factory.go
Exposed WithMongoDBStorage configuration option
  • Created WithMongoDBStorage in options.go to set type, URI, and tableName
  • Adjusted example main to append WithMongoDBStorage on 'mongodb' env selection
options.go
cmd/examples/multistorage/main.go
Enhanced documentation and examples for MongoDB usage
  • Added MongoDB section with pros/cons, JSON schema, and connection details
  • Updated API reference for WithMongoDBStorage signature and parameters
  • Extended configuration table to list WithMongoDBStorage
  • Augmented multi-storage example README and docker-compose to include MongoDB service and env vars
docs/storage-backends.md
docs/api-reference.md
docs/configuration.md
cmd/examples/multistorage/README.md
cmd/examples/multistorage/docker-compose.yml
Adjusted model and dependencies for MongoDB compatibility
  • Added bson:"_id" tag to RequestLog ID field
  • Bumped go.mongodb.org/mongo-driver and related indirect dependencies in go.mod
internal/model/request.go
go.mod
go.sum
Added integration tests for MongoDBStore
  • Created mongodb_test.go to validate Store operations against a live MongoDB instance
internal/store/mongodb_test.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @ronanzindev - I've reviewed your changes - here's some feedback:

  • Consider extending StorageConfig with dedicated fields for MongoDB database and collection instead of overloading TableName with a "db.collection" string.
  • Fix the typo in the API reference docs—WithMongoDBStorate should be WithMongoDBStorage.
  • Align the env var in your MongoDB store tests (MONGO_URI) with the GOVISUAL_MONGO_URI name used in examples/configuration to avoid confusion and skipped tests.
Here's what I looked at during the review
  • 🟡 General issues: 6 issues found
  • 🟢 Testing: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

capacity = 100
}

ctx := context.Background()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: A background context is used for all DB operations, which may hinder graceful shutdown.

Requests started with context.Background can’t be canceled or timed out. To enable graceful shutdown, accept a context parameter or use a cancellable context.

Comment on lines +77 to +78
mongoMetaData := strings.Split(config.TableName, ".")
if len(mongoMetaData) < 2 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: Splitting TableName on '.' assumes no '.' in database or collection names.

Use a more robust parsing approach for database and collection names (instead of splitting on '.') or clearly document this limitation.

@doganarif doganarif self-assigned this May 19, 2025
@doganarif doganarif added the enhancement New feature or request label May 19, 2025
doganarif and others added 6 commits May 19, 2025 20:41
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
@doganarif doganarif merged commit d41a20a into doganarif:main May 20, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants