Skip to content

Commit

Permalink
feat: add cli tool to batch add groups
Browse files Browse the repository at this point in the history
  • Loading branch information
targos committed Jul 6, 2016
1 parent 42ba417 commit 9385fe5
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions tools/batch/addGroupToEntryByKind.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/env node

'use strict';

/*
This script allows to add group(s) to entries matching a list of kinds
*/

const co = require('co');
const request = require('request-promise');
const program = require('commander');
const nanoPromise = require('../../lib/util/nanoPromise');
const getConfig = require('../../lib/config/config').getConfig;

program
.option('-c --config <path>', 'Path to custom config file')
.option('-d, --db <database>', 'Database name')
.option('-k, --kind <kind>', 'Comma-separated list of kinds')
.option('-s, --suffix <suffix>', 'Comma-separated list of suffixes')
.parse(process.argv);

if (typeof program.db !== 'string') program.missingArgument('db');
if (typeof program.kind !== 'string') program.missingArgument('kind');
if (typeof program.suffix !== 'string') program.missingArgument('suffix');

const kinds = new Set(program.kind.split(','));
const suffixes = program.suffix.split(',');

const Couch = require('../..');
const couch = Couch.get(program.db);

co(function*() {

yield couch.open();
const db = couch._db;
for (const kind of kinds) {
console.log(`treating kind ${kind}`);
const owners = suffixes.map(suffix => kind + suffix);
const body = {group: owners};
const docs = yield nanoPromise.queryView(db, 'entryByKind', {key: kind});
console.log(`${docs.length} documents match`);
for (const {id} of docs) {
yield nanoPromise.updateWithHandler(db, 'addGroupToEntry', id, body);
}
}

}).catch(console.error).then(function () {
couch.close();
});

0 comments on commit 9385fe5

Please sign in to comment.