[Persistence] Parse space from id

Parse spaces from domain object identifiers, if these have
been specified.
This commit is contained in:
Victor Woeltjen 2015-11-05 17:04:55 -08:00
parent 7811d50372
commit 9f383ab101

View File

@ -44,36 +44,24 @@ define(
* from which models should be retrieved. * from which models should be retrieved.
* @param {string} spaces additional persistence spaces to use * @param {string} spaces additional persistence spaces to use
*/ */
function PersistedModelProvider(persistenceService, $q, now, space, spaces) { function PersistedModelProvider(persistenceService, $q, now) {
this.persistenceService = persistenceService; this.persistenceService = persistenceService;
this.$q = $q; this.$q = $q;
this.spaces = [space].concat(spaces || []);
this.now = now; this.now = now;
} }
// Take the most recently modified model, for cases where
// multiple persistence spaces return models.
function takeMostRecent(modelA, modelB) {
return (!modelB || modelB.modified === undefined) ? modelA :
(!modelA || modelA.modified === undefined) ? modelB :
modelB.modified > modelA.modified ? modelB :
modelA;
}
PersistedModelProvider.prototype.getModels = function (ids) { PersistedModelProvider.prototype.getModels = function (ids) {
var persistenceService = this.persistenceService, var persistenceService = this.persistenceService,
$q = this.$q, $q = this.$q,
spaces = this.spaces, now = this.now,
space = this.space, defaultSpace = this.defaultSpace;
now = this.now;
// Load a single object model from any persistence spaces // Load a single object model from any persistence spaces
function loadModel(id) { function loadModel(id) {
return $q.all(spaces.map(function (space) { var parts = id.split(":"),
return persistenceService.readObject(space, id); space = parts.length > 1 ? parts[0] : defaultSpace,
})).then(function (models) { key = parts.length > 1 ? parts[1] : id;
return models.reduce(takeMostRecent); return persistenceService.readObject(space, key);
});
} }
// Ensure that models read from persistence have some // Ensure that models read from persistence have some
@ -88,24 +76,36 @@ define(
} }
// Package the result as id->model // Package the result as id->model
function packageResult(models) { function packageResult(ids, models) {
var result = {}; var result = {};
ids.forEach(function (id, index) { ids.forEach(function (id, index) {
if (models[index]) { if (models[index]) {
result[id] = addPersistedTimestamp(models[index]); result[id] = models[index];
} }
}); });
return result; return result;
} }
// Filter out "namespaced" identifiers; these are // Filter down to o "namespaced" identifiers; these are
// not expected to be found in database. See WTD-659. // not expected to be found in database. See WTD-659.
ids = ids.filter(function (id) { ids = ids.filter(function (id) {
return id.indexOf(":") === -1; return id.indexOf(":") !== -1;
}); });
// Give a promise for all persistence lookups... return persistenceService.listSpaces().then(function (spaces) {
return $q.all(ids.map(loadModel)).then(packageResult); return ids.filter(function (id) {
var parts = id.split(":");
return parts.length === 1 ||
spaces.indexOf(parts[0]) !== -1;
});
}).then(function (ids) {
return $q.all(ids.map(loadModel)).then(function (models) {
return packageResult(
ids,
models.map(addPersistedTimestamp)
);
});
});
}; };
return PersistedModelProvider; return PersistedModelProvider;