#6
authoradam <anton@adamansky.com>
Thu, 20 Dec 2012 05:05:10 +0000 (12:05 +0700)
committeradam <anton@adamansky.com>
Thu, 20 Dec 2012 05:05:10 +0000 (12:05 +0700)
README.md
node/ejdb.js
node/tests/t2.js

index 12ce252..f2ca07c 100644 (file)
--- a/README.md
+++ b/README.md
@@ -197,6 +197,8 @@ Each persistent object has unique identifier (OID) placed in the `_id` property.
 If a saved object does not have  `_id` it will be autogenerated.
 To identify and update object it should contains `_id` property.
 
+If callback is not provided this function will be synchronous.
+
 Call variations:
 
     save(cname, json object, [cb])
@@ -208,6 +210,9 @@ __Arguments__
  * {Array|Object} jsarr Signle JSON object or array of JSON objects to save
  * {Function} `[cb]` Callback args: (error, {Array} of OIDs for saved objects)
 
+__Return__
+
+ * {Array} of OIDs of saved objects in synchronous mode otherwise returns {this}.
 
 --------------------------------------
 
@@ -215,6 +220,7 @@ __Arguments__
 ### load(cname, oid, cb)
 
 Loads JSON object identified by OID from the collection.
+If callback is not provided this function will be synchronous.
 
 __Arguments__
 
@@ -223,12 +229,17 @@ __Arguments__
  * {Function} cb  Callback args: (error, obj)
         `obj`:  Retrieved JSON object or NULL if it is not found.
 
+__Return__
+
+ * JSON object or {null} if it is not found in synchronous mode otherwise return {this}.
+
 --------------------------------------
 
 <a name="remove"/>
 ### remove(cname, oid, cb)
 
 Removes JSON object from the collection.
+If callback is not provided this function will be synchronous.
 
 __Arguments__
 
@@ -293,6 +304,8 @@ EJDB queries inspired by MongoDB (mongodb.org) and follows same philosophy.
 
     NOTE: Only one index can be used in search query operation.
 
+    NOTE: If callback is not provided this function will be synchronous.
+
     QUERY HINTS (specified by `hints` argument):
       - $max Maximum number in the result set
       - $skip Number of skipped results in the result set
@@ -325,10 +338,11 @@ EJDB queries inspired by MongoDB (mongodb.org) and follows same philosophy.
       - Cursor#close() Closes cursor and free cursor resources. Cursor cant be used in closed state.
 
     Call variations of find():
-       - find(cname, qobj, cb)
-       - find(cname, qobj, hints, cb)
-       - find(cname, qobj, qobjarr, cb)
-       - find(cname, qobj, qobjarr, hints, cb)
+       - find(cname, [cb])
+       - find(cname, qobj, [cb])
+       - find(cname, qobj, hints, [cb])
+       - find(cname, qobj, qobjarr, [cb])
+       - find(cname, qobj, qobjarr, hints, [cb])
 
  __Arguments__
 
@@ -338,7 +352,13 @@ EJDB queries inspired by MongoDB (mongodb.org) and follows same philosophy.
  * {Object} `[hints]` JSON object with query hints.
  * {Function} cb Callback args: (error, cursor, count)
             `cursor`: Cursor object to traverse records
-            `count`:  Total number of selected records
+      qobj      `count`:  Total number of selected records
+
+__Return__
+
+ * If callback is provided returns {this}
+ * If no callback and `$onlycount` hint is set returns count {Number}.
+ * If no callback and no `$onlycount` hint returns cursor {Object}.
 
  --------------------------------------------
 
@@ -348,10 +368,11 @@ Same as #find() but retrieves only one matching JSON object.
 
 Call variations of findOne():
 
-    findOne(cname, qobj, cb)
-    findOne(cname, qobj, hints, cb)
-    findOne(cname, qobj, qobjarr, cb)
-    findOne(cname, qobj, qobjarr, hints, cb)
+    findOne(cname, [cb])
+    findOne(cname, qobj, [cb])
+    findOne(cname, qobj, hints, [cb])
+    findOne(cname, qobj, qobjarr, [cb])
+    findOne(cname, qobj, qobjarr, hints, [cb])
 
 __Arguments__
 
