collection aliases
authorVyacheslav Tyutyunkov <tyutyunkov@gmail.com>
Mon, 11 Feb 2013 07:01:27 +0000 (14:01 +0700)
committerVyacheslav Tyutyunkov <tyutyunkov@gmail.com>
Mon, 11 Feb 2013 07:01:27 +0000 (14:01 +0700)
node/bin/cli.js

index 7292bee..9fe6da1 100644 (file)
@@ -10,7 +10,7 @@ var pkg = require("../../package.json");
 
 
 //Parse aguments
-(function() {
+(function () {
     var args = process.argv;
     for (var i = 2; i < args.length; ++i) {
         var a = args[i];
@@ -50,98 +50,179 @@ var path = require("path");
 var EJDB = require("../ejdb.js");
 var clinspect = require("../clinspect.js");
 
+// help messages (for methods with collection)
+var helpGetters = {
+    save : function (selected) {
+        return "(" + (!selected ? "cname, " : "") + "object|array of object, [opts], [cb]) Save/update specified JSON objects in the collection."
+    },
+    load : function (selected) {
+        return "(" + (!selected ? "cname, " : "") + "oid, [cb]) Loads object identified by OID from the collection"
+    },
+    remove : function (selected) {
+        return "(" + (!selected ? "cname, " : "") + "oid, [cb]) Removes object from the collection"
+    },
+    find : function (selected) {
+        return "(" + (!selected ? "cname, " : "") + "[qobj], [qobjarr], [hints], [cb]) Execute query on collection"
+    },
+    findOne : function (selected) {
+        return "(" + (!selected ? "cname, " : "") + "[qobj], [qobjarr], [hints], [cb]) Retrive one object from the collection"
+    },
+    update : function (selected) {
+        return "(" + (!selected ? "cname, " : "") + "[qobj], [qobjarr], [hints], [cb]) Perform update query on collection"
+    },
+    count : function (selected) {
+        return "(" + (!selected ? "cname, " : "") + "[qobj], [qobjarr], [hints], [cb]) Convenient count(*) operation"
+    },
+    dropIndexes : function (selected) {
+        return "(" + (!selected ? "cname, " : "") + "path, [cb]) Drop indexes of all types for JSON field path"
+    },
+    optimizeIndexes : function (selected) {
+        return "(" + (!selected ? "cname, " : "") + "path, [cb]) Optimize indexes of all types for JSON field path"
+    },
+
+    ensureStringIndex : function (selected) {
+        return "(" + (!selected ? "cname, " : "") + "path, [cb]) Ensure String index for JSON field path"
+    },
+    rebuildStringIndex : function (selected) {
+        return "(" + (!selected ? "cname, " : "") + "path, [cb])"
+    },
+    dropStringIndex : function (selected) {
+        return "(" + (!selected ? "cname, " : "") + "path, [cb])"
+    },
+
+    ensureIStringIndex : function (selected) {
+        return "(" + (!selected ? "cname, " : "") + "path, [cb]) Ensure case insensitive String index for JSON field path"
+    },
+    rebuildIStringIndex : function (selected) {
+        return "(" + (!selected ? "cname, " : "") + "path, [cb])"
+    },
+    dropIStringIndex : function (selected) {
+        return "(" + (!selected ? "cname, " : "") + "path, [cb])"
+    },
+
+    ensureNumberIndex : function (selected) {
+        return "(" + (!selected ? "cname, " : "") + "path, [cb]) Ensure index presence of Number type for JSON field path"
+    },
+    rebuildNumberIndex : function (selected) {
+        return "(" + (!selected ? "cname, " : "") + "path, [cb])"
+    },
+    dropNumberIndex : function (selected) {
+        return "(" + (!selected ? "cname, " : "") + "path, [cb])"
+    },
+
+    ensureArrayIndex : function (selected) {
+        return "(" + (!selected ? "cname, " : "") + "path, [cb]) Ensure index presence of Array type for JSON field path"
+    },
+    rebuildArrayIndex : function (selected) {
+        return "(" + (!selected ? "cname, " : "") + "path, [cb])"
+    },
+    dropArrayIndex : function (selected) {
+        return "(" + (!selected ? "cname, " : "") + "path, [cb])"
+    }
+};
 
 //Init help hints
-Object.defineProperty(EJDB.open, "_help_",
-        {value : "(dbFile, [openMode]) Open database"});
-Object.defineProperty(EJDB.prototype.close, "_help_",
-        {value : "Close database"});
-Object.defineProperty(EJDB.prototype.isOpen, "_help_",
-        {value : "Check if database in opened state"});
-Object.defineProperty(EJDB.prototype.ensureCollection, "_help_",
-        {value : "(cname, [copts]) Creates new collection if it does't exists"});
-Object.defineProperty(EJDB.prototype.dropCollection, "_help_",
-        {value : "(cname, [prune], [cb]) Drop collection, " +
-                "if `prune` is true collection db files will be erased from disk."});
-Object.defineProperty(EJDB.prototype.save, "_help_",
-        {value : "(cname, object|array of object, [opts], [cb]) " +
-                "Save/update specified JSON objects in the collection."});
-Object.defineProperty(EJDB.prototype.load, "_help_",
-        {value : "(cname, oid, [cb]) Loads object identified by OID from the collection"});
-Object.defineProperty(EJDB.prototype.remove, "_help_",
-        {value : "(cname, oid, [cb]) Removes object from the collection"});
-Object.defineProperty(EJDB.prototype.find, "_help_",
-        {value : "(cname, [qobj], [qobjarr], [hints], [cb]) Execute query on collection"});
-Object.defineProperty(EJDB.prototype.findOne, "_help_",
-        {value : "(cname, [qobj], [qobjarr], [hints], [cb]) Retrive one object from the collection"});
-Object.defineProperty(EJDB.prototype.update, "_help_",
-        {value : "(cname, [qobj], [qobjarr], [hints], [cb]) Perform update query on collection"});
-Object.defineProperty(EJDB.prototype.count, "_help_",
-        {value : "(cname, [qobj], [qobjarr], [hints], [cb]) Convenient count(*) operation"});
-Object.defineProperty(EJDB.prototype.sync, "_help_",
-        {value : "Synchronize entire EJDB database with disk"});
-Object.defineProperty(EJDB.prototype.dropIndexes, "_help_",
-        {value : "(cname, path, [cb]) Drop indexes of all types for JSON field path"});
-Object.defineProperty(EJDB.prototype.optimizeIndexes, "_help_",
-        {value : "(cname, path, [cb]) Optimize indexes of all types for JSON field path"});
-
-Object.defineProperty(EJDB.prototype.ensureStringIndex, "_help_",
-        {value : "(cname, path, [cb]) Ensure String index for JSON field path"});
-Object.defineProperty(EJDB.prototype.rebuildStringIndex, "_help_",
-        {value : "(cname, path, [cb])"});
-Object.defineProperty(EJDB.prototype.dropStringIndex, "_help_",
-        {value : "(cname, path, [cb])"});
-
-Object.defineProperty(EJDB.prototype.ensureIStringIndex, "_help_",
-        {value : "(cname, path, [cb]) Ensure case insensitive String index for JSON field path"});
-Object.defineProperty(EJDB.prototype.rebuildIStringIndex, "_help_",
-        {value : "(cname, path, [cb])"});
-Object.defineProperty(EJDB.prototype.dropIStringIndex, "_help_",
-        {value : "(cname, path, [cb])"});
-
-Object.defineProperty(EJDB.prototype.ensureNumberIndex, "_help_",
-        {value : "(cname, path, [cb]) Ensure index presence of Number type for JSON field path"});
-Object.defineProperty(EJDB.prototype.rebuildNumberIndex, "_help_",
-        {value : "(cname, path, [cb])"});
-Object.defineProperty(EJDB.prototype.dropNumberIndex, "_help_",
-        {value : "(cname, path, [cb])"});
-
-Object.defineProperty(EJDB.prototype.ensureArrayIndex, "_help_",
-        {value : "(cname, path, [cb]) Ensure index presence of Array type for JSON field path"});
-Object.defineProperty(EJDB.prototype.rebuildArrayIndex, "_help_",
-        {value : "(cname, path, [cb])"});
-Object.defineProperty(EJDB.prototype.dropArrayIndex, "_help_",
-        {value : "(cname, path, [cb])"});
-
-Object.defineProperty(EJDB.prototype.getDBMeta, "_help_",
-        {value : "Get description of EJDB database and its collections"});
-
-Object.defineProperty(EJDB.prototype.beginTransaction, "_help_",
-        {value : "Begin collection transaction"});
-
-Object.defineProperty(EJDB.prototype.commitTransaction, "_help_",
-        {value : "Commit collection transaction"});
-
-Object.defineProperty(EJDB.prototype.rollbackTransaction, "_help_",
-        {value : "Rollback collection transaction"});
-
-Object.defineProperty(EJDB.prototype.getTransactionStatus, "_help_",
-        {value : "Get collection transaction status"});
-
-repl = require("repl").start({
-    prompt : "ejdb> ",
-    input : process.stdin,
-    output : process.stdout,
-    terminal : true,
-    writer : function(obj) {
-        return clinspect.inspect(obj, maxInspectDepth, useColors)
+Object.defineProperty(EJDB.open, "_help_", {value : "(dbFile, [openMode]) Open database"});
+Object.defineProperty(EJDB.prototype.close, "_help_", {value : "Close database"});
+Object.defineProperty(EJDB.prototype.isOpen, "_help_", {value : "Check if database in opened state"});
+Object.defineProperty(EJDB.prototype.ensureCollection, "_help_", {value : "(cname, [copts]) Creates new collection if it does't exists"});
+Object.defineProperty(EJDB.prototype.dropCollection, "_help_", {value : "(cname, [prune], [cb]) Drop collection, " +
+                                                                        "if `prune` is true collection db files will be erased from disk."});
+
+Object.defineProperty(EJDB.prototype.getDBMeta, "_help_", {value : "Get description of EJDB database and its collections"});
+Object.defineProperty(EJDB.prototype.sync, "_help_", {value : "Synchronize entire EJDB database with disk"});
+
+Object.defineProperty(EJDB.prototype.save, "_help_", {value : helpGetters.save()});
+Object.defineProperty(EJDB.prototype.load, "_help_", {value : helpGetters.load()});
+Object.defineProperty(EJDB.prototype.remove, "_help_", {value : helpGetters.remove()});
+Object.defineProperty(EJDB.prototype.find, "_help_", {value : helpGetters.find()});
+Object.defineProperty(EJDB.prototype.findOne, "_help_", {value : helpGetters.findOne()});
+Object.defineProperty(EJDB.prototype.update, "_help_", {value : helpGetters.update()});
+Object.defineProperty(EJDB.prototype.count, "_help_", {value : helpGetters.count()});
+Object.defineProperty(EJDB.prototype.dropIndexes, "_help_", {value : helpGetters.dropIndexes()});
+Object.defineProperty(EJDB.prototype.optimizeIndexes, "_help_", {value : helpGetters.optimizeIndexes()});
+
+Object.defineProperty(EJDB.prototype.ensureStringIndex, "_help_", {value : helpGetters.ensureStringIndex()});
+Object.defineProperty(EJDB.prototype.rebuildStringIndex, "_help_", {value : helpGetters.rebuildStringIndex()});
+Object.defineProperty(EJDB.prototype.dropStringIndex, "_help_", {value : helpGetters.dropStringIndex()});
+
+Object.defineProperty(EJDB.prototype.ensureIStringIndex, "_help_", {value : helpGetters.ensureIStringIndex()});
+Object.defineProperty(EJDB.prototype.rebuildIStringIndex, "_help_", {value : helpGetters.rebuildIStringIndex()});
+Object.defineProperty(EJDB.prototype.dropIStringIndex, "_help_", {value : helpGetters.dropIStringIndex()});
+
+Object.defineProperty(EJDB.prototype.ensureNumberIndex, "_help_", {value : helpGetters.ensureNumberIndex()});
+Object.defineProperty(EJDB.prototype.rebuildNumberIndex, "_help_", {value : helpGetters.rebuildNumberIndex()});
+Object.defineProperty(EJDB.prototype.dropNumberIndex, "_help_", {value : helpGetters.dropNumberIndex()});
+
+Object.defineProperty(EJDB.prototype.ensureArrayIndex, "_help_", {value : helpGetters.ensureArrayIndex()});
+Object.defineProperty(EJDB.prototype.rebuildArrayIndex, "_help_", {value : helpGetters.rebuildArrayIndex()});
+Object.defineProperty(EJDB.prototype.dropArrayIndex, "_help_", {value : helpGetters.dropArrayIndex()});
+
+Object.defineProperty(EJDB.prototype.beginTransaction, "_help_", {value : "Begin collection transaction"});
+Object.defineProperty(EJDB.prototype.commitTransaction, "_help_", {value : "Commit collection transaction"});
+Object.defineProperty(EJDB.prototype.rollbackTransaction, "_help_", {value : "Rollback collection transaction"});
+Object.defineProperty(EJDB.prototype.getTransactionStatus, "_help_", {value : "Get collection transaction status"});
+
+var colctl = function (db, cname) {
+    // method names for creating aliases (db.<method>(cname, ...) -> db.cname.<method>(...))
+    var mnames = [
+        "save", "load", "remove", "findOne", "update", "count",
+        "dropIndexes", "optimizeIndexes",
+        "ensureStringIndex", "rebuildStringIndex", "dropStringIndex",
+        "ensureIStringIndex", "rebuildIStringIndex", "dropIStringIndex",
+        "ensureNumberIndex", "rebuildNumberIndex", "dropNumberIndex",
+        "ensureArrayIndex", "rebuildArrayIndex", "dropArrayIndex"
+    ];
+    var buildargs = function (args) {
+        var result = [cname];
+        for (var i = 0; args[i]; ++i) {
+            result.push(args[i]);
+        }
+
+        return result;
+    };
+    var mbind = function (mname) {
+        return function () {
+            return EJDB.prototype[mname].apply(db, buildargs(arguments));
+        }
+    };
+
+    // collection controller impl
+    var colctlimpl = {
+        // manually wrap 'find' method
+        find : function () {
+            return db.find.apply(db, buildargs(arguments));
+        }
+    };
+    Object.defineProperty(colctlimpl.find, "_help_", {value : helpGetters["find"](true)});
+
+    // wrap methods
+    for (var i = 0; i < mnames.length; ++i) {
+        var method = mnames[i];
+        colctlimpl[method] = mbind(method);
+        if (helpGetters[method]) {
+            Object.defineProperty(colctlimpl[method], '_help_', {value : helpGetters[method](true)});
+        }
     }
-});
+
+    return colctlimpl
+};
+
+repl = require("repl").start(
+  {
+      prompt : "ejdb> ",
+      input : process.stdin,
+      output : process.stdout,
+      terminal : true,
+      writer : function (obj) {
+          return clinspect.inspect(obj, maxInspectDepth, useColors)
+      }
+  });
 
 //console.log("MF=" +  module.filename);
 
 var dbctl = {
-    open : function(dbpath) {
+    open : function (dbpath) {
         if (dbpath == null) {
             return error("No file path specified");
         }
@@ -157,11 +238,12 @@ var dbctl = {
         return dbstatus(cdb);
     },
 
-    status : function() {
+    status : function () {
+        syncdbctx();
         return dbstatus(cdb);
     },
 
-    close : function() {
+    close : function () {
         if (!cdb || !cdb.jb) {
             return error("Database already closed");
         }
@@ -177,7 +259,7 @@ Object.defineProperty(dbctl.open, "_help_", {value : EJDB.open._help_});
 Object.defineProperty(dbctl.close, "_help_", {value : EJDB.prototype.close._help_});
 Object.defineProperty(dbctl.status, "_help_", {value : "Get current database status"});
 
-repl.on("exit", function() {
+repl.on("exit", function () {
     dbctl.close();
     console.log("Bye!");
 });
@@ -197,21 +279,36 @@ function syncdbctx() {
         db.__proto__ = cdb.jb;
         db.close = dbctl.close;
         db.status = dbctl.status;
-        db.find = function() {
+        db.find = function () {
             var ret = cdb.jb.find.apply(cdb.jb, arguments);
             if (typeof ret === "object") {
-                if (!quiet) println("Found " + ret.length + " records");
+                if (!quiet) {
+                    println("Found " + ret.length + " records");
+                }
                 for (var i = 0; ret.next() && i < maxInspectRows; ++i) {
                     println(repl.writer(ret.object()));
                 }
                 ret.reset();
                 if (ret.length > maxInspectRows) {
-                    if (!quiet) println("Shown only first " + maxInspectRows);
+                    if (!quiet) {
+                        println("Shown only first " + maxInspectRows);
+                    }
+                }
+                if (!quiet) {
+                    println("\nReturned cursor:");
                 }
-                if (!quiet) println("\nReturned cursor:");
             }
             return ret;
         };
+
+        // build collection controllers for all known collections
+        var dbMeta = cdb.jb.getDBMeta();
+        if (dbMeta && dbMeta.collections) {
+            for (var j = 0; j < dbMeta.collections.length; ++j) {
+                var collection = dbMeta.collections[j];
+                db[collection.name] = colctl(db, collection.name);
+            }
+        }
         Object.defineProperty(db.find, "_help_", {value : EJDB.prototype.find._help_});
     } else {
         db.__proto__ = dbctl;