overleaf/services/web/scripts/helpers/batchedUpdate.js
Jakob Ackermann 403332d80d Merge pull request #3226 from overleaf/jpa-mongodb-native-scripts
[misc] migrate active scripts to the native mongo driver

GitOrigin-RevId: f3b441bc53754dc2f83ca24c3e57d2cc978dd7fe
2020-10-09 02:05:19 +00:00

71 lines
1.8 KiB
JavaScript

const { ReadPreference, ObjectId } = require('mongodb')
const { db, waitForDb } = require('../../app/src/infrastructure/mongodb')
const BATCH_SIZE = parseInt(process.env.BATCH_SIZE, 10) || 1000
let BATCH_LAST_ID
if (process.env.BATCH_LAST_ID) {
BATCH_LAST_ID = ObjectId(process.env.BATCH_LAST_ID)
}
async function getNextBatch(collection, query, maxId, projection) {
if (maxId) {
query['_id'] = { $gt: maxId }
}
const entries = await collection
.find(query)
.project(projection)
.sort({ _id: 1 })
.limit(BATCH_SIZE)
.setReadPreference(ReadPreference.SECONDARY)
.toArray()
return entries
}
async function performUpdate(collection, nextBatch, update) {
return collection.updateMany(
{ _id: { $in: nextBatch.map(entry => entry._id) } },
update
)
}
async function batchedUpdate(collectionName, query, update, projection) {
await waitForDb()
const collection = db[collectionName]
projection = projection || { _id: 1 }
let nextBatch
let updated = 0
let maxId = BATCH_LAST_ID
while (
(nextBatch = await getNextBatch(collection, query, maxId, projection))
.length
) {
maxId = nextBatch[nextBatch.length - 1]._id
updated += nextBatch.length
console.log(JSON.stringify(nextBatch))
if (typeof update === 'function') {
await update(collection, nextBatch)
} else {
await performUpdate(collection, nextBatch, update)
}
}
return updated
}
function batchedUpdateWithResultHandling(collection, query, update) {
batchedUpdate(collection, query, update)
.then(updated => {
console.error({ updated })
process.exit(0)
})
.catch(error => {
console.error({ error })
process.exit(1)
})
}
module.exports = {
batchedUpdate,
batchedUpdateWithResultHandling
}