From 5f44854d117000e0fad7bdc9493455033e2e6942 Mon Sep 17 00:00:00 2001 From: Nathan Reese Date: Fri, 9 Jun 2017 10:51:50 -0600 Subject: [PATCH] Expose Saved Object API to Server (#12182) * expose SavedObjectsClient to server * update prereqs function to use getSavedObjectsClient request decoration * fix indentation --- .../saved_objects/saved_objects_mixin.js | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/server/saved_objects/saved_objects_mixin.js b/src/server/saved_objects/saved_objects_mixin.js index 64046edc57bd3..45fe390420e70 100644 --- a/src/server/saved_objects/saved_objects_mixin.js +++ b/src/server/saved_objects/saved_objects_mixin.js @@ -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()); } }, }; @@ -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; + }); }