|
| 1 | +const { MongoClient } = require('mongodb'); |
| 2 | +const { |
| 3 | + stripHtml, |
| 4 | + areaToString, |
| 5 | + updateTestimonialFeedback, |
| 6 | + updateTableRowsDescriptions, |
| 7 | +} = require('./migrate-field-type.utils'); |
| 8 | + |
| 9 | +const getCollection = async ( |
| 10 | + mongoUri = 'mongodb://localhost:27017', |
| 11 | + dbName = 'test', |
| 12 | +) => { |
| 13 | + const client = new MongoClient(mongoUri); |
| 14 | + await client.connect(); |
| 15 | + const db = client.db(dbName); |
| 16 | + const collection = db.collection('aposDocs'); |
| 17 | + return { client, collection }; |
| 18 | +}; |
| 19 | + |
| 20 | +const processBatches = async ( |
| 21 | + batches, |
| 22 | + idKey, |
| 23 | + collection, |
| 24 | + updateFn = updateTestimonialFeedback, |
| 25 | +) => { |
| 26 | + let updatedCount = 0; |
| 27 | + const allPromises = []; |
| 28 | + for (const batch of batches) { |
| 29 | + allPromises.push(...batch.map((doc) => updateFn(collection, doc, idKey))); |
| 30 | + } |
| 31 | + const results = await Promise.all(allPromises); |
| 32 | + updatedCount = results.reduce((sum, value) => sum + value, 0); |
| 33 | + return updatedCount; |
| 34 | +}; |
| 35 | + |
| 36 | +const migrateTestimonialFeedbackToString = async (mongoUri, dbName) => { |
| 37 | + const { client, collection } = await getCollection(mongoUri, dbName); |
| 38 | + try { |
| 39 | + const documents = await collection.find({ type: 'testimonials' }).toArray(); |
| 40 | + const idKey = '_id'; |
| 41 | + const batchSize = 10; |
| 42 | + const batches = []; |
| 43 | + for (let i = 0; i < documents.length; i += batchSize) { |
| 44 | + batches.push(documents.slice(i, i + batchSize)); |
| 45 | + } |
| 46 | + const updatedCount = await processBatches(batches, idKey, collection); |
| 47 | + return updatedCount; |
| 48 | + } finally { |
| 49 | + await client.close(); |
| 50 | + } |
| 51 | +}; |
| 52 | + |
| 53 | +const migrateTableDescriptions = async (mongoUri, dbName) => { |
| 54 | + const { client, collection } = await getCollection(mongoUri, dbName); |
| 55 | + try { |
| 56 | + const docs = await collection |
| 57 | + .find({ 'main.items.type': 'table' }) |
| 58 | + .toArray(); |
| 59 | + const idKey = '_id'; |
| 60 | + const batchSize = 10; |
| 61 | + const batches = []; |
| 62 | + for (let i = 0; i < docs.length; i += batchSize) { |
| 63 | + batches.push(docs.slice(i, i + batchSize)); |
| 64 | + } |
| 65 | + const updatedCount = await processBatches( |
| 66 | + batches, |
| 67 | + idKey, |
| 68 | + collection, |
| 69 | + updateTableRowsDescriptions, |
| 70 | + ); |
| 71 | + return updatedCount; |
| 72 | + } finally { |
| 73 | + await client.close(); |
| 74 | + } |
| 75 | +}; |
| 76 | + |
| 77 | +if (require.main === module) { |
| 78 | + const yargs = require('yargs/yargs'); |
| 79 | + const { hideBin } = require('yargs/helpers'); |
| 80 | + const { argv } = yargs(hideBin(process.argv)) |
| 81 | + .option('mongoUri', { |
| 82 | + describe: 'MongoDB connection URI', |
| 83 | + type: 'string', |
| 84 | + demandOption: true, |
| 85 | + }) |
| 86 | + .option('dbName', { |
| 87 | + describe: 'MongoDB database name', |
| 88 | + type: 'string', |
| 89 | + demandOption: true, |
| 90 | + }) |
| 91 | + .help() |
| 92 | + .alias('help', 'h'); |
| 93 | + |
| 94 | + const MONGODB_URI = argv.mongoUri; |
| 95 | + const DB_NAME = argv.dbName; |
| 96 | + |
| 97 | + (async () => { |
| 98 | + try { |
| 99 | + const testimonials = await migrateTestimonialFeedbackToString( |
| 100 | + MONGODB_URI, |
| 101 | + DB_NAME, |
| 102 | + ); |
| 103 | + process.stdout.write(`Updated testimonials: ${testimonials}\n`); |
| 104 | + const tables = await migrateTableDescriptions(MONGODB_URI, DB_NAME); |
| 105 | + process.stdout.write(`Updated table rows: ${tables}\n`); |
| 106 | + } catch (error) { |
| 107 | + process.stdout.write(`Migration error: ${error}\n`); |
| 108 | + throw error; |
| 109 | + } |
| 110 | + })(); |
| 111 | +} |
| 112 | + |
| 113 | +module.exports = { |
| 114 | + stripHtml, |
| 115 | + areaToString, |
| 116 | + updateTestimonialFeedback, |
| 117 | + updateTableRowsDescriptions, |
| 118 | + getCollection, |
| 119 | + processBatches, |
| 120 | + migrateTestimonialFeedbackToString, |
| 121 | + migrateTableDescriptions, |
| 122 | +}; |
0 commit comments