Permalink
Browse files

Init

  • Loading branch information...
0 parents commit 5a151db8916ab56b48a42c0f6a8739962e9fa4ec @zzdhidden committed Mar 21, 2011
Showing with 315 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +3 −0 .npmignore
  3. +5 −0 History.md
  4. +5 −0 Makefile
  5. +70 −0 Readme.md
  6. +2 −0 index.js
  7. +176 −0 lib/mongoq.js
  8. +12 −0 package.json
  9. +26 −0 test/mongoq.js
  10. +13 −0 test/mongoq.test.js
3 .gitignore
@@ -0,0 +1,3 @@
+build
+*.swp
+.lock-wscript
3 .npmignore
@@ -0,0 +1,3 @@
+support
+test
+examples
5 History.md
@@ -0,0 +1,5 @@
+
+0.0.1 / 2010-01-03
+==================
+
+ * Initial release
5 Makefile
@@ -0,0 +1,5 @@
+
+test:
+ @echo "populate me"
+
+.PHONY: test
70 Readme.md
@@ -0,0 +1,70 @@
+
+mongoq
+============================
+
+Use mongoDB like this: db("testdb").collection("users").find(function(err, cursor){});
+
+Base on [node-mongodb-native](https://github.com/christkv/node-mongodb-native)
+
+
+Installation
+-----------------------------
+
+> npm install mongoq
+
+mongoq
+-----------------------------------------
+
+Bridge all the methods from Collection
+
+###additional methods
+
+> findItems(..., callback(err, itemsArray))
+
+Example
+-----------------------------------------
+
+ var db = require("mongoq");
+ var testdb = db("testdb");
+ var col = testdb.collection("col");
+ col.insert({"init": true}, function(err, doc) {
+ col.drop(function(err) {
+ //testdb.close();
+ var data = [{'name':'William Shakespeare', 'email':'william@shakespeare.com', 'age':587},{'name':'Jorge Luis Borges', 'email':'jorge@borges.com', 'age': 587}];
+ col.insert(data, function(err, doc) {
+ col.findItems({}, function(err, doc){
+ col.update({age: 587}, {"$set":{update:true}}, {upsert: true, multi: true, safe: true}, function(err, doc) {
+ //don't need to return the document
+ testdb.close();
+ console.log("Test ok");
+ });
+ });
+ });
+ });
+ });
+
+
+## License
+
+(The MIT License)
+
+Copyright (c) 2011 hidden <zzdhidden@gmail.com>
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2 index.js
@@ -0,0 +1,2 @@
+
+module.exports = require('./lib/mongoq');
176 lib/mongoq.js
@@ -0,0 +1,176 @@
+/**
+*
+* MongoDB lib.
+*
+* When you are collect some data. it's very useful.
+*
+* Run like this.
+*
+* var db = require("db")
+* db("testdb").collection("users").find
+*
+*/
+
+var mongodb = require("mongodb"),
+BSON = mongodb.BSONPure;
+//new BSON.ObjectID()
+
+function mcollection (db, name) {
+ this.db = db;
+ this.name = name;
+}
+
+mcollection.prototype._open = function(callback){
+ var self = this;
+ if( self.collection ) {
+ callback && callback( null, self.collection );
+ return;
+ }
+ self.db._open(function(err, client) {
+ if( err ) {
+ callback && callback(err, client);
+ } else {
+ client.collection(self.name, function(err, collection) {
+ self.collection = err ? null : collection;
+ callback && callback(err, collection);
+ });
+ }
+ });
+}
+
+var __slice = Array.prototype.slice;
+
+function proxy(ob, fn, args, handler){
+ args = __slice.call(args, 0);
+ var id, callback;
+ for (var i = args.length - 1; i >= 0; i--) {
+ if( 'function' === typeof args[i] ){
+ id = i;
+ callback = args[i];
+ break;
+ }
+ }
+ if( callback ) {
+ args[id] = function(err, doc) {
+ handler && handler(err, doc, callback);
+ };
+ }
+ return fn ? fn.apply(ob, args) : null;
+}
+
+//'checkCollectionName', 'normalizeHintField',
+//['insert', 'remove', 'rename', 'insertAll', 'save', 'update', 'distinct', 'count', 'drop', 'findAndModify', 'find', 'findOne', 'createIndex', 'ensureIndex', 'indexInformation', 'dropIndex', 'dropIndexes', 'mapReduce', 'group']
+Object.keys(mongodb.Collection.prototype).forEach(function(key) {
+ mcollection.prototype[key] = function(){
+ var self = this,
+ args = __slice.call(arguments, 0),
+ id, callback;
+ for (var i = args.length - 1; i >= 0; i--) {
+ if( 'function' === typeof args[i] ){
+ id = i;
+ callback = args[i];
+ break;
+ }
+ };
+ if( !callback ) {
+ return self.collection ? self.collection[key].apply(self.collection, args) : null;
+ }
+ self._open(function(err, collection) {
+ if( err ) {
+ callback(err, collection);
+ } else {
+ collection[key].apply(collection, args);
+ }
+ });
+ }
+});
+
+mcollection.prototype.findItems = function() {
+ return proxy( this, this.find, arguments, function(err, cursor, callback) {
+ if (err) {
+ callback(err);
+ } else {
+ cursor.toArray(callback);
+ }
+ });
+};
+
+function mdb(dbname, options) {
+ this.dbname = dbname;
+ this.collections = {};
+ this.options = options || {};
+}
+
+mdb.prototype._open = function(callback) {
+ var self = this;
+ if( self.client ) {
+ callback && callback( null, self.client );
+ return;
+ }
+ var client = new mongodb.Db(self.dbname, new mongodb.Server(self.options.host, self.options.port, self.options));
+ client.open(function(err, p_client) {
+ self.client = err ? null : client;
+ callback && callback( err, client );
+ });
+}
+
+Object.keys(mongodb.Db.prototype).forEach(function(key) {
+ mdb.prototype[key] = function(){
+ var self = this,
+ args = __slice.call(arguments, 0),
+ id, callback;
+ for (var i = args.length - 1; i >= 0; i--) {
+ if( 'function' === typeof args[i] ){
+ id = i;
+ callback = args[i];
+ break;
+ }
+ };
+ if( !callback ) {
+ return self.client ? self.client[key].apply(self.client, args) : null;
+ }
+ self._open(function(err, client) {
+ if( err ) {
+ callback(err, client);
+ } else {
+ client[key].apply(client, args);
+ }
+ });
+ }
+});
+
+//disable default open
+delete mdb.prototype.open;
+delete mdb.prototype.close;
+
+mdb.prototype.close = function() {
+ var c = this.client;
+ //Clear cache
+ this.client = null;
+ this.collections = {};
+ c && c.close();
+}
+
+mdb.prototype.collection = function(name) {
+ return this.collections[name] || (this.collections[name] = new mcollection(this, name));
+}
+
+var dbs = {};
+function getDB(dbname, options) {
+ options = options || {};
+ options.host = options.host || "127.0.0.1";
+ options.port = options.port || mongodb.Connection.DEFAULT_PORT;
+ var key = options.host + ":" + options.port + "/" + dbname;
+ return dbs[key] || (dbs[key] = new mdb(dbname, options));
+}
+
+getDB.close = function() {
+ for( var key in dbs ) {
+ dbs[key].close();
+ }
+}
+
+getDB.mongodb = mongodb;
+module.exports = getDB;
+
+
12 package.json
@@ -0,0 +1,12 @@
+{
+ "name": "mongoq"
+ , "version": "0.0.1"
+ , "description": "Use mongoDB like this: db('testdb').collection('users').find(function(err, cursor){});"
+ , "keywords": ["mongodb", "mongoq"]
+ , "author" : "Hidden <zzdhidden@gmail.com>"
+ , "repository" : { "type" : "git"
+ , "url" : "http://github.com/zzdhidden/mongoq.git" }
+ , "dependencies": {"mongodb" : ">=0.9.1"}
+ , "main": "index"
+ , "engines": { "node": "0.4.x" }
+}
26 test/mongoq.js
@@ -0,0 +1,26 @@
+var assert = require("assert");
+var db = require("mongoq");
+var testdb = db("testdb");
+var col = testdb.collection("col");
+//equal ok notEqual deepEqual
+//['insert', 'remove', 'rename', 'insertAll', 'save', 'update', 'distinct', 'count', 'drop', 'findAndModify', 'find', 'findOne', 'createIndex', 'ensureIndex', 'indexInformation', 'dropIndex', 'dropIndexes', 'mapReduce', 'group']
+col.insert({"init": true}, function(err, doc) {
+ assert.strictEqual(err, null);
+ assert.strictEqual(doc[0]["init"], true);
+ col.drop(function(err) {
+ assert.strictEqual(err, null);
+ //testdb.close();
+ var data = [{'name':'William Shakespeare', 'email':'william@shakespeare.com', 'age':587},{'name':'Jorge Luis Borges', 'email':'jorge@borges.com', 'age': 587}];
+ col.insert(data, function(err, doc) {
+ col.findItems({}, function(err, doc){
+ assert.equal(doc.length, 2);
+ col.update({age: 587}, {"$set":{update:true}}, {upsert: true, multi: true, safe: true}, function(err, doc) {
+ //don't need to return the document
+ assert.strictEqual(err, null);
+ testdb.close();
+ console.log("Test ok");
+ });
+ });
+ });
+ });
+});
13 test/mongoq.test.js
@@ -0,0 +1,13 @@
+
+/**
+ * Module dependencies.
+ */
+
+var mongoq = require('mongoq')
+ , should = require('should');
+
+module.exports = {
+ 'test .version': function(){
+ mongoq.version.should.match(/^\d+\.\d+\.\d+$/);
+ }
+};

0 comments on commit 5a151db

Please sign in to comment.