Permalink
Browse files

Initial version

  • Loading branch information...
0 parents commit c2ab9b8685e305da6a2e2d33ba6287e46c72b1c4 @canned committed Aug 5, 2012
Showing with 243 additions and 0 deletions.
  1. +120 −0 examples/q-mongodb-test.js
  2. +30 −0 package.json
  3. +93 −0 q-mongodb.js
@@ -0,0 +1,120 @@
+var Q = require('q'),
+ QMongoDB = require('./../q-mongodb');
+
+function testDbCollection() {
+ return QMongoDB.db('q-mobngodb-test').then(function (db) {
+ return QMongoDB.collection(db, 'people');
+ }).then(function (collection) {
+ return Q.ncall(collection.remove, collection).then(function () {
+ return collection;
+ });
+ }).then(function (collection) {
+ // We now have an empty collection. Verify.
+ return Q.ncall(collection.count, collection).then(function (count) {
+ if (count != 0) {
+ throw new Error("Db not empty after remove call");
+ }
+
+ return collection;
+ });
+ }).then(function (collection) {
+ // Insert some data
+ return Q.ncall(collection.insert, collection, [
+ { name:"The Dude" },
+ { name:"Walter" },
+ { name:"Donny" }
+ ])
+ .then(function () {
+ return collection;
+ });
+ }).then(function (collection) {
+ return Q.ncall(collection.find, collection);
+ }).then(function (cursor) {
+ return Q.ncall(cursor.toArray, cursor);
+ }).then(function (items) {
+ if (!items || items.length !== 3) {
+ throw new Error("Invalid items");
+ }
+
+ console.log("testDbCollection Success.");
+ console.dir(items);
+ }).fail(function (err) {
+ console.log("testDbCollection Error:");
+ console.dir(err);
+ });
+}
+
+function testPassingDbPromiseInsteadOfDb() {
+ var dbPromise = QMongoDB.db('q-mobngodb-test');
+
+ return QMongoDB.collection(dbPromise, 'people').then(function (collection) {
+ if (!collection) {
+ throw new Error("Collection is null");
+ } else {
+ console.log("testPassingDbPromiseInsteadOfDb Success.");
+ }
+ }).fail(function (err) {
+ console.log("testPassingDbPromiseInsteadOfDb Error:");
+ console.dir(err);
+ });
+}
+
+function testSameDb() {
+ var dbName = 'testSameDb', db1, db2;
+
+ return QMongoDB.db(dbName).then(function (db) {
+ db1 = db;
+
+ return QMongoDB.db(dbName);
+ }).then(function (db) {
+ db2 = db;
+
+ if (db1 && db1 === db2) {
+ console.log("testSameDb Success.");
+ } else {
+ throw new Error("Dbs not the same instance");
+ }
+ }).fail(function (err) {
+ console.log("testSameDb Error:");
+ console.dir(err);
+ });
+}
+
+function testClosingDbs() {
+ return QMongoDB.closeAll().then(function () {
+ console.log("testClosingDbs Success.");
+ }).fail(function (err) {
+ console.log("testClosingDbs Error:");
+ console.dir(err);
+ });
+
+}
+
+function testClosingAllYieldsDifferentDbs() {
+ var dbName = 'test_closing_all_yields_different_dbs',
+ db1, db2;
+
+ return QMongoDB.db(dbName).then(function (db) {
+ db1 = db;
+ }).then(QMongoDB.closeAll).then(function () {
+ return QMongoDB.db(dbName);
+ }).then(function (db) {
+ db2 = db;
+
+ if (db1 && db2 && db1 !== db2) {
+ console.log('testClosingAllYieldsDifferentDbs Success.');
+ } else {
+ throw new Error("Dbs null or same");
+ }
+ }).fail(function (err) {
+ console.log('testClosingAllYieldsDifferentDbs Error:');
+ console.dir(err);
+ });
+}
+
+testDbCollection()
+ .then(testPassingDbPromiseInsteadOfDb)
+ .then(testSameDb)
+ .then(testClosingDbs)
+ .then(testClosingAllYieldsDifferentDbs)
+ .then(QMongoDB.closeAll);
@@ -0,0 +1,30 @@
+{
+ "name": "q-mongodb",
+ "version": "0.0.1",
+ "author" : "Brian Hein <brian@cannedcoding.com>",
+ "description" : "Q (promises) wrapper for the mongodb node.js package",
+ "contributors" : [
+ {
+ "name" : "Brian Hein",
+ "email" : "brian@cannedcoding.com"
+ }
+ ],
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/canned/QMongoDB.git"
+ },
+ "main" : "./q-mongodb.js",
+ "keywords" : [
+ "Q",
+ "promises",
+ "MongoDB"
+ ],
+ "dependencies": {
+ "mongodb": "*",
+ "q" : "*"
+ },
+ "engines": {
+ "node": ">=0.6.0"
+ },
+ "license": "MIT"
+}
@@ -0,0 +1,93 @@
+var Q = require('q'),
+ Db = require('mongodb').Db,
+ Connection = require('mongodb').Connection,
+ Server = require('mongodb').Server,
+ HOST = process.env['MONGO_NODE_DRIVER_HOST'] || 'localhost',
+ PORT = process.env['MONGO_NODE_DRIVER_PORT'] || Connection.DEFAULT_PORT,
+ _dbPromiseMap = {};
+
+/**
+ * Returns a promise that resolves to an open DB. Calling db(...) again with the same parameters at a later time (but
+ * before a call to closeAll) will yield the same promise and thus the same open db. To close db connections, see
+ * closeAll().
+ *
+ * @param dbName
+ * @param host
+ * @param port
+ * @return {*}
+ */
+exports.db = function (dbName, host /*omit for default*/, port /*omit for default*/) {
+ var dbKey = dbName + (host || "") + (port || ""),
+ dbPromise = _dbPromiseMap[dbKey];
+
+ if (!dbPromise) {
+ dbPromise = Q.fcall(function () {
+ var db = new Db(dbName, new Server(host || HOST, port || PORT, {}));
+
+ return Q.ncall(db.open, db);
+ });
+
+ _dbPromiseMap[dbKey] = dbPromise;
+ }
+
+ return dbPromise;
+};
+
+/**
+ * Returns a promise that resolves to a collection. A new collection will be created in the db if one does not exist.
+ *
+ * @param db
+ * @param collectionName
+ * @return {*}
+ */
+exports.collection = function (db, collectionName) {
+ var dbPromise = null;
+
+ if (db instanceof Db) {
+ dbPromise = Q.fcall(function () {
+ return db;
+ });
+ } else {
+ // Promise
+ dbPromise = db;
+ }
+
+ return dbPromise.then(function (db) {
+ return Q.ncall(db.collection, db, collectionName);
+ }).then(function (collection) {
+ if (!collection) {
+ return Q.ncall(db.createCollection, db, collectionName);
+ } else {
+ return collection;
+ }
+ });
+}
+
+/**
+ * Closes all dbs that were opened via this module. Calling db(...) after this will result in a new db open promise
+ * being created.
+ *
+ * @return {*}
+ */
+exports.closeAll = function () {
+ return Q.fcall(function () {
+ for (var key in _dbPromiseMap) {
+ if (_dbPromiseMap.hasOwnProperty(key)) {
+ var dbPromise = _dbPromiseMap[key];
+ delete _dbPromiseMap[key];
+
+ return dbPromise.then(function (db) {
+ return Q.ncall(db.close, db);
+ }).then(function () {
+ // Call close all again to move onto next dbPromise
+ return exports.closeAll();
+ });
+ }
+ }
+
+ // If we got here, we do not have any more dbPromises in our map
+ _dbPromiseMap = {};
+
+ return null;
+ });
+}

0 comments on commit c2ab9b8

Please sign in to comment.