Permalink
Browse files

Several changes. Added new methods and classes, fixed bugs, typos and…

… general reformatting.
  • Loading branch information...
1 parent e1e71a4 commit e943adcb9baac707423a77c7369a1cf389e2a019 @sergi committed Oct 26, 2009
Showing with 83 additions and 136 deletions.
  1. +83 −136 lib/mongodb.js
View
@@ -1,95 +1,79 @@
+var Util = require("mongosupport").MongoSupport;
var BasicDBObject = Packages.com.mongodb.BasicDBObject;
-var MongoJava = Packages.com.mongodb.Mongo;
-
-/**
- * @namespace {Mongo}
- * @example
- *
- * Mongo
- * ================
- * Docs coming soon.
- */
-function Mongo(dbname, host, port) {
- //if(_dbs[dbname] != undefined)
- //return _dbs[dbname]; // TODO: check if port or address is different as well
+
+function Mongo(host, port) {
this.host = host || 'localhost';
this.port = port || 27017;
- this.db = new MongoJava(this.host, this.port).getDB(dbname);
- //_dbs[dbname] = this;
+ this._mongo = new Packages.com.mongodb.Mongo(this.host, this.port);
}
Mongo.prototype = {
- db: null,
+ _mongo: null,
+ dropDatabase: function(dbName) {
+ this._mongo.dropDatabase(dbName);
+ },
+ getDatabaseNames: function() {
+ return this._mongo.getDatabaseNames();
+ },
+ getDB: function(db) {
+ return new MongoDB(this._mongo.getDB(db));
+ }
+}
+
+
+function MongoDB(db) {
+ this._db = db;
+}
+
+MongoDB.prototype = {
+ _db: null,
host: null,
port: null,
- /*get name: function() {
- return this.db.name;
- },*/
- ///
- runCommand: function(obj) {
- if (typeof(obj) == "string" ) {
- var n = {};
- n[obj] = 1;
- obj = n;
- }
- return this.getCollection("$cmd").findOne(obj);
- },
- _dbCommand: this.runCommand,
- ///
- auth: function(username, pass) {
- },
- coll: function(name) {
- return new Collection(this.db.getCollection(name));
+
+ addUser: function(username, password) {
+ this._db.addUser(username, pasword);
},
- getCollection: function(name) {
- return this.coll(name);
+ authenticate: function(username, password) {
+ return this._db.authenticate(username, password);
},
- getName: function() {
- return this.db.name;
+ command: function(cmd) {
+ return this._db.command(cmd);
},
- getCollectionNames: function() {
- return this.db.collectionNames.toArray();
+ createCollection: function(name, obj) {
+ return new Collection(
+ this._db.createCollection(name, new BasicDBObject(obj)));
},
dropDatabase: function() {
- this.db.dropDatabase();
+ this._db.dropDatabase();
},
- authenticate: function(username, password) {
- return this.db.authenticate(username, password);
+ getCollection: function(name) {
+ return new Collection(this._db.getCollection(name));
+ },
+ getCollectionFromFull: function(fullNameSpace) {
+ return this._db.getCollectionFromFull(fullNameSpace);
+ },
+ getCollectionNames: function() {
+ return Util.toJSArray(this._db.getCollectionNames().toArray());
},
getName: function() {
- return this.db.name();
+ return this._db.getName();
+ },
+ eval: function(code, args) {
+ return this._db.eval(code, args) || {};
},
- /*
- this.eval = function(fun) {
- // TODO - what should the 2nd arg to eval consist of?
- return new ScriptableMap(db.eval(fun, {}) || {});
- }*/
getLastError: function() {
- return this.db.lastError || {};
+ return this._db.lastError || {};
+ },
+ toString: function() {
+ this.getName();
}
}
if (typeof exports != "undefined")
exports.Mongo = Mongo;
-/*
- we usually want to be able to access the object ID as a string,
- certainly if we're ever serializing things to JSON, but maybe
- there's a better way to alter the JSON serialization to account
- for this so we don't do it all the time?
-*/
-function checkforObjectId(o) {
- if(! typeof o['_id'] == "string")
- o['_id'] = o['_id'].toString();
- return o;
-}
-/*
- Light wrapper around com.mongodb.DBCollection
- - mostly pass calls through, but translate between
- maps/arrays <-> JS objects where appropriate.
-*/
function Collection(collection) {
this.collection = collection;
}
@@ -104,28 +88,38 @@ Collection.prototype = {
},
find: function(obj) {
- var dbo = obj ? new BasicDBObject(obj) : new BasicDBObject();
+ var dbo = Util.createBDObject(obj);
return new Cursor(this.collection.find(dbo));
},
findOne: function(obj) {
- var dbo = obj ? new BasicDBObject(obj) : new BasicDBObject();
- var sm = this.collection.findOne(dbo) || {};
- return checkforObjectId(sm);
+ var dbo = obj;
+ if (obj && typeof(obj) != "object")
+ dbo = { "_id": obj };
+
+ var sm = this.collection.findOne(Util.createBDObject(dbo), Util.createBDObject(arguments[1]));
+ // HACK: To obtain a pain JS object I parse the string representation into
+ // JSON, otherwise the Java Object is returned. Of course, this is inefficient
+ // and has to be fixed ASAP.
+ return sm ? JSON.parse(sm.toString()) : null;
},
save: function(obj) {
- var dbo = new BasicDBObject(obj ? obj : null);
+ var dbo = Util.createBDObject(obj);
var sm = this.collection.save(dbo) || {};
- return checkforObjectId(sm);
+ return Util.ensureStringId(sm);
},
getName: function() {
return this.collection.getName();
},
+ getCount: function() {
+ return this.collection.getCount();
+ },
+
ensureIndex: function(obj) {
- var dbo = (obj) ? new BasicDBObject(obj) : new BasicDBObject();
+ var dbo = Util.createBDObject(obj);
this.collection.ensureIndex(dbo);
},
@@ -138,23 +132,17 @@ Collection.prototype = {
},
insert: function(docs) {
- var toinsert = docs.map(function(d) {
- return d ? new BasicDBObject(d) : new BasicDBObject();
- });
-
- // TODO - this claims to return List<DBObject> but doesn't seem like it does
- // just return the length inserted for now
- return this.collection.insert(toinsert).length;
- // var rv = [];
- // for(d in inserted) {
- // for(e in d)
- // log.info("inserted: " + e);
- // // rv.push(new ScriptableMap(checkforObjectId(d) || {}));
- // }
- // return rv;
+ var insertable = docs.isArray && docs.isArray() ?
+ docs.map(createBDO) : Util.createBDObject(docs);
+
+ return this.collection.insert(insertable).length;
}
}
+if (typeof exports != "undefined")
+ exports.Collection = Collection;
+
+
function Cursor(cursor) {
this.cursor = cursor;
}
@@ -166,7 +154,7 @@ Cursor.prototype = {
},
next: function() {
var sm = this.cursor.next() || {};
- return MongoSupport.ensureStringId(sm);
+ return Util.ensureStringId(sm);
},
limit: function(lim) {
return new Cursor(this.cursor.limit(lim));
@@ -175,8 +163,7 @@ Cursor.prototype = {
return new Cursor(this.cursor.skip(num));
},
sort: function(obj) {
- var dbo = obj ? new BasicDBObject(obj) : new BasicDBObject();
- return new Cursor(this.cursor.sort(dbo));
+ return new Cursor(this.cursor.sort(Util.createBDObject(obj)));
},
count: function() {
return this.cursor.count();
@@ -185,54 +172,14 @@ Cursor.prototype = {
return this.cursor.length();
},
explain: function() {
- return new ScriptableMap(MongoSupport.ensureStringId(cursor.explain()) || {});
+ return Util.ensureStringId(this.cursor.explain()) || {};
},
toArray: function() {
- return MongoSupport.toJSArray(cursor.toArray())
- .map(function(doc) { return MongoSupport.ensureStringId(doc); });
- },
-
- hasNext: function() {
- return this.cursor.hasNext();
- },
-
- next: function() {
- var sm = this.cursor.next() || {};
- return checkforObjectId(sm);
- },
-
- limit: function(lim) {
- return new Cursor(cursor.limit(lim));
- },
-
- skip: function(num) {
- return new Cursor(cursor.skip(num));
- },
-
- sort: function(obj) {
- var dbo = (obj) ? new BasicDBObject(obj) : new BasicDBObject();
- return new Cursor(cursor.sort(dbo));
- },
-
- count: function() {
- return cursor.count();
- },
-
- length: function() {
- return cursor.length();
- },
-
- explain: function() {
- return new ScriptableMap(checkforObjectId(cursor.explain()) || {});
- },
-
- toArray: function() {
- var list = new ScriptableList(cursor.toArray());
- return list.map( function(doc) {
- return checkforObjectId(doc);
- });
- },
+ var obj = Util.toJSArray(this.cursor.copy().toArray().toArray());
+ return obj.map(function(doc) { return Util.ensureStringId(doc) });
+ }
}
if (typeof exports != "undefined")
- exports.Mongo = Mongo;
+ exports.Cursor = Cursor;
+

0 comments on commit e943adc

Please sign in to comment.