Skip to content

Commit 11e03f9

Browse files
authored
Merge pull request #31 from flutter-news-app-full-source-code/remove_fixture_data_seeding
refactor(database): remove collection seeding from `seedDatabase`
2 parents fe1646d + 1a03dd8 commit 11e03f9

File tree

1 file changed

+0
-91
lines changed

1 file changed

+0
-91
lines changed

lib/src/services/database_seeding_service.dart

Lines changed: 0 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -29,50 +29,6 @@ class DatabaseSeedingService {
2929
await _ensureIndexes();
3030
await _seedOverrideAdminUser();
3131

32-
await _seedCollection<Country>(
33-
collectionName: 'countries',
34-
fixtureData: countriesFixturesData,
35-
getId: (item) => item.id,
36-
toJson: (item) => item.toJson(),
37-
);
38-
await _seedCollection<Source>(
39-
collectionName: 'sources',
40-
fixtureData: sourcesFixturesData,
41-
getId: (item) => item.id,
42-
toJson: (item) => item.toJson(),
43-
);
44-
await _seedCollection<Topic>(
45-
collectionName: 'topics',
46-
fixtureData: topicsFixturesData,
47-
getId: (item) => item.id,
48-
toJson: (item) => item.toJson(),
49-
);
50-
await _seedCollection<Headline>(
51-
collectionName: 'headlines',
52-
fixtureData: headlinesFixturesData,
53-
getId: (item) => item.id,
54-
toJson: (item) => item.toJson(),
55-
);
56-
await _seedCollection<User>(
57-
collectionName: 'users',
58-
fixtureData: usersFixturesData,
59-
getId: (item) => item.id,
60-
toJson: (item) => item.toJson(),
61-
);
62-
await _seedCollection<UserAppSettings>(
63-
collectionName: 'user_app_settings',
64-
fixtureData: userAppSettingsFixturesData,
65-
getId: (item) => item.id,
66-
toJson: (item) => item.toJson(),
67-
);
68-
69-
await _seedCollection<RemoteConfig>(
70-
collectionName: 'remote_configs',
71-
fixtureData: remoteConfigsFixturesData,
72-
getId: (item) => item.id,
73-
toJson: (item) => item.toJson(),
74-
);
75-
7632
_log.info('Database seeding process completed.');
7733
}
7834

@@ -190,53 +146,6 @@ class DatabaseSeedingService {
190146
);
191147
}
192148

193-
/// Seeds a specific collection from a given list of fixture data.
194-
Future<void> _seedCollection<T>({
195-
required String collectionName,
196-
required List<T> fixtureData,
197-
required String Function(T) getId,
198-
required Map<String, dynamic> Function(T) toJson,
199-
}) async {
200-
_log.info('Seeding collection: "$collectionName"...');
201-
try {
202-
if (fixtureData.isEmpty) {
203-
_log.info('No documents to seed for "$collectionName".');
204-
return;
205-
}
206-
207-
final collection = _db.collection(collectionName);
208-
final operations = <Map<String, Object>>[];
209-
210-
for (final item in fixtureData) {
211-
// Use the predefined hex string ID from the fixture to create a
212-
// deterministic ObjectId. This is crucial for maintaining relationships
213-
// between documents (e.g., a headline and its source).
214-
final objectId = ObjectId.fromHexString(getId(item));
215-
final document = toJson(item)..remove('id');
216-
217-
operations.add({
218-
// Use updateOne with $set to be less destructive than replaceOne.
219-
'updateOne': {
220-
// Filter by the specific, deterministic _id.
221-
'filter': {'_id': objectId},
222-
// Set the fields of the document.
223-
'update': {r'$set': document},
224-
'upsert': true,
225-
},
226-
});
227-
}
228-
229-
final result = await collection.bulkWrite(operations);
230-
_log.info(
231-
'Seeding for "$collectionName" complete. '
232-
'Upserted: ${result.nUpserted}, Modified: ${result.nModified}.',
233-
);
234-
} on Exception catch (e, s) {
235-
_log.severe('Failed to seed collection "$collectionName".', e, s);
236-
rethrow;
237-
}
238-
}
239-
240149
/// Ensures that the necessary indexes exist on the collections.
241150
///
242151
/// This method is idempotent; it will only create indexes if they do not

0 commit comments

Comments
 (0)