Created
May 25, 2020 07:19
-
-
Save glynnbird/73fea65a12b0c420562108a80ea0c449 to your computer and use it in GitHub Desktop.
Find list of CouchDB partition keys in small batches
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
const nano = require('Nano')(process.env.COUCH_URL) | |
const db = nano.db.use('pq') | |
// get list of partition keys | |
// Parameters: | |
// - startPartition - the last partition key you know about | |
// - limit - number of partition keys to getch | |
// Returns: | |
// - an array of partition keys | |
const getPartitions = async (startPartition = '', limit = 10) => { | |
const partitions = [] | |
let currentPartition = startPartition | |
// one API call per partition key required | |
for (let i = 0; i < limit; i++) { | |
// call GET /db/_all_docs?startkey="C:\uffff"&limit=1 | |
// to get first document of next partition | |
const startkey = currentPartition + ':\uffff' | |
const response = await db.list({ | |
startkey: startkey, | |
limit: 1 | |
}) | |
// if no results, we're done | |
if (response.rows.length === 0) { | |
break | |
} | |
// extract partition key and use this as seed for next API call | |
const p = response.rows[0].id.split(':')[0] | |
partitions.push(p) | |
currentPartition = p | |
} | |
return partitions | |
} | |
getPartitions('T',10).then(console.log) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment