From 9385fe59e5fda833174d5bcad82f8ae9b2a958c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Wed, 6 Jul 2016 13:36:37 +0200 Subject: [PATCH] feat: add cli tool to batch add groups --- tools/batch/addGroupToEntryByKind.js | 49 ++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100755 tools/batch/addGroupToEntryByKind.js diff --git a/tools/batch/addGroupToEntryByKind.js b/tools/batch/addGroupToEntryByKind.js new file mode 100755 index 00000000..c6593f7f --- /dev/null +++ b/tools/batch/addGroupToEntryByKind.js @@ -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 to custom config file') + .option('-d, --db ', 'Database name') + .option('-k, --kind ', 'Comma-separated list of kinds') + .option('-s, --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(); +});