Permalink
Browse files

fixing issue 17: querystring params for db.tempView() fix

  • Loading branch information...
1 parent 4b8369c commit 7dd7666bb27a0c5d6f97b1cc04fc4fa98a039a15 Dominic Barnes committed Jan 12, 2013
Showing with 92 additions and 29 deletions.
  1. +29 −10 lib/client.js
  2. +15 −7 lib/database.js
  3. +48 −12 test/db.js
View
@@ -38,7 +38,7 @@ proto._set_url = function (input, options) {
}
delete input.pathname;
}
- if ("query" in input) {
+ if ("query" in input) {
if (input.replace) {
uri.query = _.clone(input.query);
} else {
@@ -141,17 +141,36 @@ proto._callback = function (callback, options) {
*/
proto._stream = function (stream, callback, options) {
if (options.changes) {
+ var buf = "",
+ timer = null;
+
stream.on("data", function (chunk) {
- if (typeof chunk === "string") {
- chunk = chunk.trim();
- }
+ if (typeof chunk !== "string") return;
+
+ buf += chunk.trim();
+
if (chunk) {
- chunk = JSON.parse(chunk);
- if (chunk.seq) {
- stream.emit("change", chunk);
- }
- if (chunk.last_seq) {
- stream.emit("last", chunk.last_seq);
+ try {
+ chunk = JSON.parse(buf);
+
+ buf = "";
+ if (timer) {
+ clearTimeout(timer);
+ timer = null;
+ }
+
+ if (chunk.seq) {
+ stream.emit("change", chunk);
+ }
+ if (chunk.last_seq) {
+ stream.emit("last", chunk.last_seq);
+ }
+ } catch (e) {
+ if (!timer) {
+ timer = setTimeout(function () {
+ stream.emit("error", e);
+ }, 500);
+ }
}
}
});
View
@@ -275,18 +275,26 @@ prototype.security = function (obj, callback) {
* @return {Object} chainable
*/
prototype.tempView = function (map, reduce, query, callback) {
- var args = _.toArray(arguments),
- body = { map: util.formatFunction(args.shift()) };
+ var body = { map: util.formatFunction(map) },
+ url;
- callback = args.pop();
+ if (typeof reduce === "function") {
+ callback = reduce;
+ query = null;
+ reduce = null;
+ }
+ if (typeof query === "function") {
+ callback = query;
+ query = null;
+ }
- if (args[0]) {
- body.reduce = util.formatFunction(args[0]);
+ if (reduce) {
+ body.reduce = typeof reduce === "string" ? reduce : util.formatFunction(reduce);
}
- query = args[2] || null;
+ url = !!query ? { pathname: "_temp_view", query: query } : "_temp_view";
- return this._post("_temp_view", body, query, callback);
+ return this._post(url, body, callback);
};
/**
View
@@ -182,12 +182,15 @@ module.exports = {
});
stream.on("end", function () {
- test.equal(changes, 2);
+ test.equal(changes, 3);
done();
});
db.doc({ foo: "bar" }).save(check);
db.doc({ hello: "world" }).save(check);
+ db.doc({
+ test: new Buffer(1024).toString()
+ }).save(check);
}
}));
}
@@ -281,18 +284,51 @@ module.exports = {
}));
}
},
- "Temporary View": function (done) {
- var map = function (doc) {
- emit(null, doc);
- };
+ "Temporary View": {
+ map: function (done) {
+ var map = function (doc) {
+ emit(null, doc);
+ };
- test.strictEqual(db, db.tempView(map, function (err, body, res) {
- test.ifError(err);
- if (body) {
- test.ok(body.rows);
- }
- done();
- }));
+ test.strictEqual(db, db.tempView(map, function (err, body, res) {
+ test.ifError(err);
+ if (body) {
+ test.ok(body.rows);
+ }
+ done();
+ }));
+ },
+ reduce: function (done) {
+ var map = function (doc) {
+ emit(null, doc);
+ };
+
+ var reduce = "_count";
+
+ test.strictEqual(db, db.tempView(map, reduce, function (err, body, res) {
+ test.ifError(err);
+ if (body) {
+ test.ok(body.rows);
+ }
+ done();
+ }));
+ },
+ query: function (done) {
+ var map = function (doc) {
+ emit(null, doc);
+ };
+
+ test.strictEqual(db, db.tempView(map, null, { descending: true }, function (err, body, res) {
+ test.ifError(err);
+ if (body) {
+ test.ok(body.rows);
+ }
+ if (res) {
+ test.equal(res.req.path, db._url.pathname + "/_temp_view?descending=true");
+ }
+ done();
+ }));
+ }
}
}
};

0 comments on commit 7dd7666

Please sign in to comment.