Skip to content

Instantly share code, notes, and snippets.

@nono
Last active March 16, 2018 15:15
Show Gist options
  • Save nono/42aee18de6314a621f9126f284e303bb to your computer and use it in GitHub Desktop.
Save nono/42aee18de6314a621f9126f284e303bb to your computer and use it in GitHub Desktop.
Playing with CouchDB revisions
~ ❯ curl 'http://localhost:5984/aaa/bbb?revs_info=true'
{"_id":"bbb","_rev":"4-753875d51501a6b1883a9d62b4d33f91","foo":4,"_revs_info":[{"rev":"4-753875d51501a6b1883a9d62b4d33f91","status":"available"},{"rev":"3-efc54218773c6acd910e2e97fea2a608","status":"available"},{"rev":"2-2ee767305024673cfb3f5af037cd2729","status":"available"},{"rev":"1-4a7e4ae49c4366eaed8edeaea8f784ad","status":"available"}]}
~ ❯ curl -X POST 'http://localhost:5984/aaa/_revs_diff' -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"bbb": ["1-4a7e4ae49c4366eaed8edeaea8f784ad", "2-2ee767305024673cfb3f5af037cd2729"]}'
{}
~ ❯ curl -X POST 'http://localhost:5984/aaa/_revs_diff' -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"bbb": ["1-4a7e4ae49c4366eaed8edeaea8f784ad", "3-efc54218773c6acd910e2e97fea2a608", "4-ccc", "5-ddd"]}'
{"bbb":{"missing":["4-ccc","5-ddd"],"possible_ancestors":["4-753875d51501a6b1883a9d62b4d33f91"]}}
~ ❯ curl -X POST 'http://localhost:5984/aaa/_revs_diff' -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"bbb": ["1-4a7e4ae49c4366eaed8edeaea8f784ad", "3-efc54218773c6acd910e2e97fea2a608", "5-ddd", "6-eee"]}'
{"bbb":{"missing":["5-ddd","6-eee"],"possible_ancestors":["4-753875d51501a6b1883a9d62b4d33f91"]}}
~ ❯ curl -X POST 'http://localhost:5984/aaa/_bulk_docs' -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"new_edits": false, "docs": [{ "_id": "bbb", "foo": 6, "_rev": "6-eee", "_revisions": {"start": 6, "ids": ["eee", "ddd", "753875d51501a6b1883a9d62b4d33f91", "efc54218773c6acd910e2e97fea2a608", "2ee767305024673cfb3f5af037cd2729", "4a7e4ae49c4366eaed8edeaea8f784ad"] } }]}'
[]
var PouchDB = require('pouchdb');
var db = new PouchDB('aaa');
PouchDB.replicate('http://localhost:5984/aaa', db)
db.put({ _id: "bbb", foo: 7, _rev: '6-eee' }).then(console.log)
# { ok: true, id: 'bbb', rev: '7-78deda4625a54381abde41b01ac52f29' }
db.put({ _id: "bbb", foo: 8, _rev: '7-78deda4625a54381abde41b01ac52f29' })
PouchDB.replicate(db, 'http://localhost:5984/aaa')
~/tmp/pouch ❯ curl -s "http://localhost:5984/aaa/bbb?meta=true" | jq .
{
"_id": "bbb",
"_rev": "8-c6027a48b9ca47af8f05c38d586729bc",
"foo": 8,
"_revs_info": [
{
"rev": "8-c6027a48b9ca47af8f05c38d586729bc",
"status": "available"
},
{
"rev": "7-78deda4625a54381abde41b01ac52f29",
"status": "missing"
},
{
"rev": "6-eee",
"status": "available"
},
{
"rev": "5-ddd",
"status": "missing"
},
{
"rev": "4-753875d51501a6b1883a9d62b4d33f91",
"status": "available"
},
{
"rev": "3-efc54218773c6acd910e2e97fea2a608",
"status": "available"
},
{
"rev": "2-2ee767305024673cfb3f5af037cd2729",
"status": "available"
},
{
"rev": "1-4a7e4ae49c4366eaed8edeaea8f784ad",
"status": "available"
}
]
}
~/tmp/pouch ❯ curl -X POST 'http://localhost:5984/aaa/_revs_diff' -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"bbb": ["9-fff"]}'
{"bbb":{"missing":["9-fff"],"possible_ancestors":["8-c6027a48b9ca47af8f05c38d586729bc"]}}
~/tmp/pouch ❯ curl -X POST 'http://localhost:5984/aaa/_bulk_docs' -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"new_edits": false, "docs": [{ "_id": "bbb", "foo": 9, "_rev": "9-fff", "_revisions": {"start": 9, "ids": ["fff", "c6027a48b9ca47af8f05c38d586729bc"] } }]}'
[]
~/tmp/pouch ❯ curl -s "http://localhost:5984/aaa/bbb?meta=true" | jq .
{
"_id": "bbb",
"_rev": "9-fff",
"foo": 9,
"_revs_info": [
{
"rev": "9-fff",
"status": "available"
},
{
"rev": "8-c6027a48b9ca47af8f05c38d586729bc",
"status": "available"
},
{
"rev": "7-78deda4625a54381abde41b01ac52f29",
"status": "missing"
},
{
"rev": "6-eee",
"status": "available"
},
{
"rev": "5-ddd",
"status": "missing"
},
{
"rev": "4-753875d51501a6b1883a9d62b4d33f91",
"status": "available"
},
{
"rev": "3-efc54218773c6acd910e2e97fea2a608",
"status": "available"
},
{
"rev": "2-2ee767305024673cfb3f5af037cd2729",
"status": "available"
},
{
"rev": "1-4a7e4ae49c4366eaed8edeaea8f784ad",
"status": "available"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment