-
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.
Merge pull request #251 from BaseAdresseNationale/antoineludeau/expor…
…t-to-exploitation-db First mechanism of export from internal DB (postgres) to exploitation DB (mongo)
- Loading branch information
Showing
8 changed files
with
206 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
101 changes: 101 additions & 0 deletions
101
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,101 @@ | ||
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 transaction = await sequelize.transaction({ | ||
isolationLevel: Transaction.ISOLATION_LEVELS.REPEATABLE_READ | ||
}) | ||
|
||
try { | ||
const district = await District.findOne({ | ||
where: {id: districtID}, | ||
transaction, | ||
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}, | ||
transaction, | ||
}) | ||
const totalCommonToponymPages = Math.ceil(totalCommonToponymRecords / pageSize) | ||
|
||
const fetchAndExportDataFromPage = async (model, collection, pageNumber) => { | ||
const offset = (pageNumber - 1) * pageSize | ||
const pageData = await model.findAll({ | ||
where: {districtID}, | ||
order: [['id', 'ASC']], | ||
offset, | ||
limit: pageSize, | ||
transaction, | ||
raw: true, | ||
}) | ||
// Insert the common toponyms from the related page | ||
await mongo.db.collection(collection).insertMany(pageData, {ordered: false}) | ||
} | ||
|
||
const commonToponymsExportPromises = [] | ||
|
||
for (let pageNumber = 1; pageNumber <= totalCommonToponymPages; pageNumber++) { | ||
commonToponymsExportPromises.push( | ||
fetchAndExportDataFromPage(CommonToponym, CommonToponymCollection, pageNumber) | ||
) | ||
} | ||
|
||
await Promise.all(commonToponymsExportPromises) | ||
|
||
// Address | ||
const totalAddressRecords = await Address.count({ | ||
where: {districtID}, | ||
transaction, | ||
}) | ||
const totalAddressPages = Math.ceil(totalAddressRecords / pageSize) | ||
|
||
const addressesExportPromises = [] | ||
|
||
for (let pageNumber = 1; pageNumber <= totalAddressPages; pageNumber++) { | ||
addressesExportPromises.push( | ||
fetchAndExportDataFromPage(Address, AddressCollection, pageNumber) | ||
) | ||
} | ||
|
||
await Promise.all(addressesExportPromises) | ||
|
||
await transaction.commit() | ||
console.log(`Exporting districtID ${districtID} done`) | ||
} catch (error) { | ||
await transaction.rollback() | ||
console.error(`Exporting districtID ${districtID} failed: ${error.message}`) | ||
} | ||
} | ||
|
||
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