2014-12-03 01:08:58 +00:00
|
|
|
/*global define*/
|
|
|
|
|
|
|
|
define(
|
2014-12-03 01:19:30 +00:00
|
|
|
["./CouchDocument"],
|
|
|
|
function (CouchDocument) {
|
2014-12-03 01:08:58 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
function CouchPersistenceProvider($http, $q, SPACE, PATH) {
|
|
|
|
var spaces = [ SPACE ],
|
|
|
|
revs = {};
|
|
|
|
|
2014-12-03 01:26:16 +00:00
|
|
|
// Convert a subpath to a full path, suitable to pass
|
|
|
|
// to $http.
|
2014-12-03 01:08:58 +00:00
|
|
|
function url(subpath) {
|
|
|
|
return PATH + '/' + subpath;
|
|
|
|
}
|
|
|
|
|
2014-12-03 01:26:16 +00:00
|
|
|
// Issue a request using $http; get back the plain JS object
|
|
|
|
// from the expected JSON response
|
2014-12-03 01:08:58 +00:00
|
|
|
function request(subpath, method, value) {
|
|
|
|
return $http({
|
|
|
|
method: method,
|
|
|
|
url: url(subpath),
|
|
|
|
data: value
|
|
|
|
}).then(function (response) {
|
|
|
|
return response.data;
|
|
|
|
}, function () {
|
|
|
|
return undefined;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-12-03 01:26:16 +00:00
|
|
|
// Shorthand methods for various HTTP methods
|
2014-12-03 01:19:30 +00:00
|
|
|
function get(subpath) {
|
|
|
|
return request(subpath, "GET");
|
|
|
|
}
|
|
|
|
|
|
|
|
function put(subpath, value) {
|
|
|
|
return request(subpath, "PUT", value);
|
|
|
|
}
|
|
|
|
|
2014-12-03 01:08:58 +00:00
|
|
|
function getIdsFromAllDocs(allDocs) {
|
|
|
|
return allDocs.rows.map(function (r) { return r.id; });
|
|
|
|
}
|
|
|
|
|
|
|
|
/*jslint nomen: true */ // Allow the _id and _rev that couch provides
|
|
|
|
function getModel(response) {
|
|
|
|
if (response && response.model) {
|
|
|
|
revs[response._id] = response._rev;
|
|
|
|
return response.model;
|
|
|
|
} else {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkResponse(response) {
|
|
|
|
if (response && response.ok) {
|
|
|
|
revs[response.id] = response.rev;
|
|
|
|
return response.ok;
|
|
|
|
} else {
|
2014-12-03 01:53:55 +00:00
|
|
|
return false;
|
2014-12-03 01:08:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
listSpaces: function () {
|
|
|
|
return $q.when(spaces);
|
|
|
|
},
|
|
|
|
listObjects: function (space) {
|
|
|
|
return get("_all_docs").then(getIdsFromAllDocs);
|
|
|
|
},
|
|
|
|
createObject: function (space, key, value) {
|
|
|
|
return put(key, new CouchDocument(key, value))
|
|
|
|
.then(checkResponse);
|
|
|
|
},
|
|
|
|
readObject: function (space, key) {
|
|
|
|
return get(key).then(getModel);
|
|
|
|
},
|
|
|
|
updateObject: function (space, key, value) {
|
2014-12-03 01:26:16 +00:00
|
|
|
return put(key, new CouchDocument(key, value, revs[key]))
|
2014-12-03 01:08:58 +00:00
|
|
|
.then(checkResponse);
|
|
|
|
},
|
|
|
|
deleteObject: function (space, key, value) {
|
2014-12-03 01:26:16 +00:00
|
|
|
return put(key, new CouchDocument(key, value, revs[key], true))
|
2014-12-03 01:08:58 +00:00
|
|
|
.then(checkResponse);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return CouchPersistenceProvider;
|
|
|
|
}
|
|
|
|
);
|