Skip to content

Commit

Permalink
Finished and added Cursor class
Browse files Browse the repository at this point in the history
  • Loading branch information
sergi committed Oct 24, 2009
1 parent 6e9c14b commit 21644a9
Showing 1 changed file with 49 additions and 23 deletions.
72 changes: 49 additions & 23 deletions lib/mongodb.js
Expand Up @@ -4,15 +4,15 @@ 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

this.host = host || 'localhost';
this.port = port || 27017;
this.db = new MongoJava(this.host, this.port).getDB(dbname);
Expand Down Expand Up @@ -104,7 +104,7 @@ Collection.prototype = {
},

find: function(obj) {
var dbo = new BasicDBObject(obj ? obj : null);
var dbo = obj ? new BasicDBObject(obj) : new BasicDBObject();
return new Cursor(this.collection.find(dbo));
},

Expand All @@ -128,20 +128,20 @@ Collection.prototype = {
var dbo = (obj) ? new BasicDBObject(obj) : new BasicDBObject();
this.collection.ensureIndex(dbo);
},

ensureIDIndex: function() {
this.collection.ensureIDIndex();
},

getIndexInfo: function() {
return new ScriptableList(this.collection.getIndexInfo() || {});
},

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;
Expand All @@ -155,51 +155,77 @@ Collection.prototype = {
}
}

/*
Light wrapper around com.mongodb.DBCursor
- mostly pass calls through, but translate between
maps/arrays <-> JS objects where appropriate.
*/
function Cursor(cursor) {
this.cursor = cursor;
}

Cursor.prototype = {

cursor: null,
hasNext: function() {
return this.cursor.hasNext();
},
next: function() {
var sm = this.cursor.next() || {};
return MongoSupport.ensureStringId(sm);
},
limit: function(lim) {
return new Cursor(this.cursor.limit(lim));
},
skip: function(num) {
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));
},
count: function() {
return this.cursor.count();
},
length: function() {
return this.cursor.length();
},
explain: function() {
return new ScriptableMap(MongoSupport.ensureStringId(cursor.explain()) || {});
},
toArray: function() {
return MongoSupport.toJSArray(cursor.toArray())
.map(function(doc) { return MongoSupport.ensureStringId(doc); });
},

hasNext: function() {
return cursor.hasNext();
return this.cursor.hasNext();
},

next: function() {
var sm = new ScriptableMap(cursor.next() || {});
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) {
Expand Down

0 comments on commit 21644a9

Please sign in to comment.