Skip to content

Instantly share code, notes, and snippets.

@nolanlawson
Created June 5, 2014 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nolanlawson/a40efe5aa730383733bb to your computer and use it in GitHub Desktop.
Save nolanlawson/a40efe5aa730383733bb to your computer and use it in GitHub Desktop.
<html>
<body>
<script src="//cdn.jsdelivr.net/pouchdb/2.2.3/pouchdb.js"></script>
<script src="index.js"></script>
</body>
</html>
var db = new PouchDB('pixelpark_dev',{
auto_compaction: true,
cache: false
});
var remoteCouch = 'http://couchdb.simple-url.com:5984/pixelpark_dev';
db.replicate.from(remoteCouch, {
live: true
});
var info = function () {
db.info(function (err, info) {
console.log('DATABASE INFO', info);
});
}
info();
// Create design doc for indexed query
var postsOnly = {
_id: '_design/posts_only',
views: {
'posts_only': {
map: function (doc) {
if (doc.type === 'POST' || doc.type === 'IMAGE') {
emit([doc.created, doc._id, doc.user.id], doc.created);
}
}.toString()
}
}
};
db.put(postsOnly).then(function () {
return db.query('posts_only', {
stale: 'update_after'
});
});
// Query the database for posts
db.query('posts_only').then(function (res) {
console.log('QUERY', res);
});
// Fetch all latest changes
var changes = db.changes({
live: true,
since: 'latest',
complete: function (err, changes) {
console.log(err, changes);
info();
},
onChange: function (change) {
console.log('Change', change);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment