Skip to content

Commit 5ee582d

Browse files
thejibznodkz
authored andcommitted
feat: add fetchElasticMapping helper
* add fetchElasticMapping helper * fix lint error * fix return Promise type * fix lint error
1 parent f831068 commit 5ee582d

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ node_modules
4646

4747
coverage
4848
.nyc_output
49+
package-lock.json

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export { composeWithElastic } from './composeWithElastic';
44
export { convertToSourceTC, inputPropertiesToGraphQLTypes } from './mappingConverter';
55
export { default as ElasticApiParser } from './ElasticApiParser';
66
export { elasticApiFieldConfig } from './elasticApiFieldConfig';
7+
export { fetchElasticMapping } from './utils';

src/utils.js

+33
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* @flow */
22

33
import { TypeStorage } from 'graphql-compose';
4+
import type { ElasticMappingT } from './mappingConverter';
45

56
const typeStorage = new TypeStorage();
67

@@ -29,3 +30,35 @@ export function reorderKeys<T: Object>(obj: T, names: string[]): T {
2930
});
3031
return { ...orderedFields, ...fields };
3132
}
33+
34+
export type fetchElasticMappingOptsT = {
35+
elasticIndex: string,
36+
elasticType: string,
37+
elasticMapping: ElasticMappingT,
38+
elasticClient: Object,
39+
};
40+
41+
export async function fetchElasticMapping(
42+
opts: fetchElasticMappingOptsT
43+
): Promise<ElasticMappingT> {
44+
if (!opts.elasticIndex || typeof opts.elasticIndex !== 'string') {
45+
throw new Error('Must provide `elasticIndex` string parameter from your Elastic server.');
46+
}
47+
48+
if (!opts.elasticType || typeof opts.elasticType !== 'string') {
49+
throw new Error('Must provide `elasticType` string parameter from your Elastic server.');
50+
}
51+
52+
if (!opts.elasticClient) {
53+
throw new Error(
54+
'Must provide `elasticClient` Object parameter connected to your Elastic server.'
55+
);
56+
}
57+
58+
const elasticMapping = (await opts.elasticClient.indices.getMapping({
59+
index: opts.elasticIndex,
60+
type: opts.elasticType,
61+
}))[opts.elasticIndex].mappings[opts.elasticType];
62+
63+
return elasticMapping;
64+
}

0 commit comments

Comments
 (0)