Skip to content

Instantly share code, notes, and snippets.

@glynnbird
Created May 25, 2020 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save glynnbird/73fea65a12b0c420562108a80ea0c449 to your computer and use it in GitHub Desktop.
Save glynnbird/73fea65a12b0c420562108a80ea0c449 to your computer and use it in GitHub Desktop.
Find list of CouchDB partition keys in small batches
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