Skip to content

Commit

Permalink
Merge pull request #10 from tylersmalley/import-export-so-feedback
Browse files Browse the repository at this point in the history
Single call signature and use async/await
  • Loading branch information
simianhacker authored Jun 6, 2017
2 parents 50729fb + fb9125d commit a01fdae
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@ describe('collectDashboards(req, ids)', () => {
expect(savedObjectsClient.bulkGet.calledOnce).to.equal(true);

const args = savedObjectsClient.bulkGet.getCall(0).args;
expect(args[0]).to.equal(ids);
expect(args[1]).to.equal('dashboard');
expect(args[0]).to.eql([{
id: 'dashboard-01',
type: 'dashboard'
}, {
id: 'dashboard-02',
type: 'dashboard'
}]);
});

it('should call collectPanels with dashboard docs', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ export default function collectDashboards(savedObjectsClient, ids) {

if (ids.length === 0) return Promise.resolve([]);

return savedObjectsClient.bulkGet(ids, 'dashboard')
const objects = ids.map(id => {
return {
type: 'dashboard',
id: id
};
});

return savedObjectsClient.bulkGet(objects)
.then(docs => Promise.all(docs.map(d => deps.collectPanels(savedObjectsClient, d))))
.then(results => {
return results
Expand Down
22 changes: 9 additions & 13 deletions src/server/saved_objects/client/__tests__/saved_objects_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,34 +271,30 @@ describe('SavedObjectsClient', () => {
});

describe('#bulkGet', () => {
it('accepts an array of ids', async () => {
await savedObjectsClient.bulkGet(['one', 'two'], 'index-pattern');
expect(callAdminCluster.calledOnce).to.be(true);

const options = callAdminCluster.getCall(0).args[1];
expect(options.body.docs).to.eql([
{ _type: 'index-pattern', _id: 'one' },
{ _type: 'index-pattern', _id: 'two' }
]);
});

it('accepts a array of mixed type and ids', async () => {
await savedObjectsClient.bulkGet([
{ id: 'one', type: 'config' },
{ id: 'two', type: 'index-pattern' },
{ id: 'three' }
], 'foo');
]);

expect(callAdminCluster.calledOnce).to.be(true);

const options = callAdminCluster.getCall(0).args[1];
expect(options.body.docs).to.eql([
{ _type: 'config', _id: 'one' },
{ _type: 'index-pattern', _id: 'two' },
{ _type: 'foo', _id: 'three' }
{ _type: undefined, _id: 'three' }
]);
});

it('returns early for empty objects argument', async () => {
const response = await savedObjectsClient.bulkGet([]);

expect(response).to.have.length(0);
expect(callAdminCluster.notCalled).to.be(true);
});

it('omits missed objects', async () => {
callAdminCluster.returns(Promise.resolve({
docs:[{
Expand Down
62 changes: 28 additions & 34 deletions src/server/saved_objects/client/saved_objects_client.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Boom from 'boom';
import { get, isString } from 'lodash';
import { get } from 'lodash';

import {
createFindQuery,
Expand All @@ -12,16 +12,15 @@ export class SavedObjectsClient {
this._callAdminCluster = callAdminCluster;
}

create(type, body = {}) {
return this._withKibanaIndex('index', { type, body })
.then((resp) => {
return {
id: resp._id,
type: resp._type,
version: resp._version,
attributes: body
};
});
async create(type, body = {}) {
const response = await this._withKibanaIndex('index', { type, body });

return {
id: response._id,
type: response._type,
version: response._version,
attributes: body
};
}

/**
Expand All @@ -42,7 +41,7 @@ export class SavedObjectsClient {
return acc;
}, []);

return this._withKibanaIndex('bulk', { body })
return await this._withKibanaIndex('bulk', { body })
.then(resp => get(resp, 'items', []).map((resp, i) => {
return {
id: resp[action]._id,
Expand Down Expand Up @@ -104,40 +103,35 @@ export class SavedObjectsClient {
/**
* Returns an array of objects by id
*
* @param {array} ids - an array ids, or an array of objects containing id and optionally type
* @param {string} type - applies type to each id
* @param {array} objects - an array ids, or an array of objects containing id and optionally type
* @returns {promise} Returns promise containing array of documents
* @example
*
* // single type:
* bulkGet(['one', 'two', 'config'])
*
* // mixed types:
* bulkGet([
* { id: 'one', type: 'config' },
* { id: 'foo', type: 'index-pattern'
* ])
*/
bulkGet(ids = [], type) {
if (!ids || ids.length === 0) {
return Promise.resolve([]);
async bulkGet(objects = []) {
if (objects.length === 0) {
return [];
}

const docs = ids.map(doc => {
const id = isString(doc) ? doc : doc.id;
return { _type: get(doc, 'type', type), _id: id };
const docs = objects.map(doc => {
return { _type: get(doc, 'type'), _id: get(doc, 'id') };
});

return this._withKibanaIndex('mget', { body: { docs } })
.then(resp => get(resp, 'docs', []).filter(resp => resp.found))
.then(resp => resp.map(r => {
return {
id: r._id,
type: r._type,
version: r._version,
attributes: r._source
};
}));
const response = await this._withKibanaIndex('mget', { body: { docs } })
.then(resp => get(resp, 'docs', []).filter(resp => resp.found));

return response.map(r => {
return {
id: r._id,
type: r._type,
version: r._version,
attributes: r._source
};
});
}

async get(type, id) {
Expand Down

0 comments on commit a01fdae

Please sign in to comment.