@@ -362,6 +383,11 @@ __Arguments__
  * {Function} cb Callback args: (error, obj)
              `obj`  Retrieved JSON object or NULL if it is not found.
 
+__Return__
+
+ * If callback is provided returns {this}
+ * If no callback is provided returns found {Object} or {null}
+
 -----------------------------------
 <a name="findOne"/>
 ### update(cname, qobj, orarr, hints, cb)
@@ -381,10 +407,11 @@ Convenient method to execute update queries.
 
 Call variations of update():
 
-    update(cname, qobj, cb)
-    update(cname, qobj, hints, cb)
-    update(cname, qobj, qobjarr, cb)
-    update(cname, qobj, qobjarr, hints, cb)
+    update(cname, [cb])
+    update(cname, qobj, [cb])
+    update(cname, qobj, hints, [cb])
+    update(cname, qobj, qobjarr, [cb])
+    update(cname, qobj, qobjarr, hints, [cb])
 
 __Arguments__
 
@@ -395,6 +422,11 @@ __Arguments__
  * {Function} cb Callback args: (error, count)
              `count`  The number of updated records.
 
+__Return__
+
+ * If callback is provided returns {this}.
+ * If no callback is provided returns {Number} of updated objects.
+
 
 -----------------------------------
 
@@ -404,10 +436,11 @@ Convenient count(*) operation.
 
 Call variations of count():
 
-    count(cname, qobj, cb)
-    count(cname, qobj, hints, cb)
-    count(cname, qobj, qobjarr, cb)
-    count(cname, qobj, qobjarr, hints, cb)
+    count(cname, [cb])
+    count(cname, qobj, [cb])
+    count(cname, qobj, hints, [cb])
+    count(cname, qobj, qobjarr, [cb])
+    count(cname, qobj, qobjarr, hints, [cb])
 
 __Arguments__
 
@@ -418,6 +451,11 @@ __Arguments__
  * {Function} cb Callback args: (error, count)
               `count`:  Number of matching records.
 
+__Return__
+
+ * If callback is provided returns {this}.
+ * If no callback is provided returns {Number} of matched object.
+
 -----------------------------------
 
 <a name="sync"/>
