Created
May 16, 2022 16:30
-
-
Save jkuester/b623d57aa7be85ea9602cdabf08248aa to your computer and use it in GitHub Desktop.
Script for bulk adding CHT contacts with attachment
This file contains hidden or 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
/* | |
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 = 2; | |
const STARTING_INDEX = 1; // Update this on subsequent runs to avoid id conflicts when running the script multiple times | |
const HOUSEHOLD_UUID = '59d94a99-4780-45aa-99c0-12c160d47859'; | |
const AREA_UUID = '7df6ba53-1494-450e-8b5a-edfeec563bf9'; | |
const DISTRICT_UUID = 'a6a091d3-e002-427f-ac96-6972a4a0151d'; | |
const DB_CONNECTION_INFO = 'http://myadminuser:myadminpass@localhost:5984'; | |
const fs = require('fs'); | |
const path = require('path'); | |
const nano = require('nano')({ url: DB_CONNECTION_INFO }); | |
const medic = nano.db.use('medic'); | |
const attachment = { | |
name: 'img_1.png', | |
data: fs.readFileSync(path.join(__dirname, 'SamplePNG_1.png')), | |
content_type: 'image/png' | |
}; | |
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++) { | |
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
I made a hack to give each file uploaded a different hash by uploading a (no doubt corrupt) image file that has a few bytes added to each time.