Skip to content

Commit

Permalink
Expose Saved Object API to Server (elastic#12182)
Browse files Browse the repository at this point in the history
* expose SavedObjectsClient to server

* update prereqs function to use getSavedObjectsClient request decoration

* fix indentation
  • Loading branch information
nreese authored Jun 9, 2017
1 parent 49f37e6 commit 5f44854
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/server/saved_objects/saved_objects_mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@ export function savedObjectsMixin(kbnServer, server) {
getSavedObjectsClient: {
assign: 'savedObjectsClient',
method(req, reply) {
const adminCluster = req.server.plugins.elasticsearch.getCluster('admin');
const callAdminCluster = (...args) => adminCluster.callWithRequest(req, ...args);

reply(new SavedObjectsClient(
server.config().get('kibana.index'),
callAdminCluster
));
reply(req.getSavedObjectsClient());
}
},
};
Expand All @@ -29,4 +23,22 @@ export function savedObjectsMixin(kbnServer, server) {
server.route(createFindRoute(prereqs));
server.route(createReadRoute(prereqs));
server.route(createUpdateRoute(prereqs));

const savedObjectsClientCache = new WeakMap();
server.decorate('request', 'getSavedObjectsClient', function () {
const request = this;

if (savedObjectsClientCache.has(request)) {
return savedObjectsClientCache.get(request);
}

const { callWithRequest } = server.plugins.elasticsearch.getCluster('admin');
const callAdminCluster = (...args) => callWithRequest(request, ...args);
const savedObjectsClient = new SavedObjectsClient(
server.config().get('kibana.index'),
callAdminCluster
);
savedObjectsClientCache.set(request, savedObjectsClient);
return savedObjectsClient;
});
}

0 comments on commit 5f44854

Please sign in to comment.