Skip to content

Commit 2f82e09

Browse files
committed
feat(database): add rate limit attempts TTL and key indexes
- Add TTL index for automatic document expiration in rate limit attempts collection - Add key index for faster lookups in rate limit attempts collection - Implement indexing in the DatabaseSeedingService
1 parent 6efac46 commit 2f82e09

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

lib/src/services/database_seeding_service.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:core/core.dart';
22
import 'package:flutter_news_app_api_server_full_source_code/src/config/environment_config.dart';
3+
import 'package:flutter_news_app_api_server_full_source_code/src/services/mongodb_rate_limit_service.dart';
34
import 'package:flutter_news_app_api_server_full_source_code/src/services/mongodb_token_blacklist_service.dart';
45
import 'package:flutter_news_app_api_server_full_source_code/src/services/mongodb_verification_code_storage_service.dart';
56
import 'package:logging/logging.dart';
@@ -300,6 +301,25 @@ class DatabaseSeedingService {
300301
],
301302
});
302303

304+
// Index for the rate limit attempts collection
305+
await _db.runCommand({
306+
'createIndexes': kRateLimitAttemptsCollection,
307+
'indexes': [
308+
{
309+
// This is a TTL index. MongoDB will automatically delete request
310+
// attempt documents 24 hours after they are created.
311+
'key': {'createdAt': 1},
312+
'name': 'createdAt_ttl_index',
313+
'expireAfterSeconds': 86400, // 24 hours
314+
},
315+
{
316+
// Index on the key field for faster lookups.
317+
'key': {'key': 1},
318+
'name': 'key_index',
319+
},
320+
],
321+
});
322+
303323
_log.info('Database indexes are set up correctly.');
304324
} on Exception catch (e, s) {
305325
_log.severe('Failed to create database indexes.', e, s);

0 commit comments

Comments
 (0)