Last active
June 7, 2016 13:33
-
-
Save nolanlawson/b88f46d7cbaef8d93cba to your computer and use it in GitHub Desktop.
auto_compaction
This file contains 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
<html> | |
<body> | |
<pre id="display"></pre> | |
<script src="//cdn.jsdelivr.net/pouchdb/latest/pouchdb.min.js"></script> | |
<script src="index.js"></script> | |
</body> | |
</html> |
This file contains 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
// Destroy the database before doing anything, because I want | |
// you to see the same thing if you reload. | |
// Ignore the man behind the curtain! | |
new PouchDB('sample').destroy().then(function () { | |
return new PouchDB('kittens', {auto_compaction: true}); | |
}).then(function (db) { | |
function log(str) { | |
var display = document.getElementById('display'); | |
display.innerHTML = display.innerHTML || ''; | |
display.innerHTML += '\n' + str; | |
} | |
// | |
// IMPORTANT CODE STARTS HERE | |
// | |
function printRevisions() { | |
return db.get('foo', {revs: true}).then(function (doc) { | |
// convert revision IDs into full _rev hashes | |
var start = doc._revisions.start; | |
return Promise.all(doc._revisions.ids.map(function (id, i) { | |
var rev = (start - i) + '-' + id; | |
return db.get('foo', {rev: rev}).then(function (doc) { | |
return [rev, doc]; | |
}).catch(function (err) { | |
if (err.status !== 404) { | |
throw err; | |
} | |
return [rev, err]; | |
}); | |
})).then(function (revsAndDocs) { | |
revsAndDocs.forEach(function (revAndDoc) { | |
log('Fetched revision "' + revAndDoc[0] +'": '+ JSON.stringify(revAndDoc[1])); | |
}); | |
}); | |
}); | |
} | |
db.put({_id: 'foo', version: 1}).then(function () { | |
log('After putting version 1:') | |
return printRevisions(); | |
}).then(function () { | |
return db.get('foo'); | |
}).then(function (doc) { | |
doc.version = 2; | |
return db.put(doc); | |
}).then(function () { | |
// Revision 1 is already unavailable! | |
log('\nAfter putting version 2:') | |
return printRevisions(); | |
}).catch(function (err) { | |
// handle errors | |
log(err); | |
}); | |
// | |
// IMPORTANT CODE ENDS HERE | |
// | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment