Conversation
WalkthroughTwo MongoDB composite indexes are created on the EventRepository collection during constructor initialization: one on room_id, nextEventId, depth, and createdAt; another on room_id and event type. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Suggested labels
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Tip Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs). Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #336 +/- ##
==========================================
+ Coverage 50.78% 50.82% +0.04%
==========================================
Files 97 97
Lines 10949 10960 +11
==========================================
+ Hits 5560 5570 +10
- Misses 5389 5390 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/federation-sdk/src/repositories/event.repository.ts`:
- Around line 12-23: The createIndex() calls in the constructor are
fire-and-forget which causes unhandled promise rejections and no startup
guarantee; remove the direct createIndex() calls from the constructor and add an
async initialization method (e.g., ensureIndexes() or init()) on the
EventRepository class that awaits each this.collection.createIndex(...) call (or
wraps them in Promise.all), surrounds them with try/catch to log or rethrow
errors, and then call await eventRepository.ensureIndexes() from app bootstrap
so indexes are created before queries run; alternatively, if you prefer minimal
change, at least append .catch(...) to each this.collection.createIndex(...) in
the class to handle errors and avoid unhandled rejections.
ℹ️ Review info
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
packages/federation-sdk/src/repositories/event.repository.ts
📜 Review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: cubic · AI code reviewer
| ) { | ||
| this.collection.createIndex({ | ||
| 'event.room_id': 1, | ||
| 'nextEventId': 1, | ||
| 'event.depth': 1, | ||
| 'createdAt': 1, | ||
| }); | ||
| this.collection.createIndex({ | ||
| 'event.room_id': 1, | ||
| 'event.type': 1, | ||
| }); | ||
| } |
There was a problem hiding this comment.
Unhandled promises from createIndex() calls.
createIndex() returns a Promise<string> that is neither awaited nor has error handling attached. This means:
- Index creation errors will be silently swallowed as unhandled rejections.
- There's no guarantee indexes are ready before the first query executes.
Since this is a @singleton() managed by tsyringe, consider adding an explicit async init() method that the application can await during startup, or at minimum handle the promise rejection:
Option 1: Add error handling (minimal change)
constructor(
`@inject`('EventCollection')
private readonly collection: Collection<EventStore>,
) {
- this.collection.createIndex({
+ this.collection.createIndex({
'event.room_id': 1,
'nextEventId': 1,
'event.depth': 1,
'createdAt': 1,
- });
- this.collection.createIndex({
+ }).catch((err) => console.error('Failed to create event index (room_id, nextEventId, depth, createdAt):', err));
+ this.collection.createIndex({
'event.room_id': 1,
'event.type': 1,
- });
+ }).catch((err) => console.error('Failed to create event index (room_id, type):', err));
}Option 2: Separate initialization method (recommended for startup guarantees)
constructor(
`@inject`('EventCollection')
private readonly collection: Collection<EventStore>,
- ) {
- this.collection.createIndex({
- 'event.room_id': 1,
- 'nextEventId': 1,
- 'event.depth': 1,
- 'createdAt': 1,
- });
- this.collection.createIndex({
- 'event.room_id': 1,
- 'event.type': 1,
- });
- }
+ ) {}
+
+ async ensureIndexes(): Promise<void> {
+ await Promise.all([
+ this.collection.createIndex({
+ 'event.room_id': 1,
+ 'nextEventId': 1,
+ 'event.depth': 1,
+ 'createdAt': 1,
+ }),
+ this.collection.createIndex({
+ 'event.room_id': 1,
+ 'event.type': 1,
+ }),
+ ]);
+ }Then call await eventRepository.ensureIndexes() during application bootstrap.
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| ) { | |
| this.collection.createIndex({ | |
| 'event.room_id': 1, | |
| 'nextEventId': 1, | |
| 'event.depth': 1, | |
| 'createdAt': 1, | |
| }); | |
| this.collection.createIndex({ | |
| 'event.room_id': 1, | |
| 'event.type': 1, | |
| }); | |
| } | |
| constructor( | |
| `@inject`('EventCollection') | |
| private readonly collection: Collection<EventStore>, | |
| ) { | |
| this.collection.createIndex({ | |
| 'event.room_id': 1, | |
| 'nextEventId': 1, | |
| 'event.depth': 1, | |
| 'createdAt': 1, | |
| }).catch((err) => console.error('Failed to create event index (room_id, nextEventId, depth, createdAt):', err)); | |
| this.collection.createIndex({ | |
| 'event.room_id': 1, | |
| 'event.type': 1, | |
| }).catch((err) => console.error('Failed to create event index (room_id, type):', err)); | |
| } |
| ) { | |
| this.collection.createIndex({ | |
| 'event.room_id': 1, | |
| 'nextEventId': 1, | |
| 'event.depth': 1, | |
| 'createdAt': 1, | |
| }); | |
| this.collection.createIndex({ | |
| 'event.room_id': 1, | |
| 'event.type': 1, | |
| }); | |
| } | |
| constructor( | |
| `@inject`('EventCollection') | |
| private readonly collection: Collection<EventStore>, | |
| ) {} | |
| async ensureIndexes(): Promise<void> { | |
| await Promise.all([ | |
| this.collection.createIndex({ | |
| 'event.room_id': 1, | |
| 'nextEventId': 1, | |
| 'event.depth': 1, | |
| 'createdAt': 1, | |
| }), | |
| this.collection.createIndex({ | |
| 'event.room_id': 1, | |
| 'event.type': 1, | |
| }), | |
| ]); | |
| } |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@packages/federation-sdk/src/repositories/event.repository.ts` around lines 12
- 23, The createIndex() calls in the constructor are fire-and-forget which
causes unhandled promise rejections and no startup guarantee; remove the direct
createIndex() calls from the constructor and add an async initialization method
(e.g., ensureIndexes() or init()) on the EventRepository class that awaits each
this.collection.createIndex(...) call (or wraps them in Promise.all), surrounds
them with try/catch to log or rethrow errors, and then call await
eventRepository.ensureIndexes() from app bootstrap so indexes are created before
queries run; alternatively, if you prefer minimal change, at least append
.catch(...) to each this.collection.createIndex(...) in the class to handle
errors and avoid unhandled rejections.
FGA-6
Summary by CodeRabbit