Skip to content

Commit

Permalink
[merge] Merge from master branch
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Mar 30, 2011
2 parents 0e1e9b5 + a885aeb commit 8a436f9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
22 changes: 7 additions & 15 deletions lib/resourcer/engines/database.js
Expand Up @@ -8,12 +8,12 @@ resourcer.Cache = require('resourcer/cache').Cache;

this.Connection = function (config) {
this.connection = new(cradle.Connection)({
host: config.host || '127.0.0.1',
host: config.uri || '127.0.0.1',
port: config.port || 5984,
raw: true,
cache: false,
auth: config && config.auth || null
}).database(config.uri || resourcer.env);
}).database(config.database || resourcer.env);
this.cache = new(resourcer.Cache);
};

Expand Down Expand Up @@ -56,21 +56,13 @@ this.Connection.prototype = {
return this.put.apply(this, arguments);
},
update: function (id, doc, callback) {
var that = this;

if (this.cache.has(id)) {
that.put(id, resourcer.mixin({}, this.cache.get(id).toJSON(), doc), callback);
} else {
this.get(id, function (e, res) {
var obj = resourcer.mixin({}, res, doc);
that.cache.put(id, res);
that.put(id, obj, callback);
});
}
this.request('merge', id, doc, callback);
},
destroy: function () {
var args = Array.prototype.slice.call(arguments);

var that = this,
args = Array.prototype.slice.call(arguments),
id = args.shift();

if (this.cache.has(id)) {
args.splice(1, -1, this.cache.get(id)._rev);
return this.request.apply(this, ['remove'].concat(args));
Expand Down
12 changes: 10 additions & 2 deletions lib/resourcer/resource.js
Expand Up @@ -248,8 +248,16 @@ this.Resource.sync = function (callback) {
that._design._rev = headers.etag.slice(1, -1);
}
that.connection.put(id, that._design, function (e, res) {
if (e) {/* TODO: Catch errors here. Needs a rewrite, because of the race */}
/* condition, when the design doc is trying to be written in parallel */
if (e) {
if (e.reason === 'no_db_file') {
that.connection.connection.create(function () {
that.sync(callback);
});
}

/* TODO: Catch errors here. Needs a rewrite, because of the race */
/* condition, when the design doc is trying to be written in parallel */
}
else {
// We might not need to wait for the document to be
// persisted, before returning it. If for whatever reason
Expand Down

0 comments on commit 8a436f9

Please sign in to comment.