Skip to content

Conversation

joordih
Copy link

@joordih joordih commented Oct 10, 2025

Add a modern, type-safe MongoDB integration using POJOs with Jackson annotations and Lombok to reduce boilerplate. The new implementation provides a cleaner repository pattern while maintaining backward compatibility with the existing MongoStorage implementation.

What I changed

New package structure under xyz.refinedev.api.storage.mongo

  • model.Model: Base interface for MongoDB entities with @JsonProperty("_id") mapping
  • repository.ObjectRepository: Generic CRUD interface defining find, save, remove, and findAll operations
  • repository.MongoObjectRepository: MongoDB implementation of ObjectRepository with typed collection support
  • storage.MongoStorageManager: New recommended entrypoint with POJO codec registry configuration and typed repository creation
  • storage.MongoStorage: Marked as @Deprecated and kept for backward compatibility with the Document+Gson approach

Example implementation (in src/test)

  • User model

    • Implements Model interface
    • Uses Lombok annotations (@Getter, @Setter, @Builder, @NoArgsConstructor, @AllArgsConstructor)
    • Uses Jackson annotations (@JsonProperty("_id"), @JsonIgnore for computed fields)
    • Fields: id, uuid, points, level
  • UserManager

    • Encapsulates repository access for User entities
    • Initialized with MongoStorageManager
  • Storage main class

    • Shows a complete workflow: initialize MongoStorageManager, create UserManager, save and retrieve users

Why this is useful

  • Type-safe: Work directly with POJOs instead of raw Document objects
  • Less boilerplate: Lombok annotations eliminate repetitive getters/setters/builders
  • Modern approach: Uses MongoDB's native POJO codec provider instead of Gson serialization
  • Extensible: Easy to create new repositories for custom models by implementing the Model interface
  • Backward compatible: Existing code using MongoStorage continues to work

Breaking Changes

None. This is a fully backward-compatible addition:

  • MongoStorage is deprecated but still functional
  • New code should use MongoStorageManager and typed repositories
  • Existing projects can migrate incrementally

Migration path: Replace MongoStorage with MongoStorageManager.getRepository(YourModel.class, "collection") and ensure your models implement the Model interface.


- Implement MongoStorageManager with POJO codec support
- Add ObjectRepository interface and MongoObjectRepository implementation
- Create Model interface for MongoDB document mapping
- Deprecate legacy MongoStorage (Document+Gson approach)
- Include working example with User and UserManager
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant