[Persistence] Initial import of CouchDB adapter

Initial import of CouchDB adapter from the pre-Angular
OpenMCT. Revise to use Angular services ().
WTD-537.
This commit is contained in:
Victor Woeltjen 2014-12-02 17:08:58 -08:00
parent 0bacc03e58
commit 2ce5ebcf20
3 changed files with 124 additions and 2 deletions

View File

@ -6,6 +6,5 @@
"platform/commonUI/edit",
"platform/commonUI/dialog",
"platform/commonUI/general",
"example/persistence"
"platform/persistence"
]

View File

@ -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"
}
]
}
}

View File

@ -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;
}
);