Skip to content

Instantly share code, notes, and snippets.

@eiri
Created March 22, 2017 11:55
Show Gist options
  • Save eiri/63b6418437ed96181d96b0c85690fd78 to your computer and use it in GitHub Desktop.
Save eiri/63b6418437ed96181d96b0c85690fd78 to your computer and use it in GitHub Desktop.
Working with local documents in CouchDB (medium post stub)

Working with local documents in CouchDB

Create

No such document

$ http :5984/koi/_local/020
{
    "error": "not_found", 
    "reason": "missing"
}

Create local document

$ http put :5984/koi/_local/020 number=12.71
{
    "id": "_local/020", 
    "ok": true, 
    "rev": "0-1"
}

Read local document

$ http :5984/koi/_local/020
{
    "_id": "_local/020", 
    "_rev": "0-1", 
    "number": "12.71"
}

Update

Update document (check out, no query param "rev" needed)

 http put :5984/koi/_local/020 number=12.71 local:=true
{
    "id": "_local/020", 
    "ok": true, 
    "rev": "0-1"
}

Re-read it (notice the same _rev)

$ http :5984/koi/_local/020
{
    "_id": "_local/020", 
    "_rev": "0-1", 
    "local": true, 
    "number": "12.71"
}

Copy local document

Copy to non-local document

$ http copy :5984/koi/_local/020 Destination:021
{
    "id": "021", 
    "ok": true, 
    "rev": "1-625185140e3e3e8edb88ce77e0357d15"
}

$ http :5984/koi/021
{
    "_id": "021", 
    "_rev": "1-625185140e3e3e8edb88ce77e0357d15", 
    "local": true, 
    "number": "12.71"
}

Copy to local document

$ http copy :5984/koi/_local/020 Destination:_local/021
{
    "id": "_local/021", 
    "ok": true, 
    "rev": "0-1"
}

$ http :5984/koi/_local/021
{
    "_id": "_local/021", 
    "_rev": "0-1", 
    "local": true, 
    "number": "12.71"
}

Delete local document

Delete local document (check out _rev)

$ http delete :5984/koi/_local/020
{
    "id": "_local/020", 
    "ok": true, 
    "rev": "0-0"
}

Confirm it is deleted for real

$ http :5984/koi/_local/020
{
    "error": "not_found", 
    "reason": "missing"
}

$ http :5984/koi/_local/020 rev==0-1
{
    "error": "not_found", 
    "reason": "missing"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment