import '../loadenv';
import '../utils/db';

import logger from '../utils/logger';
import Podcast from '../models/podcast';
import RSS from '../models/rss';
import {indexMany} from '../utils/search';

async function main() {
	logger.info('Reindexing all Podcasts to Algolia');
	await loadModel(Podcast);
	logger.info('Reindexing all RSS feeds to Algolia');
	await loadModel(RSS);
}

async function loadModel(Model) {
	const batchSize = 200;
	let indexed = 0;
	let accumulatedDocs = [];
	await Model.find().cursor().eachAsync(async (d) => {
		accumulatedDocs.push(d);
		if (accumulatedDocs.length >= batchSize) {
			await indexMany(accumulatedDocs);
			indexed += accumulatedDocs.length;
			process.stdout.clearLine();
			process.stdout.cursorTo(0);
			process.stdout.write(`Indexed: ${indexed}`);
			accumulatedDocs = [];
		}
	});
	if (accumulatedDocs.length >= 0) {
		await indexMany(accumulatedDocs);
		indexed += accumulatedDocs.length;
		process.stdout.clearLine();
		process.stdout.cursorTo(0);
		process.stdout.write(`Indexed: ${indexed}`);
		accumulatedDocs = [];
	}
	process.stdout.write('\n');
}

main().then(() => {
	console.info('done');
	process.exit(0);
}).catch(err => {
	console.info(`failed with err ${err}`);
	process.exit(1);
});

@@ -483,25 +483,132 @@
 %5D;%0A%09
-await Model.find(
+// XXX: enter Mongoose genius: %7B timeout: true %7D means disabling cursor timeouts%0A%09await Model.find(%7B%7D, %7B%7D, %7B timeout: true %7D
 ).cu
@@ -663,16 +663,33 @@
 s.push(d
+.searchDocument()
 );%0A%09%09if 

disable cursor timeouts and make sure we cast model as index doc