Skip to content

@ikr /couchModuleText.js
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.