Skip to content

Instantly share code, notes, and snippets.

@ypetya
Last active August 29, 2015 14:26
Show Gist options
  • Save ypetya/fabf131a9901d4172234 to your computer and use it in GitHub Desktop.
Save ypetya/fabf131a9901d4172234 to your computer and use it in GitHub Desktop.
CouchDB accessing prototype

CouchDB

  • Setup Cross Origin Access

    3.12.1. Enabling CORS To enable CORS support, you need to set the enable_cors = true option in the [httpd] section of local.ini, and add a [cors] section containing a origins = * setting. Note that by default, no origins are accepted; you must either use a wildcard or whitelist.

    [httpd] enable_cors = true

    [cors] origins = *

(function setupConfiguration(mainScope){
mainScope.config={
couchdb : {
port : 5984,
db : 'test',
url : ''
}
};
mainScope.config.couchdb.url =
'http://' +
mainScope.location.hostname + ':' + mainScope.config.couchdb.port + '/' + mainScope.config.couchdb.db;
})(window);
<!DOCTYPE html>
<meta charset="utf-8">
<meta http-equiv="Cache-Control" content="no-cache">
<style>
body {
margin:0;
}
.fade-out {
visibility: hidden;
max-height : 0;
opacity: 0;
transition: visibility 0s 2s, opacity 2s linear;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="config.js"></script>
<script src="main.js"></script>
</body>
(function setupConfiguration(mainScope, d3, config){
'use strict';
function CouchAPI(){
this.get = function(key) {
d3.json(urlFor(key), defaultResponseVisualizer);
};
function urlFor(key){
return config.url + '/' + key;
}
}
function logMessageArea() {
var AREA_CSS = 'couchdb-message-area';
var node = d3.select('.' + AREA_CSS);
if(node.empty()) {
node = createNode(mainScope.document.lastChild, 'div', AREA_CSS);
}
return node.node();
}
function defaultResponseVisualizer(error, json) {
var formattedJson = JSON.stringify(json, '\t', 3),
MESSAGE_CSS = 'couchdb-response',
FADEOUT_CSS = 'fade-out';
var node = d3.select('.' + MESSAGE_CSS);
if(!node.empty()) {
node.classed(MESSAGE_CSS,false)
.classed(FADEOUT_CSS,true);
}
node = createNode(logMessageArea(), 'pre', MESSAGE_CSS);
node.classed('error',!!error).text(formattedJson);
}
function createNode(parentNode, tagName,cssClass) {
var domNode = mainScope.document.createElement(tagName);
parentNode.appendChild(domNode);
return d3.select(domNode).classed(cssClass, true);
}
mainScope.couchDB = new CouchAPI();
})(window, d3, window.config.couchdb);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment