mirror of
https://github.com/nasa/openmct.git
synced 2025-06-23 01:18:57 +00:00
[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:
@ -6,6 +6,5 @@
|
|||||||
"platform/commonUI/edit",
|
"platform/commonUI/edit",
|
||||||
"platform/commonUI/dialog",
|
"platform/commonUI/dialog",
|
||||||
"platform/commonUI/general",
|
"platform/commonUI/general",
|
||||||
|
"platform/persistence"
|
||||||
"example/persistence"
|
|
||||||
]
|
]
|
24
platform/persistence/bundle.json
Normal file
24
platform/persistence/bundle.json
Normal 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"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
99
platform/persistence/src/CouchPersistenceProvider.js
Normal file
99
platform/persistence/src/CouchPersistenceProvider.js
Normal 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;
|
||||||
|
}
|
||||||
|
);
|
Reference in New Issue
Block a user