Skip to content
Browse files

db; add help()

  • Loading branch information...
1 parent 770989d commit a97da641057e57249730797353686ae92a9a2a55 @aheckmann committed Jan 29, 2013
Showing with 66 additions and 14 deletions.
  1. +66 −14 lib/db.js
View
80 lib/db.js
@@ -38,12 +38,33 @@ function createConstructor (db) {
}
/**
+ * Logs help text for all db methods
+ */
+
+ ;(DB.prototype.help = function () {
+ var proto = this.constructor.prototype;
+ var len = 0;
+ var msgs = [];
+ Object.keys(proto).forEach(function (method) {
+ if (proto[method].help) {
+ var msg = proto[method].help(true);
+ len = Math.max(len, method.length);
+ msgs.push({ method: method, text: msg })
+ }
+ })
+ msgs.forEach(function (msg) {
+ var space = Array(len - msg.method.length +1).join(' ');
+ log("db." + msg.method + "() " + space + msg.text);
+ })
+ }).help = help("Logs help text for all db methods");
+
+ /**
* Drop this database
*
* @param {Function} [cb]
*/
- DB.prototype.drop = function (cb) {
+ ;(DB.prototype.drop = function (cb) {
var name = db.databaseName;
db.dropDatabase(cb || function (err) {
@@ -55,55 +76,75 @@ function createConstructor (db) {
log('database "%s" was dropped', name);
});
});
- }
+ }).help = help("Drops the database");
/**
* Close this database connection
*
* @param {Function} [cb]
*/
- DB.prototype.close = function (cb) {
+ ;(DB.prototype.close = function (cb) {
if ('function' != typeof cb) {
cb = handleError;
}
db.close(true, cb);
- }
+ }).help = help("Closes the database connection");
+
+ /**
+ * Open the database connection
+ *
+ * @param {Function} [cb]
+ */
+
+ ;(DB.prototype.open = function (cb) {
+ db.open(function (err) {
+ if (err) {
+ return cb
+ ? cb(err)
+ : handleError(err)
+ }
+
+ if ('function' == typeof cb) {
+ cb();
+ }
+ });
+ }).help = help("Opens the database connection")
/**
* Use a different database
*/
- DB.prototype.use = function (name) {
+ ;(DB.prototype.use = function (name) {
return create(db.db(name));
- }
+ }).help = help("Changes to a different database")
/**
* Access a collection
*/
- DB.prototype.collection = function (name, opts) {
+ ;(DB.prototype.collection = function (name, opts) {
if (this[name]) {
return this[name];
}
return this[name] = db.collection(name, opts);
- }
+ }).help = help("Accesses a collection")
/**
* console.log helper
*/
- DB.prototype.inspect = function () {
+ ;(DB.prototype.inspect = function () {
return db.databaseName;
- }
+ }).help = help("Returns the name of the database");
/**
* Refresh and return the list of collections on this database
*/
- DB.prototype.collections = function (cb) {
+ ;(DB.prototype.collections = function (cb) {
var self = this;
db.collectionNames({ namesOnly: true }, function (err, names) {
@@ -139,7 +180,7 @@ function createConstructor (db) {
if (cb) return cb(err, names);
});
- }
+ }).help = help("Retreives an array of collection names in the db")
/**
* Execute a command on the database
@@ -149,7 +190,7 @@ function createConstructor (db) {
* @param {Function} [cb]
*/
- DB.prototype.runCommand = function (cmd, opts, cb) {
+ ;(DB.prototype.runCommand = function (cmd, opts, cb) {
if ('function' == typeof opts) {
cb = opts;
opts = {};
@@ -173,7 +214,7 @@ function createConstructor (db) {
: 'executeDbCommand'
db[method](cmd, opts, cb);
- }
+ }).help = help("Runs a command on the database")
return DB;
@@ -190,6 +231,17 @@ function handleError (err) {
}
/**
+ * help genereator
+ */
+
+function help (msg) {
+ return function (inline) {
+ if (inline) return msg;
+ log(msg);
+ }
+}
+
+/**
* use.database
*
* [x] db.drop()

0 comments on commit a97da64

Please sign in to comment.
Something went wrong with that request. Please try again.