-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added first mechanism of export to exploitation DB
- Loading branch information
1 parent
3ec7410
commit 3672feb
Showing
8 changed files
with
210 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
lib/api/consumers/export-to-exploitation-db-consumer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import {Transaction} from 'sequelize' | ||
import {sequelize, District, CommonToponym, Address} from '../../util/sequelize.js' | ||
import mongo from '../../util/mongo.cjs' | ||
|
||
const DistrictCollection = 'districts' | ||
const CommonToponymCollection = 'common_toponyms' | ||
const AddressCollection = 'addresses' | ||
|
||
const pageSize = 100 | ||
|
||
export default async function exportToExploitationDB({data}) { | ||
const {districtID} = data | ||
console.log(`Exporting districtID ${districtID} to exploitation DB...`) | ||
// Use REPEATABLE_READ isolation level to balance data consistency and concurrency | ||
// - Ensures data consistency within each table during the transaction | ||
// - Allows concurrent reads across tables, minimizing read contention | ||
const transactionMainDB = await sequelize.transaction({ | ||
isolationLevel: Transaction.ISOLATION_LEVELS.REPEATABLE_READ | ||
}) | ||
|
||
try { | ||
const district = await District.findOne({ | ||
where: {id: districtID}, | ||
transactionMainDB, | ||
raw: true, | ||
}) | ||
|
||
if (!district) { | ||
throw new Error(`District with ID ${districtID} not found.`) | ||
} | ||
|
||
// District | ||
// Delete all data related to the district | ||
await deleteAllDataRelatedToDistrict(districtID) | ||
|
||
// Insert the district | ||
await mongo.db.collection(DistrictCollection).insertOne(district) | ||
|
||
// CommonToponym | ||
const totalCommonToponymRecords = await CommonToponym.count({ | ||
where: {districtID}, | ||
transactionMainDB, | ||
}) | ||
const totalCommonToponymPages = Math.ceil(totalCommonToponymRecords / pageSize) | ||
|
||
const fetchAndExportData = async (model, collection, pageNumber) => { | ||
const offset = (pageNumber - 1) * pageSize | ||
try { | ||
const pageData = await model.findAll({ | ||
where: {districtID}, | ||
order: [['id', 'ASC']], | ||
offset, | ||
limit: pageSize, | ||
transactionMainDB, | ||
raw: true, | ||
}) | ||
// Insert the common toponyms from the related page | ||
await mongo.db.collection(collection).insertMany(pageData) | ||
} catch (error) { | ||
console.error(error) | ||
} | ||
} | ||
|
||
const commonToponymsExportPromises = [] | ||
|
||
for (let pageNumber = 1; pageNumber <= totalCommonToponymPages; pageNumber++) { | ||
commonToponymsExportPromises.push( | ||
fetchAndExportData(CommonToponym, CommonToponymCollection, pageNumber) | ||
) | ||
} | ||
|
||
await Promise.all(commonToponymsExportPromises) | ||
|
||
// Address | ||
const totalAddressRecords = await Address.count({ | ||
where: {districtID}, | ||
transactionMainDB, | ||
}) | ||
const totalAddressPages = Math.ceil(totalAddressRecords / pageSize) | ||
|
||
const addressesExportPromises = [] | ||
|
||
for (let pageNumber = 1; pageNumber <= totalAddressPages; pageNumber++) { | ||
addressesExportPromises.push( | ||
fetchAndExportData(Address, AddressCollection, pageNumber) | ||
) | ||
} | ||
|
||
await Promise.all(addressesExportPromises) | ||
|
||
await transactionMainDB.commit() | ||
console.log(`Exporting districtID ${districtID} done`) | ||
} catch (error) { | ||
await transactionMainDB.rollback() | ||
throw error | ||
} | ||
} | ||
|
||
const deleteAllDataRelatedToDistrict = async districtID => { | ||
await Promise.all([ | ||
mongo.db.collection(DistrictCollection).deleteOne({id: districtID}), | ||
mongo.db.collection(CommonToponymCollection).deleteMany({districtID}), | ||
mongo.db.collection(AddressCollection).deleteMany({districtID}) | ||
]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters