* Helper function to update the corresponding index collection. * Performs an upsert for additions/renames and a delete for deletions/renames. * @param db MongoDB Db instance. * @param filepath Path of the file being processed (used to derive index details). * @param operationType 'upsert' or 'd
( db: Db, filepath: string, operationType: 'upsert' | 'delete' )
| 159 | * @param operationType 'upsert' or 'delete'. |
| 160 | */ |
| 161 | async function updateIndexCollection( |
| 162 | db: Db, |
| 163 | filepath: string, |
| 164 | operationType: 'upsert' | 'delete' |
| 165 | ): Promise<void> { |
| 166 | const filename = filepath.split('/').pop(); |
| 167 | |
| 168 | if (!filename) { |
| 169 | console.warn(`Could not extract filename from ${filepath}. Skipping index update.`); |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | const indexName = getIndexName(filename); |
| 174 | if (!indexName) { |
| 175 | // This is expected for files not matching the SRD pattern, like the index collection itself. |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | const locale = getLocaleFromFilepath(filepath); |
| 180 | if (locale && locale !== 'en') { |
| 181 | // Translation files share the same index name as their English counterpart — never touch the |
| 182 | // collections index for them, or a deletion would remove the English entry. |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | const collectionPrefix = getCollectionPrefix(filepath); |
| 187 | const indexCollectionName = getIndexCollectionName(collectionPrefix); |
| 188 | const indexCollection = db.collection(indexCollectionName); |
| 189 | |
| 190 | try { |
| 191 | if (operationType === 'upsert') { |
| 192 | console.log(`Upserting index '${indexName}' into collection '${indexCollectionName}'...`); |
| 193 | await indexCollection.updateOne( |
| 194 | { index: indexName }, |
| 195 | { $set: { index: indexName } }, // Simple doc, just the index name |
| 196 | { upsert: true } |
| 197 | ); |
| 198 | } else if (operationType === 'delete') { |
| 199 | console.log(`Deleting index '${indexName}' from collection '${indexCollectionName}'...`); |
| 200 | await indexCollection.deleteOne({ index: indexName }); |
| 201 | } |
| 202 | } catch (error) { |
| 203 | console.error( |
| 204 | `Error performing ${operationType} for index '${indexName}' in collection '${indexCollectionName}':`, |
| 205 | error |
| 206 | ); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | // --- Status-Specific Handlers --- |
| 211 |
no test coverage detected