Skip to content

Commit 51d6efb

Browse files
committed
Forward- and reverse-engineering
1 parent e27a935 commit 51d6efb

File tree

19 files changed

+1141
-424
lines changed

19 files changed

+1141
-424
lines changed

forward_engineering/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

forward_engineering/api.js

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
const helper = require('../helper/helper.js');
2+
3+
module.exports = {
4+
generateScript(data, logger, cb) {
5+
const { jsonSchema, modelData, containerData, entityData, isUpdateScript } = data;
6+
let result = "";
7+
let mappingScript = {
8+
mappings: {
9+
[entityData.collectionName.toLowerCase()]: {
10+
properties: this.getMappingScript(JSON.parse(jsonSchema))
11+
}
12+
}
13+
};
14+
15+
if (isUpdateScript) {
16+
result = this.getCurlScript(mappingScript, modelData, containerData);
17+
} else {
18+
result += this.getKibanaScript(mappingScript, containerData);
19+
}
20+
21+
cb(null, result);
22+
},
23+
24+
getCurlScript(mapping, modelData, indexData) {
25+
const host = modelData.host || 'localhost';
26+
const port = modelData.port || 9200;
27+
const indexName = indexData.name || "";
28+
29+
return `curl -XPUT '${host}:${port}/${indexName.toLowerCase()}?pretty' -H 'Content-Type: application/json' -d '\n${JSON.stringify(mapping, null, 4)}\n'`;
30+
},
31+
32+
getKibanaScript(mapping, indexData) {
33+
const indexName = indexData.name || "";
34+
35+
return `PUT /${indexName.toLowerCase()}\n${JSON.stringify(mapping, null, 4)}`;
36+
},
37+
38+
getMappingScript(jsonSchema) {
39+
let schema = {};
40+
41+
if (!(jsonSchema.properties && jsonSchema.properties._source && jsonSchema.properties._source.properties)) {
42+
return schema;
43+
}
44+
45+
schema = this.getSchemaByItem(jsonSchema.properties._source.properties)
46+
47+
return schema;
48+
},
49+
50+
getSchemaByItem(properties) {
51+
let schema = {};
52+
53+
for (let fieldName in properties) {
54+
let field = properties[fieldName];
55+
56+
schema[fieldName] = this.getField(field);
57+
}
58+
59+
return schema;
60+
},
61+
62+
getField(field) {
63+
let schema = {};
64+
const fieldProperties = helper.getFieldProperties(field.type, field, {});
65+
let type = this.getFieldType(field);
66+
67+
if (type !== 'object' && type !== 'array') {
68+
schema.type = type;
69+
}
70+
71+
if (type === 'object') {
72+
schema.properties = {};
73+
}
74+
75+
this.setProperties(schema, fieldProperties);
76+
77+
if (type === 'geo_shape' || type === 'geo_point') {
78+
return schema;
79+
} else if (field.properties) {
80+
schema.properties = this.getSchemaByItem(field.properties);
81+
} else if (field.items) {
82+
let arrData = field.items;
83+
84+
if (Array.isArray(field.items)) {
85+
arrData = field.items[0];
86+
}
87+
88+
schema = Object.assign(schema, this.getField(arrData));
89+
}
90+
91+
return schema;
92+
},
93+
94+
getFieldType(field) {
95+
switch(field.type) {
96+
case 'geo-shape':
97+
return 'geo_shape';
98+
case 'geo-point':
99+
return 'geo_point';
100+
case 'number':
101+
return field.mode || 'long';
102+
case 'string':
103+
return field.mode || 'text';
104+
case 'range':
105+
return field.mode || 'integer_range';
106+
case 'null':
107+
return 'long';
108+
default:
109+
return field.type;
110+
}
111+
},
112+
113+
setProperties(schema, properties) {
114+
for (let propName in properties) {
115+
if (propName === 'stringfields') {
116+
try {
117+
schema['fields'] = JSON.parse(properties[propName]);
118+
} catch (e) {
119+
}
120+
} else {
121+
schema[propName] = properties[propName];
122+
}
123+
}
124+
125+
return schema;
126+
}
127+
};

forward_engineering/config.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extension": "txt",
3+
"filterName": "Plain text",
4+
"namePrefix": "Elasticsearch Mapping",
5+
"hasUpdateScript": true
6+
}

forward_engineering/package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "elasticsearch",
3+
"version": "1.0.0",
4+
"description": "",
5+
"author": "Hackolade",
6+
"dependencies": {
7+
}
8+
}

