Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added support for Object Id creation and validation, including it on …

…BDObject creation.
  • Loading branch information...
commit 5f0fe47463d7e3582c9098dfe391f867c67d84a3 1 parent 662dabf
@sergi authored
Showing with 38 additions and 13 deletions.
  1. +9 −5 lib/collection.js
  2. +2 −2 lib/cursor.js
  3. +27 −6 lib/mongosupport.js
View
14 lib/collection.js
@@ -122,9 +122,8 @@ Collection.prototype = {
return null;
},
save: function(obj) {
- var dbo = Support.createBDObject(obj);
- var sm = this.collection.save(dbo) || {};
- return Support.ensureStringId(sm);
+ var dbo = Support.createBDObject(obj, true);
+ this.collection.save(dbo);
},
/**
* Using the JavaScript way instead of the Java method because the latter
@@ -159,10 +158,15 @@ Collection.prototype = {
},
insert: function(docs) {
var insertable = docs.isArray && docs.isArray() ?
- docs.map(Support.createBDObject) : Support.createBDObject(docs);
-
+ docs.map(function(d) { Support.createBDObject(d, true); }) : Support.createBDObject(docs, true);
this.collection.insert(insertable);
},
+ update: function(criteria, obj) {
+ if (criteria && typeof(criteria) != "object") {
+ criteria = { "_id": criteria };
+ }
+ this.collection.update(Support.createBDObject(criteria), Support.createBDObject(obj));
+ },
resetIndexCache: function() {
this._indexCache = {};
this.collection.resetIndexCache();
View
4 lib/cursor.js
@@ -28,7 +28,7 @@ Cursor.prototype = {
cursor: null,
next: function() {
var sm = this.cursor.next() || {};
- return Support.ensureStringId(sm);
+ return sm;
},
limit: function(lim) {
return new Cursor(this.cursor.limit(lim));
@@ -44,7 +44,7 @@ Cursor.prototype = {
},
toArray: function() {
var obj = this.cursor.copy().toArray().toArray();
- return obj.map(function(el) { return Support.ensureStringId(el) }).slice();
+ return obj.slice();
},
snapshot: function() {
this.cursor.snapshot();
View
33 lib/mongosupport.js
@@ -1,12 +1,20 @@
var MongoSupport = {
- ensureStringId: function(o) {
- if(!typeof o['_id'] == "string")
- o['_id'] = o['_id'].toString();
+ ensureId: function(o) {
+ if (typeof o == "object") {
+ if (o._id) {
+ if (typeof o._id != "string")
+ o._id = o._id.toString();
+ } else {
+ o._id = this.createObjectId();
+ }
+ } else {
+ o = { _id: o.toString() }
+ }
return o;
},
- createBDObject: function(obj) {
- return obj ? new Packages.com.mongodb.BasicDBObject(obj)
- : new Packages.com.mongodb.BasicDBObject();
+ createBDObject: function(obj, ensureId) {
+ var o = new Packages.com.mongodb.BasicDBObject(ensureId ? this.ensureId(obj || {}) : obj || {});
+ return o
},
extend: function( dst , src , deep ) {
for (var k in src){
@@ -16,6 +24,19 @@ var MongoSupport = {
dst[k] = v;
}
return dst;
+ },
+ createObjectId: function(id) {
+ var oid;
+ var objectId = Packages.com.mongodb.ObjectId;
+
+ if (!id)
+ oid = objectId();
+ else if (objectId.isValid(id))
+ oid = id;
+ else
+ throw id + " is not a valid ObjectId.";
+
+ return oid;
}
}

0 comments on commit 5f0fe47

Please sign in to comment.
Something went wrong with that request. Please try again.