Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merging in alexanderedge's changes.

 * Simplified `lib/spatial.js` (prototypal inheritance FTW)
 * Added a unit test
  • Loading branch information...
commit 597f6c564f9a91a3d0e6e4ab5347cdc42aea9597 1 parent 7c20c41
Dominic Barnes authored
Showing with 87 additions and 169 deletions.
  1. +26 −26 lib/designdoc.js
  2. +2 −134 lib/spatial.js
  3. +11 −9 lib/view.js
  4. +48 −0 test/spatial.js
View
52 lib/designdoc.js
@@ -59,6 +59,32 @@ proto.views = function (views) {
};
/**
+ * Set a named view
+ *
+ * @param {function} map
+ * @param {function|string} [reduce]
+ *
+ * @return {object} this|view
+ */
+proto.view = function (name, map, reduce) {
+ if (map) {
+ var body = {
+ map: util.formatFunction(map)
+ };
+
+ if (reduce) {
+ body.reduce = util.formatFunction(reduce);
+ }
+
+ this.views()[name] = body;
+
+ return this;
+ } else {
+ return view.create(this, name);
+ }
+};
+
+/**
* Get/set the spatials
*
* @param {object} [spatials]
@@ -87,32 +113,6 @@ proto.spatials = function (spatials) {
};
/**
- * Set a named view
- *
- * @param {function} map
- * @param {function|string} [reduce]
- *
- * @return {object} this|view
- */
-proto.view = function (name, map, reduce) {
- if (map) {
- var body = {
- map: util.formatFunction(map)
- };
-
- if (reduce) {
- body.reduce = util.formatFunction(reduce);
- }
-
- this.views()[name] = body;
-
- return this;
- } else {
- return view.create(this, name);
- }
-};
-
-/**
* Set a named spatial
*
* @param {function} map
View
136 lib/spatial.js
@@ -1,136 +1,4 @@
-var client = require("./client"),
- doc = require("./document");
-
-// inherit from client
-var proto = Object.create(client);
-
-// dependent on url
-Object.defineProperty(proto, "name", {
- get: function () {
- return this._url.path.split("/")[5];
- },
- set: function (v) {
- var path = this._url.path.split("/");
- path[5] = v;
- this._url.path = this._url.pathname = path.join("/");
- }
-});
-
-/**
- * Perform a generic query against a stored spatial
- *
- * GET /db/ddoc/_spatial/spatial
- *
- * @param {object} [query] If only one of the optional params is provided, this is assumed to be the one
- * @param {mixed} [data] Array = pass as `keys` in body. Other = pass as complete body
- * @param {function} callback
- *
- * @return {object} this
- */
-proto.query = function (query, data, callback) {
- if (typeof query === "function") {
- callback = query;
- data = null;
- query = null;
- }
- if (typeof data === "function") {
- callback = data;
- data = null;
- }
-
- var opts = { query: query || {} };
-
- if (data) {
- return this._post(opts, Array.isArray(data) ? { keys: data } : data, callback);
- } else {
- return this._get(opts, callback);
- }
-};
-
-/**
- * Perform a map query against a stored spatial
- *
- * GET /db/ddoc/_spatial/spatial
- *
- * @param {object} [query]
- * @param {function} callback
- *
- * @return {object} this
- */
-proto.map = function (query, callback) {
- if (typeof query === "function") {
- callback = query;
- query = {};
- }
-
- query.reduce = false;
-
- return this.query(query, callback);
-};
-
-/**
- * Perform a reduce query against a stored spatial
- *
- * GET /db/ddoc/_spatial/spatial
- *
- * @param {object} [query]
- * @param {function} callback
- *
- * @return {object} this
- */
-proto.reduce = function (query, callback) {
- if (typeof query === "function") {
- callback = query;
- query = {};
- }
-
- query.reduce = true;
-
- return this.query(query, callback);
-};
-
-/**
- * Execute a list function for the current document
- *
- * GET /db/_design/design-doc/_list/list-name/doc
- *
- * @param {string} list The name of the list function in the above design document
- * `ddoc/list` ...or... `list` if using same design document as this spatial
- * @param {object} [query]
- * @param {function} callback
- *
- * @return {object} this
- */
-proto.list = function (list, query, callback) {
- if (typeof query === "function") {
- callback = query;
- query = null;
- }
-
- var list_ddoc, spatial, path, url;
-
- if (list.indexOf("/") > -1) {
- list = list.split("/");
- list_ddoc = list[0];
- list = list[1];
- spatial = this.ddoc.name + "/" + this.name;
- } else {
- list_ddoc = this.ddoc.name;
- spatial = this.name;
- }
-
- path = ["", this.db.name, "_design", list_ddoc, "_list", list, spatial];
- url = {
- replace: true,
- pathname: path.join("/")
- };
-
- if (query) {
- url.query = query;
- }
-
- return this._get(url, callback);
-};
+var view = require("./view");
/**
* Create a new spatial object
@@ -141,7 +9,7 @@ proto.list = function (list, query, callback) {
* @return {object} spatial
*/
exports.create = function (ddoc, name) {
- var spatial = Object.create(proto);
+ var spatial = Object.create(view.proto);
spatial.ddoc = ddoc;
spatial.db = ddoc.db;
View
20 lib/view.js
@@ -1,5 +1,4 @@
-var client = require("./client"),
- doc = require("./document");
+var client = require("./client");
// inherit from client
var proto = Object.create(client);
@@ -30,20 +29,20 @@ Object.defineProperty(proto, "name", {
proto.query = function (query, data, callback) {
if (typeof query === "function") {
callback = query;
- data = null;
+ data = null;
query = null;
}
- if (typeof data === "function") {
- callback = data;
+ if (typeof data === "function") {
+ callback = data;
data = null;
- }
+ }
var opts = { query: query || {} };
- if (data) {
+ if (data) {
return this._post(opts, Array.isArray(data) ? { keys: data } : data, callback);
- } else {
- return this._get(opts, callback);
+ } else {
+ return this._get(opts, callback);
}
};
@@ -132,6 +131,9 @@ proto.list = function (list, query, callback) {
return this._get(url, callback);
};
+
+exports.proto = proto;
+
/**
* Create a new view object
*
View
48 test/spatial.js
@@ -0,0 +1,48 @@
+var _ = require("underscore"),
+ test = require("assert"),
+ config = require("./config"),
+ couchdb = require("../"),
+ server = couchdb.srv(config.host, config.port, config.ssl),
+ db = server.db("test_db_view"),
+ ddoc = db.ddoc("test_ddoc_1"),
+ spatial_name = "test_spatial_1";
+
+module.exports = {
+ before: function (done) {
+ server.debug = config.debug;
+ if (!config.party) {
+ server.setUser(config.user, config.pass);
+ }
+ done();
+ },
+
+ after: function (done) {
+ done();
+ },
+
+ "Spatial": function (done) {
+ test.ifError(ddoc._body.spatials);
+
+ ddoc.spatial(spatial_name, function (doc) {
+ emit(doc._id);
+ });
+
+ test.ok(ddoc._body.spatials);
+ test.ok(ddoc._body.spatials[spatial_name]);
+ test.ok(ddoc._body.spatials[spatial_name].map);
+ test.ifError(ddoc._body.spatials[spatial_name].reduce);
+
+ ddoc.spatial(spatial_name, function (doc) {
+ emit(doc._id);
+ }, function (values, key, rereduce) {
+ return values.length;
+ });
+
+ test.ok(ddoc._body.spatials);
+ test.ok(ddoc._body.spatials[spatial_name]);
+ test.ok(ddoc._body.spatials[spatial_name].map);
+ test.ok(ddoc._body.spatials[spatial_name].reduce);
+
+ done();
+ }
+};
Please sign in to comment.
Something went wrong with that request. Please try again.