Created
July 18, 2019 19:11
-
-
Save travist/6e45ad8717bfdd0ea9c23d54d4809292 to your computer and use it in GitHub Desktop.
Script to add project ID's to submissions
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
'use strict'; | |
/** | |
* Adds project IDs to all submissions. | |
* | |
* Run this using | |
* npm install | |
* node addproject.js --NODE_CONFIG='{"mongo": "mongodb://localhost:27017/formio"}' | |
* | |
* @param db | |
* @param config | |
* @param tools | |
* @param done | |
*/ | |
const config = require('config'); | |
const async = require('async'); | |
const mongodb = require('mongodb'); | |
const _ = require('lodash'); | |
const MongoClient = mongodb.MongoClient; | |
const ObjectID = require('mongodb').ObjectID; | |
function idToBson(_id) { | |
try { | |
_id = _.isObject(_id) | |
? _id | |
: new ObjectID(_id); | |
} | |
catch (e) { | |
console.log(`Unknown _id given: ${_id}, typeof: ${typeof _id}`); | |
_id = false; | |
} | |
return _id; | |
} | |
MongoClient.connect(config.mongo, (err, client) => { | |
if (err) { | |
/* eslint-disable no-console */ | |
return console.log(`Could not connect to database ${config.mongo}`); | |
/* eslint-enable no-console */ | |
} | |
let count = 0; | |
const db = client.db(client.s.options.dbName); | |
console.log('Updating form submissions to add project IDs.'); | |
const submissionsCollection = db.collection('submissions'); | |
const formsCollection = db.collection('forms'); | |
const formsCursor = formsCollection.find({}); | |
const getNext = function(cursor, next) { | |
cursor.hasNext().then((hasNext) => { | |
if (hasNext) { | |
count++; | |
return cursor.next().then(next); | |
} | |
else { | |
return next(); | |
} | |
}); | |
}; | |
let hasNext = true; | |
async.doWhilst((next) => { | |
getNext(formsCursor, (form) => { | |
if (!form) { | |
hasNext = false; | |
return next(); | |
} | |
submissionsCollection.update({ | |
form: form._id | |
}, { | |
$set: {project: form.project} | |
}, {multi:true}, (err) => { | |
if (err) { | |
return next(err); | |
} | |
if ((count % 1000) === 0) { | |
process.stdout.write('.'); | |
} | |
if ((count % 50000) === 0) { | |
process.stdout.write("\n"); | |
} | |
return next(); | |
}); | |
}); | |
}, () => { | |
return hasNext; | |
}, (err) => { | |
if (err) { | |
return console.log(err); | |
} | |
return console.log('DONE!'); | |
}); | |
}); |
You can then run using the following.
node addproject.js --NODE_CONFIG='{"mongo": "mongodb://localhost:27017/formio"}'
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To install, you will need to do the following before running.