mirror of
https://github.com/nasa/openmct.git
synced 2025-03-10 14:34:08 +00:00
[Persistence] Separate out CouchDocument
Separate CouchDocument (used to wrap a domain object model with metadata suitable for persistence to CouchDB) into its own script, to simplify testing and maintenance. Ongoing transition of Couch persistence, WTD-537.
This commit is contained in:
parent
2ce5ebcf20
commit
4646896ac8
41
platform/persistence/src/CouchDocument.js
Normal file
41
platform/persistence/src/CouchDocument.js
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
/*global define*/
|
||||||
|
|
||||||
|
define(
|
||||||
|
[],
|
||||||
|
function () {
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A CouchDocument describes domain object model in a format
|
||||||
|
* which is easily read-written to CouchDB. This includes
|
||||||
|
* Couch's _id and _rev fields, as well as a sseparate
|
||||||
|
* metadata field which contains a subset of information found
|
||||||
|
* in the model itself (to support search optimization with
|
||||||
|
* CouchDB views.)
|
||||||
|
* @constructor
|
||||||
|
* @param {string} id the id under which to store this mode
|
||||||
|
* @param {object} model the model to store
|
||||||
|
* @param {string} rev the revision to include (or undefined,
|
||||||
|
* if no revision should be noted for couch)
|
||||||
|
* @param {boolean} whether or not to mark this documnet as
|
||||||
|
* deleted (see CouchDB docs for _deleted)
|
||||||
|
*/
|
||||||
|
function CouchDocument(id, model, rev, markDeleted) {
|
||||||
|
return {
|
||||||
|
"_id": id,
|
||||||
|
"_rev": rev,
|
||||||
|
"_deleted": markDeleted,
|
||||||
|
"metadata": {
|
||||||
|
"category": "domain object",
|
||||||
|
"type": model.type,
|
||||||
|
"owner": "admin",
|
||||||
|
"name": model.name,
|
||||||
|
"created": Date.now()
|
||||||
|
},
|
||||||
|
"model": model
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return CouchDocument;
|
||||||
|
}
|
||||||
|
);
|
@ -1,8 +1,8 @@
|
|||||||
/*global define*/
|
/*global define*/
|
||||||
|
|
||||||
define(
|
define(
|
||||||
[],
|
["./CouchDocument"],
|
||||||
function () {
|
function (CouchDocument) {
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
function CouchPersistenceProvider($http, $q, SPACE, PATH) {
|
function CouchPersistenceProvider($http, $q, SPACE, PATH) {
|
||||||
@ -25,9 +25,18 @@ define(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function get(subpath) { return request(subpath, "GET"); }
|
// Shorthand methods for various get types
|
||||||
function put(subpath, value) { return request(subpath, "PUT", value); }
|
function get(subpath) {
|
||||||
function del(subpath, value) { return request(subpath, "DELETE", value); }
|
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) {
|
function getIdsFromAllDocs(allDocs) {
|
||||||
return allDocs.rows.map(function (r) { return r.id; });
|
return allDocs.rows.map(function (r) { return r.id; });
|
||||||
@ -52,22 +61,6 @@ define(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
return {
|
||||||
listSpaces: function () {
|
listSpaces: function () {
|
||||||
return $q.when(spaces);
|
return $q.when(spaces);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user