Skip to content

Instantly share code, notes, and snippets.

@ikr
Last active December 16, 2015 15:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ikr/5458801 to your computer and use it in GitHub Desktop.
Save ikr/5458801 to your computer and use it in GitHub Desktop.
Test-drive a self-contained node module (viewUtils.js here), and then use it as a CouchDB CommonJS module. Sample schema installation and update scripts included.
(function (undefined) {
"use strict";
var _ = require("underscore");
module.exports = function (nodeModule, dependencyNameToPathMap) {
return _.map(dependencyNameToPathMap, function (path, name) {
return ["var ", name, " = require(\"", path, "\");"].join("");
}).concat(
_.isFunction(nodeModule) ?
["module.exports = ", nodeModule.toString(), ";"].join("") :
_.map(nodeModule, function (method, methodName) {
return ["exports.", methodName, " = ", method.toString(), ";"].join("");
})
).join("\n");
};
}());
(function (undefined) {
"use strict";
var couchModuleText = require("./couchModuleText"),
viewUtils = require("./viewUtils"),
viewMapWhatever = function (doc) {
emit(
require("views/lib/utils").bar(doc._id),
doc
);
};
module.exports = {
views: {
lib: { utils: couchModuleText(viewUtils) },
whatever: { map: viewMapWhatever }
}
};
}());
(function (undefined) {
"use strict";
var cradle = require("cradle"),
async = require("async"),
mainSchema = require("./couchSchemaExample"),
db = new (cradle.Connection)().database("awesome");
async.waterfall([
function (callback) {
db.exists(callback);
},
function (exists, callback) {
if (exists) {
callback({error: "DB already exists. Won't touch."});
}
else {
callback(undefined);
}
},
function (callback) {
db.create(callback);
},
function (creationResult, callback) {
db.save("_design/main", mainSchema, callback);
}
], function (err, res) {
console.info(err || res);
});
}());
(function (undefined) {
"use strict";
var cradle = require("cradle"),
db = new (cradle.Connection)().database("awesome"),
async = require("async"),
mainSchema = require("./couchSchemaExample"),
updateDesignDoc = function (id, newDocVersion, callback) {
async.waterfall([
function (callback) {
db.get(id, callback);
},
function (designDoc, callback) {
db.save(id, designDoc._rev, newDocVersion, callback);
}
], callback);
};
updateDesignDoc("_design/main", mainSchema, function (err, res) {
console.info(err || res);
});
}());
(function (undefined) {
"use strict";
exports.foo = function (x) {
return exports.bar(x);
};
exports.bar = function (x) {
return "Bar for " + x;
};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment