Skip to content

Commit 8e52d81

Browse files
committed
feat(database): add indexes to verification codes and tokens
- Added TTL index for verification codes expiry. - Added unique index for verification codes email. - Added TTL index for blacklisted tokens expiry.
1 parent eb04f61 commit 8e52d81

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

lib/src/services/database_seeding_service.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'package:ht_api/src/services/mongodb_token_blacklist_service.dart';
2+
import 'package:ht_api/src/services/mongodb_verification_code_storage_service.dart';
13
import 'package:ht_shared/ht_shared.dart';
24
import 'package:logging/logging.dart';
35
import 'package:mongo_dart/mongo_dart.dart';
@@ -143,6 +145,35 @@ class DatabaseSeedingService {
143145
name: 'sources_text_index',
144146
);
145147

148+
// Indexes for the verification codes collection
149+
await _db.runCommand({
150+
'createIndexes': kVerificationCodesCollection,
151+
'indexes': [
152+
{
153+
'key': {'expiresAt': 1},
154+
'name': 'expiresAt_ttl_index',
155+
'expireAfterSeconds': 0,
156+
},
157+
{
158+
'key': {'email': 1},
159+
'name': 'email_unique_index',
160+
'unique': true,
161+
}
162+
]
163+
});
164+
165+
// Index for the token blacklist collection
166+
await _db.runCommand({
167+
'createIndexes': kBlacklistedTokensCollection,
168+
'indexes': [
169+
{
170+
'key': {'expiry': 1},
171+
'name': 'expiry_ttl_index',
172+
'expireAfterSeconds': 0,
173+
}
174+
]
175+
});
176+
146177
_log.info('Database indexes are set up correctly.');
147178
} on Exception catch (e, s) {
148179
_log.severe('Failed to create database indexes.', e, s);

0 commit comments

Comments
 (0)