helper/helper.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const fieldLevelConfig = JSON.parse(fs.readFileSync(path.join(__dirname, '../properties_pane/field_level/fieldLevelConfig.json')).toString().replace(/\/\*[.\s\S]*\*\//ig, ""));
4+
5+
module.exports = {
6+
getTargetFieldLevelPropertyNames(type, data) {
7+
if (!fieldLevelConfig.structure[type]) {
8+
return [];
9+
}
10+
11+
return fieldLevelConfig.structure[type].filter(property => {
12+
if (typeof property === 'object' && property.isTargetProperty) {
13+
if (property.dependency) {
14+
return (data[property.dependency.key] == property.dependency.value);
15+
} else {
16+
return true;
17+
}
18+
}
19+
20+
return false;
21+
}).map(property => property.propertyKeyword);
22+
},
23+
24+
getFieldProperties(type, data, pseudonyms) {
25+
const propertyNames = this.getTargetFieldLevelPropertyNames(type, data);
26+
27+
return propertyNames.reduce((result, propertyName) => {
28+
if (Object.prototype.hasOwnProperty.call(data, propertyName)) {
29+
result[propertyName] = data[propertyName];
30+
} else if (Object.prototype.hasOwnProperty.call(data, pseudonyms[propertyName])) {
31+
result[pseudonyms[propertyName]] = data[pseudonyms[propertyName]];
32+
}
33+
34+
return result;
35+
}, {});
36+
}
37+
};

localization/en.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"MAIN_MENU___INSERT_FIELD": "Insert Field",
99
"MAIN_MENU___APPEND_FIELD": "Append Field",
1010
"MAIN_MENU___REVERSE_DB_COLLECTIONS": "Elasticsearch indices...",
11+
"MAIN_MENU___FORWARD_DB_COLLECTIONS": "Elasticsearch Mapping",
1112
"TOOLBAR___ADD_BUCKET": "Add index",
1213
"TOOLBAR___ADD_COLLECTION": "Add type",
1314
"TOOLBAR___ADD_VIEW": "Add Filtered Alias",
@@ -84,6 +85,7 @@
8485
"MODAL_WINDOW___CONTAIN_BUCKET": "indices",
8586
"MODAL_WINDOW___CONTAIN_COLLECTION": "type",
8687
"MODAL_WINDOW___DB_CONNECTION_PROCESS": "Elasticsearch Reverse-Engineering Process",
88+
"MODAL_WINDOW___DB_CONNECTIONS_LIST_TITLE": "Elasticsearch Connections",
8789
"PROGRESS_BAR___DATABASE": "Index",
8890
"PROGRESS_BAR___COLLECTION": "Type",
8991
"PROGRESS_BAR___PROCESS": "Process",
@@ -132,10 +134,13 @@
132134
"COLLECTION_SCHEMA_DEFINITION_TYPE": "document",
133135
"MONGODB_SCRIPT_WARNING_MESSAGE": "This view is not associated to a type (viewOn property).",
134136
"TYPE": {},
135-
"CENTRAL_PANE___TAB_MODEL_DEFINITIONS": "User-Defined Types",
136-
"CONTEXT_MENU___ADD_MODEL_REFERENCE": "User-Defined Type",
137-
"CONTEXT_MENU___GO_TO_DEFINITION": "Go to User-Defined Type",
138-
"DOCUMENTATION___DB_DEFINITIONS": "User-Defined Types",
137+
"CENTRAL_PANE___TAB_MODEL_DEFINITIONS": "Model Definitions",
138+
"CONTEXT_MENU___ADD_MODEL_REFERENCE": "Model Definition",
139+
"CONTEXT_MENU___GO_TO_DEFINITION": "Go to Model Definition",
140+
"DOCUMENTATION___DB_DEFINITIONS": "Model Definitions",
139141
"CONTEXT_MENU___CONVERT_TO_PATTERN_FIELD": "Convert to Pattern Field",
140-
"CONTEXT_MENU___CONVERT_PATTERN_TO_REGULAR_FIELD": "Convert to Regular Field"
142+
"CONTEXT_MENU___CONVERT_PATTERN_TO_REGULAR_FIELD": "Convert to Regular Field",
143+
"CENTRAL_PANE___FE_SCRIPT": "Elasticsearch Mapping",
144+
"MODAL_WINDOW___FE_SCRIPT_OPTION_UPDATE": "CURL Script",
145+
"MODAL_WINDOW___FE_SCRIPT_OPTION_CREATE": "Kibana Script"
141146
}

package.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "Elasticsearch",
3-
"version": "0.1.4",
4-
"versionDate": "2018-02-25",
3+
"version": "0.1.5",
4+
"versionDate": "2018-03-29",
55
"author": "hackolade",
66
"engines": {
7-
"hackolade": "1.9.x",
7+
"hackolade": "1.12.7",
88
"hackoladePlugin": "1.0.0"
99
},
1010
"contributes": {
@@ -21,12 +21,15 @@
2121
"5.4.x",
2222
"5.5.x",
2323
"5.6.x",
24-
"6.0.x"
24+
"6.0.x",
25+
"6.1.x",
26+
"6.2.x"
2527
]
2628
},
2729
"features": {
28-
"nestedCollections": true,
29-
"disableMultipleTypes": true
30+
"disableMultipleTypes": true,
31+
"enableReverseEngineering": true,
32+
"enableForwardEngineering": true
3033
}
3134
},
3235
"description": "Hackolade plugin for Elasticsearch"

0 commit comments

Comments
 (0)