-
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 b336869
Showing
7 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
102 changes: 102 additions & 0 deletions
102
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,102 @@ | ||
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 transaction = await sequelize.transaction() | ||
try { | ||
const {districtID} = data | ||
console.log(`Exporting districtID ${districtID} to exploitation DB...`) | ||
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) | ||
|
||
for (let pageNumber = 1; pageNumber <= totalCommonToponymPages; pageNumber++) { | ||
const offset = (pageNumber - 1) * pageSize | ||
|
||
try { | ||
// eslint-disable-next-line no-await-in-loop | ||
const pageData = await CommonToponym.findAll({ | ||
order: [['id', 'ASC']], | ||
offset, | ||
limit: pageSize, | ||
transaction, | ||
lock: transaction.LOCK.UPDATE, // Apply an update lock for consistency | ||
raw: true, | ||
}) | ||
// Insert the common toponyms from the related page | ||
// eslint-disable-next-line no-await-in-loop | ||
await mongo.db.collection(CommonToponymCollection).insertMany(pageData) | ||
} catch (error) { | ||
console.error(error) | ||
} | ||
} | ||
|
||
// Address | ||
const totalAddressRecords = await Address.count({ | ||
where: {districtID}, | ||
transaction, | ||
}) | ||
const totalAddressPages = Math.ceil(totalAddressRecords / pageSize) | ||
|
||
for (let pageNumber = 1; pageNumber <= totalAddressPages; pageNumber++) { | ||
const offset = (pageNumber - 1) * pageSize | ||
|
||
try { | ||
// eslint-disable-next-line no-await-in-loop | ||
const pageData = await Address.findAll({ | ||
order: [['id', 'ASC']], | ||
offset, | ||
limit: pageSize, | ||
transaction, | ||
lock: transaction.LOCK.UPDATE, // Apply an update lock for consistency | ||
raw: true, | ||
}) | ||
// Insert the addresses from the related page | ||
// eslint-disable-next-line no-await-in-loop | ||
await mongo.db.collection(AddressCollection).insertMany(pageData) | ||
} catch (error) { | ||
console.error(error) | ||
} | ||
} | ||
|
||
await transaction.commit() | ||
console.log(`Exporting districtID ${districtID} done`) | ||
} catch (error) { | ||
await transaction.rollback() | ||
console.error(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