Skip to content

Instantly share code, notes, and snippets.

@mrjones-plip
Created May 16, 2022 23:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrjones-plip/e2f48923a83fa7b54e21ba9e36d39db6 to your computer and use it in GitHub Desktop.
Save mrjones-plip/e2f48923a83fa7b54e21ba9e36d39db6 to your computer and use it in GitHub Desktop.
Script for bulk adding CHT contacts with ever so slightly different attachments ;)
/*
To use this script:
- Save this script into a directory
- Include a PNG image in the directory called SamplePNG_1.png
- From within that directory, install nano: `npm install nano`
- Update the constants below to match your desired values
- Run the script: 'node couch.js`
*/
const NUMBER_OF_DOCS = 100;
const STARTING_INDEX = 2200; // Update this on subsequent runs to avoid id conflicts when running the script multiple times
const HOUSEHOLD_UUID = 'c7240325-92f3-41af-9cb6-3bd43a7cc69e';
const AREA_UUID = '05667689-076a-4577-96e4-03e40eb8ec30';
const DISTRICT_UUID = '1759d5b7-1a74-47f6-8734-53f291865e3d';
const DB_CONNECTION_INFO = 'https://medic:HgQyNXacNL9zQBpxbWcB@gamma-cht.dev.medicmobile.org';
const fs = require('fs');
const path = require('path');
const nano = require('nano')({ url: DB_CONNECTION_INFO });
const medic = nano.db.use('medic');
const { execSync } = require("child_process");
const sleep = (ms) => {
return new Promise(resolve => setTimeout(resolve, ms));
};
const insertDocuments = async() => {
for(let docCount = STARTING_INDEX; docCount < STARTING_INDEX + NUMBER_OF_DOCS; docCount++) {
const doc = {
parent: {
_id: HOUSEHOLD_UUID,
parent: {
_id: AREA_UUID,
parent: {
_id: DISTRICT_UUID
}
}
},
type: 'person',
name: `Big ${docCount}`,
role: 'patient',
meta: {
created_by: 'medic',
},
reported_date: 1652278507739,
};
const result = await medic.insert(doc, `big-person-${docCount}`);
console.log(result);
}
await sleep(5000); // Try to avoid update conflicts with Sentinel processing for the new contacts...
for(let docCount = STARTING_INDEX; docCount < STARTING_INDEX + NUMBER_OF_DOCS; docCount++) {
execSync("echo 'mrjones wuz here' >> SamplePNG_1.png");
let attachment = {
name: 'img_1.png',
data: fs.readFileSync(path.join(__dirname, 'SamplePNG_1.png')),
content_type: 'image/png'
};
const insertedDoc = await medic.get(`big-person-${docCount}`)
const attResult = await medic.attachment.insert(insertedDoc._id, attachment.name, attachment.data, attachment.content_type, { rev: insertedDoc._rev });
console.log(attResult);
}
};
const destroyDocuments = async() => {
for(let docCount = STARTING_INDEX; docCount < STARTING_INDEX + NUMBER_OF_DOCS; docCount++) {
const doc = await medic.get(`big-person-${docCount}`);
const result = await medic.destroy(doc._id, doc._rev);
console.log(result);
}
};
// CHOOSE WHICH FUNCTION TO CALL!
(async function () {
// Use this for adding documents
await insertDocuments();
// Use this for removing documents
// await destroyDocuments();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment