-
Notifications
You must be signed in to change notification settings - Fork 0
/
resources-untag.js
72 lines (61 loc) · 2.09 KB
/
resources-untag.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env node
const request = require('request');
const user = process.argv[2];
const pass = process.argv[3];
const domain = process.argv[4] || 'localhost:5984';
const protocol = process.argv[5] || 'http';
const uri = (db) => `${protocol}://${user}:${pass}@${domain}/${db}/_all_docs?include_docs=true`;
const postUri = (db) => `${protocol}://${user}:${pass}@${domain}/${db}`;
const batchSize = 500;
const getData = (db, callback) => {
request.get({ uri: uri(db), json: true }, callback);
}
const postCallback = (db, docs, index, del) => (err, response) => {
if (err) {
console.log(err);
}
postInBatches(db, docs, index + batchSize, del);
}
const dedupeArray = (newArray, item) => {
if (newArray.indexOf(item) > -1) {
return newArray;
}
return newArray.concat(item);
};
const isBadTag = (tag) =>
[ 'subject:', 'language:', 'medium:' ].find(prefix => tag.indexOf(prefix) > -1) !== undefined
const untagResources = (err, response) => {
const resources = response.body.rows.map(({ doc }) => {
if (doc._id.indexOf('_design/') > -1) {
return;
}
if (!doc.tags) {
return doc;
}
const tags = doc.tags
.filter(tag => !isBadTag(tag))
.reduce(dedupeArray, []).map(tag => tag);
return { ...doc, tags };
}).filter(resource => resource !== undefined);
postInBatches('resources', resources, 0);
}
const deleteBadTags = (err, response) => {
const tags = response.body.rows.filter(({ doc }) =>
doc._id.indexOf('_design/') === -1 && isBadTag(doc._id)
).map(({ doc }) => doc);
postInBatches('tags', tags, 0, true);
};
const postInBatches = (db, docs, startIndex, del = false) => {
if (startIndex > docs.length) {
return;
}
console.log(`Updating ${db} ${startIndex} through ${(startIndex + batchSize - 1)}`);
const batch = docs.slice(startIndex, startIndex + batchSize);
request.post({
uri: postUri(db) + '/_bulk_docs',
body: { docs: batch.map(item => del ? { ...item, '_deleted': true } : item) },
json: true
}, postCallback(db, docs, startIndex, del));
}
getData('resources', untagResources);
getData('tags', deleteBadTags);