index 4888360..7c2d7cf 100644 (file)
@@ -135,7 +135,7 @@ EJDB.prototype.removeCollection = function(cname, prune, cb) {
  * @param {String} cname Name of collection.
  * @param {Array|Object} jsarr Signle JSON object or array of JSON objects to save
  * @param {Function} [cb] Callback function with arguments: (error, {Array} of OIDs for saved objects)
- * @return {Array} of OIDs for saved objects in synchronous mode otherwise undefined.
+ * @return {Array} of OIDs of saved objects in synchronous mode otherwise returns {this}.
  */
 EJDB.prototype.save = function(cname, jsarr, opts, cb) {
     if (!jsarr) {
@@ -181,7 +181,7 @@ EJDB.prototype.save = function(cname, jsarr, opts, cb) {
  * @param {String} oid Object identifier (OID)
  * @param {Function} [cb]  Callback function with arguments: (error, obj)
  *        `obj`:  Retrieved JSON object or NULL if it is not found.
- * @return Retrieved JSON object or NULL if it is not found in synchronous mode otherwise undefined.
+ * @return JSON object or {null} if it is not found in synchronous mode otherwise return {this}.
  */
 EJDB.prototype.load = function(cname, oid, cb) {
     return this._impl.load(cname, oid, cb);
@@ -202,6 +202,7 @@ EJDB.prototype.remove = function(cname, oid, cb) {
 
 
 /*
+ * - (cname, [cb])
  * - (cname, qobj, [cb])
  * - (cname, qobj, hints, [cb])
  * - (cname, qobj, qobjarr, [cb])
@@ -214,8 +215,13 @@ function parseQueryArgs(args) {
     if (typeof cname !== "string") {
         throw new Error("Collection name 'cname' argument must be specified");
     }
-    qobj = args[i++];
     var next = args[i++];
+    if (typeof next === "function") {
+        cb = next;
+    } else {
+        qobj = next;
+    }
+    next = args[i++];
     if (next !== undefined) {
         if (next.constructor === Array) {
             orarr = next;
@@ -232,8 +238,6 @@ function parseQueryArgs(args) {
         if (typeof next === "function") {
             cb = next;
         }
-    } else {
-        orarr = hints = cb = null;
     }
     return [cname, (qobj || {}), (orarr || []), (hints || {}), (cb || null)];
 }
@@ -323,6 +327,7 @@ function parseQueryArgs(args) {
  *      - Cursor#close() Closes cursor and free cursor resources. Cursor cant be used in closed state.
  *
  * Call variations of find():
+ *       - find(cname, [cb])
  *       - find(cname, qobj, [cb])
  *       - find(cname, qobj, hints, [cb])
  *       - find(cname, qobj, qobjarr, [cb])
@@ -335,9 +340,9 @@ function parseQueryArgs(args) {
  * @param {Function} [cb] Callback function with arguments: (error, cursor, count) where:
  *          `cursor`: Cursor object to traverse records
  *          `count`:  Total number of selected records.
- * @return If callback is provided returns undefined.
- *         If no callback and $onlycount hint is set returns count number.
- *         If no callback and no $onlycount hint returns cursor object.
+ * @return If callback is provided returns {this}.
+ *         If no callback and $onlycount hint is set returns count {Number}.
+ *         If no callback and no $onlycount hint returns cursor {Object}.
  *
  */
 EJDB.prototype.find = function() {
@@ -353,6 +358,7 @@ EJDB.prototype.find = function() {
  * If callback is not provided this function will be synchronous.
  *
  * Call variations of findOne():
+ *       - findOne(cname, [cb])
  *       - findOne(cname, qobj, [cb])
  *       - findOne(cname, qobj, hints, [cb])
  *       - findOne(cname, qobj, qobjarr, [cb])
@@ -364,8 +370,8 @@ EJDB.prototype.find = function() {
  * @param {Object} [hints] JSON object with query hints.
  * @param {Function} [cb] Callback function with arguments: (error, obj) where:
  *          `obj`:  Retrieved JSON object or NULL if it is not found.
- * @return  If callback is provided returns undefined.
- *          If no callback is provided returns found object or null.
+ * @return  If callback is provided returns {this}.
+ *          If no callback is provided returns found {Object} or {null}.
  */
 
 EJDB.prototype.findOne = function() {
@@ -434,8 +440,8 @@ EJDB.prototype.findOne = function() {
  * @param {Function} [cb] Callback function with arguments: (error, count) where:
  *          `count`:  The number of updated records.
  *
- * @return  If callback is provided returns undefined.
- *          If no callback is provided returns count of updated object.
+ * @return  If callback is provided returns {this}.
+ *          If no callback is provided returns {Number} of updated objects.
  */
 EJDB.prototype.update = function() {
     //[cname, qobj, orarr, hints, cb]
@@ -459,6 +465,7 @@ EJDB.prototype.update = function() {
  * Convenient count(*) operation.
  *
  * Call variations of count():
+ *       - count(cname, [cb])
  *       - count(cname, qobj, [cb])
  *       - count(cname, qobj, hints, [cb])
  *       - count(cname, qobj, qobjarr, [cb])
@@ -470,8 +477,9 @@ EJDB.prototype.update = function() {
  * @param {Object} [hints] JSON object with query hints.
  * @param {Function} [cb] Callback function with arguments: (error, count) where:
  *          `count`:  Number of matching records.
- * @return  If callback is provided returns undefined.
- *          If no callback is provided returns count of updated object.
+ *
+ * @return  If callback is provided returns {this}.
+ *          If no callback is provided returns {Number} of matched object.
  */
 EJDB.prototype.count = function() {
     //[cname, qobj, orarr, hints, cb]
index da2d2be..2e1a64e 100644 (file)
@@ -91,7 +91,14 @@ module.exports.testQuery1 = function(test) {
         test.ok(typeof obj["name"] === "string");
         test.ok(typeof obj["age"] === "number");
 
-        test.done();
+        //Async with one callback
+        jb.find("parrots", function(err, cursor, count) {
+            test.ifError(err);
+            test.equal(count, 2);
+            test.ok(cursor);
+            test.equal(cursor.length, 2);
+            test.done();
+        });
     });
 };