Permalink
Browse files

More Mongo

  • Loading branch information...
1 parent ca9cf8c commit be9bb114c35c5ef2f4c5241542806b7a955e4492 mde committed Sep 2, 2012
Showing with 66 additions and 14 deletions.
  1. +64 −13 lib/adapters/mongo/index.js
  2. +2 −1 test/adapters/shared.js
@@ -86,10 +86,13 @@ utils.mixin(Adapter.prototype, new (function () {
//}
}
else {
- inst = query.model.create(data);
- inst.id = id;
- inst._saved = true;
- res.push(inst);
+ if (data) {
+ inst = query.model.create(data);
+ inst.id = id;
+ inst._id = data._id;
+ inst._saved = true;
+ res.push(inst);
+ }
// If explicitly limited to one, just return the single instance
// This is also used by the `first` method
if (query.opts.limit == 1) {
@@ -99,16 +102,11 @@ utils.mixin(Adapter.prototype, new (function () {
}
});
}
- // Teh mapreducy
+ // Collection
else {
- /*
- conditions = this._serializeConditions(query.conditions);
+ conditions = this._serializeConditions(query.rawConditions);
sort = this._serializeSortOrder(query.opts.sort);
- requestOpts = {
- url: '/mapred'
- , method: 'POST'
- , data: _mapReduceQuery(bucket, conditions, sort)
- };
+ /*
this.request(requestOpts, function (err, data) {
var rows
, res = [];
@@ -137,9 +135,61 @@ utils.mixin(Adapter.prototype, new (function () {
this.update = function (data, query, opts, callback) {
+ var collectionName = _collectionizeModelName(query.model.modelName)
+ , collection = this.client.collection(collectionName)
+ , id = query.byId
+ , requestOpts
+ , item = data;
+ // Single instance-lookup by id
+ if (id) {
+ // Bail out if instance isn't valid
+ if (!item.isValid()) {
+ return callback(data.errors, null);
+ }
+
+ item = item.toData({whitelist: ['_id', 'id', 'createdAt']});
+
+ collection.update({id: id}, item, function (err, data) {
+ if (err) {
+ callback(err, null);
+ }
+ else {
+ // FIXME: What is the right data to return here? Right now this
@Techwraith

Techwraith Sep 2, 2012

Mongo supports something called at "set" operator: http://www.mongodb.org/display/DOCS/Updating

It might be best to use that here instead of just overwriting the doc.

@mde

mde Sep 2, 2012

Contributor

I played around with $set, but I couldn't get it to work right. If you have a spare moment, feel free to play around with the tests. :)

@Techwraith

Techwraith Sep 2, 2012

Will do after the honeymoon :)

+ // is basically overwriting a doc, but we might be supporting
+ // bulk-updates at some point
+ callback(null, true);
+ }
+ });
+ }
+ // Bulk update?
+ else {
+ callback(new Error('Bulk update is not supported'), null);
+ }
};
this.remove = function (query, opts, callback) {
+ var collectionName = _collectionizeModelName(query.model.modelName)
+ , collection = this.client.collection(collectionName)
+ , id = query.byId
+ , requestOpts;
+
+ // Single instance-lookup by id
+ if (id) {
+ collection.remove({id: id}, function (err, data) {
+ var inst
+ , res = [];
+ if (err) {
+ callback(err, null);
+ }
+ else {
+ callback(null, true);
+ }
+ });
+ }
+ // Collection
+ else {
+ callback(new Error('Bulk remove is not supported'), null);
+ }
};
this.insert = function (data, opts, callback) {
@@ -158,12 +208,13 @@ utils.mixin(Adapter.prototype, new (function () {
item.id = id;
item = item.toData({whitelist: ['id', 'createdAt']});
- collection.save(item, function (err, res) {
+ collection.insert(item, function (err, res) {
if (err) {
callback(err, null);
}
else {
item.id = id;
+ item._id = res._id;
item._saved = true;
ret.push(data);
insert();
View
@@ -30,7 +30,7 @@ tests = {
next();
});
}
-/*
+
// TODO: Load via array of ids
, 'test first via object': function (next) {
@@ -101,6 +101,7 @@ tests = {
});
}
+/*
, 'test all, by string LIKE case-sensitive': function (next) {
Zooby.all({foo: {'like': 'B'}}, {}, function (err, data) {
if (err) {

0 comments on commit be9bb11

Please sign in to comment.