3

Cloudant offers a hosted CouchDB, with a free starter level allowing 6GB of IO per month. Good for developers learning CouchDB.

Since CouchDB allows specification of the map/reduce functions in Javascript, it might make sense to connect to it via Javascript, running in Classic ASP.

Possible?

Cheeso
  • 189,189
  • 101
  • 473
  • 713

1 Answers1

2

Yes, why not?

Cloudant is accessible via HTTP/REST. Nothing special there.

ASP Classic / Javascript can use MSXML2.ServerXMLHttp to send out requests, in much the same way that XMLHttpRequest can be used on client-side Javascript.

What would be nice is a Javascript library for CouchDB that does not assume it is running in a browser, nor in Node, since ASP Classic is neither of those. Here's a start:

https://gist.github.com/3016476

Example ASP code:

    var creds = getCloudantCredentials("cloudantCreds.txt");
    var couch = new CouchDB(couchUrl);
    couch.connect(creds[0],creds[1]);
    var r = couch.listDbs();
    say("all dbs: " + JSON.stringify(r, null, 2));

    r = couch.view('dbname', 'baseViews', 'bywords',
                   { include_docs: false,
                     key: "whatever",
                     reduce:true} );
    say("view: " + JSON.stringify(r, null, 2));

This is how you might create a set of views:

function createViews(dbName, viewSet) {
    var r, doc,
        empty = function(doc) {
            if ( ! doc.observation || doc.observation === '') {
                emit(null, doc);
            }
        },
        bywordsMap = function(doc) {
            var tokens, re1,
                uniq = function(a) {
                    var o = {}, i = 0, L = a.length, r = [];
                    for (; i < L; i++) {
                        if (a[i] !== '' && a[i] !== ' ') {
                            o[a[i]] = a[i];
                        }
                    }
                    for (i in o) { r.push(o[i]); }
                    return r;
                };

            if ( doc.observation && doc.observation !== '') {
                tokens = uniq(doc.observation.split(/( +)|\./));
                if (tokens && tokens.length > 0) {
                    tokens.map(function(token) {
                        emit(token, 1);
                    });
                }
            }
        };

    viewSet = viewSet || 'baseViews';
    try {
        r = couch.deleteView(dbName, viewSet);
        doc = { views: { empty:   { map:stringRep(empty) },
                         bywords: { map:stringRep(bywordsMap)}}};
        r = couch.createView(dbName, viewSet, doc);
    }
    catch (exc1) {
        say ('createViews() failed: ' + JSON.stringify(exc1));
    }
}


function stringRep(fn) {
    return fn.toString()
        .replace(/[\s\t]*\/\/.*$/gm, '') // comments
        .replace(/\n */gm, ' ')
        .replace(/\r */gm, ' ')
        .replace(/\{ +/gm, '{')
        .replace(/ +\}/gm, '}');
}
Cheeso
  • 189,189
  • 101
  • 473
  • 713