graylog2-server icon indicating copy to clipboard operation
graylog2-server copied to clipboard

Add utility methods for MongoDB collections

Open thll opened this issue 10 months ago • 2 comments

Adds basic utility methods for interacting with MongoDB collections to ease the transition to the new API.

Functionality added by this PR for:

  • Pagination
  • Extracting inserted IDs from a MongoDB InsertOneResult
  • Getting/Deleting documents by ID
  • Streaming documents from a FindIterable
  • A MongoEntity interface that at the moment only enforces an _id field of type ObjectId but serves as a marker interface to be extended in the future.

I have deliberately not implemented #save(MongoEntity entity). IMO, a caller should explicitly call create or update operations, and in most cases, e.g., when called from a REST resource, we know which path to choose anyway.

If it turns out that #save(MongoEntity entity) would be desirable, we could add that. E.g., by defining MongoEntity#withId(String id) to return a copy of the entity with the given ID and use that in our implementation of #save(MongoEntity entity) to construct the return value.

/prd https://github.com/Graylog2/graylog-plugin-enterprise/pull/7198 /nocl not user-facing

thll avatar Apr 09 '24 07:04 thll

I think these utility methods are nice! I took a stab at rewriting TDIR's largest DB service using them and it was straightforward and (to me) cleaned things up a bit. Nice work!

kingzacko1 avatar Apr 18 '24 21:04 kingzacko1

@kingzacko1: awesome, thanks for checking it out!

thll avatar Apr 19 '24 12:04 thll