Browse files

Giving access to mongodb-native Db object

  • Loading branch information...
1 parent 3c2af44 commit b43cd04abc2f56de88eebcadeca98e8e1efb3d3d @jiem committed Nov 25, 2012
Showing with 54 additions and 14 deletions.
  1. +18 −2 README.md
  2. +6 −1 example.js
  3. +29 −10 index.js
  4. +1 −1 package.json
View
20 README.md
@@ -46,8 +46,23 @@ The --harmony flag is necessary because we're using Harmony proxies to open and
console.log('Meal:\n', meal);
console.log('Desserts:\n', desserts, '\nBon appetit!\n');
});
+
+ // Closing DB after 3 seconds
+ setTimeout(function() {
+ db.close();
+ }, 3000);
```
-
+
+## Using the underlying mongodb-native `Db` object
+
+The documentation of the mongodb-native `Db` object is [here][1].
+To access and manipulate the underlying mongodb-native `Db` object,
+there are 3 hooks on the mongo-lazy `db` object:
+
+* `db.close()` to close the database
+* `db.ready(listener)` to attach a listener which will be fired when `Db` is ready
+* `db.$` which is the mongodb-native `Db` object itself (or `null` when `Db` is not ready)
+
## MIT License
@@ -73,4 +88,5 @@ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-[0]: http://mongodb.github.com/node-mongodb-native/api-generated/collection.html
+[0]: http://mongodb.github.com/node-mongodb-native/api-generated/collection.html
+[1]: http://mongodb.github.com/node-mongodb-native/api-generated/db.html
View
7 example.js
@@ -26,4 +26,9 @@ db([
console.log('Drink:\n', drink);
console.log('Meal:\n', meal);
console.log('Desserts:\n', desserts, '\nBon appetit!\n');
-});
+});
+
+// Closing DB after 3 seconds
+setTimeout(function() {
+ db.close();
+}, 3000);
View
39 index.js
@@ -3,15 +3,28 @@ var mongo = require('mongodb');
exports.open = function(a, b, c) {
var COLLECTION = {};
+ var DB_READY_LISTENERS = [];
var DB, ERROR_CALLBACK;
//============================================================================
+ function addDbReadyListener(cb) {
+ DB || DB_READY_LISTENERS.push(cb);
+ }
+
+ //============================================================================
+ function closeDb() {
+ DB && DB.close();
+ }
+
+ //============================================================================
function dbReady(err, db) {
- var coll;
+ var i, listener, coll;
if (err) {
ERROR_CALLBACK(err);
} else {
- DB = db;
+ DB = db;
+ for (i = 0; listener = DB_READY_LISTENERS[i]; i++)
+ listener(db);
for (coll in COLLECTION)
db.collection(coll, collectionReady);
}
@@ -33,21 +46,27 @@ exports.open = function(a, b, c) {
}
//============================================================================
- function collection(_, coll) {
- if (COLLECTION[coll])
- return COLLECTION[coll];
- COLLECTION[coll] = new Collection();
+ function get(_, attr) {
+ if (attr === '$')
+ return DB;
+ if (attr === 'close')
+ return closeDb;
+ if (attr === 'ready')
+ return addDbReadyListener;
+ if (COLLECTION[attr])
+ return COLLECTION[attr];
+ COLLECTION[attr] = new Collection();
if (DB)
- DB.collection(coll, collectionReady);
- return COLLECTION[coll];
+ DB.collection(attr, collectionReady);
+ return COLLECTION[attr];
}
//============================================================================
function parallel(queries, callback) {
var general = {results: [], callback: callback, countdown: 0};
var i, query, coll, method;
for (i = 0; query = queries[i]; i++) {
- coll = collection(null, query.shift());
+ coll = get(null, query.shift());
method = query.shift();
if (typeof coll[method] === 'function') {
query.push(createCallback(i, general));
@@ -80,7 +99,7 @@ exports.open = function(a, b, c) {
if (!global.Proxy)
throw new Error('run node with harmony: "node --harmony yourScript.js"')
- return Proxy.createFunction({get: collection}, parallel);
+ return Proxy.createFunction({get: get}, parallel);
}
View
2 package.json
@@ -1,7 +1,7 @@
{
"name": "mongo-lazy",
"description": "Node-mongodb-native for lazy people. Don't ever open a collection again. Eat a piece of cake while doing parallel queries.",
- "version": "0.0.1",
+ "version": "0.0.2",
"author": "Jie Meng-Gerard <contact@jie.fr>",
"repository": "git://github.com/jiem/mongo-lazy",
"keywords": ["mongo", "mongodb", "mongodb-native", "db"],

0 comments on commit b43cd04

Please sign in to comment.