Skip to content

feat(database): add index creation to database seeding process #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions lib/src/services/database_seeding_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class DatabaseSeedingService {
Future<void> seedInitialData() async {
_log.info('Starting database seeding process...');

await _ensureIndexes();

await _seedCollection<Country>(
collectionName: 'countries',
fixtureData: countriesFixturesData,
Expand Down Expand Up @@ -115,4 +117,38 @@ class DatabaseSeedingService {
rethrow;
}
}

/// Ensures that the necessary indexes exist on the collections.
///
/// This method is idempotent; it will only create indexes if they do not
/// already exist. It's crucial for enabling efficient text searches.
Future<void> _ensureIndexes() async {
_log.info('Ensuring database indexes exist...');
try {
// Text index for searching headlines by title
await _db.collection('headlines').createIndex(
keys: {'title': 'text'},
name: 'headlines_text_index',
);

// Text index for searching topics by name
await _db.collection('topics').createIndex(
keys: {'name': 'text'},
name: 'topics_text_index',
);

// Text index for searching sources by name
await _db.collection('sources').createIndex(
keys: {'name': 'text'},
name: 'sources_text_index',
);
Comment on lines +128 to +144

Choose a reason for hiding this comment

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

medium

This block can be improved for both efficiency and maintainability:

  • Efficiency: The three createIndex operations are independent and can be executed in parallel using Future.wait. This will make the seeding process faster, which is beneficial during application startup.

  • Maintainability: The code uses several hardcoded strings for collection names, field names, and index names (e.g., 'headlines', 'title'). This is a "magic string" anti-pattern that can lead to typos and make the code harder to maintain. It's a best practice in Dart to define these as static const variables.

The suggestion below addresses the efficiency point. I strongly recommend also refactoring the string literals into constants for better code quality.

      // Run index creation in parallel for efficiency.
      await Future.wait([
        _db.collection('headlines').createIndex(
          keys: {'title': 'text'},
          name: 'headlines_text_index',
        ),
        _db.collection('topics').createIndex(
          keys: {'name': 'text'},
          name: 'topics_text_index',
        ),
        _db.collection('sources').createIndex(
          keys: {'name': 'text'},
          name: 'sources_text_index',
        ),
      ]);


_log.info('Database indexes are set up correctly.');
} on Exception catch (e, s) {
_log.severe('Failed to create database indexes.', e, s);
// We rethrow here because if indexes can't be created,
// critical features like search will fail.
rethrow;
}
}
}
Loading