2015-05-13 23:42:35 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* Open MCT Web, Copyright (c) 2014-2015, United States Government
|
|
|
|
* as represented by the Administrator of the National Aeronautics and Space
|
|
|
|
* Administration. All rights reserved.
|
|
|
|
*
|
|
|
|
* Open MCT Web is licensed under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0.
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
* Open MCT Web includes source code licensed under additional open source
|
|
|
|
* licenses. See the Open Source Licenses file (LICENSES.md) included with
|
|
|
|
* this source code distribution or the Licensing information page available
|
|
|
|
* at runtime from the About dialog for additional information.
|
|
|
|
*****************************************************************************/
|
2014-12-03 01:08:58 +00:00
|
|
|
/*global define*/
|
|
|
|
|
2015-08-07 19:13:15 +00:00
|
|
|
/**
|
|
|
|
* This bundle implements a persistence service which uses CouchDB to
|
|
|
|
* store documents.
|
|
|
|
* @namespace platform/persistence/cache
|
|
|
|
*/
|
2014-12-03 01:08:58 +00:00
|
|
|
define(
|
2014-12-03 01:19:30 +00:00
|
|
|
["./CouchDocument"],
|
|
|
|
function (CouchDocument) {
|
2014-12-03 01:08:58 +00:00
|
|
|
'use strict';
|
|
|
|
|
2014-12-03 16:59:40 +00:00
|
|
|
// JSLint doesn't like dangling _'s, but CouchDB uses these, so
|
|
|
|
// hide this behind variables.
|
|
|
|
var REV = "_rev",
|
|
|
|
ID = "_id";
|
|
|
|
|
2014-12-03 02:06:49 +00:00
|
|
|
/**
|
|
|
|
* The CouchPersistenceProvider reads and writes JSON documents
|
|
|
|
* (more specifically, domain object models) to/from a CouchDB
|
|
|
|
* instance.
|
2015-08-07 18:44:54 +00:00
|
|
|
* @memberof platform/persistence/couch
|
2014-12-03 02:06:49 +00:00
|
|
|
* @constructor
|
|
|
|
*/
|
2014-12-03 01:08:58 +00:00
|
|
|
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 02:06:49 +00:00
|
|
|
// Shorthand methods for GET/PUT 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 02:06:49 +00:00
|
|
|
// Pull out a list of document IDs from CouchDB's
|
|
|
|
// _all_docs response
|
2014-12-03 01:08:58 +00:00
|
|
|
function getIdsFromAllDocs(allDocs) {
|
|
|
|
return allDocs.rows.map(function (r) { return r.id; });
|
|
|
|
}
|
|
|
|
|
2014-12-03 02:06:49 +00:00
|
|
|
// Get a domain object model out of CouchDB's response
|
2014-12-03 01:08:58 +00:00
|
|
|
function getModel(response) {
|
|
|
|
if (response && response.model) {
|
2014-12-03 16:59:40 +00:00
|
|
|
revs[response[ID]] = response[REV];
|
2014-12-03 01:08:58 +00:00
|
|
|
return response.model;
|
|
|
|
} else {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-03 02:06:49 +00:00
|
|
|
// Check the response to a create/update/delete request;
|
|
|
|
// track the rev if it's valid, otherwise return false to
|
|
|
|
// indicate that the request failed.
|
2014-12-03 01:08:58 +00:00
|
|
|
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 {
|
2014-12-03 02:06:49 +00:00
|
|
|
/**
|
|
|
|
* List all persistence spaces which this provider
|
|
|
|
* recognizes.
|
|
|
|
*
|
|
|
|
* @returns {Promise.<string[]>} a promise for a list of
|
|
|
|
* spaces supported by this provider
|
2015-08-07 18:44:54 +00:00
|
|
|
* @memberof platform/persistence/couch.CouchPersistenceProvider#
|
2014-12-03 02:06:49 +00:00
|
|
|
*/
|
2014-12-03 01:08:58 +00:00
|
|
|
listSpaces: function () {
|
|
|
|
return $q.when(spaces);
|
|
|
|
},
|
2014-12-03 02:06:49 +00:00
|
|
|
/**
|
|
|
|
* List all objects (by their identifiers) that are stored
|
|
|
|
* in the given persistence space, per this provider.
|
|
|
|
* @param {string} space the space to check
|
|
|
|
* @returns {Promise.<string[]>} a promise for the list of
|
|
|
|
* identifiers
|
2015-08-07 18:44:54 +00:00
|
|
|
* @memberof platform/persistence/couch.CouchPersistenceProvider#
|
2014-12-03 02:06:49 +00:00
|
|
|
*/
|
2014-12-03 01:08:58 +00:00
|
|
|
listObjects: function (space) {
|
|
|
|
return get("_all_docs").then(getIdsFromAllDocs);
|
|
|
|
},
|
2014-12-03 02:06:49 +00:00
|
|
|
/**
|
|
|
|
* Create a new object in the specified persistence space.
|
|
|
|
* @param {string} space the space in which to store the object
|
|
|
|
* @param {string} key the identifier for the persisted object
|
|
|
|
* @param {object} value a JSONifiable object that should be
|
|
|
|
* stored and associated with the provided identifier
|
|
|
|
* @returns {Promise.<boolean>} a promise for an indication
|
|
|
|
* of the success (true) or failure (false) of this
|
|
|
|
* operation
|
2015-08-07 18:44:54 +00:00
|
|
|
* @memberof platform/persistence/couch.CouchPersistenceProvider#
|
2014-12-03 02:06:49 +00:00
|
|
|
*/
|
2014-12-03 01:08:58 +00:00
|
|
|
createObject: function (space, key, value) {
|
|
|
|
return put(key, new CouchDocument(key, value))
|
|
|
|
.then(checkResponse);
|
|
|
|
},
|
2014-12-03 02:06:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Read an existing object back from persistence.
|
|
|
|
* @param {string} space the space in which to look for
|
|
|
|
* the object
|
|
|
|
* @param {string} key the identifier for the persisted object
|
|
|
|
* @returns {Promise.<object>} a promise for the stored
|
|
|
|
* object; this will resolve to undefined if no such
|
|
|
|
* object is found.
|
2015-08-07 18:44:54 +00:00
|
|
|
* @memberof platform/persistence/couch.CouchPersistenceProvider#
|
2014-12-03 02:06:49 +00:00
|
|
|
*/
|
2014-12-03 01:08:58 +00:00
|
|
|
readObject: function (space, key) {
|
|
|
|
return get(key).then(getModel);
|
|
|
|
},
|
2014-12-03 02:06:49 +00:00
|
|
|
/**
|
|
|
|
* Update an existing object in the specified persistence space.
|
|
|
|
* @param {string} space the space in which to store the object
|
|
|
|
* @param {string} key the identifier for the persisted object
|
|
|
|
* @param {object} value a JSONifiable object that should be
|
|
|
|
* stored and associated with the provided identifier
|
|
|
|
* @returns {Promise.<boolean>} a promise for an indication
|
|
|
|
* of the success (true) or failure (false) of this
|
|
|
|
* operation
|
2015-08-07 18:44:54 +00:00
|
|
|
* @memberof platform/persistence/couch.CouchPersistenceProvider#
|
2014-12-03 02:06:49 +00:00
|
|
|
*/
|
2014-12-03 01:08:58 +00:00
|
|
|
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);
|
|
|
|
},
|
2014-12-03 02:06:49 +00:00
|
|
|
/**
|
|
|
|
* Delete an object in the specified persistence space.
|
|
|
|
* @param {string} space the space from which to delete this
|
|
|
|
* object
|
|
|
|
* @param {string} key the identifier of the persisted object
|
|
|
|
* @param {object} value a JSONifiable object that should be
|
|
|
|
* deleted
|
|
|
|
* @returns {Promise.<boolean>} a promise for an indication
|
|
|
|
* of the success (true) or failure (false) of this
|
|
|
|
* operation
|
2015-08-07 18:44:54 +00:00
|
|
|
* @memberof platform/persistence/couch.CouchPersistenceProvider#
|
2014-12-03 02:06:49 +00:00
|
|
|
*/
|
2014-12-03 01:08:58 +00:00
|
|
|
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;
|
|
|
|
}
|
2015-08-07 18:44:54 +00:00
|
|
|
);
|