Last active
December 16, 2015 15:49
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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"); | |
}; | |
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 } | |
} | |
}; | |
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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); | |
}); | |
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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); | |
}); | |
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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