Skip to content
This repository has been archived by the owner on May 9, 2018. It is now read-only.

Commit

Permalink
Merge flatiron#147 from minthead/master
Browse files Browse the repository at this point in the history
  • Loading branch information
ndrut committed Aug 5, 2013
2 parents 4c457fe + bf3c369 commit b5258b6
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 34 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
language: node_js
node_js:
- 0.4
- 0.6
- 0.7
install: npm install -d
16 changes: 11 additions & 5 deletions lib/cradle.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,20 @@ cradle.Connection.prototype.request = function (options, callback) {
options.headers["Content-Length"] = Buffer.byteLength(options.body);
options.headers["Content-Type"] = "application/json";
}

if (options.cache) {
options.headers["If-None-Match"] = options.cache.etag;
}

if (options.method === "DELETE" && !options.headers["Content-Length"]) {
options.headers["Content-Length"] = 0;
}

var attempts = 0;
return this.rawRequest(options, function _onResponse(err, res, body) {
attempts++;
return this.rawRequest(options, function (err, res, body) {
if (options.cache && res.statusCode === 304) {
return callback(null, options.cache.store, true, res.headers.etag);
}

if (err) {
if (self.options.retries &&
(!options.method || options.method.toLowerCase() === 'get' || options.body) &&
Expand All @@ -220,7 +226,7 @@ cradle.Connection.prototype.request = function (options, callback) {
body.headers.status = res.statusCode;
return callback(body);
}

try { body = JSON.parse(body) }
catch (err) { }

Expand All @@ -230,7 +236,7 @@ cradle.Connection.prototype.request = function (options, callback) {
return callback(body);
}

callback(null, self.options.raw ? body : new cradle.Response(body, res));
callback(null, (self.options.raw || options.raw) ? body : new cradle.Response(body, res), false, res.headers.etag);
});
};

Expand Down
32 changes: 22 additions & 10 deletions lib/cradle/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ this.Cache = function (options) {

this.Cache.prototype = {
// API
get: function (id) { return this.query('get', id) },
save: function (id, doc) { return this.query('save', id, doc) },
purge: function (id) { return this.query('purge', id) },
has: function (id) { return this.query('has', id) },
get: function (id) { return this.query('get', id) },
headers: function (id) { return this.query('headers', id) },
save: function (id, doc) { return this.query('save', id, doc) },
purge: function (id) { return this.query('purge', id) },
has: function (id) { return this.query('has', id) },

_get: function (id) {
var entry;
Expand All @@ -42,6 +43,13 @@ this.Cache.prototype = {
}
}
},
_headers: function (id) {
if (id in this.store) {
return this.store[id].document.headers;
} else {
return null;
}
},
_has: function (id) {
return id in this.store;
},
Expand Down Expand Up @@ -93,10 +101,14 @@ this.Cache.prototype = {
};

function clone(obj) {
return Object.keys(obj).reduce(function (clone, k) {
if (! obj.__lookupGetter__(k)) {
clone[k] = obj[k];
}
return clone;
}, {});
if (Array.isArray(obj)) {
return obj.slice(0);
} else {
return Object.keys(obj).reduce(function (clone, k) {
if (! obj.__lookupGetter__(k)) {
clone[k] = obj[k];
}
return clone;
}, {});
}
}
43 changes: 39 additions & 4 deletions lib/cradle/database/documents.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Database.prototype.head = function (id, callback) {
Database.prototype.get = function (id, rev) {
var args = new (Args)(arguments),
options = null,
cache = null,
that = this;

if (Array.isArray(id)) { // Bulk GET
Expand All @@ -35,13 +36,15 @@ Database.prototype.get = function (id, rev) {
if (typeof(rev) === 'string') { options = { rev: rev } }
else if (typeof(rev) === 'object') { options = rev }
} else if (this.cache.has(id)) {
return args.callback(null, this.cache.get(id));
cache = { store: this.cache.get(id) };
cache.etag = '"' + cache.store._rev + '"';
}
this.query({
path: cradle.escape(id),
query: options
}, function (err, res) {
if (! err) that.cache.save(res.id, res.json);
query: options,
cache: cache
}, function (err, res, cached) {
if (! err && ! cached) that.cache.save(res.id, res.json);
args.callback(err, res);
});
}
Expand Down Expand Up @@ -234,4 +237,36 @@ Database.prototype.remove = function (id, rev) {
}

remove();

};

// Query a show, passing any options to the query string.
// Some query string parameters' values have to be JSON-encoded.
Database.prototype.show = function (path, options) {
var args = new(Args)(arguments),
that = this,
cachepath = path,
cache = null;

path = path.split('/');
path = ['_design', path[0], '_show', path[1], path[2]].map(querystring.escape).join('/');

if (typeof(options) === 'object') {
cachepath += '?' + querystring.stringify(options);
}

if (this.cache.has(cachepath)) {
cache = { store: this.cache.get(cachepath).value, etag: this.cache.headers(cachepath).etag };
}

return this.query({
method: 'GET',
path: path,
query: options,
raw: true,
cache: cache
}, function(err, res, cached, etag) {
if (! err && ! cached) that.cache.save(cachepath, { value: res, headers: { etag: etag } });
args.callback(err, (! cached && Array.isArray(res)) ? res.slice(0) : res, etag);
});
};
81 changes: 66 additions & 15 deletions lib/cradle/database/views.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,38 @@ Database.prototype.all = function (options, callback) {
callback = options;
options = {};
}

return this._getOrPostView('/_all_docs', options, callback);
return this._getOrPostView('/_all_docs', { query: options }, callback);
};

// Query a view, passing any options to the query string.
// Some query string parameters' values have to be JSON-encoded.
Database.prototype.view = function (path, options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}
Database.prototype.view = function (path, options) {
var callback = new(Args)(arguments).callback,
body,
cachepath,
cache = null,
that = this;
>>>>>>> bf3c3697e07a6773de4a2ccbf4beda565c103462

path = path.split('/');
path = ['_design', path[0], '_view', path[1]].map(querystring.escape).join('/');

return this._getOrPostView(path, options, callback);
cachepath = path;

if (!options.body) {
if (options && typeof options === 'object') {
cachepath += '?' + querystring.stringify(options);
}
if(this.cache.has(cachepath)) {
cache = { store: this.cache.get(cachepath), etag: this.cache.headers(cachepath).etag };
}
}

return this._getOrPostView(path, { query: options, cache: cache }, function(err, res, cached, etag) {
if (! err && ! cached) that.cache.save(cachepath, res);
callback(err, (!cached && Array.isArray(res)) ? res.slice(0) : res, etag);
});
};

Database.prototype.fti = function (path, options, callback) {
Expand Down Expand Up @@ -83,22 +99,40 @@ Database.prototype.compact = function (design) {
// Query a list, passing any options to the query string.
// Some query string parameters' values have to be JSON-encoded.
Database.prototype.list = function (path, options) {
var callback = new(Args)(arguments).callback;
path = path.split('/');
var callback = new(Args)(arguments).callback,
cachepath = path,
cache = null,
that = this;

path = path.split('/'),
path = ['_design', path[0], '_list', path[1], path[2]].map(querystring.escape).join('/');

if (!options.body) {
if (options && typeof options === 'object') {
cachepath += '?' + querystring.stringify(options);
}
if(this.cache.has(cachepath)) {
cache = { store: this.cache.get(cachepath).value, etag: this.cache.headers(cachepath).etag };
}
}

this._getOrPostView(
['_design', path[0], '_list', path[1], path[2]].map(querystring.escape).join('/'),
options,
callback
path,
{ query: options, cache: cache, raw: true },
function(err, res, cached, etag) {
if (! err && ! cached) that.cache.save(cachepath, { value: res, headers: { etag: etag } });
callback(err, (! cached && Array.isArray(res)) ? res.slice(0) : res, etag);
}
);
};

//
// Helper function which parses options and makes either a `GET`
// or `POST` request to `path` depending on if `options.keys` or
// `options.body` is present.
// or `POST` request to `path` depending on if `options.query.keys` or
// `options.query.body` is present.
//
Database.prototype._getOrPostView = function (path, options, callback) {
<<<<<<< HEAD
options = parseOptions(options);

if (options && options.body) {
Expand All @@ -118,6 +152,23 @@ Database.prototype._getOrPostView = function (path, options, callback) {
path: path,
query: options
}, callback);
=======
var query = parseOptions(options.query);

if (query && query.body) {
options.body = query.body;
delete query.body;

options.method = 'POST';
} else {
options.method = 'GET';
}

options.path = path;
options.query = query;

return this.query(options, callback);
>>>>>>> bf3c3697e07a6773de4a2ccbf4beda565c103462
}

//
Expand Down

0 comments on commit b5258b6

Please sign in to comment.