diff --git a/bundles.json b/bundles.json index d7edba5db7..1e3746129d 100644 --- a/bundles.json +++ b/bundles.json @@ -6,6 +6,5 @@ "platform/commonUI/edit", "platform/commonUI/dialog", "platform/commonUI/general", - - "example/persistence" + "platform/persistence" ] \ No newline at end of file diff --git a/platform/persistence/bundle.json b/platform/persistence/bundle.json new file mode 100644 index 0000000000..64d60f2504 --- /dev/null +++ b/platform/persistence/bundle.json @@ -0,0 +1,24 @@ +{ + "name": "Couch Persistence", + "description": "Adapter to read and write objects using a CouchDB instance.", + "extensions": { + "components": [ + { + "provides": "persistenceService", + "type": "provider", + "implementation": "CouchPersistenceProvider.js", + "depends": [ "$http", "$q", "PERSISTENCE_SPACE", "COUCHDB_PATH" ] + } + ], + "constants": [ + { + "key": "PERSISTENCE_SPACE", + "value": "mct" + }, + { + "key": "COUCHDB_PATH", + "value": "/couch/openmct" + } + ] + } +} \ No newline at end of file diff --git a/platform/persistence/src/CouchPersistenceProvider.js b/platform/persistence/src/CouchPersistenceProvider.js new file mode 100644 index 0000000000..85a5800ec6 --- /dev/null +++ b/platform/persistence/src/CouchPersistenceProvider.js @@ -0,0 +1,99 @@ +/*global define*/ + +define( + [], + function () { + 'use strict'; + + function CouchPersistenceProvider($http, $q, SPACE, PATH) { + var spaces = [ SPACE ], + revs = {}; + + function url(subpath) { + return PATH + '/' + subpath; + } + + function request(subpath, method, value) { + return $http({ + method: method, + url: url(subpath), + data: value + }).then(function (response) { + return response.data; + }, function () { + return undefined; + }); + } + + function get(subpath) { return request(subpath, "GET"); } + function put(subpath, value) { return request(subpath, "PUT", value); } + function del(subpath, value) { return request(subpath, "DELETE", value); } + + 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 { + return undefined; + } + } + + function CouchDocument(key, value, includeRevision, markDeleted) { + return { + "_id": key, + "_rev": includeRevision ? revs[key] : undefined, + "_deleted": markDeleted, + "metadata": { + "category": "domain object", + "type": value.type, + "owner": "admin", + "name": value.name, + "created": Date.now() + }, + "model": value + }; + } + + 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) { + return put(key, new CouchDocument(key, value, true)) + .then(checkResponse); + }, + deleteObject: function (space, key, value) { + return put(key, new CouchDocument(key, value, true, true)) + .then(checkResponse); + } + }; + + } + + return CouchPersistenceProvider; + } +); \ No newline at end of file